-
Notifications
You must be signed in to change notification settings - Fork 3
/
encoder.test.cc
176 lines (128 loc) · 5.1 KB
/
encoder.test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
#include "snn-core/json/stream/encoder.hh"
#include "snn-core/unittest.hh"
#include "snn-core/json/is_valid.hh"
namespace snn::app
{
namespace
{
constexpr bool example()
{
namespace js = json::stream;
strbuf buf;
// Promise up front that nothing added to this object will overlap the buffer.
js::encoder enc{buf, promise::no_overlap};
enc << js::object;
enc << "One" << js::colon << js::as_bool(true) << js::comma;
enc << "Tw\0" << js::colon << js::as_num(2) << js::comma;
enc << "Three";
enc << js::colon;
enc << js::array;
enc << "3" << js::comma;
enc << js::as_num("333") << js::comma;
enc << js::array_end;
enc << js::comma;
enc << js::object_end;
snn_require(buf == R"({"One":true,"Tw\u0000":2,"Three":["3",333]})");
snn_require(json::is_valid(buf));
return true;
}
constexpr bool test_encoder()
{
{
namespace js = json::stream;
strbuf buf;
js::encoder enc{buf, promise::no_overlap};
enc << js::array << js::array_end;
snn_require(buf == "[]");
snn_require(json::is_valid(buf));
}
{
namespace js = json::stream;
strbuf buf;
js::encoder enc{buf, promise::no_overlap};
enc << js::object << js::object_end;
snn_require(buf == "{}");
snn_require(json::is_valid(buf));
}
{
namespace js = json::stream;
strbuf buf;
js::encoder enc{buf, promise::no_overlap};
enc << js::array;
for (auto i : {1, 2, 7, 28, 231})
{
enc << js::as_num(i) << js::comma;
}
enc << js::array_end;
// The trailing comma is removed by array_end.
snn_require(buf == "[1,2,7,28,231]");
snn_require(json::is_valid(buf));
}
{
namespace js = json::stream;
strbuf buf;
js::encoder enc{buf, promise::no_overlap};
enc << js::object;
enc << "One" << js::colon << '1' << js::comma;
enc << "Two" << js::colon << js::as_bool(true) << js::comma;
enc << cstrview{"Three"} << js::colon << js::as_num(-123) << js::comma;
enc << "Four" << js::colon << js::as_num(3856294) << js::comma;
enc << "Five" << js::colon;
enc << js::array;
enc << "FooBar" << js::comma;
enc << js::as_bool(cstrview{"true"}) << js::comma;
enc << js::as_num("-123") << js::comma;
enc << js::as_null("null") << js::comma;
enc << js::array_end;
enc << js::comma;
enc << js::object_end;
// The trailing commas are removed by array_end and object_end.
snn_require(buf == R"({"One":"1","Two":true,"Three":-123,"Four":)"
R"(3856294,"Five":["FooBar",true,-123,null]})");
snn_require(json::is_valid(buf));
}
{
namespace js = json::stream;
strbuf buf;
js::encoder enc{buf, promise::no_overlap};
enc << js::array;
enc << js::as_bool(true) << js::comma;
enc << js::as_bool("true") << js::comma;
enc << js::as_bool(false) << js::comma;
enc << js::as_bool("false") << js::comma;
enc << js::as_null(nullptr) << js::comma;
enc << js::as_null("null") << js::comma;
enc << js::as_num(82819) << js::comma;
enc << js::as_num("-123") << js::comma;
enc << js::as_num("1.09") << js::comma;
enc << js::array_end;
snn_require(buf == "[true,true,false,false,null,null,82819,-123,1.09]");
snn_require(json::is_valid(buf));
}
{
namespace js = json::stream;
strbuf buf;
js::encoder enc{buf, promise::no_overlap};
enc << js::array;
enc << "" << js::comma;
enc << "abc" << js::comma;
enc << "a\0c" << js::comma;
enc << "ÅÄÖ\U0001f348 \U0001f349 " << js::comma;
enc << js::array_end;
snn_require(buf == R"(["","abc","a\u0000c","ÅÄÖ\ud83c\udf48 \ud83c\udf49 "])");
snn_require(json::is_valid(buf));
}
return true;
}
}
}
namespace snn
{
void unittest()
{
snn_static_require(app::example());
snn_static_require(app::test_encoder());
}
}