blob: 1aaa278fb8ac719d6896868fa38c4f0d6a12afe8 [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 },
Florin Malitafedfd542018-06-14 15:03:21 -040025 { "[[]" , nullptr },
26 { "[]]" , nullptr },
27 { "[]f" , nullptr },
Florin Malita7796f002018-06-08 12:25:38 -040028 { "{" , nullptr },
29 { "}" , nullptr },
Florin Malitafedfd542018-06-14 15:03:21 -040030 { "{{}" , nullptr },
31 { "{}}" , nullptr },
32 { "{}f" , nullptr },
Florin Malita7796f002018-06-08 12:25:38 -040033 { "{]" , nullptr },
34 { "[}" , nullptr },
35 { "1" , nullptr },
36 { "true" , nullptr },
37 { "false", nullptr },
38 { "null" , nullptr },
39
40 { "[nulll]" , nullptr },
41 { "[false2]", nullptr },
42 { "[true:]" , nullptr },
43
44 { "[1 2]" , nullptr },
45 { "[1,,2]" , nullptr },
46 { "[1,2,]" , nullptr },
47 { "[,1,2]" , nullptr },
48
49 { "[ \"foo" , nullptr },
50 { "[ \"fo\0o\" ]" , nullptr },
51
Florin Malita587f5a92018-06-15 09:21:36 -040052 { "{\"\":{}" , nullptr },
Florin Malita7796f002018-06-08 12:25:38 -040053 { "{ null }" , nullptr },
54 { "{ \"k\" : }" , nullptr },
55 { "{ : null }" , nullptr },
56 { "{ \"k\" : : null }" , nullptr },
57 { "{ \"k\" : null , }" , nullptr },
58 { "{ \"k\" : null \"k\" : 1 }", nullptr },
59
60
Florin Malitafedfd542018-06-14 15:03:21 -040061 { "[]" , "[]" },
Florin Malita7796f002018-06-08 12:25:38 -040062 { " \n\r\t [ \n\r\t ] \n\r\t " , "[]" },
Florin Malitafedfd542018-06-14 15:03:21 -040063 { "[[]]" , "[[]]" },
Florin Malita7796f002018-06-08 12:25:38 -040064 { "[ null ]" , "[null]" },
65 { "[ true ]" , "[true]" },
66 { "[ false ]" , "[false]" },
67 { "[ 0 ]" , "[0]" },
68 { "[ 1 ]" , "[1]" },
69 { "[ 1.248 ]" , "[1.248]" },
70 { "[ \"\" ]" , "[\"\"]" },
71 { "[ \" f o o \" ]" , "[\" f o o \"]" },
72 { "[ \"123456\" ]" , "[\"123456\"]" },
73 { "[ \"1234567\" ]" , "[\"1234567\"]" },
74 { "[ \"12345678\" ]" , "[\"12345678\"]" },
75 { "[ \"123456789\" ]" , "[\"123456789\"]" },
76 { "[ null , true, false,0,12.8 ]", "[null,true,false,0,12.8]" },
77
Florin Malitafedfd542018-06-14 15:03:21 -040078 { "{}" , "{}" },
Florin Malita7796f002018-06-08 12:25:38 -040079 { " \n\r\t { \n\r\t } \n\r\t " , "{}" },
80 { "{ \"k\" : null }" , "{\"k\":null}" },
81 { "{ \"k1\" : null, \"k2 \":0 }", "{\"k1\":null,\"k2 \":0}" },
82 { "{ \"k1\" : null, \"k1\":0 }" , "{\"k1\":null,\"k1\":0}" },
83
84 { "{ \"k1\" : null, \n\
85 \"k2\" : 0, \n\
86 \"k3\" : [ \n\
87 true, \r\n\
88 { \"kk1\" : \"foo\" , \n\
89 \"kk2\" : \"bar\" , \n\
90 \"kk3\" : 1.28 , \n\
91 \"kk4\" : [ 42 ] \n\
92 } , \n\
93 \"boo\" , \n\
94 null \n\
95 ] \n\
96 }",
97 "{\"k1\":null,\"k2\":0,\"k3\":[true,"
98 "{\"kk1\":\"foo\",\"kk2\":\"bar\",\"kk3\":1.28,\"kk4\":[42]},\"boo\",null]}" },
99 };
100
101 for (const auto& tst : g_tests) {
Florin Malitafedfd542018-06-14 15:03:21 -0400102 DOM dom(tst.in, strlen(tst.in));
Florin Malita7796f002018-06-08 12:25:38 -0400103 const auto success = !dom.root().is<NullValue>();
104 REPORTER_ASSERT(reporter, success == (tst.out != nullptr));
105 if (!success) continue;
106
107 SkDynamicMemoryWStream str;
108 dom.write(&str);
109 str.write8('\0');
110
111 auto data = str.detachAsData();
112 REPORTER_ASSERT(reporter, !strcmp(tst.out, static_cast<const char*>(data->data())));
113 }
114
115}
116
117template <typename T, typename VT>
118static void check_primitive(skiatest::Reporter* reporter, const Value& v, T pv,
119 bool is_type) {
120
121 REPORTER_ASSERT(reporter, v.is<VT>() == is_type);
Florin Malitaae252792018-06-14 11:24:50 -0400122 const VT* cast_t = v;
123 REPORTER_ASSERT(reporter, (cast_t != nullptr) == is_type);
124
125 if (is_type) {
126 REPORTER_ASSERT(reporter, &v.as<VT>() == cast_t);
127 REPORTER_ASSERT(reporter, *v.as<VT>() == pv);
128 }
Florin Malita7796f002018-06-08 12:25:38 -0400129}
130
131template <typename T>
132static void check_vector(skiatest::Reporter* reporter, const Value& v, size_t expected_size,
133 bool is_vector) {
134 REPORTER_ASSERT(reporter, v.is<T>() == is_vector);
Florin Malitaae252792018-06-14 11:24:50 -0400135 const T* cast_t = v;
136 REPORTER_ASSERT(reporter, (cast_t != nullptr) == is_vector);
Florin Malita7796f002018-06-08 12:25:38 -0400137
Florin Malitaae252792018-06-14 11:24:50 -0400138 if (is_vector) {
139 const auto& vec = v.as<T>();
140 REPORTER_ASSERT(reporter, &vec == cast_t);
141 REPORTER_ASSERT(reporter, vec.size() == expected_size);
142 REPORTER_ASSERT(reporter, vec.begin() != nullptr);
143 REPORTER_ASSERT(reporter, vec.end() == vec.begin() + expected_size);
144 }
Florin Malita7796f002018-06-08 12:25:38 -0400145}
146
147static void check_string(skiatest::Reporter* reporter, const Value& v, const char* s) {
148 check_vector<StringValue>(reporter, v, s ? strlen(s) : 0, !!s);
149 if (s) {
150 REPORTER_ASSERT(reporter, !strcmp(v.as<StringValue>().begin(), s));
151 }
152}
153
Florin Malitaae252792018-06-14 11:24:50 -0400154DEF_TEST(SkJSON_DOM_visit, reporter) {
Florin Malita7796f002018-06-08 12:25:38 -0400155 static constexpr char json[] = "{ \n\
156 \"k1\": null, \n\
157 \"k2\": false, \n\
158 \"k3\": true, \n\
159 \"k4\": 42, \n\
160 \"k5\": .75, \n\
161 \"k6\": \"foo\", \n\
162 \"k7\": [ 1, true, \"bar\" ], \n\
163 \"k8\": { \"kk1\": 2, \"kk2\": false, \"kk1\": \"baz\" } \n\
164 }";
165
Florin Malitafedfd542018-06-14 15:03:21 -0400166 DOM dom(json, strlen(json));
Florin Malita7796f002018-06-08 12:25:38 -0400167
168 const auto& jroot = dom.root().as<ObjectValue>();
169 REPORTER_ASSERT(reporter, jroot.is<ObjectValue>());
170
171 {
172 const auto& v = jroot["k1"];
173 REPORTER_ASSERT(reporter, v.is<NullValue>());
174
175 check_primitive<bool, BoolValue>(reporter, v, false, false);
176 check_primitive<float, NumberValue>(reporter, v, 0, false);
177
178 check_string(reporter, v, nullptr);
179 check_vector<ArrayValue >(reporter, v, 0, false);
180 check_vector<ObjectValue>(reporter, v, 0, false);
181 }
182
183 {
184 const auto& v = jroot["k2"];
185 REPORTER_ASSERT(reporter, !v.is<NullValue>());
186
187 check_primitive<bool, BoolValue>(reporter, v, false, true);
188 check_primitive<float, NumberValue>(reporter, v, 0, false);
189
190 check_string(reporter, v, nullptr);
191 check_vector<ArrayValue >(reporter, v, 0, false);
192 check_vector<ObjectValue>(reporter, v, 0, false);
193 }
194
195 {
196 const auto& v = jroot["k3"];
197 REPORTER_ASSERT(reporter, !v.is<NullValue>());
198
199 check_primitive<bool, BoolValue>(reporter, v, true, true);
200 check_primitive<float, NumberValue>(reporter, v, 0, false);
201
202 check_string(reporter, v, nullptr);
203 check_vector<ArrayValue >(reporter, v, 0, false);
204 check_vector<ObjectValue>(reporter, v, 0, false);
205 }
206
207 {
208 const auto& v = jroot["k4"];
209 REPORTER_ASSERT(reporter, !v.is<NullValue>());
210
211 check_primitive<bool, BoolValue>(reporter, v, false, false);
212 check_primitive<float, NumberValue>(reporter, v, 42, true);
213
214 check_string(reporter, v, nullptr);
215 check_vector<ArrayValue >(reporter, v, 0, false);
216 check_vector<ObjectValue>(reporter, v, 0, false);
217 }
218
219 {
220 const auto& v = jroot["k5"];
221 REPORTER_ASSERT(reporter, !v.is<NullValue>());
222
223 check_primitive<bool, BoolValue>(reporter, v, false, false);
224 check_primitive<float, NumberValue>(reporter, v, .75f, true);
225
226 check_string(reporter, v, nullptr);
227 check_vector<ArrayValue >(reporter, v, 0, false);
228 check_vector<ObjectValue>(reporter, v, 0, false);
229 }
230
231 {
232 const auto& v = jroot["k6"];
233 REPORTER_ASSERT(reporter, !v.is<NullValue>());
234
235 check_primitive<bool, BoolValue>(reporter, v, false, false);
236 check_primitive<float, NumberValue>(reporter, v, 0, false);
237
238 check_string(reporter, v, "foo");
239 check_vector<ArrayValue >(reporter, v, 0, false);
240 check_vector<ObjectValue>(reporter, v, 0, false);
241 }
242
243 {
244 const auto& v = jroot["k7"];
245 REPORTER_ASSERT(reporter, !v.is<NullValue>());
246
247 check_primitive<bool, BoolValue>(reporter, v, false, false);
248 check_primitive<float, NumberValue>(reporter, v, 0, false);
249
250 check_string(reporter, v, nullptr);
251 check_vector<ObjectValue>(reporter, v, 0, false);
252
253 check_vector<ArrayValue >(reporter, v, 3, true);
254 check_primitive<float, NumberValue>(reporter, v.as<ArrayValue>()[0], 1, true);
255 check_primitive<bool, BoolValue>(reporter, v.as<ArrayValue>()[1], true, true);
256 check_vector<StringValue>(reporter, v.as<ArrayValue>()[2], 3, true);
Florin Malita7796f002018-06-08 12:25:38 -0400257 }
258
259 {
260 const auto& v = jroot["k8"];
261 REPORTER_ASSERT(reporter, !v.is<NullValue>());
262
263 check_primitive<bool, BoolValue>(reporter, v, false, false);
264 check_primitive<float, NumberValue>(reporter, v, 0, false);
265
266 check_string(reporter, v, nullptr);
267 check_vector<ArrayValue >(reporter, v, 0, false);
268
269 check_vector<ObjectValue>(reporter, v, 3, true);
270
271 const auto& m0 = v.as<ObjectValue>().begin()[0];
272 check_string(reporter, m0.fKey, "kk1");
273 check_primitive<float, NumberValue>(reporter, m0.fValue, 2, true);
274
275 const auto& m1 = v.as<ObjectValue>().begin()[1];
276 check_string(reporter, m1.fKey, "kk2");
277 check_primitive<bool, BoolValue>(reporter, m1.fValue, false, true);
278
279 const auto& m2 = v.as<ObjectValue>().begin()[2];
280 check_string(reporter, m2.fKey, "kk1");
281 check_string(reporter, m2.fValue, "baz");
282
283 REPORTER_ASSERT(reporter, v.as<ObjectValue>()[""].is<NullValue>());
284 REPORTER_ASSERT(reporter, v.as<ObjectValue>()["nosuchkey"].is<NullValue>());
285 check_string(reporter, v.as<ObjectValue>()["kk1"], "baz");
286 check_primitive<bool, BoolValue>(reporter, v.as<ObjectValue>()["kk2"], false, true);
287 }
Florin Malitaae252792018-06-14 11:24:50 -0400288}
Florin Malita7796f002018-06-08 12:25:38 -0400289
Florin Malitaae252792018-06-14 11:24:50 -0400290template <typename T>
291void check_value(skiatest::Reporter* reporter, const Value& v, const char* expected_string) {
292 REPORTER_ASSERT(reporter, v.is<T>());
293
294 const T* cast_t = v;
295 REPORTER_ASSERT(reporter, cast_t == &v.as<T>());
296
297 const auto vstr = v.toString();
298 REPORTER_ASSERT(reporter, 0 == strcmp(expected_string, vstr.c_str()));
299}
300
301DEF_TEST(SkJSON_DOM_build, reporter) {
302 SkArenaAlloc alloc(4096);
303
304 const auto v0 = NullValue();
305 check_value<NullValue>(reporter, v0, "null");
306
307 const auto v1 = BoolValue(true);
308 check_value<BoolValue>(reporter, v1, "true");
309
310 const auto v2 = BoolValue(false);
311 check_value<BoolValue>(reporter, v2, "false");
312
313 const auto v3 = NumberValue(0);
314 check_value<NumberValue>(reporter, v3, "0");
315
316 const auto v4 = NumberValue(42);
317 check_value<NumberValue>(reporter, v4, "42");
318
319 const auto v5 = NumberValue(42.75f);
320 check_value<NumberValue>(reporter, v5, "42.75");
321
322 const auto v6 = StringValue(nullptr, 0, alloc);
323 check_value<StringValue>(reporter, v6, "\"\"");
324
325 const auto v7 = StringValue(" foo ", 5, alloc);
326 check_value<StringValue>(reporter, v7, "\" foo \"");
327
328 const auto v8 = StringValue(" foo bar baz ", 13, alloc);
329 check_value<StringValue>(reporter, v8, "\" foo bar baz \"");
330
331 const auto v9 = ArrayValue(nullptr, 0, alloc);
332 check_value<ArrayValue>(reporter, v9, "[]");
333
334 const Value values0[] = { v0, v3, v9 };
335 const auto v10 = ArrayValue(values0, SK_ARRAY_COUNT(values0), alloc);
336 check_value<ArrayValue>(reporter, v10, "[null,0,[]]");
337
338 const auto v11 = ObjectValue(nullptr, 0, alloc);
339 check_value<ObjectValue>(reporter, v11, "{}");
340
341 const Member members0[] = {
342 { StringValue("key_0", 5, alloc), v1 },
343 { StringValue("key_1", 5, alloc), v4 },
344 { StringValue("key_2", 5, alloc), v11 },
345 };
346 const auto v12 = ObjectValue(members0, SK_ARRAY_COUNT(members0), alloc);
347 check_value<ObjectValue>(reporter, v12, "{"
348 "\"key_0\":true,"
349 "\"key_1\":42,"
350 "\"key_2\":{}"
351 "}");
352
353 const Value values1[] = { v2, v6, v12 };
354 const auto v13 = ArrayValue(values1, SK_ARRAY_COUNT(values1), alloc);
355 check_value<ArrayValue>(reporter, v13, "["
356 "false,"
357 "\"\","
358 "{"
359 "\"key_0\":true,"
360 "\"key_1\":42,"
361 "\"key_2\":{}"
362 "}"
363 "]");
364
365 const Member members1[] = {
366 { StringValue("key_00", 6, alloc), v5 },
367 { StringValue("key_01", 6, alloc), v7 },
368 { StringValue("key_02", 6, alloc), v13 },
369 };
370 const auto v14 = ObjectValue(members1, SK_ARRAY_COUNT(members1), alloc);
371 check_value<ObjectValue>(reporter, v14, "{"
372 "\"key_00\":42.75,"
373 "\"key_01\":\" foo \","
374 "\"key_02\":["
375 "false,"
376 "\"\","
377 "{"
378 "\"key_0\":true,"
379 "\"key_1\":42,"
380 "\"key_2\":{}"
381 "}"
382 "]"
383 "}");
Florin Malita7796f002018-06-08 12:25:38 -0400384}