summaryrefslogtreecommitdiffstats
path: root/tools/slang-unit-test/unit-test-source-map.cpp
diff options
context:
space:
mode:
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, 67 insertions, 28 deletions
diff --git a/tools/slang-unit-test/unit-test-source-map.cpp b/tools/slang-unit-test/unit-test-source-map.cpp
index 125c4eccc..d4363e02c 100644
--- a/tools/slang-unit-test/unit-test-source-map.cpp
+++ b/tools/slang-unit-test/unit-test-source-map.cpp
@@ -15,6 +15,47 @@
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;
@@ -35,40 +76,38 @@ static SlangResult _check()
RefPtr<JSONContainer> container = new JSONContainer(&sourceManager);
- JSONValue rootValue;
- {
- // Now need to parse as JSON
- String contents(jsonSource);
- 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));
-
- rootValue = builder.getRootValue();
- }
-
SourceMap sourceMap;
-
- SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::decode(container, rootValue, &sink, sourceMap));
+ SLANG_RETURN_ON_FAIL(_read(container, jsonSource, &sink, sourceMap));
- // Write it out
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
{
- JSONValue jsonValue;
+ SourceMap copy(sourceMap);
- SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::encode(sourceMap, container, &sink, jsonValue));
-
- // Convert into a string
- JSONWriter writer(JSONWriter::IndentationStyle::Allman);
- container->traverseRecursively(jsonValue, &writer);
+ if (copy != sourceMap)
+ {
+ return SLANG_FAIL;
+ }
+ }
- json = writer.getBuilder();
+ // Lets try assignment
+ {
+ SourceMap assign;
+ assign = sourceMap;
+ if (assign != sourceMap)
+ {
+ return SLANG_FAIL;
+ }
}
return SLANG_OK;