Coverage Report

Created: 2023-12-13 14:58

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