From 85398a9410a3ab36786c1e436986309ee6163f2f Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 11 Jun 2023 20:20:52 +0200 Subject: c++, codec-json: Added struct to json encoding and fixed a buffer bug on the fly --- tests/codec-json.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 8 deletions(-) (limited to 'tests/codec-json.cpp') 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 namespace { +namespace schema { +using namespace saw::schema; + +using TestTuple = Tuple< + String, + Int32 +>; + +using TestArray = Array< + String +>; + +using TestStruct = Struct< + Member, + Member +>; +} SAW_TEST("Int32 write"){ using namespace saw; data 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 native{3}; + data json; + + native.at(0).set("foo"); + native.at(1).set("bar"); + native.at(2).set("baz"); + + codec json_codec; + + error_or 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 native; + data json; + + native.get<"foo">().set(5); + native.get<"bar">().set("baz"); + + codec json_codec; + + error_or 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}); +} } -- cgit v1.2.3