Coverage Report

Created: 2024-07-18 11:41

test/zserio/DebugStringUtilTest.cpp
Line
Count
Source
1
#include <array>
2
#include <sstream>
3
4
#include "gtest/gtest.h"
5
#include "test_object/polymorphic_allocator/DebugStringObject.h"
6
#include "test_object/polymorphic_allocator/DebugStringParamObject.h"
7
#include "test_object/std_allocator/DebugStringObject.h"
8
#include "test_object/std_allocator/DebugStringParamObject.h"
9
#include "zserio/CppRuntimeException.h"
10
#include "zserio/DebugStringUtil.h"
11
#include "zserio/Reflectable.h"
12
#include "zserio/StringView.h"
13
#include "zserio/pmr/PolymorphicAllocator.h"
14
15
using StdDebugStringObject = test_object::std_allocator::DebugStringObject;
16
using StdDebugStringParamObject = test_object::std_allocator::DebugStringParamObject;
17
using PmrDebugStringObject = test_object::polymorphic_allocator::DebugStringObject;
18
using PmrDebugStringParamObject = test_object::polymorphic_allocator::DebugStringParamObject;
19
20
using std_alloc = std::allocator<uint8_t>;
21
using pmr_alloc = zserio::pmr::PropagatingPolymorphicAllocator<uint8_t>;
22
23
namespace zserio
24
{
25
26
TEST(DebugStringUtilTest, toJsonStreamDefault)
27
1
{
28
1
    std::ostringstream stream;
29
1
    StdDebugStringObject debugStringObject;
30
1
    toJsonStream(debugStringObject, stream);
31
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stream.str());
32
1
}
33
34
TEST(DebugStringUtilTest, toJsonStreamDefaultWithAlloc)
35
1
{
36
1
    std::ostringstream stream;
37
1
    const StdDebugStringObject debugStringObject;
38
1
    toJsonStream(debugStringObject, stream, std_alloc());
39
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stream.str());
40
1
}
41
42
TEST(DebugStringUtilTest, toJsonStreamDefaultWithPolymorphicAlloc)
43
1
{
44
1
    std::ostringstream stream;
45
1
    const PmrDebugStringObject debugStringObject;
46
1
    toJsonStream(debugStringObject, stream, pmr_alloc());
47
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stream.str());
48
1
}
49
50
TEST(DebugStringUtilTest, toJsonStreamIndent2)
51
1
{
52
1
    std::ostringstream stream;
53
1
    const StdDebugStringObject debugStringObject;
54
1
    toJsonStream(debugStringObject, stream, 2);
55
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stream.str());
56
1
}
57
58
TEST(DebugStringUtilTest, toJsonStreamIndent2WithAlloc)
59
1
{
60
1
    std::ostringstream stream;
61
1
    const StdDebugStringObject debugStringObject;
62
1
    toJsonStream(debugStringObject, stream, 2, std_alloc());
63
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stream.str());
64
1
}
65
66
TEST(DebugStringUtilTest, toJsonStreamIndent2WithPolymorphicAlloc)
67
1
{
68
1
    std::ostringstream stream;
69
1
    const PmrDebugStringObject debugStringObject;
70
1
    toJsonStream(debugStringObject, stream, 2, pmr_alloc());
71
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stream.str());
72
1
}
73
74
TEST(DebugStringUtilTest, toJsonStreamFilter)
75
1
{
76
1
    std::ostringstream stream;
77
1
    const StdDebugStringObject debugStringObject;
78
1
    toJsonStream(debugStringObject, stream, DepthWalkFilter(0));
79
1
    ASSERT_EQ("{\n}", stream.str());
80
1
}
81
82
TEST(DebugStringUtilTest, toJsonStreamFilterWithAlloc)
83
1
{
84
1
    std::ostringstream stream;
85
1
    const StdDebugStringObject debugStringObject;
86
1
    toJsonStream(debugStringObject, stream, DefaultWalkFilter(), std_alloc());
87
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stream.str());
88
1
}
89
90
TEST(DebugStringUtilTest, toJsonStreamFilterWithPolymorphicAlloc)
91
1
{
92
1
    std::ostringstream stream;
93
1
    const PmrDebugStringObject debugStringObject;
94
1
    toJsonStream(debugStringObject, stream, BasicDefaultWalkFilter<pmr_alloc>(), pmr_alloc());
95
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stream.str());
96
1
}
97
98
TEST(DebugStringUtilTest, toJsonStreamIndent2Filter)
99
1
{
100
1
    std::ostringstream stream;
101
1
    const StdDebugStringObject debugStringObject;
102
1
    toJsonStream(debugStringObject, stream, 2, DefaultWalkFilter());
103
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stream.str());
104
1
}
105
106
TEST(DebugStringUtilTest, toJsonStreamIndent2FilterWithAlloc)
107
1
{
108
1
    std::ostringstream stream;
109
1
    const StdDebugStringObject debugStringObject;
110
1
    toJsonStream(debugStringObject, stream, 2, DepthWalkFilter(0), std_alloc());
111
1
    ASSERT_EQ("{\n}", stream.str());
112
1
}
113
114
TEST(DebugStringUtilTest, toJsonStreamIndent2FilterWithPolymorphicAlloc)
115
1
{
116
1
    std::ostringstream stream;
117
1
    const PmrDebugStringObject debugStringObject;
118
1
    toJsonStream(debugStringObject, stream, 2, BasicDepthWalkFilter<pmr_alloc>(0), pmr_alloc());
119
1
    ASSERT_EQ("{\n}", stream.str());
120
1
}
121
122
TEST(DebugStringUtilTest, toJsonStringDefault)
123
1
{
124
1
    const StdDebugStringObject debugStringObject;
125
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", toJsonString(debugStringObject));
126
1
}
127
128
TEST(DebugStringUtilTest, toJsonStringDefaultWithAlloc)
129
1
{
130
1
    const StdDebugStringObject debugStringObject;
131
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", toJsonString(debugStringObject, std_alloc()));
132
1
}
133
134
TEST(DebugStringUtilTest, toJsonStringDefaultWithPolymorphicAlloc)
135
1
{
136
1
    const PmrDebugStringObject debugStringObject;
137
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", toJsonString(debugStringObject, pmr_alloc()));
138
1
}
139
140
TEST(DebugStringUtilTest, toJsonStringIndent2)
141
1
{
142
1
    const StdDebugStringObject debugStringObject;
143
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", toJsonString(debugStringObject, 2));
144
1
}
145
146
TEST(DebugStringUtilTest, toJsonStringIndent2WithAlloc)
147
1
{
148
1
    const StdDebugStringObject debugStringObject;
149
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", toJsonString(debugStringObject, 2, std_alloc()));
150
1
}
151
152
TEST(DebugStringUtilTest, toJsonStringIndent2WithPolymorphicAlloc)
153
1
{
154
1
    const PmrDebugStringObject debugStringObject;
155
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", toJsonString(debugStringObject, 2, pmr_alloc()));
156
1
}
157
158
TEST(DebugStringUtilTest, toJsonStringFilter)
159
1
{
160
1
    const StdDebugStringObject debugStringObject;
161
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", toJsonString(debugStringObject, DefaultWalkFilter()));
162
1
}
163
164
TEST(DebugStringUtilTest, toJsonStringFilterWithAlloc)
165
1
{
166
1
    const StdDebugStringObject debugStringObject;
167
1
    ASSERT_EQ(
168
1
            "{\n    \"text\": \"test\"\n}", toJsonString(debugStringObject, DefaultWalkFilter(), std_alloc()));
169
1
}
170
171
TEST(DebugStringUtilTest, toJsonStringFilterWithPolymorphicAlloc)
172
1
{
173
1
    const PmrDebugStringObject debugStringObject;
174
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}",
175
1
            toJsonString(debugStringObject, BasicDefaultWalkFilter<pmr_alloc>(), pmr_alloc()));
176
1
}
177
178
TEST(DebugStringUtilTest, toJsonStringIndent2Filter)
179
1
{
180
1
    const StdDebugStringObject debugStringObject;
181
1
    ASSERT_EQ("{\n}", toJsonString(debugStringObject, 2, DepthWalkFilter(0)));
182
1
}
183
184
TEST(DebugStringUtilTest, toJsonStringIndent2FilterWithAlloc)
185
1
{
186
1
    const StdDebugStringObject debugStringObject;
187
1
    ASSERT_EQ(
188
1
            "{\n  \"text\": \"test\"\n}", toJsonString(debugStringObject, 2, DefaultWalkFilter(), std_alloc()));
189
1
}
190
191
TEST(DebugStringUtilTest, toJsonStringIndent2FilterWithPolymorphicAlloc)
192
1
{
193
1
    const PmrDebugStringObject debugStringObject;
194
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}",
195
1
            toJsonString(debugStringObject, 2, BasicDefaultWalkFilter<pmr_alloc>(), pmr_alloc()));
196
1
}
197
198
TEST(DebugStringUtilTest, toJsonFileDefault)
199
1
{
200
1
    const StdDebugStringObject debugStringObject;
201
1
    const std::string fileName = "DebugStringUtilTest_toJsonFileDefault.json";
202
1
    toJsonFile(debugStringObject, fileName);
203
1
    ASSERT_THROW(toJsonFile(debugStringObject, ""), CppRuntimeException);
204
205
1
    std::ifstream stream(fileName.c_str());
206
1
    std::stringstream stringStream;
207
1
    stringStream << stream.rdbuf();
208
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stringStream.str());
209
1
}
210
211
TEST(DebugStringUtilTest, toJsonFileDefaultWithAlloc)
212
1
{
213
1
    const StdDebugStringObject debugStringObject;
214
1
    const std::string fileName = "DebugStringUtilTest_toJsonFileDefaultWithAlloc.json";
215
1
    toJsonFile(debugStringObject, fileName, std_alloc());
216
217
1
    std::ifstream stream(fileName.c_str());
218
1
    std::stringstream stringStream;
219
1
    stringStream << stream.rdbuf();
220
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stringStream.str());
221
1
}
222
223
TEST(DebugStringUtilTest, toJsonFileDefaultWithPolymorphicAlloc)
224
1
{
225
1
    const PmrDebugStringObject debugStringObject;
226
1
    const string<pmr_alloc> fileName = "DebugStringUtilTest_toJsonFileDefaultWithPolymorphicAlloc.json";
227
1
    toJsonFile(debugStringObject, fileName, pmr_alloc());
228
229
1
    std::ifstream stream(fileName.c_str());
230
1
    std::stringstream stringStream;
231
1
    stringStream << stream.rdbuf();
232
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stringStream.str());
233
1
}
234
235
TEST(DebugStringUtilTest, toJsonFileIndent2)
236
1
{
237
1
    const StdDebugStringObject debugStringObject;
238
1
    const std::string fileName = "DebugStringUtilTest_toJsonFileIndent2.json";
239
1
    toJsonFile(debugStringObject, fileName, 2);
240
241
1
    std::ifstream stream(fileName.c_str());
242
1
    std::stringstream stringStream;
243
1
    stringStream << stream.rdbuf();
244
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stringStream.str());
245
1
}
246
247
TEST(DebugStringUtilTest, toJsonFileIndent2WithAlloc)
248
1
{
249
1
    const StdDebugStringObject debugStringObject;
250
1
    const std::string fileName = "DebugStringUtilTest_toJsonFileIndent2WithAlloc.json";
251
1
    toJsonFile(debugStringObject, fileName, 2, std_alloc());
252
253
1
    std::ifstream stream(fileName.c_str());
254
1
    std::stringstream stringStream;
255
1
    stringStream << stream.rdbuf();
256
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stringStream.str());
257
1
}
258
259
TEST(DebugStringUtilTest, toJsonFileIndent2WithPolymorphicAlloc)
260
1
{
261
1
    const PmrDebugStringObject debugStringObject;
262
1
    const string<pmr_alloc> fileName = "DebugStringUtilTest_toJsonFileIndent2WithPolymorphicAlloc.json";
263
1
    toJsonFile(debugStringObject, fileName, 2, pmr_alloc());
264
265
1
    std::ifstream stream(fileName.c_str());
266
1
    std::stringstream stringStream;
267
1
    stringStream << stream.rdbuf();
268
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stringStream.str());
269
1
}
270
271
TEST(DebugStringUtilTest, toJsonFileFilter)
272
1
{
273
1
    const StdDebugStringObject debugStringObject;
274
1
    const std::string fileName = "DebugStringUtilTest_toJsonFileFilter.json";
275
1
    toJsonFile(debugStringObject, fileName, DefaultWalkFilter());
276
277
1
    std::ifstream stream(fileName.c_str());
278
1
    std::stringstream stringStream;
279
1
    stringStream << stream.rdbuf();
280
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stringStream.str());
281
1
}
282
283
TEST(DebugStringUtilTest, toJsonFileFilterWithAlloc)
284
1
{
285
1
    const StdDebugStringObject debugStringObject;
286
1
    DefaultWalkFilter defaultWalkFilter;
287
1
    const std::string fileName = "DebugStringUtilTest_toJsonFileFilterWithAlloc.json";
288
1
    toJsonFile(debugStringObject, fileName, defaultWalkFilter, std_alloc());
289
290
1
    std::ifstream stream(fileName.c_str());
291
1
    std::stringstream stringStream;
292
1
    stringStream << stream.rdbuf();
293
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stringStream.str());
294
1
}
295
296
TEST(DebugStringUtilTest, toJsonFileFilterWithPolymorphicAlloc)
297
1
{
298
1
    const PmrDebugStringObject debugStringObject;
299
1
    BasicDefaultWalkFilter<pmr_alloc> defaultWalkFilter;
300
1
    const string<pmr_alloc> fileName = "DebugStringUtilTest_toJsonFileFilterWithPolymorphicAlloc.json";
301
1
    toJsonFile(debugStringObject, fileName, defaultWalkFilter, pmr_alloc());
302
303
1
    std::ifstream stream(fileName.c_str());
304
1
    std::stringstream stringStream;
305
1
    stringStream << stream.rdbuf();
306
1
    ASSERT_EQ("{\n    \"text\": \"test\"\n}", stringStream.str());
307
1
}
308
309
TEST(DebugStringUtilTest, toJsonFileIndent2Filter)
310
1
{
311
1
    const StdDebugStringObject debugStringObject;
312
1
    DepthWalkFilter depthWalkFilter(0);
313
1
    const std::string fileName = "DebugStringUtilTest_toJsonFileIndent2Filter.json";
314
1
    toJsonFile(debugStringObject, fileName, 2, depthWalkFilter);
315
316
1
    std::ifstream stream(fileName.c_str());
317
1
    std::stringstream stringStream;
318
1
    stringStream << stream.rdbuf();
319
1
    ASSERT_EQ("{\n}", stringStream.str());
320
1
}
321
322
TEST(DebugStringUtilTest, toJsonFileIndent2FilterWithAlloc)
323
1
{
324
1
    const StdDebugStringObject debugStringObject;
325
1
    DefaultWalkFilter defaultWalkFilter;
326
1
    const std::string fileName = "DebugStringUtilTest_toJsonFileIndent2FilterWithAlloc.json";
327
1
    toJsonFile(debugStringObject, fileName, 2, defaultWalkFilter, std_alloc());
328
329
1
    std::ifstream stream(fileName.c_str());
330
1
    std::stringstream stringStream;
331
1
    stringStream << stream.rdbuf();
332
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stringStream.str());
333
1
}
334
335
TEST(DebugStringUtilTest, toJsonFileIndent2FilterWithPolymorphicAlloc)
336
1
{
337
1
    const PmrDebugStringObject debugStringObject;
338
1
    BasicDefaultWalkFilter<pmr_alloc> defaultWalkFilter;
339
1
    const string<pmr_alloc> fileName = "DebugStringUtilTest_toJsonFileIndent2FilterWithPolymorphicAlloc.json";
340
1
    toJsonFile(debugStringObject, fileName, 2, defaultWalkFilter, pmr_alloc());
341
342
1
    std::ifstream stream(fileName.c_str());
343
1
    std::stringstream stringStream;
344
1
    stringStream << stream.rdbuf();
345
1
    ASSERT_EQ("{\n  \"text\": \"test\"\n}", stringStream.str());
346
1
}
347
348
TEST(DebugStringUtilTest, fromJsonStreamTypeInfo)
349
1
{
350
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
351
1
    IReflectablePtr reflectable = fromJsonStream(StdDebugStringObject::typeInfo(), stream);
352
1
    ASSERT_TRUE(reflectable);
353
354
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
355
1
}
356
357
TEST(DebugStringUtilTest, fromJsonStreamParameterizedTypeInfo)
358
1
{
359
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
360
1
    IReflectablePtr reflectable = fromJsonStream(StdDebugStringParamObject::typeInfo(), stream);
361
1
    ASSERT_TRUE(reflectable);
362
363
1
    ASSERT_THROW(reflectable->getParameter("param"), CppRuntimeException);
364
1
    reflectable->initialize(vector<AnyHolder<>>{AnyHolder<>{10}});
365
366
1
    ASSERT_EQ(10, reflectable->getParameter("param")->getInt32());
367
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
368
1
}
369
370
TEST(DebugStringUtilTest, fromJsonStreamTypeInfoWithAlloc)
371
1
{
372
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
373
1
    IReflectablePtr reflectable = fromJsonStream(StdDebugStringObject::typeInfo(), stream, std_alloc());
374
1
    ASSERT_TRUE(reflectable);
375
376
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
377
1
}
378
379
TEST(DebugStringUtilTest, fromJsonStreamTypeInfoWithPolymorphicAllocDefault)
380
1
{
381
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
382
1
    IBasicReflectablePtr<pmr_alloc> reflectable = fromJsonStream(PmrDebugStringObject::typeInfo(), stream);
383
1
    ASSERT_TRUE(reflectable);
384
385
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
386
1
}
387
388
TEST(DebugStringUtilTest, fromJsonStreamTypeInfoWithPolymorphicAlloc)
389
1
{
390
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
391
1
    IBasicReflectablePtr<pmr_alloc> reflectable =
392
1
            fromJsonStream(PmrDebugStringObject::typeInfo(), stream, pmr_alloc());
393
1
    ASSERT_TRUE(reflectable);
394
395
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
396
1
}
397
398
TEST(DebugStringUtilTest, fromJsonStreamType)
399
1
{
400
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
401
1
    StdDebugStringObject debugStringObject = fromJsonStream<StdDebugStringObject>(stream);
402
403
1
    ASSERT_EQ("something", debugStringObject.getText());
404
1
}
405
406
TEST(DebugStringUtilTest, fromJsonStreamParameterizedType)
407
1
{
408
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
409
1
    StdDebugStringParamObject debugStringParamObject = fromJsonStream<StdDebugStringParamObject>(stream);
410
411
1
    ASSERT_THROW(debugStringParamObject.getParam(), CppRuntimeException);
412
413
1
    ASSERT_EQ("something", debugStringParamObject.getText());
414
1
}
415
416
TEST(DebugStringUtilTest, fromJsonStreamTypeWithAlloc)
417
1
{
418
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
419
1
    StdDebugStringObject debugStringObject = fromJsonStream<StdDebugStringObject>(stream, std_alloc());
420
421
1
    ASSERT_EQ("something", debugStringObject.getText());
422
1
}
423
424
TEST(DebugStringUtilTest, fromJsonStreamTypeWithPolymorphicAllocDefault)
425
1
{
426
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
427
1
    PmrDebugStringObject debugStringObject = fromJsonStream<PmrDebugStringObject>(stream);
428
429
1
    ASSERT_EQ("something", debugStringObject.getText());
430
1
}
431
432
TEST(DebugStringUtilTest, fromJsonStreamTypeWithPolymorphicAlloc)
433
1
{
434
1
    std::istringstream stream("{\n  \"text\": \"something\"\n}");
435
1
    PmrDebugStringObject debugStringObject = fromJsonStream<PmrDebugStringObject>(stream, pmr_alloc());
436
437
1
    ASSERT_EQ("something", debugStringObject.getText());
438
1
}
439
440
TEST(DebugStringUtilTest, fromJsonStringTypeInfo)
441
1
{
442
1
    std::string jsonString("{\n  \"text\": \"something\"\n}");
443
1
    IReflectablePtr reflectable = fromJsonString(StdDebugStringObject::typeInfo(), jsonString);
444
1
    ASSERT_TRUE(reflectable);
445
446
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
447
1
}
448
449
TEST(DebugStringUtilTest, fromJsonStringParameterizedTypeInfo)
450
1
{
451
1
    std::string jsonString("{\n  \"text\": \"something\"\n}");
452
1
    IReflectablePtr reflectable = fromJsonString(StdDebugStringParamObject::typeInfo(), jsonString);
453
1
    ASSERT_TRUE(reflectable);
454
455
1
    ASSERT_THROW(reflectable->getParameter("param"), CppRuntimeException);
456
1
    reflectable->initialize(vector<AnyHolder<>>{AnyHolder<>{10}});
457
458
1
    ASSERT_EQ(10, reflectable->getParameter("param")->getInt32());
459
460
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
461
1
}
462
463
TEST(DebugStringUtilTest, fromJsonStringTypeInfoWithAlloc)
464
1
{
465
1
    std::string jsonString("{\n  \"text\": \"something\"\n}");
466
1
    IReflectablePtr reflectable = fromJsonString(StdDebugStringObject::typeInfo(), jsonString, std_alloc());
467
1
    ASSERT_TRUE(reflectable);
468
469
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
470
1
}
471
472
TEST(DebugStringUtilTest, fromJsonStringTypeInfoWithPolymorphicAllocDefault)
473
1
{
474
1
    string<pmr_alloc> jsonString("{\n  \"text\": \"something\"\n}");
475
1
    IBasicReflectablePtr<pmr_alloc> reflectable = fromJsonString(PmrDebugStringObject::typeInfo(), jsonString);
476
1
    ASSERT_TRUE(reflectable);
477
478
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
479
1
}
480
481
TEST(DebugStringUtilTest, fromJsonStringTypeInfoWithPolymorphicAlloc)
482
1
{
483
1
    string<pmr_alloc> jsonString("{\n  \"text\": \"something\"\n}");
484
1
    IBasicReflectablePtr<pmr_alloc> reflectable =
485
1
            fromJsonString(PmrDebugStringObject::typeInfo(), jsonString, pmr_alloc());
486
1
    ASSERT_TRUE(reflectable);
487
488
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
489
1
}
490
491
TEST(DebugStringUtilTest, fromJsonStringType)
492
1
{
493
1
    std::string jsonString("{\n  \"text\": \"something\"\n}");
494
1
    StdDebugStringObject debugStringObject = fromJsonString<StdDebugStringObject>(jsonString);
495
496
1
    ASSERT_EQ("something", debugStringObject.getText());
497
1
}
498
499
TEST(DebugStringUtilTest, fromJsonStringParameterizedType)
500
1
{
501
1
    std::string jsonString("{\n  \"text\": \"something\"\n}");
502
1
    StdDebugStringParamObject debugStringParamObject = fromJsonString<StdDebugStringParamObject>(jsonString);
503
504
1
    ASSERT_THROW(debugStringParamObject.getParam(), CppRuntimeException);
505
506
1
    ASSERT_EQ("something", debugStringParamObject.getText());
507
1
}
508
509
TEST(DebugStringUtilTest, fromJsonStringTypeWithAlloc)
510
1
{
511
1
    std::string jsonString("{\n  \"text\": \"something\"\n}");
512
1
    StdDebugStringObject debugStringObject = fromJsonString<StdDebugStringObject>(jsonString, std_alloc());
513
514
1
    ASSERT_EQ("something", debugStringObject.getText());
515
1
}
516
517
TEST(DebugStringUtilTest, fromJsonStringTypeWithPolymorphicAllocDefault)
518
1
{
519
1
    string<pmr_alloc> jsonString("{\n  \"text\": \"something\"\n}");
520
1
    PmrDebugStringObject debugStringObject = fromJsonString<PmrDebugStringObject>(jsonString);
521
522
1
    ASSERT_EQ("something", debugStringObject.getText());
523
1
}
524
525
TEST(DebugStringUtilTest, fromJsonStringTypeWithPolymorphicAlloc)
526
1
{
527
1
    string<pmr_alloc> jsonString("{\n  \"text\": \"something\"\n}");
528
1
    PmrDebugStringObject debugStringObject = fromJsonString<PmrDebugStringObject>(jsonString, pmr_alloc());
529
530
1
    ASSERT_EQ("something", debugStringObject.getText());
531
1
}
532
533
TEST(DebugStringUtilTest, fromJsonFileTypeInfo)
534
1
{
535
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileTypeInfo.json";
536
1
    {
537
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
538
1
        stream << "{\n  \"text\": \"something\"\n}";
539
1
    }
540
541
1
    IReflectablePtr reflectable = fromJsonFile(StdDebugStringObject::typeInfo(), fileName);
542
1
    ASSERT_TRUE(reflectable);
543
544
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
545
546
1
    ASSERT_THROW(fromJsonFile(StdDebugStringObject::typeInfo(), ""), CppRuntimeException);
547
1
}
548
549
TEST(DebugStringUtilTest, fromJsonFileParameterizedTypeInfo)
550
1
{
551
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileParameterizedTypeInfo.json";
552
1
    {
553
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
554
1
        stream << "{\n  \"text\": \"something\"\n}";
555
1
    }
556
557
1
    IReflectablePtr reflectable = fromJsonFile(StdDebugStringParamObject::typeInfo(), fileName);
558
1
    ASSERT_TRUE(reflectable);
559
560
1
    ASSERT_THROW(reflectable->getParameter("param"), CppRuntimeException);
561
1
    reflectable->initialize(vector<AnyHolder<>>{AnyHolder<>{10}});
562
563
1
    ASSERT_EQ(10, reflectable->getParameter("param")->getInt32());
564
565
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
566
1
}
567
568
TEST(DebugStringUtilTest, fromJsonFileTypeInfoWithAlloc)
569
1
{
570
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileTypeInfoWithAlloc.json";
571
1
    {
572
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
573
1
        stream << "{\n  \"text\": \"something\"\n}";
574
1
    }
575
576
1
    IReflectablePtr reflectable = fromJsonFile(StdDebugStringObject::typeInfo(), fileName, std_alloc());
577
1
    ASSERT_TRUE(reflectable);
578
579
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
580
1
}
581
582
TEST(DebugStringUtilTest, fromJsonFileTypeInfoWithPolymorphicAllocDefault)
583
1
{
584
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileTypeInfoWithPolymorphicAllocDefault.json";
585
1
    {
586
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
587
1
        stream << "{\n  \"text\": \"something\"\n}";
588
1
    }
589
590
1
    IBasicReflectablePtr<pmr_alloc> reflectable = fromJsonFile(PmrDebugStringObject::typeInfo(), fileName);
591
1
    ASSERT_TRUE(reflectable);
592
593
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
594
1
}
595
596
TEST(DebugStringUtilTest, fromJsonFileTypeInfoWithPolymorphicAlloc)
597
1
{
598
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileTypeInfoWithPolymorphicAlloc.json";
599
1
    {
600
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
601
1
        stream << "{\n  \"text\": \"something\"\n}";
602
1
    }
603
604
1
    IBasicReflectablePtr<pmr_alloc> reflectable =
605
1
            fromJsonFile(PmrDebugStringObject::typeInfo(), fileName, pmr_alloc());
606
1
    ASSERT_TRUE(reflectable);
607
608
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
609
1
}
610
611
TEST(DebugStringUtilTest, fromJsonFileParameterizedTypeInfoWithPolymorphicAlloc)
612
1
{
613
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileParameterizedTypeInfoWithPolymorphicAlloc.json";
614
1
    {
615
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
616
1
        stream << "{\n  \"text\": \"something\"\n}";
617
1
    }
618
619
1
    IBasicReflectablePtr<pmr_alloc> reflectable =
620
1
            fromJsonFile(PmrDebugStringParamObject::typeInfo(), fileName, pmr_alloc());
621
1
    ASSERT_TRUE(reflectable);
622
623
1
    ASSERT_THROW(reflectable->getParameter("param"), CppRuntimeException);
624
1
    reflectable->initialize(vector<AnyHolder<pmr_alloc>, pmr_alloc>{AnyHolder<pmr_alloc>{10}});
625
626
1
    ASSERT_EQ(10, reflectable->getParameter("param")->getInt32());
627
628
1
    ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
629
1
}
630
631
TEST(DebugStringUtilTest, fromJsonFileType)
632
1
{
633
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileType.json";
634
1
    {
635
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
636
1
        stream << "{\n  \"text\": \"something\"\n}";
637
1
    }
638
639
1
    StdDebugStringObject debugStringObject = fromJsonFile<StdDebugStringObject>(fileName);
640
641
1
    ASSERT_EQ("something", debugStringObject.getText());
642
1
}
643
644
TEST(DebugStringUtilTest, fromJsonFileParameterizedType)
645
1
{
646
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileParameterizedType.json";
647
1
    {
648
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
649
1
        stream << "{\n  \"text\": \"something\"\n}";
650
1
    }
651
652
1
    StdDebugStringParamObject debugStringParamObject = fromJsonFile<StdDebugStringParamObject>(fileName);
653
654
1
    ASSERT_THROW(debugStringParamObject.getParam(), CppRuntimeException);
655
656
1
    ASSERT_EQ("something", debugStringParamObject.getText());
657
1
}
658
659
TEST(DebugStringUtilTest, fromJsonFileTypeWithAlloc)
660
1
{
661
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileTypeWithAlloc.json";
662
1
    {
663
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
664
1
        stream << "{\n  \"text\": \"something\"\n}";
665
1
    }
666
667
1
    StdDebugStringObject debugStringObject = fromJsonFile<StdDebugStringObject>(fileName, std_alloc());
668
669
1
    ASSERT_EQ("something", debugStringObject.getText());
670
1
}
671
672
TEST(DebugStringUtilTest, fromJsonFileTypeWithPolymorphicAllocDefault)
673
1
{
674
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileTypeWithPolymorphicAllocDefault.json";
675
1
    {
676
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
677
1
        stream << "{\n  \"text\": \"something\"\n}";
678
1
    }
679
680
1
    PmrDebugStringObject debugStringObject = fromJsonFile<PmrDebugStringObject>(fileName);
681
682
1
    ASSERT_EQ("something", debugStringObject.getText());
683
1
}
684
685
TEST(DebugStringUtilTest, fromJsonFileTypeWithPolymorphicAlloc)
686
1
{
687
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileTypeWithPolymorphicAlloc.json";
688
1
    {
689
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
690
1
        stream << "{\n  \"text\": \"something\"\n}";
691
1
    }
692
693
1
    PmrDebugStringObject debugStringObject = fromJsonFile<PmrDebugStringObject>(fileName, pmr_alloc());
694
695
1
    ASSERT_EQ("something", debugStringObject.getText());
696
1
}
697
698
TEST(DebugStringUtilTest, fromJsonFileParameterizedTypeWithPolymorphicAlloc)
699
1
{
700
1
    const char* fileName = "DebugStringUtilTest_fromJsonFileParameteriezedTypeWithPolymorphicAlloc.json";
701
1
    {
702
1
        std::ofstream stream(fileName, std::ofstream::out | std::ofstream::trunc);
703
1
        stream << "{\n  \"text\": \"something\"\n}";
704
1
    }
705
706
1
    PmrDebugStringParamObject debugStringParamObject =
707
1
            fromJsonFile<PmrDebugStringParamObject>(fileName, pmr_alloc());
708
709
1
    ASSERT_THROW(debugStringParamObject.getParam(), CppRuntimeException);
710
711
1
    ASSERT_EQ("something", debugStringParamObject.getText());
712
1
}
713
714
} // namespace zserio