diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2023-04-20 13:42:20 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-20 13:42:20 -0400 |
| commit | 088644c21c015eb60a41a59f417990023f1f4ef8 (patch) | |
| tree | 596d224f99e35dcd3d66507cfe297bc25407336d /tools/slang-unit-test/unit-test-source-map.cpp | |
| parent | 4e67cdedbef8f643c90b48172d5419d3dd1839db (diff) | |
Improve SourceMap coverage/testing (#2818)
* #include an absolute path didn't work - because paths were taken to always be relative.
* WIP around more value like behavior for SourceMap/StringPool.
* Use default impls on SourceMap ctor/assignment.
* Handle comparison of SourceMaps.
Some small fixes.
Expand unit tests.
Diffstat (limited to 'tools/slang-unit-test/unit-test-source-map.cpp')
| -rw-r--r-- | tools/slang-unit-test/unit-test-source-map.cpp | 95 |
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; |
