File: test_introspection.py

package info (click to toggle)
python-dbus-next 0.2.3-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 696 kB
  • sloc: python: 6,018; makefile: 45; xml: 29
file content (122 lines) | stat: -rw-r--r-- 3,745 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from dbus_next import introspection as intr, ArgDirection, PropertyAccess, SignatureType

import os

example_data = open(f'{os.path.dirname(__file__)}/data/introspection.xml', 'r').read()


def test_example_introspection_from_xml():
    node = intr.Node.parse(example_data)

    assert len(node.interfaces) == 1
    interface = node.interfaces[0]

    assert len(node.nodes) == 2
    assert len(interface.methods) == 3
    assert len(interface.signals) == 2
    assert len(interface.properties) == 1

    assert type(node.nodes[0]) is intr.Node
    assert node.nodes[0].name == 'child_of_sample_object'
    assert type(node.nodes[1]) is intr.Node
    assert node.nodes[1].name == 'another_child_of_sample_object'

    assert interface.name == 'com.example.SampleInterface0'

    frobate = interface.methods[0]
    assert type(frobate) is intr.Method
    assert frobate.name == 'Frobate'
    assert len(frobate.in_args) == 1
    assert len(frobate.out_args) == 2

    foo = frobate.in_args[0]
    assert type(foo) is intr.Arg
    assert foo.name == 'foo'
    assert foo.direction == ArgDirection.IN
    assert foo.signature == 'i'
    assert type(foo.type) is SignatureType
    assert foo.type.token == 'i'

    bar = frobate.out_args[0]
    assert type(bar) is intr.Arg
    assert bar.name == 'bar'
    assert bar.direction == ArgDirection.OUT
    assert bar.signature == 's'
    assert type(bar.type) is SignatureType
    assert bar.type.token == 's'

    prop = interface.properties[0]
    assert type(prop) is intr.Property
    assert prop.name == 'Bar'
    assert prop.signature == 'y'
    assert type(prop.type) is SignatureType
    assert prop.type.token == 'y'
    assert prop.access == PropertyAccess.WRITE

    changed = interface.signals[0]
    assert type(changed) is intr.Signal
    assert changed.name == 'Changed'
    assert len(changed.args) == 1
    new_value = changed.args[0]
    assert type(new_value) is intr.Arg
    assert new_value.name == 'new_value'
    assert new_value.signature == 'b'


def test_example_introspection_to_xml():
    node = intr.Node.parse(example_data)
    tree = node.to_xml()
    assert tree.tag == 'node'
    assert tree.attrib.get('name') == '/com/example/sample_object0'
    assert len(tree) == 3
    interface = tree[0]
    assert interface.tag == 'interface'
    assert interface.get('name') == 'com.example.SampleInterface0'
    assert len(interface) == 6
    method = interface[0]
    assert method.tag == 'method'
    assert method.get('name') == 'Frobate'
    # TODO annotations
    assert len(method) == 3

    arg = method[0]
    assert arg.tag == 'arg'
    assert arg.attrib.get('name') == 'foo'
    assert arg.attrib.get('type') == 'i'
    assert arg.attrib.get('direction') == 'in'

    signal = interface[3]
    assert signal.tag == 'signal'
    assert signal.attrib.get('name') == 'Changed'
    assert len(signal) == 1

    arg = signal[0]
    assert arg.tag == 'arg'
    assert arg.attrib.get('name') == 'new_value'
    assert arg.attrib.get('type') == 'b'

    signal = interface[4]
    assert signal.tag == 'signal'
    assert signal.attrib.get('name') == 'ChangedMulti'
    assert len(signal) == 2

    arg = signal[0]
    assert arg.tag == 'arg'
    assert arg.attrib.get('name') == 'new_value1'
    assert arg.attrib.get('type') == 'b'

    arg = signal[1]
    assert arg.tag == 'arg'
    assert arg.attrib.get('name') == 'new_value2'
    assert arg.attrib.get('type') == 'y'

    prop = interface[5]
    assert prop.attrib.get('name') == 'Bar'
    assert prop.attrib.get('type') == 'y'
    assert prop.attrib.get('access') == 'write'


def test_default_interfaces():
    # just make sure it doesn't throw
    default = intr.Node.default()
    assert type(default) is intr.Node