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

using namespace Slang;

static SlangResult _read(
    JSONContainer* container,
    const String& json,
    DiagnosticSink* sink,
    SourceMap& outSourceMap)
{
    auto sourceManager = sink->getSourceManager();

    JSONValue rootValue;
    {
        // Now need to parse as JSON
        SourceFile* sourceFile =
            sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), json);
        SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc());

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

        JSONBuilder builder(container);

        JSONParser parser;
        SLANG_RETURN_ON_FAIL(parser.parse(&lexer, sourceView, &builder, sink));

        rootValue = builder.getRootValue();
    }

    SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::decode(container, rootValue, sink, outSourceMap));

    return SLANG_OK;
}

static SlangResult _write(
    JSONContainer* container,
    const SourceMap& sourceMap,
    DiagnosticSink* sink,
    String& out)
{
    // Write it out
    JSONValue jsonValue;

    SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::encode(sourceMap, container, sink, jsonValue));

    // Convert into a string
    JSONWriter writer(JSONWriter::IndentationStyle::Allman);
    container->traverseRecursively(jsonValue, &writer);

    out = writer.getBuilder();
    return SLANG_OK;
}

static SlangResult _check()
{
    SourceManager sourceManager;
    sourceManager.initialize(nullptr, nullptr);
    DiagnosticSink sink(&sourceManager, nullptr);

    const char jsonSource[] = R"(
{
        "version" : 3,
        "file" : "out.js",
        "sourceRoot" : "",
        "sources" : ["foo.js", "bar.js"],
        "sourcesContent" : [null, null],
        "names" : ["src", "maps", "are", "fun"],
        "mappings" : "A,AAAB;;ABCEG;" 
}
)";

    RefPtr<JSONContainer> container = new JSONContainer(&sourceManager);

    SourceMap sourceMap;
    SLANG_RETURN_ON_FAIL(_read(container, jsonSource, &sink, sourceMap));

    String json;
    SLANG_RETURN_ON_FAIL(_write(container, sourceMap, &sink, json));

    SourceMap readSourceMap;
    SLANG_RETURN_ON_FAIL(_read(container, json, &sink, readSourceMap));

    if (readSourceMap != sourceMap)
    {
        return SLANG_FAIL;
    }

    // Lets try copy construction
    {
        SourceMap copy(sourceMap);

        if (copy != sourceMap)
        {
            return SLANG_FAIL;
        }
    }

    // Lets try assignment
    {
        SourceMap assign;
        assign = sourceMap;
        if (assign != sourceMap)
        {
            return SLANG_FAIL;
        }
    }

    return SLANG_OK;
}

SLANG_UNIT_TEST(sourceMap)
{
    SLANG_CHECK(SLANG_SUCCEEDED(_check()));
}