yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7b570feed
master
1// unit-test-json-native.cpp 2 3#include "../../source/compiler-core/slang-json-native.h" 4#include "../../source/compiler-core/slang-json-parser.h" 5#include "../../source/core/slang-rtti-info.h" 6#include "unit-test/slang-unit-test.h" 7 8using namespace Slang ; 9 10namespace 11{// anonymous 12 13struct OtherStruct 14{ 15typedef OtherStruct ThisType ; 16 17bool operator== (const ThisType & rhs )const {return f == rhs .f && value == rhs .value ; } 18bool operator!= (const ThisType & rhs )const {return !(* this == rhs ); } 19 20float f = 1.0f ; 21String value ; 22 23static const StructRttiInfo g_rttiInfo ; 24}; 25 26static const StructRttiInfo _makeOtherStructRtti () 27{ 28OtherStruct obj ; 29StructRttiBuilder builder (& obj ,"OtherStruct" ,nullptr ); 30builder .addField ("f" ,& obj .f ); 31builder .addField ("value" ,& obj .value ); 32return builder .make (); 33} 34/* static */ const StructRttiInfo OtherStruct ::g_rttiInfo = _makeOtherStructRtti (); 35 36struct SomeStruct 37{ 38typedef SomeStruct ThisType ; 39 40bool operator== (const ThisType & rhs )const 41 { 42return a == rhs .a && b == rhs .b && s == rhs .s && list == rhs .list && 43boolValue == rhs .boolValue && structList == rhs .structList && 44makeConstArrayView (fixedArray )== makeConstArrayView (rhs .fixedArray ); 45 } 46bool operator!= (const ThisType & rhs )const {return !(* this == rhs ); } 47 48int a = 0 ; 49float b = 2.0f ; 50String s ; 51List < String > list ; 52bool boolValue = false; 53 54List < OtherStruct > structList ; 55 56int fixedArray [2 ]= {0 ,0 }; 57 58static const StructRttiInfo g_rttiInfo ; 59}; 60 61static const StructRttiInfo _makeSomeStructRtti () 62{ 63SomeStruct obj ; 64StructRttiBuilder builder (& obj ,"SomeStruct" ,nullptr ); 65 66builder .addField ("a" ,& obj .a ); 67builder .addField ("b" ,& obj .b ); 68builder .addField ("s" ,& obj .s ); 69builder .addField ("list" ,& obj .list ); 70builder .addField ("boolValue" ,& obj .boolValue ); 71builder .addField ("structList" ,& obj .structList ); 72builder .addField ("fixedArray" ,& obj .fixedArray ); 73 74return builder .make (); 75} 76/* static */ const StructRttiInfo SomeStruct ::g_rttiInfo = _makeSomeStructRtti (); 77 78}// namespace 79 80 81static SlangResult _check () 82{ 83// Convert into a JSON string 84 85SomeStruct s ; 86s .list .add ("Hello!" ); 87s .s = "There" ; 88s .boolValue = true; 89 90OtherStruct o ; 91o .f = 27.5f ; 92o .value = "This works!" ; 93 94s .structList .add (o ); 95s .fixedArray [1 ]= 8 ; 96s .fixedArray [0 ]= -1 ; 97 98// Try serializing it out 99 100SourceManager sourceManager ; 101sourceManager .initialize (nullptr ,nullptr ); 102 103DiagnosticSink sink (& sourceManager ,& JSONLexer ::calcLexemeLocation ); 104 105auto typeMap = JSONNativeUtil ::getTypeFuncsMap (); 106 107RefPtr < JSONContainer > container (new JSONContainer (& sourceManager )); 108 109String json ; 110 { 111NativeToJSONConverter converter (container ,& typeMap ,& sink ); 112 113JSONValue value ; 114SLANG_RETURN_ON_FAIL (converter .convert (GetRttiInfo < SomeStruct > ::get (),& s ,value )); 115 116// Convert into a string 117JSONWriter writer (JSONWriter ::IndentationStyle ::Allman ); 118container -> traverseRecursively (value ,& writer ); 119 120json = writer .getBuilder (); 121 } 122 123JSONValue readValue ; 124 { 125// Now need to parse as JSON 126String contents (json ); 127SourceFile * sourceFile = 128sourceManager .createSourceFileWithString (PathInfo ::makeUnknown (),contents ); 129SourceView * sourceView = sourceManager .createSourceView (sourceFile ,nullptr ,SourceLoc ()); 130 131JSONLexer lexer ; 132lexer .init (sourceView ,& sink ); 133 134JSONBuilder builder (container ); 135 136JSONParser parser ; 137SLANG_RETURN_ON_FAIL (parser .parse (& lexer ,sourceView ,& builder ,& sink )); 138 139readValue = builder .getRootValue (); 140 } 141 142// Convert back to native 143 { 144JSONToNativeConverter converter (container ,& typeMap ,& sink ); 145 146 { 147SomeStruct readS ; 148SLANG_RETURN_ON_FAIL ( 149converter .convert (readValue ,GetRttiInfo < SomeStruct > ::get (),& readS )); 150 151// Should be equal 152SLANG_CHECK (readS == s ); 153 } 154 } 155 156return SLANG_OK ; 157} 158 159SLANG_UNIT_TEST (JSONNative ) 160{ 161SLANG_CHECK (SLANG_SUCCEEDED (_check ())); 162}