From c94420cdfac3cf5f4631a5eb63ca33360b719db4 Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Fri, 16 Aug 2024 14:36:48 +0200 Subject: Didn't consider the empty array case in decoding --- modules/codec-json/tests/codec-json.cpp | 85 +++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'modules/codec-json/tests') diff --git a/modules/codec-json/tests/codec-json.cpp b/modules/codec-json/tests/codec-json.cpp index dd8df8a..3c97935 100644 --- a/modules/codec-json/tests/codec-json.cpp +++ b/modules/codec-json/tests/codec-json.cpp @@ -24,6 +24,12 @@ using TestStruct = Struct< Member, Member >; + +using TestArrayStruct = Array; + +using TestStructArrayStruct = Struct< + Member +>; } SAW_TEST("UInt8 write"){ @@ -317,6 +323,85 @@ SAW_TEST("Struct read and write"){ SAW_EXPECT(native.get<"bar">() == "baz", "Invalid value for bar"); } +SAW_TEST("Array Struct Empty read and write"){ + using namespace saw; + data native{0u}; + data json; + + codec json_codec; + + error_or eov = json_codec.encode(native, json); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "[]"; + 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}); + + native = {}; + eov = json_codec.decode(json, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); +} + +SAW_TEST("Array Struct read and write"){ + using namespace saw; + data native{4u}; + data json; + + native.at(0).get<"foo">().set(5); + native.at(0).get<"bar">().set("baz"); + + native.at(1).get<"foo">().set(6); + native.at(1).get<"bar">().set("baz1"); + + native.at(2).get<"foo">().set(326); + native.at(2).get<"bar">().set("baz12"); + + 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\"},{\"foo\":6,\"bar\":\"baz1\"},{\"foo\":326,\"bar\":\"baz12\"},{\"foo\":0,\"bar\":\"\"}]"; + 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}); + + native = {}; + eov = json_codec.decode(json, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); +} + +SAW_TEST("Struct Array Struct read and write"){ + using namespace saw; + data native; + native.template get<"banana">() = {4u}; + data json; + + native.template get<"banana">().at(0).get<"foo">().set(5); + native.template get<"banana">().at(0).get<"bar">().set("baz"); + + native.template get<"banana">().at(1).get<"foo">().set(6); + native.template get<"banana">().at(1).get<"bar">().set("baz1"); + + native.template get<"banana">().at(2).get<"foo">().set(326); + native.template get<"banana">().at(2).get<"bar">().set("baz12"); + + codec json_codec; + + error_or eov = json_codec.encode(native, json); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "{\"banana\":[{\"foo\":5,\"bar\":\"baz\"},{\"foo\":6,\"bar\":\"baz1\"},{\"foo\":326,\"bar\":\"baz12\"},{\"foo\":0,\"bar\":\"\"}]}"; + 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}); + + native = {}; + eov = json_codec.decode(json, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); +} + SAW_TEST("Int8 read"){ using namespace saw; data native_int; -- cgit v1.2.3