aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/JSONTest.cpp
blob: 72c71e3f5685a1d41b0d99379f8d3372c03c5ab9 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Test.h"

#include "SkArenaAlloc.h"
#include "SkJSON.h"
#include "SkString.h"
#include "SkStream.h"

using namespace skjson;

DEF_TEST(JSON_Parse, reporter) {
    static constexpr struct {
        const char* in;
        const char* out;
    } g_tests[] = {
        { ""     , nullptr },
        { "["    , nullptr },
        { "]"    , nullptr },
        { "[[]"  , nullptr },
        { "[]]"  , nullptr },
        { "[]f"  , nullptr },
        { "{"    , nullptr },
        { "}"    , nullptr },
        { "{{}"  , nullptr },
        { "{}}"  , nullptr },
        { "{}f"  , nullptr },
        { "{]"   , nullptr },
        { "[}"   , nullptr },
        { "{\"}" , nullptr },
        { "[\"]" , nullptr },
        { "1"    , nullptr },
        { "true" , nullptr },
        { "false", nullptr },
        { "null" , nullptr },

        { "[nulll]" , nullptr },
        { "[false2]", nullptr },
        { "[true:]" , nullptr },

        { "[1 2]"   , nullptr },
        { "[1,,2]"  , nullptr },
        { "[1,2,]"  , nullptr },
        { "[,1,2]"  , nullptr },

        { "[ \"foo"       , nullptr },
        { "[ \"fo\0o\" ]" , nullptr },

        { "{\"\":{}"                  , nullptr },
        { "{ null }"                  , nullptr },
        { "{ \"k\" : }"               , nullptr },
        { "{ : null }"                , nullptr },
        { "{ \"k\" : : null }"        , nullptr },
        { "{ \"k\" : null , }"        , nullptr },
        { "{ \"k\" : null \"k\" : 1 }", nullptr },


        { "[]"                           , "[]" },
        { " \n\r\t [ \n\r\t ] \n\r\t "   , "[]" },
        { "[[]]"                         , "[[]]" },
        { "[ null ]"                     , "[null]" },
        { "[ true ]"                     , "[true]" },
        { "[ false ]"                    , "[false]" },
        { "[ 0 ]"                        , "[0]" },
        { "[ 1 ]"                        , "[1]" },
        { "[ 1.248 ]"                    , "[1.248]" },
        { "[ \"\" ]"                     , "[\"\"]" },
        { "[ \"foo{bar}baz\" ]"          , "[\"foo{bar}baz\"]" },
        { "[ \" f o o \" ]"              , "[\" f o o \"]" },
        { "[ \"123456\" ]"               , "[\"123456\"]" },
        { "[ \"1234567\" ]"              , "[\"1234567\"]" },
        { "[ \"12345678\" ]"             , "[\"12345678\"]" },
        { "[ \"123456789\" ]"            , "[\"123456789\"]" },
        { "[ null , true, false,0,12.8 ]", "[null,true,false,0,12.8]" },

        { "{}"                          , "{}" },
        { " \n\r\t { \n\r\t } \n\r\t "  , "{}" },
        { "{ \"k\" : null }"            , "{\"k\":null}" },
        { "{ \"foo{\" : \"bar}baz\" }"  , "{\"foo{\":\"bar}baz\"}" },
        { "{ \"k1\" : null, \"k2 \":0 }", "{\"k1\":null,\"k2 \":0}" },
        { "{ \"k1\" : null, \"k1\":0 }" , "{\"k1\":null,\"k1\":0}" },

        { "{ \"k1\" : null,                   \n\
             \"k2\" : 0,                      \n\
             \"k3\" : [                       \n\
                        true,                 \r\n\
                        { \"kk1\" : \"foo\" , \n\
                          \"kk2\" : \"bar\" , \n\
                          \"kk3\" : 1.28 ,    \n\
                          \"kk4\" : [ 42 ]    \n\
                        } ,                   \n\
                        \"boo\" ,             \n\
                        null                  \n\
                      ]                       \n\
           }",
          "{\"k1\":null,\"k2\":0,\"k3\":[true,"
              "{\"kk1\":\"foo\",\"kk2\":\"bar\",\"kk3\":1.28,\"kk4\":[42]},\"boo\",null]}" },
    };

    for (const auto& tst : g_tests) {
        DOM dom(tst.in, strlen(tst.in));
        const auto success = !dom.root().is<NullValue>();
        REPORTER_ASSERT(reporter, success == (tst.out != nullptr));
        if (!success) continue;

        SkDynamicMemoryWStream str;
        dom.write(&str);
        str.write8('\0');

        auto data = str.detachAsData();
        REPORTER_ASSERT(reporter, !strcmp(tst.out, static_cast<const char*>(data->data())));
    }

}

template <typename T, typename VT>
static void check_primitive(skiatest::Reporter* reporter, const Value& v, T pv,
                            bool is_type) {

    REPORTER_ASSERT(reporter,  v.is<VT>() == is_type);
    const VT* cast_t = v;
    REPORTER_ASSERT(reporter, (cast_t != nullptr) == is_type);

    if (is_type) {
        REPORTER_ASSERT(reporter, &v.as<VT>() == cast_t);
        REPORTER_ASSERT(reporter, *v.as<VT>() == pv);
    }
}

template <typename T>
static void check_vector(skiatest::Reporter* reporter, const Value& v, size_t expected_size,
                         bool is_vector) {
    REPORTER_ASSERT(reporter, v.is<T>() == is_vector);
    const T* cast_t = v;
    REPORTER_ASSERT(reporter, (cast_t != nullptr) == is_vector);

    if (is_vector) {
        const auto& vec = v.as<T>();
        REPORTER_ASSERT(reporter, &vec == cast_t);
        REPORTER_ASSERT(reporter, vec.size()  == expected_size);
        REPORTER_ASSERT(reporter, vec.begin() != nullptr);
        REPORTER_ASSERT(reporter, vec.end()   == vec.begin() + expected_size);
    }
}

static void check_string(skiatest::Reporter* reporter, const Value& v, const char* s) {
    check_vector<StringValue>(reporter, v, s ? strlen(s) : 0, !!s);
    if (s) {
        REPORTER_ASSERT(reporter, !strcmp(v.as<StringValue>().begin(), s));
    }
}

DEF_TEST(JSON_DOM_visit, reporter) {
    static constexpr char json[] = "{ \n\
        \"k1\": null,                \n\
        \"k2\": false,               \n\
        \"k3\": true,                \n\
        \"k4\": 42,                  \n\
        \"k5\": .75,                 \n\
        \"k6\": \"foo\",             \n\
        \"k7\": [ 1, true, \"bar\" ], \n\
        \"k8\": { \"kk1\": 2, \"kk2\": false, \"kk1\": \"baz\" } \n\
    }";

    DOM dom(json, strlen(json));

    const auto& jroot = dom.root().as<ObjectValue>();
    REPORTER_ASSERT(reporter, jroot.is<ObjectValue>());

    {
        const auto& v = jroot["k1"];
        REPORTER_ASSERT(reporter,  v.is<NullValue>());

        check_primitive<bool, BoolValue>(reporter, v, false, false);
        check_primitive<float, NumberValue>(reporter, v, 0, false);

        check_string(reporter, v, nullptr);
        check_vector<ArrayValue >(reporter, v, 0, false);
        check_vector<ObjectValue>(reporter, v, 0, false);
    }

    {
        const auto& v = jroot["k2"];
        REPORTER_ASSERT(reporter, !v.is<NullValue>());

        check_primitive<bool, BoolValue>(reporter, v, false, true);
        check_primitive<float, NumberValue>(reporter, v, 0, false);

        check_string(reporter, v, nullptr);
        check_vector<ArrayValue >(reporter, v, 0, false);
        check_vector<ObjectValue>(reporter, v, 0, false);
    }

    {
        const auto& v = jroot["k3"];
        REPORTER_ASSERT(reporter, !v.is<NullValue>());

        check_primitive<bool, BoolValue>(reporter, v, true, true);
        check_primitive<float, NumberValue>(reporter, v, 0, false);

        check_string(reporter, v, nullptr);
        check_vector<ArrayValue >(reporter, v, 0, false);
        check_vector<ObjectValue>(reporter, v, 0, false);
    }

    {
        const auto& v = jroot["k4"];
        REPORTER_ASSERT(reporter, !v.is<NullValue>());

        check_primitive<bool, BoolValue>(reporter, v, false, false);
        check_primitive<float, NumberValue>(reporter, v, 42, true);

        check_string(reporter, v, nullptr);
        check_vector<ArrayValue >(reporter, v, 0, false);
        check_vector<ObjectValue>(reporter, v, 0, false);
    }

    {
        const auto& v = jroot["k5"];
        REPORTER_ASSERT(reporter, !v.is<NullValue>());

        check_primitive<bool, BoolValue>(reporter, v, false, false);
        check_primitive<float, NumberValue>(reporter, v, .75f, true);

        check_string(reporter, v, nullptr);
        check_vector<ArrayValue >(reporter, v, 0, false);
        check_vector<ObjectValue>(reporter, v, 0, false);
    }

    {
        const auto& v = jroot["k6"];
        REPORTER_ASSERT(reporter, !v.is<NullValue>());

        check_primitive<bool, BoolValue>(reporter, v, false, false);
        check_primitive<float, NumberValue>(reporter, v, 0, false);

        check_string(reporter, v, "foo");
        check_vector<ArrayValue >(reporter, v, 0, false);
        check_vector<ObjectValue>(reporter, v, 0, false);
    }

    {
        const auto& v = jroot["k7"];
        REPORTER_ASSERT(reporter, !v.is<NullValue>());

        check_primitive<bool, BoolValue>(reporter, v, false, false);
        check_primitive<float, NumberValue>(reporter, v, 0, false);

        check_string(reporter, v, nullptr);
        check_vector<ObjectValue>(reporter, v, 0, false);

        check_vector<ArrayValue >(reporter, v, 3, true);
        check_primitive<float, NumberValue>(reporter, v.as<ArrayValue>()[0], 1, true);
        check_primitive<bool, BoolValue>(reporter, v.as<ArrayValue>()[1], true, true);
        check_vector<StringValue>(reporter, v.as<ArrayValue>()[2], 3, true);
    }

    {
        const auto& v = jroot["k8"];
        REPORTER_ASSERT(reporter, !v.is<NullValue>());

        check_primitive<bool, BoolValue>(reporter, v, false, false);
        check_primitive<float, NumberValue>(reporter, v, 0, false);

        check_string(reporter, v, nullptr);
        check_vector<ArrayValue >(reporter, v, 0, false);

        check_vector<ObjectValue>(reporter, v, 3, true);

        const auto& m0 = v.as<ObjectValue>().begin()[0];
        check_string(reporter, m0.fKey, "kk1");
        check_primitive<float, NumberValue>(reporter, m0.fValue, 2, true);

        const auto& m1 = v.as<ObjectValue>().begin()[1];
        check_string(reporter, m1.fKey, "kk2");
        check_primitive<bool, BoolValue>(reporter, m1.fValue, false, true);

        const auto& m2 = v.as<ObjectValue>().begin()[2];
        check_string(reporter, m2.fKey, "kk1");
        check_string(reporter, m2.fValue, "baz");

        REPORTER_ASSERT(reporter, v.as<ObjectValue>()[""].is<NullValue>());
        REPORTER_ASSERT(reporter, v.as<ObjectValue>()["nosuchkey"].is<NullValue>());
        check_string(reporter, v.as<ObjectValue>()["kk1"], "baz");
        check_primitive<bool, BoolValue>(reporter, v.as<ObjectValue>()["kk2"], false, true);
    }
}

template <typename T>
void check_value(skiatest::Reporter* reporter, const Value& v, const char* expected_string) {
    REPORTER_ASSERT(reporter, v.is<T>());

    const T* cast_t = v;
    REPORTER_ASSERT(reporter, cast_t == &v.as<T>());

    const auto vstr = v.toString();
    REPORTER_ASSERT(reporter, 0 == strcmp(expected_string, vstr.c_str()));
}

DEF_TEST(JSON_DOM_build, reporter) {
    SkArenaAlloc alloc(4096);

    const auto v0  = NullValue();
    check_value<NullValue>(reporter, v0, "null");

    const auto v1  = BoolValue(true);
    check_value<BoolValue>(reporter, v1, "true");

    const auto v2  = BoolValue(false);
    check_value<BoolValue>(reporter, v2, "false");

    const auto v3  = NumberValue(0);
    check_value<NumberValue>(reporter, v3, "0");

    const auto v4  = NumberValue(42);
    check_value<NumberValue>(reporter, v4, "42");

    const auto v5  = NumberValue(42.75f);
    check_value<NumberValue>(reporter, v5, "42.75");

    const auto v6  = StringValue(nullptr, 0, alloc);
    check_value<StringValue>(reporter, v6, "\"\"");

    const auto v7  = StringValue(" foo ", 5, alloc);
    check_value<StringValue>(reporter, v7, "\" foo \"");

    const auto v8  = StringValue(" foo bar baz ", 13, alloc);
    check_value<StringValue>(reporter, v8, "\" foo bar baz \"");

    const auto v9  = ArrayValue(nullptr, 0, alloc);
    check_value<ArrayValue>(reporter, v9, "[]");

    const Value values0[] = { v0, v3, v9 };
    const auto v10 = ArrayValue(values0, SK_ARRAY_COUNT(values0), alloc);
    check_value<ArrayValue>(reporter, v10, "[null,0,[]]");

    const auto v11 = ObjectValue(nullptr, 0, alloc);
    check_value<ObjectValue>(reporter, v11, "{}");

    const Member members0[] = {
        { StringValue("key_0", 5, alloc), v1  },
        { StringValue("key_1", 5, alloc), v4  },
        { StringValue("key_2", 5, alloc), v11 },
    };
    const auto v12 = ObjectValue(members0, SK_ARRAY_COUNT(members0), alloc);
    check_value<ObjectValue>(reporter, v12, "{"
                                                "\"key_0\":true,"
                                                "\"key_1\":42,"
                                                "\"key_2\":{}"
                                            "}");

    const Value values1[] = { v2, v6, v12 };
    const auto v13 = ArrayValue(values1, SK_ARRAY_COUNT(values1), alloc);
    check_value<ArrayValue>(reporter, v13, "["
                                               "false,"
                                               "\"\","
                                               "{"
                                                   "\"key_0\":true,"
                                                   "\"key_1\":42,"
                                                   "\"key_2\":{}"
                                               "}"
                                           "]");

    const Member members1[] = {
        { StringValue("key_00", 6, alloc), v5  },
        { StringValue("key_01", 6, alloc), v7  },
        { StringValue("key_02", 6, alloc), v13 },
    };
    const auto v14 = ObjectValue(members1, SK_ARRAY_COUNT(members1), alloc);
    check_value<ObjectValue>(reporter, v14, "{"
                                                "\"key_00\":42.75,"
                                                "\"key_01\":\" foo \","
                                                "\"key_02\":["
                                                                "false,"
                                                                "\"\","
                                                                "{"
                                                                    "\"key_0\":true,"
                                                                    "\"key_1\":42,"
                                                                    "\"key_2\":{}"
                                                                "}"
                                                            "]"
                                            "}");
}