Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_debugstring.py: 100%
138 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-12-05 10:43 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-12-05 10:43 +0000
1import io
2import unittest
4from zserio.debugstring import (
5 to_json_stream,
6 to_json_string,
7 to_json_file,
8 from_json_stream,
9 from_json_string,
10 from_json_file,
11)
12from zserio.typeinfo import TypeInfo, TypeAttribute, MemberInfo, MemberAttribute
13from zserio.walker import DepthWalkFilter, DefaultWalkFilter
16class DummyObject:
17 def __init__(self, text_: str = str()):
18 self._text_ = text_
20 @staticmethod
21 def type_info():
22 return TypeInfo(
23 "DummyObject",
24 DummyObject,
25 attributes={
26 TypeAttribute.FIELDS: [
27 MemberInfo(
28 "text",
29 TypeInfo("str", str),
30 attributes={MemberAttribute.PROPERTY_NAME: "text"},
31 )
32 ]
33 },
34 )
36 @property
37 def text(self):
38 return self._text_
40 @text.setter
41 def text(self, text_):
42 self._text_ = text_
45class ParameterizedDummyObject:
46 def __init__(self, param_: int, text_: str = str()) -> None:
47 self._param_: int = param_
48 self._text_ = text_
50 @staticmethod
51 def type_info() -> TypeInfo:
52 field_list = [
53 MemberInfo(
54 "text",
55 TypeInfo("string", str),
56 attributes={MemberAttribute.PROPERTY_NAME: "text"},
57 )
58 ]
59 parameter_list = [
60 MemberInfo(
61 "param",
62 TypeInfo("int", int),
63 attributes={MemberAttribute.PROPERTY_NAME: "param"},
64 )
65 ]
66 attribute_list = {
67 TypeAttribute.FIELDS: field_list,
68 TypeAttribute.PARAMETERS: parameter_list,
69 }
71 return TypeInfo(
72 "ParameterizedDummyObject",
73 ParameterizedDummyObject,
74 attributes=attribute_list,
75 )
77 @property
78 def param(self) -> int:
79 return self._param_
81 @property
82 def text(self) -> str:
83 return self._text_
85 @text.setter
86 def text(self, text_: str) -> None:
87 self._text_ = text_
90class DebugStringTest(unittest.TestCase):
92 def test_to_json_stream_default(self):
93 obj = DummyObject("test")
94 text_io = io.StringIO()
95 to_json_stream(obj, text_io)
96 self.assertEqual('{\n "text": "test"\n}', text_io.getvalue())
98 def test_to_json_stream_indent_2(self):
99 obj = DummyObject("test")
100 text_io = io.StringIO()
101 to_json_stream(obj, text_io, indent=2)
102 self.assertEqual('{\n "text": "test"\n}', text_io.getvalue())
104 def test_to_json_stream_indent_str(self):
105 obj = DummyObject("test")
106 text_io = io.StringIO()
107 to_json_stream(obj, text_io, indent=" ")
108 self.assertEqual('{\n "text": "test"\n}', text_io.getvalue())
110 def test_to_json_stream_filter(self):
111 obj = DummyObject("test")
112 text_io = io.StringIO()
113 to_json_stream(obj, text_io, walk_filter=DepthWalkFilter(0))
114 self.assertEqual("{\n}", text_io.getvalue())
116 def test_to_json_stream_indent_2_filter(self):
117 obj = DummyObject("test")
118 text_io = io.StringIO()
119 to_json_stream(obj, text_io, indent=2, walk_filter=DefaultWalkFilter())
120 self.assertEqual('{\n "text": "test"\n}', text_io.getvalue())
122 def test_to_json_string_default(self):
123 obj = DummyObject("test")
124 self.assertEqual('{\n "text": "test"\n}', to_json_string(obj))
126 def test_to_json_string_indent_2(self):
127 obj = DummyObject("test")
128 self.assertEqual('{\n "text": "test"\n}', to_json_string(obj, indent=2))
130 def test_to_json_string_indent_str(self):
131 obj = DummyObject("test")
132 self.assertEqual('{\n "text": "test"\n}', to_json_string(obj, indent=" "))
134 def test_to_json_string_filter(self):
135 obj = DummyObject("test")
136 self.assertEqual("{\n}", to_json_string(obj, walk_filter=DepthWalkFilter(0)))
138 def test_to_json_string_indent_2_filter(self):
139 obj = DummyObject("test")
140 self.assertEqual(
141 '{\n "text": "test"\n}',
142 to_json_string(obj, indent=2, walk_filter=DefaultWalkFilter()),
143 )
145 def test_to_json_file_default(self):
146 obj = DummyObject("test")
147 to_json_file(obj, self.TEST_FILE_NAME)
148 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io:
149 self.assertEqual('{\n "text": "test"\n}', text_io.read())
151 def test_to_json_file_indent_2(self):
152 obj = DummyObject("test")
153 to_json_file(obj, self.TEST_FILE_NAME, indent=2)
154 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io:
155 self.assertEqual('{\n "text": "test"\n}', text_io.read())
157 def test_to_json_file_indent_str(self):
158 obj = DummyObject("test")
159 to_json_file(obj, self.TEST_FILE_NAME, indent=" ")
160 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io:
161 self.assertEqual('{\n "text": "test"\n}', text_io.read())
163 def test_to_json_file_filter(self):
164 obj = DummyObject("test")
165 to_json_file(obj, self.TEST_FILE_NAME, walk_filter=DepthWalkFilter(0))
166 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io:
167 self.assertEqual("{\n}", text_io.read())
169 def test_to_json_file_indent_2_filter(self):
170 obj = DummyObject("test")
171 to_json_file(obj, self.TEST_FILE_NAME, indent=2, walk_filter=DefaultWalkFilter())
172 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io:
173 self.assertEqual('{\n "text": "test"\n}', text_io.read())
175 def test_from_json_stream(self):
176 text_io = io.StringIO('{"text": "something"}')
177 obj = from_json_stream(DummyObject, text_io)
178 self.assertTrue(isinstance(obj, DummyObject))
179 self.assertEqual("something", obj.text)
181 def test_from_json_stream_arguments(self):
182 text_io = io.StringIO('{"text": "something"}')
183 obj = from_json_stream(ParameterizedDummyObject, text_io, 10)
184 self.assertTrue(isinstance(obj, ParameterizedDummyObject))
185 self.assertEqual(10, obj.param)
186 self.assertEqual("something", obj.text)
188 def test_from_json_string(self):
189 json_string = '{"text": "something"}'
190 obj = from_json_string(DummyObject, json_string)
191 self.assertTrue(isinstance(obj, DummyObject))
192 self.assertEqual("something", obj.text)
194 def test_from_json_string_arguments(self):
195 json_string = '{"text": "something"}'
196 obj = from_json_string(ParameterizedDummyObject, json_string, 10)
197 self.assertTrue(isinstance(obj, ParameterizedDummyObject))
198 self.assertEqual(10, obj.param)
199 self.assertEqual("something", obj.text)
201 def test_from_json_file(self):
202 with open(self.TEST_FILE_NAME, "w", encoding="utf-8") as text_io:
203 text_io.write('{"text": "something"}')
204 obj = from_json_file(DummyObject, self.TEST_FILE_NAME)
205 self.assertTrue(isinstance(obj, DummyObject))
206 self.assertEqual("something", obj.text)
208 def test_from_json_file_arguments(self):
209 with open(self.TEST_FILE_NAME, "w", encoding="utf-8") as text_io:
210 text_io.write('{"text": "something"}')
211 obj = from_json_file(ParameterizedDummyObject, self.TEST_FILE_NAME, 10)
212 self.assertTrue(isinstance(obj, ParameterizedDummyObject))
213 self.assertEqual(10, obj.param)
214 self.assertEqual("something", obj.text)
216 TEST_FILE_NAME = "DebugStringTest.json"