summaryrefslogtreecommitdiffstats
path: root/tools/slang-unit-test/unit-test-json.cpp
blob: 5f3e7ed56d2e62b331776bed07f62cf46018189a (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
#include "../../source/compiler-core/slang-json-lexer.h"
#include "../../source/compiler-core/slang-json-parser.h"
#include "../../source/compiler-core/slang-json-value.h"
#include "../../source/core/slang-string-escape-util.h"
#include "unit-test/slang-unit-test.h"

using namespace Slang;

namespace
{ // anonymous

struct Element
{
    JSONTokenType type;
    const char* value;
};

} // namespace

static SlangResult _lex(const char* in, DiagnosticSink* sink, List<JSONToken>& toks)
{
    SourceManager* sourceManager = sink->getSourceManager();

    String contents(in);
    SourceFile* sourceFile =
        sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), contents);
    SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc());

    JSONLexer lexer;

    lexer.init(sourceView, sink);

    while (lexer.peekType() != JSONTokenType::EndOfFile)
    {
        if (lexer.peekType() == JSONTokenType::Invalid)
        {
            toks.add(lexer.peekToken());
            return SLANG_FAIL;
        }

        toks.add(lexer.peekToken());
        lexer.advance();
    }

    toks.add(lexer.peekToken());

    // If we advance from end of file we should still be at EndOfFile
    SLANG_ASSERT(lexer.advance() == JSONTokenType::EndOfFile);

    return SLANG_OK;
}

static SlangResult _parse(const char* in, DiagnosticSink* sink, JSONListener* listener)
{
    SourceManager* sourceManager = sink->getSourceManager();

    String contents(in);
    SourceFile* sourceFile =
        sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), contents);
    SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc());

    JSONLexer lexer;
    lexer.init(sourceView, sink);

    JSONParser parser;
    SLANG_RETURN_ON_FAIL(parser.parse(&lexer, sourceView, listener, sink));
    return SLANG_OK;
}

static bool _areEqual(
    SourceManager* sourceManager,
    const List<JSONToken>& toks,
    const Element* eles,
    Index elesCount)
{
    if (toks.getCount() != elesCount)
    {
        return false;
    }

    SourceView* sourceView = toks.getCount() ? sourceManager->findSourceView(toks[0].loc) : nullptr;
    const char* const content = sourceView ? sourceView->getContent().begin() : nullptr;

    for (Index i = 0; i < toks.getCount(); ++i)
    {
        const JSONToken& tok = toks[i];
        const auto& ele = eles[i];

        if (tok.type != ele.type)
        {
            return false;
        }

        SLANG_ASSERT(sourceView->getRange().contains(tok.loc));

        const char* start = content + sourceView->getRange().getOffset(tok.loc);

        UnownedStringSlice lexeme(start, tok.length);

        if (lexeme != ele.value)
        {
            return false;
        }
    }

    return true;
}

SLANG_UNIT_TEST(json)
{
    SourceManager sourceManager;
    sourceManager.initialize(nullptr, nullptr);
    DiagnosticSink sink(&sourceManager, nullptr);

    {
        const char text[] =
            " { \"Hello\" : [ \"World\", 1, 2.0, -3.0, -435.5345435, 45e-10, 421.00e+20, 17e1] }";

        const Element eles[] = {
            {JSONTokenType::LBrace, "{"},
            {JSONTokenType::StringLiteral, "\"Hello\""},
            {JSONTokenType::Colon, ":"},
            {JSONTokenType::LBracket, "["},
            {JSONTokenType::StringLiteral, "\"World\""},
            {JSONTokenType::Comma, ","},
            {JSONTokenType::IntegerLiteral, "1"},
            {JSONTokenType::Comma, ","},
            {JSONTokenType::FloatLiteral, "2.0"},
            {JSONTokenType::Comma, ","},
            {JSONTokenType::FloatLiteral, "-3.0"},
            {JSONTokenType::Comma, ","},
            {JSONTokenType::FloatLiteral, "-435.5345435"},
            {JSONTokenType::Comma, ","},
            {JSONTokenType::FloatLiteral, "45e-10"},
            {JSONTokenType::Comma, ","},
            {JSONTokenType::FloatLiteral, "421.00e+20"},
            {JSONTokenType::Comma, ","},
            {JSONTokenType::FloatLiteral, "17e1"},
            {JSONTokenType::RBracket, "]"},
            {JSONTokenType::RBrace, "}"},
            {JSONTokenType::EndOfFile, ""},
        };

        List<JSONToken> toks;
        SLANG_CHECK(SLANG_SUCCEEDED(_lex(text, &sink, toks)));

        SLANG_CHECK(_areEqual(&sourceManager, toks, eles, SLANG_COUNT_OF(eles)));
    }

    {
        StringEscapeHandler* handler = StringEscapeUtil::getHandler(StringEscapeUtil::Style::JSON);


        {
            const auto slice = UnownedStringSlice::fromLiteral("\n\r\b\f\t \"\\/ Some text...");

            SLANG_CHECK(handler->isEscapingNeeded(slice));
            SLANG_CHECK(!handler->isEscapingNeeded(UnownedStringSlice::fromLiteral("Hello!")));

            StringBuilder escaped;
            handler->appendEscaped(slice, escaped);

            StringBuilder unescaped;
            handler->appendUnescaped(escaped.getUnownedSlice(), unescaped);

            SLANG_CHECK(unescaped == slice);
        }

        {
            uint32_t v = 0x7f;

            StringBuilder buf;
            while (v < 0x10000)
            {
                char work[10] = "\\u";

                for (Int i = 0; i < 4; ++i)
                {
                    const uint32_t digitValue = (v >> ((3 - i) * 4)) & 0xf;

                    char digitC =
                        (digitValue > 9) ? char(digitValue - 10 + 'a') : char(digitValue + '0');
                    work[i + 2] = digitC;
                }

                buf << UnownedStringSlice(work, 6);

                v += v;
            }

            // Decode it
            StringBuilder unescaped;
            handler->appendUnescaped(buf.getUnownedSlice(), unescaped);

            // Encode it
            StringBuilder escaped;
            handler->appendEscaped(unescaped.getUnownedSlice(), escaped);

            SLANG_CHECK(escaped == buf);
        }
    }

    {
        const char in[] = "{ \"Hello\" : \"Json\", \"!\" : 10, \"array\" : [1, 2, 3.0] }";

        {
            auto style = JSONWriter::IndentationStyle::Allman;

            JSONWriter writer(style);
            _parse(in, &sink, &writer);

            JSONWriter writerCheck(style);
            _parse(writer.getBuilder().getBuffer(), &sink, &writerCheck);

            SLANG_CHECK(writerCheck.getBuilder() == writer.getBuilder());
        }

        {
            auto style = JSONWriter::IndentationStyle::KNR;

            JSONWriter writer(style, 80);
            _parse(in, &sink, &writer);

            JSONWriter writerCheck(style);
            _parse(writer.getBuilder().getBuffer(), &sink, &writerCheck);

            SLANG_CHECK(writerCheck.getBuilder() == writer.getBuilder());
        }

        {
            // Let's parse into a Value
            RefPtr<JSONContainer> container = new JSONContainer(&sourceManager);

            JSONValue value;
            {
                JSONBuilder builder(container);

                SLANG_CHECK(SLANG_SUCCEEDED(_parse(in, &sink, &builder)));
                value = builder.getRootValue();
            }
            // Let's recreate
            JSONValue copy;
            {
                JSONBuilder builder(container);
                container->traverseRecursively(value, &builder);
                copy = builder.getRootValue();
            }

            SLANG_CHECK(container->areEqual(value, copy));
        }
    }

    {
        // Only need a SourceManager if we are going to store lexemes
        RefPtr<JSONContainer> container = new JSONContainer(nullptr);

        {
            List<JSONValue> values;

            for (Int i = 0; i < 100; ++i)
            {

                values.add(JSONValue::makeInt(i));
                values.add(JSONValue::makeFloat(-double(i)));
            }

            JSONValue array = container->createArray(values.getBuffer(), values.getCount());

            auto arrayView = container->getArray(array);

            SLANG_CHECK(arrayView.getCount() == values.getCount());

            // Check the values are the same
            SLANG_CHECK(container->areEqual(
                arrayView.getBuffer(),
                values.getBuffer(),
                arrayView.getCount()));

            {
                JSONWriter writer(JSONWriter::IndentationStyle::KNR, 80);

                container->traverseRecursively(array, &writer);
            }
        }
        {
            JSONValue obj = JSONValue::makeEmptyObject();

            JSONKey key = container->getKey(UnownedStringSlice::fromLiteral("Hello"));

            container->setKeyValue(obj, key, JSONValue::makeNull());
            container->setKeyValue(obj, key, JSONValue::makeInt(10));

            auto objView = container->getObject(obj);

            SLANG_CHECK(objView.getCount() == 1);

            SLANG_CHECK(objView[0].value.asInteger() == 10);
        }
    }

    // Check repeated keys works out
    // Check out comparison works with different key orders
    {
        RefPtr<JSONContainer> container = new JSONContainer(&sourceManager);
        const char aText[] = "{ \"a\" : 10, \"b\" : 20.0, \"a\" : \"Hello\" }";


        JSONBuilder builder(container);
        SLANG_CHECK(SLANG_SUCCEEDED(_parse(aText, &sink, &builder)));
        const JSONValue a = builder.getRootValue();

        builder.reset();

        const char bText[] = "{ \"b\" : 20.0, \"a\" : \"Hello\"}";
        SLANG_CHECK(SLANG_SUCCEEDED(_parse(bText, &sink, &builder)));
        const JSONValue b = builder.getRootValue();

        SLANG_CHECK(container->areEqual(a, b));

        JSONBuilder convertBuilder(container, JSONBuilder::Flag::ConvertLexemes);

        SLANG_CHECK(SLANG_SUCCEEDED(_parse(aText, &sink, &convertBuilder)));
        const JSONValue c = builder.getRootValue();

        SLANG_CHECK(container->areEqual(a, c));
    }

    {
        RefPtr<JSONContainer> container = new JSONContainer(&sourceManager);
        const char aText[] = "{ \"a\" : \"Hi!\", \"b\" : 20.0, \"c\" : \"Hello\", \"d\" : 30, "
                             "\"e\": null, \"f\": true }";

        JSONBuilder builder(container);
        SLANG_CHECK(SLANG_SUCCEEDED(_parse(aText, &sink, &builder)));
        const JSONValue rootValue = builder.getRootValue();

        List<PersistentJSONValue> values;

        for (char c = 'a'; c <= 'f'; c++)
        {
            const char name[] = {c, 0};
            JSONKey key = container->getKey(UnownedStringSlice(name, 1));
            auto value = container->findObjectValue(rootValue, key);

            SLANG_CHECK(value.type != JSONValue::Type::Invalid);

            PersistentJSONValue persistentValue(value, container);
            values.add(persistentValue);

            PersistentJSONValue copyValue(persistentValue);
            PersistentJSONValue assignValue;
            assignValue = persistentValue;

            SLANG_CHECK(copyValue == persistentValue);
            SLANG_CHECK(assignValue == persistentValue);
        }

        // Destroy the container
        container.setNull();

        SLANG_CHECK(values[0].getSlice() == "Hi!");
        SLANG_CHECK(values[1].asFloat() == 20.0f);
        SLANG_CHECK(values[2].getSlice() == "Hello");
        SLANG_CHECK(values[3].asInteger() == 30);
        SLANG_CHECK(values[4].type == JSONValue::Type::Null);
        SLANG_CHECK(values[5].asBool() == true);
    }
}