Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_debugstring.py: 100%

165 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2026-10-19 14:18 +0000

1import io 

2import unittest 

3 

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 

14 

15 

16class DummyObject: 

17 def __init__(self, text_: str = str()): 

18 self._text_ = text_ 

19 

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 ) 

35 

36 @property 

37 def text(self): 

38 return self._text_ 

39 

40 @text.setter 

41 def text(self, text_): 

42 self._text_ = text_ 

43 

44 

45class ParameterizedDummyObject: 

46 def __init__(self, param_: int, text_: str = str()) -> None: 

47 self._param_: int = param_ 

48 self._text_ = text_ 

49 

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 } 

70 

71 return TypeInfo( 

72 "ParameterizedDummyObject", 

73 ParameterizedDummyObject, 

74 attributes=attribute_list, 

75 ) 

76 

77 @property 

78 def param(self) -> int: 

79 return self._param_ 

80 

81 @property 

82 def text(self) -> str: 

83 return self._text_ 

84 

85 @text.setter 

86 def text(self, text_: str) -> None: 

87 self._text_ = text_ 

88 

89 

90class FloatValueObject: 

91 def __init__(self, first_: float = 0, second_: float = 0) -> None: 

92 self._first_ = first_ 

93 self._second_ = second_ 

94 

95 @staticmethod 

96 def type_info() -> TypeInfo: 

97 field_list = [ 

98 MemberInfo( 

99 "first", 

100 TypeInfo("float", float), 

101 attributes={MemberAttribute.PROPERTY_NAME: "first"}, 

102 ), 

103 MemberInfo( 

104 "second", 

105 TypeInfo("float", float), 

106 attributes={MemberAttribute.PROPERTY_NAME: "second"}, 

107 ), 

108 ] 

109 attribute_list = { 

110 TypeAttribute.FIELDS: field_list, 

111 TypeAttribute.PARAMETERS: [], 

112 } 

113 

114 return TypeInfo( 

115 "FloatValueObject", 

116 FloatValueObject, 

117 attributes=attribute_list, 

118 ) 

119 

120 @property 

121 def first(self) -> float: 

122 return self._first_ 

123 

124 @first.setter 

125 def first(self, first_: float) -> None: 

126 self._first_ = first_ 

127 

128 @property 

129 def second(self) -> float: 

130 return self._second_ 

131 

132 @second.setter 

133 def second(self, second_: float) -> None: 

134 self._second_ = second_ 

135 

136 

137class DebugStringTest(unittest.TestCase): 

138 

139 def test_to_json_stream_default(self): 

140 obj = DummyObject("test") 

141 text_io = io.StringIO() 

142 to_json_stream(obj, text_io) 

143 self.assertEqual('{\n "text": "test"\n}', text_io.getvalue()) 

144 

145 def test_to_json_stream_indent_2(self): 

146 obj = DummyObject("test") 

147 text_io = io.StringIO() 

148 to_json_stream(obj, text_io, indent=2) 

149 self.assertEqual('{\n "text": "test"\n}', text_io.getvalue()) 

150 

151 def test_to_json_stream_indent_str(self): 

152 obj = DummyObject("test") 

153 text_io = io.StringIO() 

154 to_json_stream(obj, text_io, indent=" ") 

155 self.assertEqual('{\n "text": "test"\n}', text_io.getvalue()) 

156 

157 def test_to_json_stream_filter(self): 

158 obj = DummyObject("test") 

159 text_io = io.StringIO() 

160 to_json_stream(obj, text_io, walk_filter=DepthWalkFilter(0)) 

161 self.assertEqual("{\n}", text_io.getvalue()) 

162 

163 def test_to_json_stream_indent_2_filter(self): 

164 obj = DummyObject("test") 

165 text_io = io.StringIO() 

166 to_json_stream(obj, text_io, indent=2, walk_filter=DefaultWalkFilter()) 

167 self.assertEqual('{\n "text": "test"\n}', text_io.getvalue()) 

168 

169 def test_to_json_string_default(self): 

170 obj = DummyObject("test") 

171 self.assertEqual('{\n "text": "test"\n}', to_json_string(obj)) 

172 

173 def test_to_json_string_indent_2(self): 

174 obj = DummyObject("test") 

175 self.assertEqual('{\n "text": "test"\n}', to_json_string(obj, indent=2)) 

176 

177 def test_to_json_string_indent_str(self): 

178 obj = DummyObject("test") 

179 self.assertEqual('{\n "text": "test"\n}', to_json_string(obj, indent=" ")) 

180 

181 def test_to_json_string_filter(self): 

182 obj = DummyObject("test") 

183 self.assertEqual("{\n}", to_json_string(obj, walk_filter=DepthWalkFilter(0))) 

184 

185 def test_to_json_string_indent_2_filter(self): 

186 obj = DummyObject("test") 

187 self.assertEqual( 

188 '{\n "text": "test"\n}', 

189 to_json_string(obj, indent=2, walk_filter=DefaultWalkFilter()), 

190 ) 

191 

192 def test_to_json_file_default(self): 

193 obj = DummyObject("test") 

194 to_json_file(obj, self.TEST_FILE_NAME) 

195 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io: 

196 self.assertEqual('{\n "text": "test"\n}', text_io.read()) 

197 

198 def test_to_json_file_indent_2(self): 

199 obj = DummyObject("test") 

200 to_json_file(obj, self.TEST_FILE_NAME, indent=2) 

201 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io: 

202 self.assertEqual('{\n "text": "test"\n}', text_io.read()) 

203 

204 def test_to_json_file_indent_str(self): 

205 obj = DummyObject("test") 

206 to_json_file(obj, self.TEST_FILE_NAME, indent=" ") 

207 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io: 

208 self.assertEqual('{\n "text": "test"\n}', text_io.read()) 

209 

210 def test_to_json_file_filter(self): 

211 obj = DummyObject("test") 

212 to_json_file(obj, self.TEST_FILE_NAME, walk_filter=DepthWalkFilter(0)) 

213 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io: 

214 self.assertEqual("{\n}", text_io.read()) 

215 

216 def test_to_json_file_indent_2_filter(self): 

217 obj = DummyObject("test") 

218 to_json_file(obj, self.TEST_FILE_NAME, indent=2, walk_filter=DefaultWalkFilter()) 

219 with open(self.TEST_FILE_NAME, "r", encoding="utf-8") as text_io: 

220 self.assertEqual('{\n "text": "test"\n}', text_io.read()) 

221 

222 def test_from_json_stream(self): 

223 text_io = io.StringIO('{"text": "something"}') 

224 obj = from_json_stream(DummyObject, text_io) 

225 self.assertTrue(isinstance(obj, DummyObject)) 

226 self.assertEqual("something", obj.text) 

227 

228 def test_from_json_stream_arguments(self): 

229 text_io = io.StringIO('{"text": "something"}') 

230 obj = from_json_stream(ParameterizedDummyObject, text_io, 10) 

231 self.assertTrue(isinstance(obj, ParameterizedDummyObject)) 

232 self.assertEqual(10, obj.param) 

233 self.assertEqual("something", obj.text) 

234 

235 def test_from_json_string(self): 

236 json_string = '{"text": "something"}' 

237 obj = from_json_string(DummyObject, json_string) 

238 self.assertTrue(isinstance(obj, DummyObject)) 

239 self.assertEqual("something", obj.text) 

240 

241 def test_from_json_string_arguments(self): 

242 json_string = '{"text": "something"}' 

243 obj = from_json_string(ParameterizedDummyObject, json_string, 10) 

244 self.assertTrue(isinstance(obj, ParameterizedDummyObject)) 

245 self.assertEqual(10, obj.param) 

246 self.assertEqual("something", obj.text) 

247 

248 def test_json_string_round_trip(self): 

249 obj1 = FloatValueObject(13.5, 0) 

250 json = to_json_string(obj1) 

251 # should correctly parse int value as float 

252 obj2 = from_json_string(FloatValueObject, json) 

253 self.assertEqual(obj1.first, obj2.first) 

254 self.assertEqual(obj2.second, obj2.second) 

255 

256 def test_from_json_file(self): 

257 with open(self.TEST_FILE_NAME, "w", encoding="utf-8") as text_io: 

258 text_io.write('{"text": "something"}') 

259 obj = from_json_file(DummyObject, self.TEST_FILE_NAME) 

260 self.assertTrue(isinstance(obj, DummyObject)) 

261 self.assertEqual("something", obj.text) 

262 

263 def test_from_json_file_arguments(self): 

264 with open(self.TEST_FILE_NAME, "w", encoding="utf-8") as text_io: 

265 text_io.write('{"text": "something"}') 

266 obj = from_json_file(ParameterizedDummyObject, self.TEST_FILE_NAME, 10) 

267 self.assertTrue(isinstance(obj, ParameterizedDummyObject)) 

268 self.assertEqual(10, obj.param) 

269 self.assertEqual("something", obj.text) 

270 

271 TEST_FILE_NAME = "DebugStringTest.json"