summaryrefslogtreecommitdiff
path: root/tools/slang-unit-test/unit-test-source-map.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-03-16 15:19:20 -0400
committerGitHub <noreply@github.com>2023-03-16 15:19:20 -0400
commit4cb899f824ee5e4421f36506e4c77f682b238b09 (patch)
treec348029866666fad59531032ba76f325d67c32ab /tools/slang-unit-test/unit-test-source-map.cpp
parent1036d1a9edec83d8840577f388af8599b5e18f5f (diff)
Preliminary SourceMap support (#2701)
* #include an absolute path didn't work - because paths were taken to always be relative. * WIP source map. * Split out handling of RttiTypeFuncs to a map type. * Make RttiTypeFuncsMap hold default impls. * Slightly more sophisticated RttiTypeFuncsMap * Source map decoding. * Fix tabs. * Fix asserts due to negative values. * Use less obscure mechanisms in SourceMap.
Diffstat (limited to 'tools/slang-unit-test/unit-test-source-map.cpp')
-rw-r--r--tools/slang-unit-test/unit-test-source-map.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/tools/slang-unit-test/unit-test-source-map.cpp b/tools/slang-unit-test/unit-test-source-map.cpp
new file mode 100644
index 000000000..1b48980f8
--- /dev/null
+++ b/tools/slang-unit-test/unit-test-source-map.cpp
@@ -0,0 +1,95 @@
+
+#include "../../source/compiler-core/slang-json-lexer.h"
+#include "../../source/core/slang-string-escape-util.h"
+#include "../../source/compiler-core/slang-json-parser.h"
+#include "../../source/compiler-core/slang-json-value.h"
+
+#include "../../source/compiler-core/slang-json-native.h"
+
+#include "../../source/compiler-core/slang-source-map.h"
+
+#include "../../source/core/slang-rtti-info.h"
+
+#include "tools/unit-test/slang-unit-test.h"
+
+using namespace Slang;
+
+static SlangResult _check()
+{
+ SourceManager sourceManager;
+ sourceManager.initialize(nullptr, nullptr);
+ DiagnosticSink sink(&sourceManager, nullptr);
+
+ const char json[] = 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);
+
+ RttiTypeFuncsMap typeMap = JSONNativeUtil::getTypeFuncsMap();
+
+ JSONValue readValue;
+ {
+ // Now need to parse as JSON
+ String contents(json);
+ SourceFile* sourceFile = sourceManager.createSourceFileWithString(PathInfo::makeUnknown(), contents);
+ 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));
+
+ readValue = builder.getRootValue();
+ }
+
+ // Convert to native
+ JSONSourceMap readS;
+ {
+ JSONToNativeConverter converter(container, &typeMap, &sink);
+
+ // Read it back
+ SLANG_RETURN_ON_FAIL(converter.convert(readValue, GetRttiInfo<JSONSourceMap>::get(), &readS));
+ }
+
+ // Write it out
+ {
+ String json;
+
+ NativeToJSONConverter converter(container, &typeMap, &sink);
+
+ JSONValue value;
+ SLANG_RETURN_ON_FAIL(converter.convert(GetRttiInfo<JSONSourceMap>::get(), &readS, value));
+
+ // Convert into a string
+ JSONWriter writer(JSONWriter::IndentationStyle::Allman);
+ container->traverseRecursively(value, &writer);
+
+ json = writer.getBuilder();
+ }
+
+ {
+ SourceMap sourceMap;
+
+ SLANG_RETURN_ON_FAIL(sourceMap.decode(container, readS));
+ }
+
+ return SLANG_OK;
+}
+
+SLANG_UNIT_TEST(sourceMap)
+{
+ SLANG_CHECK(SLANG_SUCCEEDED(_check()));
+}
+