diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-06-11 20:20:52 +0200 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-06-11 20:20:52 +0200 |
commit | 85398a9410a3ab36786c1e436986309ee6163f2f (patch) | |
tree | 7eb07905cdf63c8c1080219d0537baf1a75e5a71 /tests | |
parent | b84b576a221684a39ef12891bafd233ba6289b09 (diff) |
c++, codec-json: Added struct to json encoding and fixed a buffer bug on
the fly
Diffstat (limited to 'tests')
-rw-r--r-- | tests/codec-json.cpp | 64 |
1 files changed, 56 insertions, 8 deletions
diff --git a/tests/codec-json.cpp b/tests/codec-json.cpp index b5caaa9..67253d7 100644 --- a/tests/codec-json.cpp +++ b/tests/codec-json.cpp @@ -4,6 +4,23 @@ #include <iostream> namespace { +namespace schema { +using namespace saw::schema; + +using TestTuple = Tuple< + String, + Int32 +>; + +using TestArray = Array< + String +>; + +using TestStruct = Struct< + Member<Int32, "foo">, + Member<String, "bar"> +>; +} SAW_TEST("Int32 write"){ using namespace saw; data<schema::Int32, encode::Native> native_int; @@ -44,14 +61,6 @@ SAW_TEST("String write"){ SAW_EXPECT(encoded_value == str_view, "String not encoded correctly"); } -namespace schema { -using namespace saw::schema; - -using TestTuple = Tuple< - String, - Int32 ->; -} SAW_TEST("Tuple write"){ using namespace saw; @@ -74,4 +83,43 @@ SAW_TEST("Tuple write"){ SAW_EXPECT(enc_val == str_v, std::string{"Tuple not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); } + +SAW_TEST("Array write"){ + using namespace saw; + data<schema::TestArray, encode::Native> native{3}; + data<schema::TestArray, encode::Json> json; + + native.at(0).set("foo"); + native.at(1).set("bar"); + native.at(2).set("baz"); + + codec<schema::TestArray, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native, json); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "[\"foo\",\"bar\",\"baz\"]"; + std::string enc_val = convert_to_string(json.get_buffer()); + + SAW_EXPECT(enc_val == str_v, std::string{"Array not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); +} + +SAW_TEST("Struct write"){ + using namespace saw; + data<schema::TestStruct, encode::Native> native; + data<schema::TestStruct, encode::Json> json; + + native.get<"foo">().set(5); + native.get<"bar">().set("baz"); + + codec<schema::TestStruct, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native, json); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "{\"foo\":5,\"bar\":\"baz\"}"; + std::string enc_val = convert_to_string(json.get_buffer()); + + SAW_EXPECT(enc_val == str_v, std::string{"Struct not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); +} } |