diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2021-11-18 15:58:12 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-18 15:58:12 -0500 |
| commit | 1d5f815b3964edee8a2d701e1a6cc078c89d677f (patch) | |
| tree | aa5b4b1473344e635d7ce1d2159fc57eeb40b841 /tools/slang-unit-test/unit-test-json-native.cpp | |
| parent | b482844b689eb109ee1d70c527e098400ac6d409 (diff) | |
RTTI/JSON (#2021)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Use 'Process' to communicate with an command line tool.
* Remove slang-win-stream
* Tidy up windows ProcessUtil.
* First version of BufferedReadStream.
* Windows working IPC for steams.
* Test proxy count option.
* Split Process/ProcessUtil. Process is platform dependant. ProcessUtil are functions that are platform independent.
* First implementation of Unix Process interface.
* Unix process compiles on cygwin.
* Fix typo in unix process.
* Separate unix pipe stream error of invalid access, from pipe availability.
* Fix in standard line extraction.
* Make fd non blocking.
* Fix issues with Windows Process streams.
* Added UnixPipe.
* Some fixes around UnixPipeStream.
* Make a unix stream closed explicit.
* Hack to debug linux process/stream.
* Revert to old linux pipe handling.
* Pass executable path for unit tests.
Split out CommandLine into own source.
* Small improvements in process/command line.
* Check process behavior with crash.
* Make stderr and stdout unbuffered for crash testing.
* Only turn disable buffering in crash test.
* Disable crash test on CI.
* Fix crash on clang/linux.
* Enable crash test.
Remove _appendBuffer as can use StreamUtil functionality.
* Added inital processing for http headers.
* Small improvements to HttpHeader.
* First pass HTTPPacketConnection working on windows.
* Enable other Process communication tests.
* Update comments.
* WIP JSON RPC.
* Add terminate to Process.
Made JSONRPC a Util.
* Small tidy up around HTTPPacketConnection.
* Improve process termination options.
* WIP for test-server.
* Add diagnostics error handling to test-server.
* Improved JSON support.
Parsing/creating JSON-RPC messages.
* WIP JSONRPC parsing.
* First pass RttiInfo support.
* WIP converting between JSON/native types.
* Project files.
* Split out RttiUtil.
Made RttiInfo constuction thread safe.
* WIP RTTI<->JSON.
* Add diagnostics to JSON<->native conversions.
* Make RttiInfo for structs globals. Avoids problem around derived types (like pointers), being able to cause an abort.
* Add pointer support to RTTI.
Fixed some compilation issues on linux.
* Add fixed array support.
* Added Rtti unit test.
* Add rtti unit test.
* Split out quoted/unquoted key handling.
Fix bugs in JSON value/container.
Added JSON native test.
* Make default array allocator use malloc/free.
Remove the new[] handler (doesn't work on visuals studio).
* Fix for linux warning.
* Remove some test code.
* Fix issues on x86 win.
* Fix warning on aarch64.
Diffstat (limited to 'tools/slang-unit-test/unit-test-json-native.cpp')
| -rw-r--r-- | tools/slang-unit-test/unit-test-json-native.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/tools/slang-unit-test/unit-test-json-native.cpp b/tools/slang-unit-test/unit-test-json-native.cpp new file mode 100644 index 000000000..1d2085751 --- /dev/null +++ b/tools/slang-unit-test/unit-test-json-native.cpp @@ -0,0 +1,117 @@ +// unit-test-json-native.cpp + +#include "../../source/core/slang-rtti-info.h" + +#include "../../source/compiler-core/slang-json-native.h" +#include "../../source/compiler-core/slang-json-parser.h" + +#include "tools/unit-test/slang-unit-test.h" + +using namespace Slang; + +namespace { // anonymous + +struct SomeStruct +{ + typedef SomeStruct ThisType; + + bool operator==(const ThisType& rhs) const + { + return a == rhs.a && b == rhs.b && s == rhs.s && list == rhs.list; + } + bool operator!=(const ThisType& rhs) const { return !(*this == rhs); } + + int a = 0; + float b = 2.0f; + String s; + List<String> list; + + static const StructRttiInfo g_rttiInfo; +}; + +} // anonymous + +static const StructRttiInfo _makeSomeStructRtti() +{ + SomeStruct obj; + StructRttiBuilder builder(&obj, "SomeStruct", nullptr); + + builder.addField("a", &obj.a); + builder.addField("b", &obj.b); + builder.addField("s", &obj.s); + builder.addField("list", &obj.list); + + return builder.make(); +} +/* static */const StructRttiInfo SomeStruct::g_rttiInfo = _makeSomeStructRtti(); + +static SlangResult _check() +{ + // Convert into a JSON string + + SomeStruct s; + s.list.add("Hello!"); + s.s = "There"; + + // Try serializing it out + + SourceManager sourceManager; + sourceManager.initialize(nullptr, nullptr); + + DiagnosticSink sink(&sourceManager, &JSONLexer::calcLexemeLocation); + + RefPtr<JSONContainer> container(new JSONContainer(&sourceManager)); + + String json; + { + NativeToJSONConverter converter(container, &sink); + + JSONValue value; + SLANG_RETURN_ON_FAIL(converter.convert(GetRttiInfo<SomeStruct>::get(), &s, value)); + + // Convert into a string + JSONWriter writer(JSONWriter::IndentationStyle::Allman); + container->traverseRecursively(value, &writer); + + json = writer.getBuilder(); + } + + 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 back to native + { + JSONToNativeConverter converter(container, &sink); + + { + SomeStruct readS; + SLANG_RETURN_ON_FAIL(converter.convert(readValue, GetRttiInfo<SomeStruct>::get(), &readS)); + + // Should be equal + SLANG_CHECK(readS == s); + } + } + + return SLANG_OK; +} + +SLANG_UNIT_TEST(JSONNative) +{ + SLANG_CHECK(SLANG_SUCCEEDED(_check())); + +} |
