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