File: test_xml.py

package info (click to toggle)
dasbus 1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 880 kB
  • sloc: python: 7,550; makefile: 101; sh: 4
file content (200 lines) | stat: -rw-r--r-- 6,767 bytes parent folder | download | duplicates (2)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#
# Copyright (C) 2019  Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
# USA
#
import unittest
from dasbus.xml import XMLParser, XMLGenerator


class XMLParserTestCase(unittest.TestCase):

    def test_is_member(self):
        """Test if the element is a member of an interface."""
        element = XMLParser.xml_to_element('<method name="MethodName" />')
        self.assertEqual(XMLParser.is_member(element), True)

        element = XMLParser.xml_to_element('<signal name="SignalName" />')
        self.assertEqual(XMLParser.is_member(element), True)

        element = XMLParser.xml_to_element(
            '<property '
            'access="PropertyAccess" '
            'name="PropertyName" '
            'type="PropertyType" />'
        )
        self.assertEqual(XMLParser.is_member(element), True)

    def test_is_interface(self):
        """Test if the element is an interface."""
        element = XMLParser.xml_to_element(
            '<interface name="InterfaceName" />'
        )
        self.assertEqual(XMLParser.is_interface(element), True)

    def test_is_signal(self):
        """Test if the element is a signal."""
        element = XMLParser.xml_to_element('<signal name="SignalName" />')
        self.assertEqual(XMLParser.is_signal(element), True)

    def test_is_method(self):
        """Test if the element is a method."""
        element = XMLParser.xml_to_element('<method name="MethodName" />')
        self.assertEqual(XMLParser.is_method(element), True)

    def test_is_property(self):
        """Test if the element is a property."""
        element = XMLParser.xml_to_element(
            '<property '
            'access="PropertyAccess" '
            'name="PropertyName" '
            'type="PropertyType" />'
        )
        self.assertEqual(XMLParser.is_property(element), True)

    def test_is_parameter(self):
        """Test if the element is a parameter."""
        element = XMLParser.xml_to_element(
            '<arg '
            'direction="ParameterDirection" '
            'name="ParameterName" '
            'type="ParameterType" />'
        )
        self.assertEqual(XMLParser.is_parameter(element), True)

    def test_has_name(self):
        """Test if the element has the specified name."""
        element = XMLParser.xml_to_element('<method name="MethodName" />')
        self.assertEqual(XMLParser.has_name(element, "MethodName"), True)
        self.assertEqual(XMLParser.has_name(element, "AnotherName"), False)

    def test_get_name(self):
        """Get the name attribute."""
        element = XMLParser.xml_to_element('<method name="MethodName" />')
        self.assertEqual(XMLParser.get_name(element), "MethodName")

    def test_get_type(self):
        """Get the type attribute."""
        element = XMLParser.xml_to_element(
            '<arg '
            'direction="ParameterDirection" '
            'name="ParameterName" '
            'type="ParameterType" />'
        )
        self.assertEqual(
            XMLParser.get_type(element),
            "ParameterType"
        )

    def test_get_access(self):
        """Get the access attribute."""
        element = XMLParser.xml_to_element(
            '<property '
            'access="PropertyAccess" '
            'name="PropertyName" '
            'type="PropertyType" />'
        )
        self.assertEqual(
            XMLParser.get_access(element),
            "PropertyAccess"
        )

    def test_get_direction(self):
        """Get the direction attribute."""
        element = XMLParser.xml_to_element(
            '<arg '
            'direction="ParameterDirection" '
            'name="ParameterName" '
            'type="ParameterType" />'
        )
        self.assertEqual(
            XMLParser.get_direction(element),
            "ParameterDirection"
        )

    def test_get_interfaces_from_node(self):
        """Get interfaces from the node."""
        element = XMLParser.xml_to_element('''
        <node>
            <interface name="A" />
            <interface name="B" />
            <interface name="C" />
        </node>
        ''')
        interfaces = XMLParser.get_interfaces_from_node(element)
        self.assertEqual(interfaces.keys(), {"A", "B", "C"})


class XMLGeneratorTestCase(unittest.TestCase):

    def _compare(self, element, xml):
        self.assertEqual(
            XMLGenerator.prettify_xml(
                XMLGenerator.element_to_xml(element)
            ),
            XMLGenerator.prettify_xml(xml)
        )

    def test_node(self):
        """Test the node element."""
        self._compare(XMLGenerator.create_node(), '<node />')

    def test_interface(self):
        """Test the interface element."""
        self._compare(
            XMLGenerator.create_interface("InterfaceName"),
            '<interface name="InterfaceName" />'
        )

    def test_parameter(self):
        """Test the parameter element."""
        self._compare(
            XMLGenerator.create_parameter(
                "ParameterName",
                "ParameterType",
                "ParameterDirection"
            ),
            '<arg '
            'direction="ParameterDirection" '
            'name="ParameterName" '
            'type="ParameterType" />'
        )

    def test_property(self):
        """Test the property element."""
        self._compare(
            XMLGenerator.create_property(
                "PropertyName",
                "PropertyType",
                "PropertyAccess"
            ),
            '<property '
            'access="PropertyAccess" '
            'name="PropertyName" '
            'type="PropertyType" />'
        )

    def test_method(self):
        """Test the method element."""
        element = XMLGenerator.create_method("MethodName")
        xml = '<method name="MethodName" />'
        self._compare(element, xml)

    def test_signal(self):
        """Test the signal element."""
        element = XMLGenerator.create_signal("SignalName")
        xml = '<signal name="SignalName" />'
        self._compare(element, xml)