Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_creator.py: 100%
226 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-10-29 13:10 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-10-29 13:10 +0000
1import unittest
3from test_object.api import CreatorEnum, CreatorBitmask, CreatorObject
5from zserio.exception import PythonRuntimeException
6from zserio.creator import ZserioTreeCreator
7from zserio.bitbuffer import BitBuffer
10class ZserioTreeCreatorTest(unittest.TestCase):
12 def test_create_object(self):
13 creator = ZserioTreeCreator(CreatorObject.type_info())
14 creator.begin_root()
15 obj = creator.end_root()
16 self.assertTrue(isinstance(obj, CreatorObject))
18 def test_create_object_set_fields(self):
19 creator = ZserioTreeCreator(CreatorObject.type_info())
21 creator.begin_root()
22 creator.set_value("value", 13)
23 creator.set_value("text", "test")
24 obj = creator.end_root()
26 self.assertEqual(13, obj.value)
27 self.assertEqual("test", obj.text)
29 def test_create_object_full(self):
30 creator = ZserioTreeCreator(CreatorObject.type_info())
32 creator.begin_root()
33 creator.set_value("value", 13)
34 creator.set_value("text", "test")
35 creator.begin_compound("nested")
36 creator.set_value("value", 10)
37 creator.set_value("text", "nested")
38 creator.set_value("externData", BitBuffer(bytes([0x3C]), 6))
39 creator.set_value("bytesData", bytearray([0xFF]))
40 creator.set_value("creatorEnum", CreatorEnum.ONE)
41 creator.set_value("creatorBitmask", CreatorBitmask.Values.WRITE)
42 creator.end_compound()
43 creator.begin_array("nestedArray")
44 creator.begin_compound_element()
45 creator.set_value("value", 5)
46 creator.set_value("text", "nestedArray")
47 creator.set_value("creatorEnum", CreatorEnum.TWO)
48 creator.set_value("creatorBitmask", CreatorBitmask.Values.READ)
49 creator.end_compound_element()
50 creator.end_array()
51 creator.begin_array("textArray")
52 creator.add_value_element("this")
53 creator.add_value_element("is")
54 creator.add_value_element("text")
55 creator.add_value_element("array")
56 creator.end_array()
57 creator.begin_array("externArray")
58 creator.add_value_element(BitBuffer(bytes([0x0F]), 4))
59 creator.end_array()
60 creator.begin_array("bytesArray")
61 creator.add_value_element(bytearray([0xCA, 0xFE]))
62 creator.end_array()
63 creator.set_value("optionalBool", False)
64 creator.begin_compound("optionalNested")
65 creator.set_value("text", "optionalNested")
66 creator.end_compound()
67 obj = creator.end_root()
69 self.assertEqual(13, obj.value)
70 self.assertEqual("test", obj.text)
71 self.assertEqual(13, obj.nested.param)
72 self.assertEqual(10, obj.nested.value)
73 self.assertEqual("nested", obj.nested.text)
74 self.assertEqual(bytes([0x3C]), obj.nested.extern_data.buffer)
75 self.assertEqual(bytes([0xFF]), obj.nested.bytes_data)
76 self.assertEqual(6, obj.nested.extern_data.bitsize)
77 self.assertEqual(CreatorEnum.ONE, obj.nested.creator_enum)
78 self.assertEqual(CreatorBitmask.Values.WRITE, obj.nested.creator_bitmask)
79 self.assertEqual(1, len(obj.nested_array))
80 self.assertEqual(5, obj.nested_array[0].value)
81 self.assertEqual("nestedArray", obj.nested_array[0].text)
82 self.assertEqual(CreatorEnum.TWO, obj.nested_array[0].creator_enum)
83 self.assertEqual(CreatorBitmask.Values.READ, obj.nested_array[0].creator_bitmask)
84 self.assertEqual(["this", "is", "text", "array"], obj.text_array)
85 self.assertEqual(1, len(obj.extern_array))
86 self.assertEqual(bytes([0x0F]), obj.extern_array[0].buffer)
87 self.assertEqual(4, obj.extern_array[0].bitsize)
88 self.assertEqual(1, len(obj.bytes_array))
89 self.assertEqual(bytes([0xCA, 0xFE]), obj.bytes_array[0])
90 self.assertEqual(False, obj.optional_bool)
91 self.assertEqual("optionalNested", obj.optional_nested.text)
93 def test_exceptions_before_root(self):
94 creator = ZserioTreeCreator(CreatorObject.type_info())
96 with self.assertRaises(PythonRuntimeException):
97 creator.end_root()
98 with self.assertRaises(PythonRuntimeException):
99 creator.begin_array("nestedArray")
100 with self.assertRaises(PythonRuntimeException):
101 creator.end_array()
102 with self.assertRaises(PythonRuntimeException):
103 creator.begin_compound("nested")
104 with self.assertRaises(PythonRuntimeException):
105 creator.end_compound()
106 with self.assertRaises(PythonRuntimeException):
107 creator.set_value("value", 13)
108 with self.assertRaises(PythonRuntimeException):
109 creator.begin_compound_element()
110 with self.assertRaises(PythonRuntimeException):
111 creator.end_compound_element()
112 with self.assertRaises(PythonRuntimeException):
113 creator.add_value_element(13)
115 def test_exceptions_in_root(self):
116 creator = ZserioTreeCreator(CreatorObject.type_info())
118 creator.begin_root()
120 with self.assertRaises(PythonRuntimeException):
121 creator.begin_root()
122 with self.assertRaises(PythonRuntimeException):
123 creator.begin_array("nonexistent")
124 with self.assertRaises(PythonRuntimeException):
125 creator.begin_array("nested") # not an array
126 with self.assertRaises(PythonRuntimeException):
127 creator.end_array()
128 with self.assertRaises(PythonRuntimeException):
129 creator.begin_compound("nonexistent")
130 with self.assertRaises(PythonRuntimeException):
131 creator.begin_compound("nestedArray") # is array
132 with self.assertRaises(PythonRuntimeException):
133 creator.end_compound()
134 with self.assertRaises(PythonRuntimeException):
135 creator.set_value("nonexistent", 13)
136 with self.assertRaises(PythonRuntimeException):
137 creator.set_value("nestedArray", 13) # is array
138 with self.assertRaises(PythonRuntimeException):
139 creator.begin_compound_element()
140 with self.assertRaises(PythonRuntimeException):
141 creator.end_compound_element()
142 with self.assertRaises(PythonRuntimeException):
143 creator.add_value_element(13)
145 def test_exceptions_in_compound(self):
146 creator = ZserioTreeCreator(CreatorObject.type_info())
148 creator.begin_root()
149 creator.begin_compound("nested")
151 with self.assertRaises(PythonRuntimeException):
152 creator.begin_root()
153 with self.assertRaises(PythonRuntimeException):
154 creator.end_root()
155 with self.assertRaises(PythonRuntimeException):
156 creator.begin_array("nonexistent")
157 with self.assertRaises(PythonRuntimeException):
158 creator.begin_array("value") # not an array
159 with self.assertRaises(PythonRuntimeException):
160 creator.end_array()
161 with self.assertRaises(PythonRuntimeException):
162 creator.begin_compound("nonexistent")
163 with self.assertRaises(PythonRuntimeException):
164 creator.begin_compound("text") # not a compound
165 with self.assertRaises(PythonRuntimeException):
166 creator.set_value("nonexistent", "test")
167 with self.assertRaises(PythonRuntimeException):
168 creator.set_value("value", "test") # wrong type
169 with self.assertRaises(PythonRuntimeException):
170 creator.begin_compound_element()
171 with self.assertRaises(PythonRuntimeException):
172 creator.end_compound_element()
173 with self.assertRaises(PythonRuntimeException):
174 creator.add_value_element(13)
175 with self.assertRaises(PythonRuntimeException):
176 creator.get_element_type()
178 def test_exceptions_in_compound_array(self):
179 creator = ZserioTreeCreator(CreatorObject.type_info())
180 creator.begin_root()
181 creator.begin_array("nestedArray")
183 with self.assertRaises(PythonRuntimeException):
184 creator.begin_root()
185 with self.assertRaises(PythonRuntimeException):
186 creator.end_root()
187 with self.assertRaises(PythonRuntimeException):
188 creator.begin_array("nonexistent")
189 with self.assertRaises(PythonRuntimeException):
190 creator.begin_compound("nonexistent")
191 with self.assertRaises(PythonRuntimeException):
192 creator.end_compound()
193 with self.assertRaises(PythonRuntimeException):
194 creator.set_value("nonexistent", 13)
195 with self.assertRaises(PythonRuntimeException):
196 creator.end_compound_element()
197 with self.assertRaises(PythonRuntimeException):
198 creator.add_value_element(13)
199 with self.assertRaises(PythonRuntimeException):
200 creator.get_field_type("nonexistent")
202 def test_exceptions_in_simple_array(self):
203 creator = ZserioTreeCreator(CreatorObject.type_info())
204 creator.begin_root()
205 creator.begin_array("textArray")
207 with self.assertRaises(PythonRuntimeException):
208 creator.begin_root()
209 with self.assertRaises(PythonRuntimeException):
210 creator.end_root()
211 with self.assertRaises(PythonRuntimeException):
212 creator.begin_array("nonexistent")
213 with self.assertRaises(PythonRuntimeException):
214 creator.begin_compound("nonexistent")
215 with self.assertRaises(PythonRuntimeException):
216 creator.end_compound()
217 with self.assertRaises(PythonRuntimeException):
218 creator.set_value("nonexistent", 13)
219 with self.assertRaises(PythonRuntimeException):
220 creator.begin_compound_element() # not a compound array
221 with self.assertRaises(PythonRuntimeException):
222 creator.end_compound_element()
223 with self.assertRaises(PythonRuntimeException):
224 creator.add_value_element(13) # wrong type
225 with self.assertRaises(PythonRuntimeException):
226 creator.get_field_type("nonexistent")
228 def test_exceptions_in_compound_element(self):
229 creator = ZserioTreeCreator(CreatorObject.type_info())
230 creator.begin_root()
231 creator.begin_array("nestedArray")
232 creator.begin_compound_element()
234 with self.assertRaises(PythonRuntimeException):
235 creator.begin_root()
236 with self.assertRaises(PythonRuntimeException):
237 creator.end_root()
238 with self.assertRaises(PythonRuntimeException):
239 creator.begin_array("nonexistent")
240 with self.assertRaises(PythonRuntimeException):
241 creator.end_array()
242 with self.assertRaises(PythonRuntimeException):
243 creator.begin_compound("nonexistent")
244 with self.assertRaises(PythonRuntimeException):
245 creator.end_compound()
246 with self.assertRaises(PythonRuntimeException):
247 creator.set_value("nonexistent", 13)
248 with self.assertRaises(PythonRuntimeException):
249 creator.begin_compound_element()
250 with self.assertRaises(PythonRuntimeException):
251 creator.add_value_element(13)