Coverage for /home/jenkins/workspace/NDS/Zserio/NDS_ZSERIO-linux-build/compiler/extensions/python/runtime/tests/test_debugstring.py: 100%

138 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-12-13 15:12 +0000

1import io 

2import unittest 

3 

4from zserio.debugstring import (to_json_stream, to_json_string, to_json_file, 

5 from_json_stream, from_json_string, from_json_file) 

6from zserio.typeinfo import TypeInfo, TypeAttribute, MemberInfo, MemberAttribute 

7from zserio.walker import DepthWalkFilter, DefaultWalkFilter 

8 

9class DummyObject: 

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

11 self._text_ = text_ 

12 

13 @staticmethod 

14 def type_info(): 

15 return TypeInfo("DummyObject", DummyObject, 

16 attributes={TypeAttribute.FIELDS : [ 

17 MemberInfo("text", TypeInfo("str", str), attributes={ 

18 MemberAttribute.PROPERTY_NAME: "text" 

19 }) 

20 ]} 

21 ) 

22 

23 @property 

24 def text(self): 

25 return self._text_ 

26 

27 @text.setter 

28 def text(self, text_): 

29 self._text_ = text_ 

30 

31class ParameterizedDummyObject: 

32 def __init__( 

33 self, 

34 param_: int, 

35 text_: str = str()) -> None: 

36 self._param_: int = param_ 

37 self._text_ = text_ 

38 

39 @staticmethod 

40 def type_info() -> TypeInfo: 

41 field_list = [ 

42 MemberInfo( 

43 'text', TypeInfo('string', str), 

44 attributes={ 

45 MemberAttribute.PROPERTY_NAME : 'text' 

46 } 

47 ) 

48 ] 

49 parameter_list = [ 

50 MemberInfo( 

51 'param', TypeInfo('int', int), 

52 attributes={ 

53 MemberAttribute.PROPERTY_NAME : 'param' 

54 } 

55 ) 

56 ] 

57 attribute_list = { 

58 TypeAttribute.FIELDS : field_list, 

59 TypeAttribute.PARAMETERS : parameter_list 

60 } 

61 

62 return TypeInfo("ParameterizedDummyObject", ParameterizedDummyObject, attributes=attribute_list) 

63 

64 @property 

65 def param(self) -> int: 

66 return self._param_ 

67 

68 @property 

69 def text(self) -> str: 

70 return self._text_ 

71 

72 @text.setter 

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

74 self._text_ = text_ 

75 

76class DebugStringTest(unittest.TestCase): 

77 

78 def test_to_json_stream_default(self): 

79 obj = DummyObject("test") 

80 text_io = io.StringIO() 

81 to_json_stream(obj, text_io) 

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

83 

84 def test_to_json_stream_indent_2(self): 

85 obj = DummyObject("test") 

86 text_io = io.StringIO() 

87 to_json_stream(obj, text_io, indent=2) 

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

89 

90 def test_to_json_stream_indent_str(self): 

91 obj = DummyObject("test") 

92 text_io = io.StringIO() 

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

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

95 

96 def test_to_json_stream_filter(self): 

97 obj = DummyObject("test") 

98 text_io = io.StringIO() 

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

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

101 

102 def test_to_json_stream_indent_2_filter(self): 

103 obj = DummyObject("test") 

104 text_io = io.StringIO() 

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

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

107 

108 def test_to_json_string_default(self): 

109 obj = DummyObject("test") 

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

111 

112 def test_to_json_string_indent_2(self): 

113 obj = DummyObject("test") 

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

115 

116 def test_to_json_string_indent_str(self): 

117 obj = DummyObject("test") 

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

119 

120 def test_to_json_string_filter(self): 

121 obj = DummyObject("test") 

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

123 

124 def test_to_json_string_indent_2_filter(self): 

125 obj = DummyObject("test") 

126 self.assertEqual("{\n \"text\": \"test\"\n}", 

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

128 

129 def test_to_json_file_default(self): 

130 obj = DummyObject("test") 

131 to_json_file(obj, self.TEST_FILE_NAME) 

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

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

134 

135 def test_to_json_file_indent_2(self): 

136 obj = DummyObject("test") 

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

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

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

140 

141 def test_to_json_file_indent_str(self): 

142 obj = DummyObject("test") 

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

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

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

146 

147 def test_to_json_file_filter(self): 

148 obj = DummyObject("test") 

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

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

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

152 

153 def test_to_json_file_indent_2_filter(self): 

154 obj = DummyObject("test") 

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

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

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

158 

159 def test_from_json_stream(self): 

160 text_io = io.StringIO("{\"text\": \"something\"}") 

161 obj = from_json_stream(DummyObject, text_io) 

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

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

164 

165 def test_from_json_stream_arguments(self): 

166 text_io = io.StringIO("{\"text\": \"something\"}") 

167 obj = from_json_stream(ParameterizedDummyObject, text_io, 10) 

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

169 self.assertEqual(10, obj.param) 

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

171 

172 def test_from_json_string(self): 

173 json_string = "{\"text\": \"something\"}" 

174 obj = from_json_string(DummyObject, json_string) 

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

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

177 

178 def test_from_json_string_arguments(self): 

179 json_string = "{\"text\": \"something\"}" 

180 obj = from_json_string(ParameterizedDummyObject, json_string, 10) 

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

182 self.assertEqual(10, obj.param) 

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

184 

185 def test_from_json_file(self): 

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

187 text_io.write("{\"text\": \"something\"}") 

188 obj = from_json_file(DummyObject, self.TEST_FILE_NAME) 

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

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

191 

192 def test_from_json_file_arguments(self): 

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

194 text_io.write("{\"text\": \"something\"}") 

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

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

197 self.assertEqual(10, obj.param) 

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

199 

200 TEST_FILE_NAME = "DebugStringTest.json"