test/zserio/JsonReaderTest.cpp
Line | Count | Source |
1 | | #include "gtest/gtest.h" |
2 | | #include "test_object/std_allocator/CreatorBitmask.h" |
3 | | #include "test_object/std_allocator/CreatorEnum.h" |
4 | | #include "test_object/std_allocator/CreatorNested.h" |
5 | | #include "test_object/std_allocator/CreatorObject.h" |
6 | | #include "zserio/JsonReader.h" |
7 | | #include "zserio/ReflectableUtil.h" |
8 | | |
9 | | using test_object::std_allocator::CreatorBitmask; |
10 | | using test_object::std_allocator::CreatorEnum; |
11 | | using test_object::std_allocator::CreatorNested; |
12 | | using test_object::std_allocator::CreatorObject; |
13 | | |
14 | | namespace zserio |
15 | | { |
16 | | |
17 | | namespace |
18 | | { |
19 | | |
20 | | void checkReadStringifiedEnum(const char* stringValue, CreatorEnum expectedValue) |
21 | 2 | { |
22 | 2 | std::stringstream str; |
23 | 2 | str << "{\n" |
24 | 2 | " \"nested\": {\n" |
25 | 2 | " \"creatorEnum\": \"" |
26 | 2 | << stringValue |
27 | 2 | << "\"\n" |
28 | 2 | " }\n" |
29 | 2 | "}"; |
30 | | |
31 | 2 | JsonReader jsonReader(str); |
32 | 2 | auto reflectable = jsonReader.read(CreatorObject::typeInfo()); |
33 | 2 | ASSERT_TRUE(reflectable); |
34 | | |
35 | 2 | ASSERT_EQ(expectedValue, ReflectableUtil::getValue<CreatorEnum>(reflectable->find("nested.creatorEnum"))); |
36 | 2 | } |
37 | | |
38 | | void checkReadStringifiedEnumThrows(const char* stringValue, const char* expectedMessage) |
39 | 5 | { |
40 | 5 | std::stringstream str; |
41 | 5 | str << "{\n" |
42 | 5 | " \"nested\": {\n" |
43 | 5 | " \"creatorEnum\": \"" |
44 | 5 | << stringValue |
45 | 5 | << "\"\n" |
46 | 5 | " }\n" |
47 | 5 | "}"; |
48 | | |
49 | 5 | JsonReader jsonReader(str); |
50 | 5 | ASSERT_THROW( |
51 | 5 | { |
52 | 5 | try |
53 | 5 | { |
54 | 5 | jsonReader.read(CreatorObject::typeInfo()); |
55 | 5 | } |
56 | 5 | catch (const CppRuntimeException& e) |
57 | 5 | { |
58 | 5 | ASSERT_STREQ(expectedMessage, e.what()); |
59 | 5 | throw; |
60 | 5 | } |
61 | 5 | }, |
62 | 5 | CppRuntimeException); |
63 | 5 | } |
64 | | |
65 | | void checkReadStringifiedBitmask(const char* stringValue, CreatorBitmask expectedValue) |
66 | 5 | { |
67 | 5 | std::stringstream str; |
68 | 5 | str << "{\n" |
69 | 5 | " \"nested\": {\n" |
70 | 5 | " \"creatorBitmask\": \"" |
71 | 5 | << stringValue |
72 | 5 | << "\"\n" |
73 | 5 | " }\n" |
74 | 5 | "}"; |
75 | | |
76 | 5 | JsonReader jsonReader(str); |
77 | 5 | auto reflectable = jsonReader.read(CreatorObject::typeInfo()); |
78 | 5 | ASSERT_TRUE(reflectable); |
79 | | |
80 | 5 | ASSERT_EQ(expectedValue, |
81 | 5 | ReflectableUtil::getValue<CreatorBitmask>(reflectable->find("nested.creatorBitmask"))); |
82 | 5 | } |
83 | | |
84 | | void checkReadStringifiedBitmaskThrows(const char* stringValue, const char* expectedMessage) |
85 | 6 | { |
86 | 6 | std::stringstream str; |
87 | 6 | str << "{\n" |
88 | 6 | " \"nested\": {\n" |
89 | 6 | " \"creatorBitmask\": \"" |
90 | 6 | << stringValue << "\"\n" |
91 | 6 | << " }\n" |
92 | 6 | "}"; |
93 | | |
94 | 6 | JsonReader jsonReader(str); |
95 | 6 | ASSERT_THROW( |
96 | 6 | { |
97 | 6 | try |
98 | 6 | { |
99 | 6 | jsonReader.read(CreatorObject::typeInfo()); |
100 | 6 | } |
101 | 6 | catch (const CppRuntimeException& e) |
102 | 6 | { |
103 | 6 | ASSERT_STREQ(expectedMessage, e.what()); |
104 | 6 | throw; |
105 | 6 | } |
106 | 6 | }, |
107 | 6 | CppRuntimeException); |
108 | 6 | } |
109 | | |
110 | | } // namespace |
111 | | |
112 | | TEST(JsonReaderTest, readObjectWithoutOptional) |
113 | 1 | { |
114 | 1 | std::stringstream str( |
115 | 1 | "{\n" |
116 | 1 | " \"value\": 13,\n" |
117 | 1 | " \"nested\": {\n" |
118 | 1 | " \"value\": 10,\n" |
119 | 1 | " \"text\": \"nested\",\n" |
120 | 1 | " \"externData\": {\n" |
121 | 1 | " \"buffer\": [\n" |
122 | 1 | " 203,\n" |
123 | 1 | " 240\n" |
124 | 1 | " ],\n" |
125 | 1 | " \"bitSize\": 12\n" |
126 | 1 | " },\n" |
127 | 1 | " \"bytesData\": {\n" |
128 | 1 | " \"buffer\": [\n" |
129 | 1 | " 202,\n" |
130 | 1 | " 254\n" |
131 | 1 | " ]\n" |
132 | 1 | " },\n" |
133 | 1 | " \"creatorEnum\": 0,\n" |
134 | 1 | " \"creatorBitmask\": 1\n" |
135 | 1 | " },\n" |
136 | 1 | " \"text\": \"test\",\n" |
137 | 1 | " \"nestedArray\": [\n" |
138 | 1 | " {\n" |
139 | 1 | " \"value\": 5,\n" |
140 | 1 | " \"text\": \"nestedArray\",\n" |
141 | 1 | " \"externData\": {\n" |
142 | 1 | " \"buffer\": [\n" |
143 | 1 | " 202,\n" |
144 | 1 | " 254\n" |
145 | 1 | " ]," |
146 | 1 | " \"bitSize\": 15\n" |
147 | 1 | " },\n" |
148 | 1 | " \"bytesData\": {\n" |
149 | 1 | " \"buffer\": [\n" |
150 | 1 | " 203,\n" |
151 | 1 | " 240\n" |
152 | 1 | " ]\n" |
153 | 1 | " },\n" |
154 | 1 | " \"creatorEnum\": 1,\n" |
155 | 1 | " \"creatorBitmask\": 2\n" |
156 | 1 | " }\n" |
157 | 1 | " ],\n" |
158 | 1 | " \"textArray\": [\n" |
159 | 1 | " \"this\",\n" |
160 | 1 | " \"is\",\n" |
161 | 1 | " \"text\",\n" |
162 | 1 | " \"array\"\n" |
163 | 1 | " ],\n" |
164 | 1 | " \"externArray\": [\n" |
165 | 1 | " {\n" |
166 | 1 | " \"buffer\": [\n" |
167 | 1 | " 222,\n" |
168 | 1 | " 209\n" |
169 | 1 | " ]," |
170 | 1 | " \"bitSize\": 13\n" |
171 | 1 | " }\n" |
172 | 1 | " ],\n" |
173 | 1 | " \"bytesArray\": [\n" |
174 | 1 | " {\n" |
175 | 1 | " \"buffer\": [\n" |
176 | 1 | " 0\n" |
177 | 1 | " ]\n" |
178 | 1 | " }\n" |
179 | 1 | " ],\n" |
180 | 1 | " \"optionalBool\": null\n" |
181 | 1 | "}"); |
182 | | |
183 | 1 | JsonReader jsonReader(str); |
184 | 1 | IReflectablePtr reflectable = jsonReader.read(CreatorObject::typeInfo()); |
185 | 1 | ASSERT_TRUE(reflectable); |
186 | | |
187 | 1 | reflectable->initializeChildren(); |
188 | | |
189 | 1 | ASSERT_EQ(13, reflectable->getField("value")->getUInt32()); |
190 | 1 | ASSERT_EQ(13, reflectable->getField("nested")->getParameter("param")->getUInt32()); |
191 | 1 | ASSERT_EQ(10, reflectable->find("nested.value")->getUInt32()); |
192 | 1 | ASSERT_EQ("nested"_sv, reflectable->find("nested.text")->getStringView()); |
193 | 1 | ASSERT_EQ(BitBuffer({0xCB, 0xF0}, 12), reflectable->find("nested.externData")->getBitBuffer()); |
194 | 1 | const Span<const uint8_t> bytesData = reflectable->find("nested.bytesData")->getBytes(); |
195 | 1 | ASSERT_EQ(2, bytesData.size()); |
196 | 1 | ASSERT_EQ(0xCA, bytesData[0]); |
197 | 1 | ASSERT_EQ(0xFE, bytesData[1]); |
198 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::ONE), reflectable->find("nested.creatorEnum")->getInt8()); |
199 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), |
200 | 1 | reflectable->find("nested.creatorBitmask")->getUInt8()); |
201 | 1 | ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView()); |
202 | 1 | ASSERT_EQ(1, reflectable->getField("nestedArray")->size()); |
203 | 1 | ASSERT_EQ(5, reflectable->getField("nestedArray")->at(0)->getField("value")->getUInt32()); |
204 | 1 | ASSERT_EQ("nestedArray"_sv, reflectable->getField("nestedArray")->at(0)->getField("text")->getStringView()); |
205 | 1 | ASSERT_EQ(BitBuffer({0xCA, 0xFE}, 15), |
206 | 1 | reflectable->getField("nestedArray")->at(0)->getField("externData")->getBitBuffer()); |
207 | 1 | const Span<const uint8_t> nestedBytesData = |
208 | 1 | reflectable->getField("nestedArray")->at(0)->getField("bytesData")->getBytes(); |
209 | 1 | ASSERT_EQ(2, nestedBytesData.size()); |
210 | 1 | ASSERT_EQ(0xCB, nestedBytesData[0]); |
211 | 1 | ASSERT_EQ(0xF0, nestedBytesData[1]); |
212 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::TWO), |
213 | 1 | reflectable->getField("nestedArray")->at(0)->getField("creatorEnum")->getInt8()); |
214 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::WRITE).getValue(), |
215 | 1 | reflectable->getField("nestedArray")->at(0)->getField("creatorBitmask")->getUInt8()); |
216 | 1 | ASSERT_EQ(4, reflectable->getField("textArray")->size()); |
217 | 1 | ASSERT_EQ("this"_sv, reflectable->getField("textArray")->at(0)->getStringView()); |
218 | 1 | ASSERT_EQ("is"_sv, reflectable->getField("textArray")->at(1)->getStringView()); |
219 | 1 | ASSERT_EQ("text"_sv, reflectable->getField("textArray")->at(2)->getStringView()); |
220 | 1 | ASSERT_EQ("array"_sv, reflectable->getField("textArray")->at(3)->getStringView()); |
221 | 1 | ASSERT_EQ(1, reflectable->getField("externArray")->size()); |
222 | 1 | ASSERT_EQ(BitBuffer({0xDE, 0xD1}, 13), reflectable->getField("externArray")->at(0)->getBitBuffer()); |
223 | 1 | ASSERT_EQ(1, reflectable->getField("bytesArray")->size()); |
224 | 1 | Span<const uint8_t> bytesElement = reflectable->getField("bytesArray")->at(0)->getBytes(); |
225 | 1 | ASSERT_EQ(1, bytesElement.size()); |
226 | 1 | ASSERT_EQ(0, bytesElement[0]); |
227 | 1 | ASSERT_EQ(nullptr, reflectable->getField("optionalBool")); |
228 | 1 | ASSERT_EQ(nullptr, reflectable->getField("optionalNested")); // not present in json |
229 | 1 | } |
230 | | |
231 | | TEST(JsonReaderTest, readObjectWithOptional) |
232 | 1 | { |
233 | 1 | std::stringstream str( |
234 | 1 | "{\n" |
235 | 1 | " \"value\": 13,\n" |
236 | 1 | " \"nested\": {\n" |
237 | 1 | " \"value\": 10,\n" |
238 | 1 | " \"text\": \"nested\",\n" |
239 | 1 | " \"externData\": {\n" |
240 | 1 | " \"buffer\": [\n" |
241 | 1 | " 203,\n" |
242 | 1 | " 240\n" |
243 | 1 | " ],\n" |
244 | 1 | " \"bitSize\": 12\n" |
245 | 1 | " },\n" |
246 | 1 | " \"bytesData\": {\n" |
247 | 1 | " \"buffer\": [\n" |
248 | 1 | " 202,\n" |
249 | 1 | " 254\n" |
250 | 1 | " ]\n" |
251 | 1 | " },\n" |
252 | 1 | " \"creatorEnum\": 0,\n" |
253 | 1 | " \"creatorBitmask\": 1\n" |
254 | 1 | " },\n" |
255 | 1 | " \"text\": \"test\",\n" |
256 | 1 | " \"nestedArray\": [\n" |
257 | 1 | " {\n" |
258 | 1 | " \"value\": 5,\n" |
259 | 1 | " \"text\": \"nestedArray\",\n" |
260 | 1 | " \"externData\": {\n" |
261 | 1 | " \"buffer\": [\n" |
262 | 1 | " 202,\n" |
263 | 1 | " 254\n" |
264 | 1 | " ]," |
265 | 1 | " \"bitSize\": 15\n" |
266 | 1 | " },\n" |
267 | 1 | " \"bytesData\": {\n" |
268 | 1 | " \"buffer\": [\n" |
269 | 1 | " 203,\n" |
270 | 1 | " 240\n" |
271 | 1 | " ]\n" |
272 | 1 | " },\n" |
273 | 1 | " \"creatorEnum\": 1,\n" |
274 | 1 | " \"creatorBitmask\": 2\n" |
275 | 1 | " }\n" |
276 | 1 | " ],\n" |
277 | 1 | " \"textArray\": [\n" |
278 | 1 | " \"this\",\n" |
279 | 1 | " \"is\",\n" |
280 | 1 | " \"text\",\n" |
281 | 1 | " \"array\"\n" |
282 | 1 | " ],\n" |
283 | 1 | " \"externArray\": [\n" |
284 | 1 | " {\n" |
285 | 1 | " \"buffer\": [\n" |
286 | 1 | " 222,\n" |
287 | 1 | " 209\n" |
288 | 1 | " ]," |
289 | 1 | " \"bitSize\": 13\n" |
290 | 1 | " }\n" |
291 | 1 | " ],\n" |
292 | 1 | " \"bytesArray\": [\n" |
293 | 1 | " {\n" |
294 | 1 | " \"buffer\": [\n" |
295 | 1 | " 0\n" |
296 | 1 | " ]\n" |
297 | 1 | " }\n" |
298 | 1 | " ],\n" |
299 | 1 | " \"optionalBool\": true\n" |
300 | 1 | "}"); |
301 | | |
302 | 1 | JsonReader jsonReader(str); |
303 | 1 | IReflectablePtr reflectable = jsonReader.read(CreatorObject::typeInfo()); |
304 | 1 | ASSERT_TRUE(reflectable); |
305 | | |
306 | 1 | reflectable->initializeChildren(); |
307 | | |
308 | 1 | ASSERT_EQ(13, reflectable->getField("value")->getUInt32()); |
309 | 1 | ASSERT_EQ(13, reflectable->getField("nested")->getParameter("param")->getUInt32()); |
310 | 1 | ASSERT_EQ(10, reflectable->find("nested.value")->getUInt32()); |
311 | 1 | ASSERT_EQ("nested"_sv, reflectable->find("nested.text")->getStringView()); |
312 | 1 | ASSERT_EQ(BitBuffer({0xCB, 0xF0}, 12), reflectable->find("nested.externData")->getBitBuffer()); |
313 | 1 | const Span<const uint8_t> bytesData = reflectable->find("nested.bytesData")->getBytes(); |
314 | 1 | ASSERT_EQ(2, bytesData.size()); |
315 | 1 | ASSERT_EQ(0xCA, bytesData[0]); |
316 | 1 | ASSERT_EQ(0xFE, bytesData[1]); |
317 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::ONE), reflectable->find("nested.creatorEnum")->getInt8()); |
318 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), |
319 | 1 | reflectable->find("nested.creatorBitmask")->getUInt8()); |
320 | 1 | ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView()); |
321 | 1 | ASSERT_EQ(1, reflectable->getField("nestedArray")->size()); |
322 | 1 | ASSERT_EQ(5, reflectable->getField("nestedArray")->at(0)->getField("value")->getUInt32()); |
323 | 1 | ASSERT_EQ("nestedArray"_sv, reflectable->getField("nestedArray")->at(0)->getField("text")->getStringView()); |
324 | 1 | ASSERT_EQ(BitBuffer({0xCA, 0xFE}, 15), |
325 | 1 | reflectable->getField("nestedArray")->at(0)->getField("externData")->getBitBuffer()); |
326 | 1 | const Span<const uint8_t> nestedBytesData = |
327 | 1 | reflectable->getField("nestedArray")->at(0)->getField("bytesData")->getBytes(); |
328 | 1 | ASSERT_EQ(2, nestedBytesData.size()); |
329 | 1 | ASSERT_EQ(0xCB, nestedBytesData[0]); |
330 | 1 | ASSERT_EQ(0xF0, nestedBytesData[1]); |
331 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::TWO), |
332 | 1 | reflectable->getField("nestedArray")->at(0)->getField("creatorEnum")->getInt8()); |
333 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::WRITE).getValue(), |
334 | 1 | reflectable->getField("nestedArray")->at(0)->getField("creatorBitmask")->getUInt8()); |
335 | 1 | ASSERT_EQ(4, reflectable->getField("textArray")->size()); |
336 | 1 | ASSERT_EQ("this"_sv, reflectable->getField("textArray")->at(0)->getStringView()); |
337 | 1 | ASSERT_EQ("is"_sv, reflectable->getField("textArray")->at(1)->getStringView()); |
338 | 1 | ASSERT_EQ("text"_sv, reflectable->getField("textArray")->at(2)->getStringView()); |
339 | 1 | ASSERT_EQ("array"_sv, reflectable->getField("textArray")->at(3)->getStringView()); |
340 | 1 | ASSERT_EQ(1, reflectable->getField("externArray")->size()); |
341 | 1 | ASSERT_EQ(BitBuffer({0xDE, 0xD1}, 13), reflectable->getField("externArray")->at(0)->getBitBuffer()); |
342 | 1 | ASSERT_EQ(1, reflectable->getField("bytesArray")->size()); |
343 | 1 | Span<const uint8_t> bytesElement = reflectable->getField("bytesArray")->at(0)->getBytes(); |
344 | 1 | ASSERT_EQ(1, bytesElement.size()); |
345 | 1 | ASSERT_EQ(0, bytesElement[0]); |
346 | 1 | ASSERT_EQ(true, reflectable->getField("optionalBool")->getBool()); |
347 | 1 | ASSERT_EQ(nullptr, reflectable->getField("optionalNested")); // not present in json |
348 | 1 | } |
349 | | |
350 | | TEST(JsonReaderTest, readTwoObjects) |
351 | 1 | { |
352 | 1 | std::stringstream str( |
353 | 1 | "{\"value\": 13}\n" |
354 | 1 | "{\"value\": 42, \"text\": \"test\"}\n"); |
355 | | |
356 | 1 | JsonReader jsonReader(str); |
357 | | |
358 | 1 | auto obj1 = jsonReader.read(CreatorObject::typeInfo()); |
359 | 1 | ASSERT_TRUE(obj1); |
360 | 1 | ASSERT_EQ(13, obj1->getField("value")->getUInt32()); |
361 | 1 | ASSERT_EQ(""_sv, obj1->getField("text")->getStringView()); |
362 | | |
363 | 1 | auto obj2 = jsonReader.read(CreatorObject::typeInfo()); |
364 | 1 | ASSERT_TRUE(obj2); |
365 | 1 | ASSERT_EQ(42, obj2->getField("value")->getUInt32()); |
366 | 1 | ASSERT_EQ("test"_sv, obj2->getField("text")->getStringView()); |
367 | 1 | } |
368 | | |
369 | | TEST(JsonReaderTest, readParameterizedObject) |
370 | 1 | { |
371 | 1 | std::stringstream str( |
372 | 1 | "{\n" |
373 | 1 | " \"value\": 10,\n" |
374 | 1 | " \"text\": \"nested\",\n" |
375 | 1 | " \"externData\": {\n" |
376 | 1 | " \"bitSize\": 12,\n" |
377 | 1 | " \"buffer\": [\n" |
378 | 1 | " 203,\n" |
379 | 1 | " 240\n" |
380 | 1 | " ]\n" |
381 | 1 | " },\n" |
382 | 1 | " \"bytesData\": {\n" |
383 | 1 | " \"buffer\": [\n" |
384 | 1 | " 202,\n" |
385 | 1 | " 254\n" |
386 | 1 | " ]\n" |
387 | 1 | " },\n" |
388 | 1 | " \"creatorEnum\": 0,\n" |
389 | 1 | " \"creatorBitmask\": 1\n" |
390 | 1 | "}\n"); |
391 | | |
392 | 1 | JsonReader jsonReader(str); |
393 | 1 | auto reflectable = jsonReader.read(CreatorNested::typeInfo()); |
394 | | |
395 | 1 | reflectable->initialize(vector<AnyHolder<>>{AnyHolder<>{static_cast<uint32_t>(13)}}); |
396 | | |
397 | 1 | ASSERT_EQ(13, reflectable->getParameter("param")->getUInt32()); |
398 | 1 | ASSERT_EQ(10, reflectable->getField("value")->getUInt32()); |
399 | 1 | } |
400 | | |
401 | | TEST(JsonReaderTest, readUnorderedBitBuffer) |
402 | 1 | { |
403 | 1 | std::stringstream str( |
404 | 1 | "{\n" |
405 | 1 | " \"value\": 13,\n" |
406 | 1 | " \"nested\": {\n" |
407 | 1 | " \"value\": 10,\n" |
408 | 1 | " \"text\": \"nested\",\n" |
409 | 1 | " \"externData\": {\n" |
410 | 1 | " \"bitSize\": 12,\n" |
411 | 1 | " \"buffer\": [\n" |
412 | 1 | " 203,\n" |
413 | 1 | " 240\n" |
414 | 1 | " ]\n" |
415 | 1 | " },\n" |
416 | 1 | " \"bytesData\": {\n" |
417 | 1 | " \"buffer\": [\n" |
418 | 1 | " 202,\n" |
419 | 1 | " 254\n" |
420 | 1 | " ]\n" |
421 | 1 | " },\n" |
422 | 1 | " \"creatorEnum\": -1,\n" |
423 | 1 | " \"creatorBitmask\": 1\n" |
424 | 1 | " }\n" |
425 | 1 | "}"); |
426 | | |
427 | 1 | JsonReader jsonReader(str); |
428 | 1 | auto reflectable = jsonReader.read(CreatorObject::typeInfo()); |
429 | 1 | ASSERT_TRUE(reflectable); |
430 | | |
431 | 1 | reflectable->initializeChildren(); |
432 | | |
433 | 1 | ASSERT_EQ(13, reflectable->getField("value")->getUInt32()); |
434 | 1 | ASSERT_EQ(13, reflectable->getField("nested")->getParameter("param")->getUInt32()); |
435 | 1 | ASSERT_EQ(10, reflectable->find("nested.value")->getUInt32()); |
436 | 1 | ASSERT_EQ("nested"_sv, reflectable->find("nested.text")->getStringView()); |
437 | 1 | ASSERT_EQ(BitBuffer({0xCB, 0xF0}, 12), reflectable->find("nested.externData")->getBitBuffer()); |
438 | 1 | const Span<const uint8_t> bytesData = reflectable->find("nested.bytesData")->getBytes(); |
439 | 1 | ASSERT_EQ(2, bytesData.size()); |
440 | 1 | ASSERT_EQ(0xCA, bytesData[0]); |
441 | 1 | ASSERT_EQ(0xFE, bytesData[1]); |
442 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::MinusOne), reflectable->find("nested.creatorEnum")->getInt8()); |
443 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), |
444 | 1 | reflectable->find("nested.creatorBitmask")->getUInt8()); |
445 | 1 | } |
446 | | |
447 | | TEST(JsonReaderTest, readEmptyBitBuffer) |
448 | 1 | { |
449 | 1 | std::stringstream str( |
450 | 1 | "{\n" |
451 | 1 | " \"value\": 13,\n" |
452 | 1 | " \"nested\": {\n" |
453 | 1 | " \"value\": 10,\n" |
454 | 1 | " \"text\": \"nested\",\n" |
455 | 1 | " \"externData\": {\n" |
456 | 1 | " \"buffer\": [\n" |
457 | 1 | " ],\n" |
458 | 1 | " \"bitSize\": 0\n" |
459 | 1 | " },\n" |
460 | 1 | " \"bytesData\": {\n" |
461 | 1 | " \"buffer\": [\n" |
462 | 1 | " 202,\n" |
463 | 1 | " 254\n" |
464 | 1 | " ]\n" |
465 | 1 | " },\n" |
466 | 1 | " \"creatorEnum\": -1,\n" |
467 | 1 | " \"creatorBitmask\": 1\n" |
468 | 1 | " }\n" |
469 | 1 | "}"); |
470 | | |
471 | 1 | JsonReader jsonReader(str); |
472 | 1 | auto reflectable = jsonReader.read(CreatorObject::typeInfo()); |
473 | 1 | ASSERT_TRUE(reflectable); |
474 | | |
475 | 1 | reflectable->initializeChildren(); |
476 | | |
477 | 1 | ASSERT_EQ(13, reflectable->getField("value")->getUInt32()); |
478 | 1 | ASSERT_EQ(13, reflectable->getField("nested")->getParameter("param")->getUInt32()); |
479 | 1 | ASSERT_EQ(10, reflectable->find("nested.value")->getUInt32()); |
480 | 1 | ASSERT_EQ("nested"_sv, reflectable->find("nested.text")->getStringView()); |
481 | 1 | ASSERT_EQ(BitBuffer(), reflectable->find("nested.externData")->getBitBuffer()); |
482 | 1 | const Span<const uint8_t> bytesData = reflectable->find("nested.bytesData")->getBytes(); |
483 | 1 | ASSERT_EQ(2, bytesData.size()); |
484 | 1 | ASSERT_EQ(0xCA, bytesData[0]); |
485 | 1 | ASSERT_EQ(0xFE, bytesData[1]); |
486 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::MinusOne), reflectable->find("nested.creatorEnum")->getInt8()); |
487 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), |
488 | 1 | reflectable->find("nested.creatorBitmask")->getUInt8()); |
489 | 1 | } |
490 | | |
491 | | TEST(JsonReaderTest, readEmptyBytes) |
492 | 1 | { |
493 | 1 | std::stringstream str( |
494 | 1 | "{\n" |
495 | 1 | " \"value\": 13,\n" |
496 | 1 | " \"nested\": {\n" |
497 | 1 | " \"value\": 10,\n" |
498 | 1 | " \"text\": \"nested\",\n" |
499 | 1 | " \"externData\": {\n" |
500 | 1 | " \"bitSize\": 0,\n" |
501 | 1 | " \"buffer\": [\n" |
502 | 1 | " ]\n" |
503 | 1 | " },\n" |
504 | 1 | " \"bytesData\": {\n" |
505 | 1 | " \"buffer\": [\n" |
506 | 1 | " ]\n" |
507 | 1 | " },\n" |
508 | 1 | " \"creatorEnum\": -1,\n" |
509 | 1 | " \"creatorBitmask\": 1\n" |
510 | 1 | " }\n" |
511 | 1 | "}"); |
512 | | |
513 | 1 | JsonReader jsonReader(str); |
514 | 1 | auto reflectable = jsonReader.read(CreatorObject::typeInfo()); |
515 | 1 | ASSERT_TRUE(reflectable); |
516 | | |
517 | 1 | reflectable->initializeChildren(); |
518 | | |
519 | 1 | ASSERT_EQ(13, reflectable->getField("value")->getUInt32()); |
520 | 1 | ASSERT_EQ(13, reflectable->getField("nested")->getParameter("param")->getUInt32()); |
521 | 1 | ASSERT_EQ(10, reflectable->find("nested.value")->getUInt32()); |
522 | 1 | ASSERT_EQ("nested"_sv, reflectable->find("nested.text")->getStringView()); |
523 | 1 | ASSERT_EQ(BitBuffer(), reflectable->find("nested.externData")->getBitBuffer()); |
524 | 1 | const Span<const uint8_t> bytesData = reflectable->find("nested.bytesData")->getBytes(); |
525 | 1 | ASSERT_EQ(0, bytesData.size()); |
526 | 1 | ASSERT_EQ(enumToValue(CreatorEnum::MinusOne), reflectable->find("nested.creatorEnum")->getInt8()); |
527 | 1 | ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), |
528 | 1 | reflectable->find("nested.creatorBitmask")->getUInt8()); |
529 | 1 | } |
530 | | |
531 | | TEST(JsonReaderTest, readStringifiedEnum) |
532 | 1 | { |
533 | 1 | checkReadStringifiedEnum("ONE", CreatorEnum::ONE); |
534 | 1 | checkReadStringifiedEnum("MinusOne", CreatorEnum::MinusOne); |
535 | 1 | checkReadStringifiedEnumThrows("NONEXISTING", |
536 | 1 | "ZserioTreeCreator: Cannot create enum 'test_object.std_allocator.CreatorEnum' " |
537 | 1 | "from string value 'NONEXISTING'! (JsonParser:3:24)"); |
538 | 1 | checkReadStringifiedEnumThrows("***", |
539 | 1 | "ZserioTreeCreator: Cannot create enum 'test_object.std_allocator.CreatorEnum' " |
540 | 1 | "from string value '***'! (JsonParser:3:24)"); |
541 | 1 | checkReadStringifiedEnumThrows("10 /* no match */", |
542 | 1 | "ZserioTreeCreator: Cannot create enum 'test_object.std_allocator.CreatorEnum' " |
543 | 1 | "from string value '10 /* no match */'! (JsonParser:3:24)"); |
544 | 1 | checkReadStringifiedEnumThrows("-10 /* no match */", |
545 | 1 | "ZserioTreeCreator: Cannot create enum 'test_object.std_allocator.CreatorEnum' " |
546 | 1 | "from string value '-10 /* no match */'! (JsonParser:3:24)"); |
547 | 1 | checkReadStringifiedEnumThrows("", |
548 | 1 | "ZserioTreeCreator: Cannot create enum 'test_object.std_allocator.CreatorEnum' " |
549 | 1 | "from string value ''! (JsonParser:3:24)"); |
550 | 1 | } |
551 | | |
552 | | TEST(JsonReaderTest, readStringifiedBitmask) |
553 | 1 | { |
554 | 1 | checkReadStringifiedBitmask("READ", CreatorBitmask::Values::READ); |
555 | 1 | checkReadStringifiedBitmask("READ | WRITE", CreatorBitmask::Values::READ | CreatorBitmask::Values::WRITE); |
556 | 1 | checkReadStringifiedBitmaskThrows("NONEXISTING", |
557 | 1 | "ZserioTreeCreator: Cannot create bitmask 'test_object.std_allocator.CreatorBitmask' " |
558 | 1 | "from string value 'NONEXISTING'! (JsonParser:3:27)"); |
559 | 1 | checkReadStringifiedBitmaskThrows("READ | NONEXISTING", |
560 | 1 | "ZserioTreeCreator: Cannot create bitmask 'test_object.std_allocator.CreatorBitmask' " |
561 | 1 | "from string value 'READ | NONEXISTING'! (JsonParser:3:27)"); |
562 | 1 | checkReadStringifiedBitmaskThrows("READ * NONEXISTING", |
563 | 1 | "ZserioTreeCreator: Cannot create bitmask 'test_object.std_allocator.CreatorBitmask' " |
564 | 1 | "from string value 'READ * NONEXISTING'! (JsonParser:3:27)"); |
565 | 1 | checkReadStringifiedBitmask("7 /* READ | WRITE */", CreatorBitmask(7)); |
566 | 1 | checkReadStringifiedBitmask("15 /* READ | WRITE */", CreatorBitmask(15)); |
567 | 1 | checkReadStringifiedBitmask("4 /* no match */", CreatorBitmask(4)); |
568 | 1 | checkReadStringifiedBitmaskThrows("", |
569 | 1 | "ZserioTreeCreator: Cannot create bitmask 'test_object.std_allocator.CreatorBitmask' " |
570 | 1 | "from string value ''! (JsonParser:3:27)"); |
571 | 1 | checkReadStringifiedBitmaskThrows(" ", |
572 | 1 | "ZserioTreeCreator: Cannot create bitmask 'test_object.std_allocator.CreatorBitmask' " |
573 | 1 | "from string value ' '! (JsonParser:3:27)"); |
574 | 1 | checkReadStringifiedBitmaskThrows(" | ", |
575 | 1 | "ZserioTreeCreator: Cannot create bitmask 'test_object.std_allocator.CreatorBitmask' " |
576 | 1 | "from string value ' | '! (JsonParser:3:27)"); |
577 | 1 | } |
578 | | |
579 | | TEST(JsonReaderTest, jsonParserException) |
580 | 1 | { |
581 | 1 | std::stringstream str("{\"value\"\n\"value\""); |
582 | 1 | JsonReader jsonReader(str); |
583 | | |
584 | 1 | ASSERT_THROW( |
585 | 1 | { |
586 | 1 | try |
587 | 1 | { |
588 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
589 | 1 | } |
590 | 1 | catch (const JsonParserException& e) |
591 | 1 | { |
592 | 1 | ASSERT_STREQ("JsonParser:2:1: unexpected token: VALUE, expecting KEY_SEPARATOR!", e.what()); |
593 | 1 | throw; |
594 | 1 | } |
595 | 1 | }, |
596 | 1 | JsonParserException); |
597 | 1 | } |
598 | | |
599 | | TEST(JsonReaderTest, wrongKeyException) |
600 | 1 | { |
601 | 1 | std::stringstream str("{\"value\": 13,\n\"nonexisting\": 10}"); |
602 | 1 | JsonReader jsonReader(str); |
603 | | |
604 | 1 | ASSERT_THROW( |
605 | 1 | { |
606 | 1 | try |
607 | 1 | { |
608 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
609 | 1 | } |
610 | 1 | catch (const CppRuntimeException& e) |
611 | 1 | { |
612 | 1 | ASSERT_STREQ("ZserioTreeCreator: Member 'nonexisting' not found in " |
613 | 1 | "'test_object.std_allocator.CreatorObject'! (JsonParser:2:16)", |
614 | 1 | e.what()); |
615 | 1 | throw; |
616 | 1 | } |
617 | 1 | }, |
618 | 1 | CppRuntimeException); |
619 | 1 | } |
620 | | |
621 | | TEST(JsonReaderTest, wrongValueTypeException) |
622 | 1 | { |
623 | 1 | std::stringstream str("{\n \"value\": \"13\"\n}"); |
624 | 1 | JsonReader jsonReader(str); |
625 | | |
626 | 1 | ASSERT_THROW( |
627 | 1 | { |
628 | 1 | try |
629 | 1 | { |
630 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
631 | 1 | } |
632 | 1 | catch (const CppRuntimeException& e) |
633 | 1 | { |
634 | 1 | ASSERT_STREQ("ZserioTreeCreator: Value '13' cannot be converted to integral value! " |
635 | 1 | "(JsonParser:2:12)", |
636 | 1 | e.what()); |
637 | 1 | throw; |
638 | 1 | } |
639 | 1 | }, |
640 | 1 | CppRuntimeException); |
641 | 1 | } |
642 | | |
643 | | TEST(JsonReaderTest, wrongBitBufferException) |
644 | 1 | { |
645 | 1 | std::stringstream str( |
646 | 1 | "{\n" |
647 | 1 | " \"value\": 13,\n" |
648 | 1 | " \"nested\": {\n" |
649 | 1 | " \"value\": 10,\n" |
650 | 1 | " \"text\": \"nested\",\n" |
651 | 1 | " \"externData\": {\n" |
652 | 1 | " \"buffer\": [\n" |
653 | 1 | " 203,\n" |
654 | 1 | " 240\n" |
655 | 1 | " ],\n" |
656 | 1 | " \"bitSize\": {\n" |
657 | 1 | " }\n" |
658 | 1 | " }\n" |
659 | 1 | " }\n" |
660 | 1 | "}"); |
661 | 1 | JsonReader jsonReader(str); |
662 | | |
663 | 1 | ASSERT_THROW( |
664 | 1 | { |
665 | 1 | try |
666 | 1 | { |
667 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
668 | 1 | } |
669 | 1 | catch (const CppRuntimeException& e) |
670 | 1 | { |
671 | 1 | ASSERT_STREQ( |
672 | 1 | "JsonReader: Unexpected beginObject in BitBuffer! (JsonParser:12:14)", e.what()); |
673 | 1 | throw; |
674 | 1 | } |
675 | 1 | }, |
676 | 1 | CppRuntimeException); |
677 | 1 | } |
678 | | |
679 | | TEST(JsonReaderTest, partialBitBufferException) |
680 | 1 | { |
681 | 1 | std::stringstream str( |
682 | 1 | "{\n" |
683 | 1 | " \"value\": 13,\n" |
684 | 1 | " \"nested\": {\n" |
685 | 1 | " \"value\": 10,\n" |
686 | 1 | " \"text\": \"nested\",\n" |
687 | 1 | " \"externData\": {\n" |
688 | 1 | " \"buffer\": [\n" |
689 | 1 | " 203,\n" |
690 | 1 | " 240\n" |
691 | 1 | " ]\n" |
692 | 1 | " }\n" |
693 | 1 | " }\n" |
694 | 1 | "}"); |
695 | 1 | JsonReader jsonReader(str); |
696 | | |
697 | 1 | ASSERT_THROW( |
698 | 1 | { |
699 | 1 | try |
700 | 1 | { |
701 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
702 | 1 | } |
703 | 1 | catch (const CppRuntimeException& e) |
704 | 1 | { |
705 | 1 | ASSERT_STREQ("JsonReader: Unexpected end in BitBuffer! (JsonParser:12:5)", e.what()); |
706 | 1 | throw; |
707 | 1 | } |
708 | 1 | }, |
709 | 1 | CppRuntimeException); |
710 | 1 | } |
711 | | |
712 | | TEST(JsonReaderTest, bigBitBufferByteValueException) |
713 | 1 | { |
714 | 1 | std::stringstream str( |
715 | 1 | "{\n" |
716 | 1 | " \"nested\": {\n" |
717 | 1 | " \"externData\": {\n" |
718 | 1 | " \"buffer\": [\n" |
719 | 1 | " 256\n" |
720 | 1 | " ],\n" |
721 | 1 | " \"bitSize\": 7\n" |
722 | 1 | " }\n" |
723 | 1 | " }\n" |
724 | 1 | "}"); |
725 | 1 | JsonReader jsonReader(str); |
726 | | |
727 | 1 | ASSERT_THROW( |
728 | 1 | { |
729 | 1 | try |
730 | 1 | { |
731 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
732 | 1 | } |
733 | 1 | catch (const CppRuntimeException& e) |
734 | 1 | { |
735 | 1 | ASSERT_STREQ( |
736 | 1 | "JsonReader: Cannot create byte for Bit Buffer from value '256'! (JsonParser:5:18)", |
737 | 1 | e.what()); |
738 | 1 | throw; |
739 | 1 | } |
740 | 1 | }, |
741 | 1 | CppRuntimeException); |
742 | 1 | } |
743 | | |
744 | | TEST(JsonReaderTest, negativeBitBufferByteValueException) |
745 | 1 | { |
746 | 1 | std::stringstream str( |
747 | 1 | "{\n" |
748 | 1 | " \"nested\": {\n" |
749 | 1 | " \"externData\": {\n" |
750 | 1 | " \"buffer\": [\n" |
751 | 1 | " -1\n" |
752 | 1 | " ],\n" |
753 | 1 | " \"bitSize\": 7\n" |
754 | 1 | " }\n" |
755 | 1 | " }\n" |
756 | 1 | "}"); |
757 | 1 | JsonReader jsonReader(str); |
758 | | |
759 | 1 | ASSERT_THROW( |
760 | 1 | { |
761 | 1 | try |
762 | 1 | { |
763 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
764 | 1 | } |
765 | 1 | catch (const CppRuntimeException& e) |
766 | 1 | { |
767 | 1 | ASSERT_STREQ("JsonReader: Unexpected visitValue (int) in BitBuffer! (JsonParser:5:18)", |
768 | 1 | e.what()); |
769 | 1 | throw; |
770 | 1 | } |
771 | 1 | }, |
772 | 1 | CppRuntimeException); |
773 | 1 | } |
774 | | |
775 | | TEST(JsonReaderTest, wrongBitBufferSizeValueException) |
776 | 1 | { |
777 | 1 | std::stringstream str( |
778 | 1 | "{\n" |
779 | 1 | " \"nested\": {\n" |
780 | 1 | " \"externData\": {\n" |
781 | 1 | " \"buffer\": [\n" |
782 | 1 | " 255\n" |
783 | 1 | " ],\n" |
784 | 1 | " \"bitSize\": 18446744073709551616\n" |
785 | 1 | " }\n" |
786 | 1 | " }\n" |
787 | 1 | "}"); |
788 | 1 | JsonReader jsonReader(str); |
789 | | |
790 | 1 | ASSERT_THROW( |
791 | 1 | { |
792 | 1 | try |
793 | 1 | { |
794 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
795 | 1 | } |
796 | 1 | catch (const CppRuntimeException& e) |
797 | 1 | { |
798 | 1 | ASSERT_STREQ("JsonTokenizer:7:25: Value is outside of the 64-bit integer range!", e.what()); |
799 | 1 | throw; |
800 | 1 | } |
801 | 1 | }, |
802 | 1 | CppRuntimeException); |
803 | 1 | } |
804 | | |
805 | | TEST(JsonReaderTest, wrongBitBufferNullPtrValueException) |
806 | 1 | { |
807 | 1 | std::stringstream str( |
808 | 1 | "{\n" |
809 | 1 | " \"nested\": {\n" |
810 | 1 | " \"externData\": {\n" |
811 | 1 | " \"buffer\": [\n" |
812 | 1 | " 255\n" |
813 | 1 | " ],\n" |
814 | 1 | " \"bitSize\": null\n" |
815 | 1 | " }\n" |
816 | 1 | " }\n" |
817 | 1 | "}"); |
818 | 1 | JsonReader jsonReader(str); |
819 | | |
820 | 1 | ASSERT_THROW( |
821 | 1 | { |
822 | 1 | try |
823 | 1 | { |
824 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
825 | 1 | } |
826 | 1 | catch (const CppRuntimeException& e) |
827 | 1 | { |
828 | 1 | ASSERT_STREQ("JsonReader: Unexpected visitValue (null) in BitBuffer! (JsonParser:7:25)", |
829 | 1 | e.what()); |
830 | 1 | throw; |
831 | 1 | } |
832 | 1 | }, |
833 | 1 | CppRuntimeException); |
834 | 1 | } |
835 | | |
836 | | TEST(JsonReaderTest, wrongBitBufferBoolValueException) |
837 | 1 | { |
838 | 1 | std::stringstream str( |
839 | 1 | "{\n" |
840 | 1 | " \"nested\": {\n" |
841 | 1 | " \"externData\": {\n" |
842 | 1 | " \"buffer\": [\n" |
843 | 1 | " 255\n" |
844 | 1 | " ],\n" |
845 | 1 | " \"bitSize\": true\n" |
846 | 1 | " }\n" |
847 | 1 | " }\n" |
848 | 1 | "}"); |
849 | 1 | JsonReader jsonReader(str); |
850 | | |
851 | 1 | ASSERT_THROW( |
852 | 1 | { |
853 | 1 | try |
854 | 1 | { |
855 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
856 | 1 | } |
857 | 1 | catch (const CppRuntimeException& e) |
858 | 1 | { |
859 | 1 | ASSERT_STREQ("JsonReader: Unexpected visitValue (bool) in BitBuffer! (JsonParser:7:25)", |
860 | 1 | e.what()); |
861 | 1 | throw; |
862 | 1 | } |
863 | 1 | }, |
864 | 1 | CppRuntimeException); |
865 | 1 | } |
866 | | |
867 | | TEST(JsonReaderTest, wrongBitBufferDoubleValueException) |
868 | 1 | { |
869 | 1 | std::stringstream str( |
870 | 1 | "{\n" |
871 | 1 | " \"nested\": {\n" |
872 | 1 | " \"externData\": {\n" |
873 | 1 | " \"buffer\": [\n" |
874 | 1 | " 255\n" |
875 | 1 | " ],\n" |
876 | 1 | " \"bitSize\": 1.0\n" |
877 | 1 | " }\n" |
878 | 1 | " }\n" |
879 | 1 | "}"); |
880 | 1 | JsonReader jsonReader(str); |
881 | | |
882 | 1 | ASSERT_THROW( |
883 | 1 | { |
884 | 1 | try |
885 | 1 | { |
886 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
887 | 1 | } |
888 | 1 | catch (const CppRuntimeException& e) |
889 | 1 | { |
890 | 1 | ASSERT_STREQ("JsonReader: Unexpected visitValue (double) in BitBuffer! (JsonParser:7:25)", |
891 | 1 | e.what()); |
892 | 1 | throw; |
893 | 1 | } |
894 | 1 | }, |
895 | 1 | CppRuntimeException); |
896 | 1 | } |
897 | | |
898 | | TEST(JsonReaderTest, wrongBitBufferStringValueException) |
899 | 1 | { |
900 | 1 | std::stringstream str( |
901 | 1 | "{\n" |
902 | 1 | " \"nested\": {\n" |
903 | 1 | " \"externData\": {\n" |
904 | 1 | " \"buffer\": [\n" |
905 | 1 | " 255\n" |
906 | 1 | " ],\n" |
907 | 1 | " \"bitSize\": \"Text\"\n" |
908 | 1 | " }\n" |
909 | 1 | " }\n" |
910 | 1 | "}"); |
911 | 1 | JsonReader jsonReader(str); |
912 | | |
913 | 1 | ASSERT_THROW( |
914 | 1 | { |
915 | 1 | try |
916 | 1 | { |
917 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
918 | 1 | } |
919 | 1 | catch (const CppRuntimeException& e) |
920 | 1 | { |
921 | 1 | ASSERT_STREQ("JsonReader: Unexpected visitValue (string) in BitBuffer! (JsonParser:7:25)", |
922 | 1 | e.what()); |
923 | 1 | throw; |
924 | 1 | } |
925 | 1 | }, |
926 | 1 | CppRuntimeException); |
927 | 1 | } |
928 | | |
929 | | TEST(JsonReaderTest, wrongBytesException) |
930 | 1 | { |
931 | 1 | std::stringstream str( |
932 | 1 | "{\n" |
933 | 1 | " \"value\": 13,\n" |
934 | 1 | " \"nested\": {\n" |
935 | 1 | " \"value\": 10,\n" |
936 | 1 | " \"text\": \"nested\",\n" |
937 | 1 | " \"externData\": {\n" |
938 | 1 | " \"buffer\": [\n" |
939 | 1 | " 203,\n" |
940 | 1 | " 240\n" |
941 | 1 | " ],\n" |
942 | 1 | " \"bitSize\": 12\n" |
943 | 1 | " },\n" |
944 | 1 | " \"bytesData\" : {\n" |
945 | 1 | " \"buffer\" : {}\n" |
946 | 1 | " }\n" |
947 | 1 | " }\n" |
948 | 1 | "}"); |
949 | 1 | JsonReader jsonReader(str); |
950 | | |
951 | 1 | ASSERT_THROW( |
952 | 1 | { |
953 | 1 | try |
954 | 1 | { |
955 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
956 | 1 | } |
957 | 1 | catch (const CppRuntimeException& e) |
958 | 1 | { |
959 | 1 | ASSERT_STREQ("JsonReader: Unexpected beginObject in bytes! (JsonParser:14:25)", e.what()); |
960 | 1 | throw; |
961 | 1 | } |
962 | 1 | }, |
963 | 1 | CppRuntimeException); |
964 | 1 | } |
965 | | |
966 | | TEST(JsonReaderTest, negativeBytesByteValueException) |
967 | 1 | { |
968 | 1 | std::stringstream str( |
969 | 1 | "{\n" |
970 | 1 | " \"nested\": {\n" |
971 | 1 | " \"bytesData\": {\n" |
972 | 1 | " \"buffer\": [\n" |
973 | 1 | " -1\n" |
974 | 1 | " ]\n" |
975 | 1 | " }\n" |
976 | 1 | " }\n" |
977 | 1 | "}"); |
978 | 1 | JsonReader jsonReader(str); |
979 | | |
980 | 1 | ASSERT_THROW( |
981 | 1 | { |
982 | 1 | try |
983 | 1 | { |
984 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
985 | 1 | } |
986 | 1 | catch (const CppRuntimeException& e) |
987 | 1 | { |
988 | 1 | ASSERT_STREQ( |
989 | 1 | "JsonReader: Unexpected visitValue (int) in bytes! (JsonParser:5:18)", e.what()); |
990 | 1 | throw; |
991 | 1 | } |
992 | 1 | }, |
993 | 1 | CppRuntimeException); |
994 | 1 | } |
995 | | |
996 | | TEST(JsonReaderTest, jsonArrayException) |
997 | 1 | { |
998 | 1 | std::stringstream str("[1, 2]"); |
999 | 1 | JsonReader jsonReader(str); |
1000 | | |
1001 | 1 | ASSERT_THROW(jsonReader.read(CreatorObject::typeInfo()), CppRuntimeException); |
1002 | 1 | } |
1003 | | |
1004 | | TEST(JsonReaderTest, jsonValueException) |
1005 | 1 | { |
1006 | 1 | std::stringstream str("\"text\""); |
1007 | 1 | JsonReader jsonReader(str); |
1008 | | |
1009 | 1 | ASSERT_THROW(jsonReader.read(CreatorObject::typeInfo()), CppRuntimeException); |
1010 | 1 | } |
1011 | | |
1012 | | TEST(JsonReaderTest, bigLongValueException) |
1013 | 1 | { |
1014 | 1 | std::stringstream str( |
1015 | 1 | "{\n" |
1016 | 1 | " \"value\": 4294967296\n" |
1017 | 1 | "}"); |
1018 | 1 | JsonReader jsonReader(str); |
1019 | | |
1020 | 1 | ASSERT_THROW( |
1021 | 1 | { |
1022 | 1 | try |
1023 | 1 | { |
1024 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
1025 | 1 | } |
1026 | 1 | catch (const CppRuntimeException& e) |
1027 | 1 | { |
1028 | 1 | ASSERT_STREQ("ZserioTreeCreator: Integral value '4294967296' overflow (<0, 4294967295>)! " |
1029 | 1 | "(JsonParser:2:14)", |
1030 | 1 | e.what()); |
1031 | 1 | throw; |
1032 | 1 | } |
1033 | 1 | }, |
1034 | 1 | CppRuntimeException); |
1035 | 1 | } |
1036 | | |
1037 | | TEST(JsonReaderTest, floatLongValueException) |
1038 | 1 | { |
1039 | 1 | std::stringstream str( |
1040 | 1 | "{\n" |
1041 | 1 | " \"value\": 1.234\n" |
1042 | 1 | "}"); |
1043 | 1 | JsonReader jsonReader(str); |
1044 | | |
1045 | 1 | ASSERT_THROW( |
1046 | 1 | { |
1047 | 1 | try |
1048 | 1 | { |
1049 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
1050 | 1 | } |
1051 | 1 | catch (const CppRuntimeException& e) |
1052 | 1 | { |
1053 | 1 | ASSERT_STREQ("ZserioTreeCreator: Value '1.233' cannot be converted to integral value! " |
1054 | 1 | "(JsonParser:2:14)", |
1055 | 1 | e.what()); |
1056 | 1 | throw; |
1057 | 1 | } |
1058 | 1 | }, |
1059 | 1 | CppRuntimeException); |
1060 | 1 | } |
1061 | | |
1062 | | TEST(JsonReaderTest, bigBytesByteValueException) |
1063 | 1 | { |
1064 | 1 | std::stringstream str( |
1065 | 1 | "{\n" |
1066 | 1 | " \"nested\": {\n" |
1067 | 1 | " \"bytesData\": {\n" |
1068 | 1 | " \"buffer\": [\n" |
1069 | 1 | " 256\n" |
1070 | 1 | " ]\n" |
1071 | 1 | " }\n" |
1072 | 1 | " }\n" |
1073 | 1 | "}"); |
1074 | 1 | JsonReader jsonReader(str); |
1075 | | |
1076 | 1 | ASSERT_THROW( |
1077 | 1 | { |
1078 | 1 | try |
1079 | 1 | { |
1080 | 1 | jsonReader.read(CreatorObject::typeInfo()); |
1081 | 1 | } |
1082 | 1 | catch (const CppRuntimeException& e) |
1083 | 1 | { |
1084 | 1 | ASSERT_STREQ("JsonReader: Cannot create byte for bytes from value '256'! (JsonParser:5:18)", |
1085 | 1 | e.what()); |
1086 | 1 | throw; |
1087 | 1 | } |
1088 | 1 | }, |
1089 | 1 | CppRuntimeException); |
1090 | 1 | } |
1091 | | |
1092 | | TEST(JsonReaderTest, bytesAdapterUninitializedCalls) |
1093 | 1 | { |
1094 | 1 | detail::BytesAdapter<std::allocator<uint8_t>> bytesAdapter{std::allocator<uint8_t>()}; |
1095 | | |
1096 | 1 | ASSERT_THROW(bytesAdapter.get(), CppRuntimeException); |
1097 | 1 | ASSERT_THROW(bytesAdapter.beginObject(), CppRuntimeException); |
1098 | 1 | ASSERT_THROW(bytesAdapter.endObject(), CppRuntimeException); |
1099 | 1 | ASSERT_THROW(bytesAdapter.beginArray(), CppRuntimeException); |
1100 | 1 | ASSERT_THROW(bytesAdapter.endArray(), CppRuntimeException); |
1101 | 1 | ASSERT_THROW(bytesAdapter.visitKey("nonexisting"_sv), CppRuntimeException); |
1102 | 1 | ASSERT_THROW(bytesAdapter.visitValue(static_cast<uint64_t>(10)), CppRuntimeException); |
1103 | 1 | bytesAdapter.visitKey("buffer"_sv); |
1104 | 1 | ASSERT_THROW(bytesAdapter.get(), CppRuntimeException); |
1105 | 1 | ASSERT_THROW(bytesAdapter.visitKey("nonexisting"_sv), CppRuntimeException); |
1106 | 1 | ASSERT_THROW(bytesAdapter.visitValue(nullptr), CppRuntimeException); |
1107 | 1 | ASSERT_THROW(bytesAdapter.visitValue(true), CppRuntimeException); |
1108 | 1 | ASSERT_THROW(bytesAdapter.visitValue(static_cast<int64_t>(-1)), CppRuntimeException); |
1109 | 1 | ASSERT_THROW(bytesAdapter.visitValue(static_cast<uint64_t>(1)), CppRuntimeException); |
1110 | 1 | ASSERT_THROW(bytesAdapter.visitValue(0.0), CppRuntimeException); |
1111 | 1 | ASSERT_THROW(bytesAdapter.visitValue("BadValue"_sv), CppRuntimeException); |
1112 | 1 | } |
1113 | | |
1114 | | TEST(JsonReaderTest, bitBufferAdapterUninitializedCalls) |
1115 | 1 | { |
1116 | 1 | detail::BitBufferAdapter<std::allocator<uint8_t>> bitBufferAdapter{std::allocator<uint8_t>()}; |
1117 | | |
1118 | 1 | ASSERT_THROW(bitBufferAdapter.get(), CppRuntimeException); |
1119 | 1 | ASSERT_THROW(bitBufferAdapter.beginObject(), CppRuntimeException); |
1120 | 1 | ASSERT_THROW(bitBufferAdapter.endObject(), CppRuntimeException); |
1121 | 1 | ASSERT_THROW(bitBufferAdapter.beginArray(), CppRuntimeException); |
1122 | 1 | ASSERT_THROW(bitBufferAdapter.endArray(), CppRuntimeException); |
1123 | 1 | ASSERT_THROW(bitBufferAdapter.visitKey("nonexisting"_sv), CppRuntimeException); |
1124 | 1 | ASSERT_THROW(bitBufferAdapter.visitValue(static_cast<uint64_t>(10)), CppRuntimeException); |
1125 | 1 | bitBufferAdapter.visitKey("buffer"_sv); |
1126 | 1 | ASSERT_THROW(bitBufferAdapter.get(), CppRuntimeException); |
1127 | 1 | ASSERT_THROW(bitBufferAdapter.visitKey("nonexisting"_sv), CppRuntimeException); |
1128 | 1 | ASSERT_THROW(bitBufferAdapter.visitValue(nullptr), CppRuntimeException); |
1129 | 1 | ASSERT_THROW(bitBufferAdapter.visitValue(true), CppRuntimeException); |
1130 | 1 | ASSERT_THROW(bitBufferAdapter.visitValue(static_cast<int64_t>(-1)), CppRuntimeException); |
1131 | 1 | ASSERT_THROW(bitBufferAdapter.visitValue(static_cast<uint64_t>(1)), CppRuntimeException); |
1132 | 1 | ASSERT_THROW(bitBufferAdapter.visitValue(0.0), CppRuntimeException); |
1133 | 1 | ASSERT_THROW(bitBufferAdapter.visitValue("BadValue"_sv), CppRuntimeException); |
1134 | 1 | } |
1135 | | |
1136 | | TEST(JsonReaderTest, creatorAdapter) |
1137 | 1 | { |
1138 | 1 | detail::CreatorAdapter<std::allocator<uint8_t>> creatorAdapter{std::allocator<uint8_t>()}; |
1139 | | |
1140 | 1 | ASSERT_THROW(creatorAdapter.get(), CppRuntimeException); |
1141 | 1 | ASSERT_THROW(creatorAdapter.beginObject(), CppRuntimeException); |
1142 | 1 | ASSERT_THROW(creatorAdapter.endObject(), CppRuntimeException); |
1143 | 1 | ASSERT_THROW(creatorAdapter.beginArray(), CppRuntimeException); |
1144 | 1 | ASSERT_THROW(creatorAdapter.endArray(), CppRuntimeException); |
1145 | 1 | ASSERT_THROW(creatorAdapter.visitKey("nonexistent"_sv), CppRuntimeException); |
1146 | 1 | ASSERT_THROW(creatorAdapter.visitValue(nullptr), CppRuntimeException); |
1147 | 1 | ASSERT_THROW(creatorAdapter.visitValue(true), CppRuntimeException); |
1148 | 1 | ASSERT_THROW(creatorAdapter.visitValue(static_cast<int64_t>(-1)), CppRuntimeException); |
1149 | 1 | ASSERT_THROW(creatorAdapter.visitValue(static_cast<uint64_t>(1)), CppRuntimeException); |
1150 | 1 | ASSERT_THROW(creatorAdapter.visitValue(0.0), CppRuntimeException); |
1151 | 1 | ASSERT_THROW(creatorAdapter.visitValue("BadValue"_sv), CppRuntimeException); |
1152 | 1 | } |
1153 | | |
1154 | | } // namespace zserio |