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