Mercurial > hg > orthanc-stone
changeset 690:f185cfcb72a0 am-dev
CodeGen: tests improvements
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 16 May 2019 19:10:38 +0200 |
parents | 8c0b073efda8 |
children | 8190213e2279 |
files | Resources/CodeGeneration/template.in.h.j2 Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js Resources/CodeGeneration/testWasmIntegrated/build-web.sh Resources/CodeGeneration/testWasmIntegrated/main.cpp Resources/CodeGeneration/testWasmIntegrated/styles.css Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.ts Resources/CodeGeneration/test_data/testTestStoneCodeGen.yaml |
diffstat | 9 files changed, 136 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/Resources/CodeGeneration/template.in.h.j2 Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/template.in.h.j2 Thu May 16 19:10:38 2019 +0200 @@ -182,6 +182,17 @@ return out; } + inline std::string ToString(const std::string& str) + { + return str; + } + + inline void FromString(std::string& value, std::string strValue) + { + value = strValue; + } + + /** Throws in case of problem */ template<typename TK, typename TV> void _StoneDeserializeValue( @@ -195,8 +206,10 @@ itr != jsonValue.end(); itr++) { + std::string strKey; + _StoneDeserializeValue(strKey, itr.key()); TK key; - _StoneDeserializeValue(key, itr.key()); + FromString(key, strKey); // if you have a compile error here, it means that your type is not suitable to be the key of a map (or you should overwrite the FromString/ToString in template.in.h.j2) TV innerDestValue; _StoneDeserializeValue(innerDestValue, *itr); @@ -205,6 +218,7 @@ } } } + template<typename TK, typename TV> Json::Value _StoneSerializeValue(const std::map<TK, TV>& value) { @@ -214,7 +228,7 @@ it != value.cend(); ++it) { // it->first it->second - result[it->first] = _StoneSerializeValue(it->second); + result[ToString(it->first)] = _StoneSerializeValue(it->second); } return result; } @@ -239,7 +253,7 @@ void _StoneDeserializeValue( std::vector<T>& destValue, const Json::Value& jsonValue) { - if (!jsonValue.isNull()) + if (!jsonValue.isNull() && jsonValue.isArray()) { destValue.clear(); destValue.reserve(jsonValue.size()); @@ -281,7 +295,7 @@ void _StoneDeserializeValue( std::set<T>& destValue, const Json::Value& jsonValue) { - if (!jsonValue.isNull()) + if (!jsonValue.isNull() && jsonValue.isArray()) { destValue.clear(); for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++)
--- a/Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt Thu May 16 19:10:38 2019 +0200 @@ -21,14 +21,14 @@ set(jsoncppRootDir ${CMAKE_CURRENT_LIST_DIR}/jsoncpp-1.8.4) add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.hpp ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.ts + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.hpp ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.ts COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/../stonegentool.py -o ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/../test_data/testTestStoneCodeGen.yaml DEPENDS ${testCppHandler_Codegen_Deps} ${CMAKE_CURRENT_LIST_DIR}/../test_data/testTestStoneCodeGen.yaml ) add_executable(testWasmIntegratedCpp main.cpp - ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.hpp + ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.hpp ${jsoncppRootDir}/jsoncpp.cpp ${testCppHandler_Codegen_Deps})
--- a/Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js Thu May 16 19:10:38 2019 +0200 @@ -9,7 +9,7 @@ }, SendMessageFromCppJS: function(statusUpdateMessage) { var statusUpdateMessage_ = UTF8ToString(statusUpdateMessage); - SendMessageFromCpp(statusUpdateMessage_); + window.SendMessageFromCpp(statusUpdateMessage_); } });
--- a/Resources/CodeGeneration/testWasmIntegrated/build-web.sh Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/build-web.sh Thu May 16 19:10:38 2019 +0200 @@ -4,7 +4,7 @@ mkdir -p build-final # compile TS to JS -tsc --module commonjs --sourceMap -t ES2015 --outDir "build-tsc/" build-wasm/testWasmIntegratedCpp_generated.ts testWasmIntegrated.ts +tsc --module commonjs --sourceMap -t ES2015 --outDir "build-tsc/" build-wasm/TestStoneCodeGen_generated.ts testWasmIntegrated.ts # bundle JS files to final build dir browserify "build-tsc/build-wasm/testWasmIntegratedCpp_generated.js" "build-tsc/testWasmIntegrated.js" -o "build-final/testWasmIntegratedApp.js"
--- a/Resources/CodeGeneration/testWasmIntegrated/main.cpp Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/main.cpp Thu May 16 19:10:38 2019 +0200 @@ -59,6 +59,15 @@ SendFreeTextFromCppJS(ss.str().c_str()); \ return true; +#define ECHO_MESSAGE(Type,value) \ + stringstream ss; \ + ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ + TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ + SendFreeTextFromCppJS(ss.str().c_str()); \ + std::string serializedInCpp = StoneSerialize(value); \ + SendMessageFromCppJS(serializedInCpp.c_str()); \ + return true; + class MyHandler : public TestStoneCodeGen::IHandler { public: @@ -87,6 +96,34 @@ } }; +class MyEchoHandler : public TestStoneCodeGen::IHandler +{ + public: + virtual bool Handle(const TestStoneCodeGen::A& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::A,value) + } + virtual bool Handle(const TestStoneCodeGen::B& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::B,value) + } + + virtual bool Handle(const TestStoneCodeGen::Message1& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::Message1,value) + } + + virtual bool Handle(const TestStoneCodeGen::Message2& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::Message2,value) + } + + virtual bool Handle(const TestStoneCodeGen::C& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::C,value) + } +}; + extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message) { MyHandler handler; @@ -106,6 +143,25 @@ } } +extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCppForEcho(const char* message) +{ + MyEchoHandler echoHandler; + try + { + bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&echoHandler); + if(!handled) + { + SendFreeTextFromCppJS("This message is valid JSON, but was not handled by the echo handler!"); + } + } + catch(std::exception& e) + { + stringstream ss; + ss << "Error while parsing message: " << e.what() << "\n"; + SendFreeTextFromCppJS(ss.str().c_str()); + } +} + void EMSCRIPTEN_KEEPALIVE StartWasmApplication(const char* baseUri) { printf("Hello! (this is sent from C++)\n");
--- a/Resources/CodeGeneration/testWasmIntegrated/styles.css Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/styles.css Thu May 16 19:10:38 2019 +0200 @@ -9,7 +9,7 @@ "Test1 Test2 Test3 Test4 . . ." ". . . . . . ." "Test5 Test6 Test7 Test8 . . ." - ". . . . . . ." + "TestTsCppTs . . . . . ." ". . . . . . ." ; height: 480px; @@ -59,6 +59,8 @@ .TestWasm-Test8 { grid-area: Test8; } +.TestWasm-ts-cpp-ts { grid-area: TestTsCppTs; } + .TestWasm-button { width:80px; }
--- a/Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html Thu May 16 19:10:38 2019 +0200 @@ -54,6 +54,9 @@ <div class="TestWasm-Test8"> <button class="TestWasm-button" tool-selector="Test 8">Test 8</button> </div> + <div class="TestWasm-ts-cpp-ts"> + <button class="TestWasm-button" tool-selector="Test-ts-cpp-ts">Test ts-cpp-ts</button> + </div> <!-- <button class="TestWasm-button" class="TestWasm-Test1" tool-selector="Test 1">Test 1</button> <button class="TestWasm-button" class="TestWasm-Test2" tool-selector="Test 2">Test 2</button>
--- a/Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.ts Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.ts Thu May 16 19:10:38 2019 +0200 @@ -1,6 +1,8 @@ var SendMessageToCpp: Function = null; export var TestWasmIntegratedModule : any; +import * as TestStoneCodeGen from './build-wasm/TestStoneCodeGen_generated' + /* +--------------------------------------------------+ | install emscripten handlers | @@ -107,11 +109,60 @@ } (<any> window).SendFreeTextFromCpp = SendFreeTextFromCpp; +var referenceMessages = Array<any>(); + +function testTsCppTs() { + var r = new TestStoneCodeGen.Message2(); + r.memberEnumMovieType = TestStoneCodeGen.MovieType.RomCom; + r.memberStringWithDefault = "overriden"; + r.memberMapEnumFloat[TestStoneCodeGen.CrispType.CreamAndChives] = 0.5; + r.memberString = "reference-messsage2-test1"; + + referenceMessages[r.memberString] = r; + var strMsg2 = r.StoneSerialize(); + let SendMessageToCppForEchoLocal = (<any> window).Module.cwrap('SendMessageToCppForEcho', 'string', ['string']); + SendMessageToCppForEchoLocal(strMsg2); +} + +class MyEchoHandler implements TestStoneCodeGen.IHandler +{ + public HandleMessage2(value: TestStoneCodeGen.Message2): boolean + { + if (value.memberString in referenceMessages) { + let r = referenceMessages[value.memberString]; + let equals = (value.memberStringWithDefault == r.memberStringWithDefault); + if (TestStoneCodeGen.CrispType.CreamAndChives in r.memberMapEnumFloat) { + equals == equals && r.memberMapEnumFloat[TestStoneCodeGen.CrispType.CreamAndChives] == value.memberMapEnumFloat[TestStoneCodeGen.CrispType.CreamAndChives]; + } + // TODO continue comparison + + if (equals) { + console.log("objects are equals after round trip"); + return true; + } + } + console.log("problem after round trip"); + return true; + } +} + +function SendMessageFromCpp(txt: string):string +{ + setCppOutputValue(getCppOutputValue() + "\n" + txt); + TestStoneCodeGen.StoneDispatchToHandler(txt, new MyEchoHandler()); + return ""; +} +(<any> window).SendMessageFromCpp = SendMessageFromCpp; + + function ButtonClick(buttonName: string) { if (buttonName.startsWith('Test ')) { setSerializedInputValue(stockSerializedMessages[buttonName]); } + else if (buttonName == "Test-ts-cpp-ts") { + testTsCppTs(); + } else if(buttonName == 'Trigger') { let serializedInputValue:string = getSerializedInputValue();
--- a/Resources/CodeGeneration/test_data/testTestStoneCodeGen.yaml Thu May 16 17:54:22 2019 +0200 +++ b/Resources/CodeGeneration/test_data/testTestStoneCodeGen.yaml Thu May 16 19:10:38 2019 +0200 @@ -34,7 +34,7 @@ memberFloat64: float64 struct Message2: - __handler: cpp + __handler: [cpp, ts] memberString: string memberStringWithDefault: string = "my-default-value"