blob: 6d4338cbbb6544378fc68f04f51e192400ef6c25 [file] [log] [blame]
Florin Malita7796f002018-06-08 12:25:38 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
9
Florin Malitaae252792018-06-14 11:24:50 -040010#include "SkArenaAlloc.h"
Florin Malita7796f002018-06-08 12:25:38 -040011#include "SkJSON.h"
Florin Malitaae252792018-06-14 11:24:50 -040012#include "SkString.h"
Florin Malita7796f002018-06-08 12:25:38 -040013#include "SkStream.h"
14
15using namespace skjson;
16
17DEF_TEST(SkJSON_Parse, reporter) {
18 static constexpr struct {
19 const char* in;
20 const char* out;
21 } g_tests[] = {
22 { "" , nullptr },
23 { "[" , nullptr },
24 { "]" , nullptr },
25 { "{" , nullptr },
26 { "}" , nullptr },
27 { "{]" , nullptr },
28 { "[}" , nullptr },
29 { "1" , nullptr },
30 { "true" , nullptr },
31 { "false", nullptr },
32 { "null" , nullptr },
33
34 { "[nulll]" , nullptr },
35 { "[false2]", nullptr },
36 { "[true:]" , nullptr },
37
38 { "[1 2]" , nullptr },
39 { "[1,,2]" , nullptr },
40 { "[1,2,]" , nullptr },
41 { "[,1,2]" , nullptr },
42
43 { "[ \"foo" , nullptr },
44 { "[ \"fo\0o\" ]" , nullptr },
45
46 { "{ null }" , nullptr },
47 { "{ \"k\" : }" , nullptr },
48 { "{ : null }" , nullptr },
49 { "{ \"k\" : : null }" , nullptr },
50 { "{ \"k\" : null , }" , nullptr },
51 { "{ \"k\" : null \"k\" : 1 }", nullptr },
52
53
54 { " \n\r\t [ \n\r\t ] \n\r\t " , "[]" },
55 { "[ null ]" , "[null]" },
56 { "[ true ]" , "[true]" },
57 { "[ false ]" , "[false]" },
58 { "[ 0 ]" , "[0]" },
59 { "[ 1 ]" , "[1]" },
60 { "[ 1.248 ]" , "[1.248]" },
61 { "[ \"\" ]" , "[\"\"]" },
62 { "[ \" f o o \" ]" , "[\" f o o \"]" },
63 { "[ \"123456\" ]" , "[\"123456\"]" },
64 { "[ \"1234567\" ]" , "[\"1234567\"]" },
65 { "[ \"12345678\" ]" , "[\"12345678\"]" },
66 { "[ \"123456789\" ]" , "[\"123456789\"]" },
67 { "[ null , true, false,0,12.8 ]", "[null,true,false,0,12.8]" },
68
69 { " \n\r\t { \n\r\t } \n\r\t " , "{}" },
70 { "{ \"k\" : null }" , "{\"k\":null}" },
71 { "{ \"k1\" : null, \"k2 \":0 }", "{\"k1\":null,\"k2 \":0}" },
72 { "{ \"k1\" : null, \"k1\":0 }" , "{\"k1\":null,\"k1\":0}" },
73
74 { "{ \"k1\" : null, \n\
75 \"k2\" : 0, \n\
76 \"k3\" : [ \n\
77 true, \r\n\
78 { \"kk1\" : \"foo\" , \n\
79 \"kk2\" : \"bar\" , \n\
80 \"kk3\" : 1.28 , \n\
81 \"kk4\" : [ 42 ] \n\
82 } , \n\
83 \"boo\" , \n\
84 null \n\
85 ] \n\
86 }",
87 "{\"k1\":null,\"k2\":0,\"k3\":[true,"
88 "{\"kk1\":\"foo\",\"kk2\":\"bar\",\"kk3\":1.28,\"kk4\":[42]},\"boo\",null]}" },
89 };
90
91 for (const auto& tst : g_tests) {
92 DOM dom(tst.in);
93 const auto success = !dom.root().is<NullValue>();
94 REPORTER_ASSERT(reporter, success == (tst.out != nullptr));
95 if (!success) continue;
96
97 SkDynamicMemoryWStream str;
98 dom.write(&str);
99 str.write8('\0');
100
101 auto data = str.detachAsData();
102 REPORTER_ASSERT(reporter, !strcmp(tst.out, static_cast<const char*>(data->data())));
103 }
104
105}
106
107template <typename T, typename VT>
108static void check_primitive(skiatest::Reporter* reporter, const Value& v, T pv,
109 bool is_type) {
110
111 REPORTER_ASSERT(reporter, v.is<VT>() == is_type);
Florin Malitaae252792018-06-14 11:24:50 -0400112 const VT* cast_t = v;
113 REPORTER_ASSERT(reporter, (cast_t != nullptr) == is_type);
114
115 if (is_type) {
116 REPORTER_ASSERT(reporter, &v.as<VT>() == cast_t);
117 REPORTER_ASSERT(reporter, *v.as<VT>() == pv);
118 }
Florin Malita7796f002018-06-08 12:25:38 -0400119}
120
121template <typename T>
122static void check_vector(skiatest::Reporter* reporter, const Value& v, size_t expected_size,
123 bool is_vector) {
124 REPORTER_ASSERT(reporter, v.is<T>() == is_vector);
Florin Malitaae252792018-06-14 11:24:50 -0400125 const T* cast_t = v;
126 REPORTER_ASSERT(reporter, (cast_t != nullptr) == is_vector);
Florin Malita7796f002018-06-08 12:25:38 -0400127
Florin Malitaae252792018-06-14 11:24:50 -0400128 if (is_vector) {
129 const auto& vec = v.as<T>();
130 REPORTER_ASSERT(reporter, &vec == cast_t);
131 REPORTER_ASSERT(reporter, vec.size() == expected_size);
132 REPORTER_ASSERT(reporter, vec.begin() != nullptr);
133 REPORTER_ASSERT(reporter, vec.end() == vec.begin() + expected_size);
134 }
Florin Malita7796f002018-06-08 12:25:38 -0400135}
136
137static void check_string(skiatest::Reporter* reporter, const Value& v, const char* s) {
138 check_vector<StringValue>(reporter, v, s ? strlen(s) : 0, !!s);
139 if (s) {
140 REPORTER_ASSERT(reporter, !strcmp(v.as<StringValue>().begin(), s));
141 }
142}
143
Florin Malitaae252792018-06-14 11:24:50 -0400144DEF_TEST(SkJSON_DOM_visit, reporter) {
Florin Malita7796f002018-06-08 12:25:38 -0400145 static constexpr char json[] = "{ \n\
146 \"k1\": null, \n\
147 \"k2\": false, \n\
148 \"k3\": true, \n\
149 \"k4\": 42, \n\
150 \"k5\": .75, \n\
151 \"k6\": \"foo\", \n\
152 \"k7\": [ 1, true, \"bar\" ], \n\
153 \"k8\": { \"kk1\": 2, \"kk2\": false, \"kk1\": \"baz\" } \n\
154 }";
155
156 DOM dom(json);
157
158 const auto& jroot = dom.root().as<ObjectValue>();
159 REPORTER_ASSERT(reporter, jroot.is<ObjectValue>());
160
161 {
162 const auto& v = jroot["k1"];
163 REPORTER_ASSERT(reporter, v.is<NullValue>());
164
165 check_primitive<bool, BoolValue>(reporter, v, false, false);
166 check_primitive<float, NumberValue>(reporter, v, 0, false);
167
168 check_string(reporter, v, nullptr);
169 check_vector<ArrayValue >(reporter, v, 0, false);
170 check_vector<ObjectValue>(reporter, v, 0, false);
171 }
172
173 {
174 const auto& v = jroot["k2"];
175 REPORTER_ASSERT(reporter, !v.is<NullValue>());
176
177 check_primitive<bool, BoolValue>(reporter, v, false, true);
178 check_primitive<float, NumberValue>(reporter, v, 0, false);
179
180 check_string(reporter, v, nullptr);
181 check_vector<ArrayValue >(reporter, v, 0, false);
182 check_vector<ObjectValue>(reporter, v, 0, false);
183 }
184
185 {
186 const auto& v = jroot["k3"];
187 REPORTER_ASSERT(reporter, !v.is<NullValue>());
188
189 check_primitive<bool, BoolValue>(reporter, v, true, true);
190 check_primitive<float, NumberValue>(reporter, v, 0, false);
191
192 check_string(reporter, v, nullptr);
193 check_vector<ArrayValue >(reporter, v, 0, false);
194 check_vector<ObjectValue>(reporter, v, 0, false);
195 }
196
197 {
198 const auto& v = jroot["k4"];
199 REPORTER_ASSERT(reporter, !v.is<NullValue>());
200
201 check_primitive<bool, BoolValue>(reporter, v, false, false);
202 check_primitive<float, NumberValue>(reporter, v, 42, true);
203
204 check_string(reporter, v, nullptr);
205 check_vector<ArrayValue >(reporter, v, 0, false);
206 check_vector<ObjectValue>(reporter, v, 0, false);
207 }
208
209 {
210 const auto& v = jroot["k5"];
211 REPORTER_ASSERT(reporter, !v.is<NullValue>());
212
213 check_primitive<bool, BoolValue>(reporter, v, false, false);
214 check_primitive<float, NumberValue>(reporter, v, .75f, true);
215
216 check_string(reporter, v, nullptr);
217 check_vector<ArrayValue >(reporter, v, 0, false);
218 check_vector<ObjectValue>(reporter, v, 0, false);
219 }
220
221 {
222 const auto& v = jroot["k6"];
223 REPORTER_ASSERT(reporter, !v.is<NullValue>());
224
225 check_primitive<bool, BoolValue>(reporter, v, false, false);
226 check_primitive<float, NumberValue>(reporter, v, 0, false);
227
228 check_string(reporter, v, "foo");
229 check_vector<ArrayValue >(reporter, v, 0, false);
230 check_vector<ObjectValue>(reporter, v, 0, false);
231 }
232
233 {
234 const auto& v = jroot["k7"];
235 REPORTER_ASSERT(reporter, !v.is<NullValue>());
236
237 check_primitive<bool, BoolValue>(reporter, v, false, false);
238 check_primitive<float, NumberValue>(reporter, v, 0, false);
239
240 check_string(reporter, v, nullptr);
241 check_vector<ObjectValue>(reporter, v, 0, false);
242
243 check_vector<ArrayValue >(reporter, v, 3, true);
244 check_primitive<float, NumberValue>(reporter, v.as<ArrayValue>()[0], 1, true);
245 check_primitive<bool, BoolValue>(reporter, v.as<ArrayValue>()[1], true, true);
246 check_vector<StringValue>(reporter, v.as<ArrayValue>()[2], 3, true);
Florin Malita7796f002018-06-08 12:25:38 -0400247 }
248
249 {
250 const auto& v = jroot["k8"];
251 REPORTER_ASSERT(reporter, !v.is<NullValue>());
252
253 check_primitive<bool, BoolValue>(reporter, v, false, false);
254 check_primitive<float, NumberValue>(reporter, v, 0, false);
255
256 check_string(reporter, v, nullptr);
257 check_vector<ArrayValue >(reporter, v, 0, false);
258
259 check_vector<ObjectValue>(reporter, v, 3, true);
260
261 const auto& m0 = v.as<ObjectValue>().begin()[0];
262 check_string(reporter, m0.fKey, "kk1");
263 check_primitive<float, NumberValue>(reporter, m0.fValue, 2, true);
264
265 const auto& m1 = v.as<ObjectValue>().begin()[1];
266 check_string(reporter, m1.fKey, "kk2");
267 check_primitive<bool, BoolValue>(reporter, m1.fValue, false, true);
268
269 const auto& m2 = v.as<ObjectValue>().begin()[2];
270 check_string(reporter, m2.fKey, "kk1");
271 check_string(reporter, m2.fValue, "baz");
272
273 REPORTER_ASSERT(reporter, v.as<ObjectValue>()[""].is<NullValue>());
274 REPORTER_ASSERT(reporter, v.as<ObjectValue>()["nosuchkey"].is<NullValue>());
275 check_string(reporter, v.as<ObjectValue>()["kk1"], "baz");
276 check_primitive<bool, BoolValue>(reporter, v.as<ObjectValue>()["kk2"], false, true);
277 }
Florin Malitaae252792018-06-14 11:24:50 -0400278}
Florin Malita7796f002018-06-08 12:25:38 -0400279
Florin Malitaae252792018-06-14 11:24:50 -0400280template <typename T>
281void check_value(skiatest::Reporter* reporter, const Value& v, const char* expected_string) {
282 REPORTER_ASSERT(reporter, v.is<T>());
283
284 const T* cast_t = v;
285 REPORTER_ASSERT(reporter, cast_t == &v.as<T>());
286
287 const auto vstr = v.toString();
288 REPORTER_ASSERT(reporter, 0 == strcmp(expected_string, vstr.c_str()));
289}
290
291DEF_TEST(SkJSON_DOM_build, reporter) {
292 SkArenaAlloc alloc(4096);
293
294 const auto v0 = NullValue();
295 check_value<NullValue>(reporter, v0, "null");
296
297 const auto v1 = BoolValue(true);
298 check_value<BoolValue>(reporter, v1, "true");
299
300 const auto v2 = BoolValue(false);
301 check_value<BoolValue>(reporter, v2, "false");
302
303 const auto v3 = NumberValue(0);
304 check_value<NumberValue>(reporter, v3, "0");
305
306 const auto v4 = NumberValue(42);
307 check_value<NumberValue>(reporter, v4, "42");
308
309 const auto v5 = NumberValue(42.75f);
310 check_value<NumberValue>(reporter, v5, "42.75");
311
312 const auto v6 = StringValue(nullptr, 0, alloc);
313 check_value<StringValue>(reporter, v6, "\"\"");
314
315 const auto v7 = StringValue(" foo ", 5, alloc);
316 check_value<StringValue>(reporter, v7, "\" foo \"");
317
318 const auto v8 = StringValue(" foo bar baz ", 13, alloc);
319 check_value<StringValue>(reporter, v8, "\" foo bar baz \"");
320
321 const auto v9 = ArrayValue(nullptr, 0, alloc);
322 check_value<ArrayValue>(reporter, v9, "[]");
323
324 const Value values0[] = { v0, v3, v9 };
325 const auto v10 = ArrayValue(values0, SK_ARRAY_COUNT(values0), alloc);
326 check_value<ArrayValue>(reporter, v10, "[null,0,[]]");
327
328 const auto v11 = ObjectValue(nullptr, 0, alloc);
329 check_value<ObjectValue>(reporter, v11, "{}");
330
331 const Member members0[] = {
332 { StringValue("key_0", 5, alloc), v1 },
333 { StringValue("key_1", 5, alloc), v4 },
334 { StringValue("key_2", 5, alloc), v11 },
335 };
336 const auto v12 = ObjectValue(members0, SK_ARRAY_COUNT(members0), alloc);
337 check_value<ObjectValue>(reporter, v12, "{"
338 "\"key_0\":true,"
339 "\"key_1\":42,"
340 "\"key_2\":{}"
341 "}");
342
343 const Value values1[] = { v2, v6, v12 };
344 const auto v13 = ArrayValue(values1, SK_ARRAY_COUNT(values1), alloc);
345 check_value<ArrayValue>(reporter, v13, "["
346 "false,"
347 "\"\","
348 "{"
349 "\"key_0\":true,"
350 "\"key_1\":42,"
351 "\"key_2\":{}"
352 "}"
353 "]");
354
355 const Member members1[] = {
356 { StringValue("key_00", 6, alloc), v5 },
357 { StringValue("key_01", 6, alloc), v7 },
358 { StringValue("key_02", 6, alloc), v13 },
359 };
360 const auto v14 = ObjectValue(members1, SK_ARRAY_COUNT(members1), alloc);
361 check_value<ObjectValue>(reporter, v14, "{"
362 "\"key_00\":42.75,"
363 "\"key_01\":\" foo \","
364 "\"key_02\":["
365 "false,"
366 "\"\","
367 "{"
368 "\"key_0\":true,"
369 "\"key_1\":42,"
370 "\"key_2\":{}"
371 "}"
372 "]"
373 "}");
Florin Malita7796f002018-06-08 12:25:38 -0400374}