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-10-29 13:10 +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 DebugStringTest(unittest.TestCase): 

91 

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()) 

97 

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()) 

103 

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()) 

109 

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()) 

115 

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()) 

121 

122 def test_to_json_string_default(self): 

123 obj = DummyObject("test") 

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

125 

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)) 

129 

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=" ")) 

133 

134 def test_to_json_string_filter(self): 

135 obj = DummyObject("test") 

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

137 

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 ) 

144 

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()) 

150 

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()) 

156 

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()) 

162 

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()) 

168 

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()) 

174 

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) 

180 

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) 

187 

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) 

193 

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) 

200 

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) 

207 

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) 

215 

216 TEST_FILE_NAME = "DebugStringTest.json"