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