diff options
Diffstat (limited to 'modules/codec-json/tests/codec-json.cpp')
| -rw-r--r-- | modules/codec-json/tests/codec-json.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/modules/codec-json/tests/codec-json.cpp b/modules/codec-json/tests/codec-json.cpp index ab33f5a..a87b237 100644 --- a/modules/codec-json/tests/codec-json.cpp +++ b/modules/codec-json/tests/codec-json.cpp @@ -323,6 +323,56 @@ SAW_TEST("Struct read and write"){ SAW_EXPECT(native.get<"bar">() == "baz", "Invalid value for bar"); } +SAW_TEST("Struct read with missing member"){ + using namespace saw; + data<schema::TestStruct, encode::Native> native; + data<schema::TestStruct, encode::Json> json{"{\"bar\":\"baz\"}"}; + + codec<schema::TestStruct, encode::Json> json_codec; + + error_or<void> eov = json_codec.decode(json, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); + SAW_EXPECT(native.get<"foo">().get() == 0, "Missing member foo should keep its default value"); + SAW_EXPECT(native.get<"bar">() == "baz", "Invalid value for bar"); +} + +SAW_TEST("Struct read with missing member in strict mode"){ + using namespace saw; + data<schema::TestStruct, encode::Native> native; + data<schema::TestStruct, encode::Json> json{"{\"bar\":\"baz\"}"}; + + codec_config<encode::Json> json_config; + json_config.strict = true; + codec<schema::TestStruct, encode::Json> json_codec{json_config}; + + error_or<void> eov = json_codec.decode(json, native); + SAW_EXPECT(eov.is_error(), "Strict decoding should reject incomplete structs"); +} + +SAW_TEST("Empty Struct read"){ + using namespace saw; + data<schema::TestStruct, encode::Native> native; + data<schema::TestStruct, encode::Json> json{"{}"}; + + codec<schema::TestStruct, encode::Json> json_codec; + + error_or<void> eov = json_codec.decode(json, native); + SAW_EXPECT(eov.is_value(), "Decoding error"); + SAW_EXPECT(native.get<"foo">().get() == 0, "Missing member foo should keep its default value"); + SAW_EXPECT(native.get<"bar">() == "", "Missing member bar should keep its default value"); +} + +SAW_TEST("Struct read with unknown member"){ + using namespace saw; + data<schema::TestStruct, encode::Native> native; + data<schema::TestStruct, encode::Json> json{"{\"foo\":5,\"baz\":3,\"bar\":\"baz\"}"}; + + codec<schema::TestStruct, encode::Json> json_codec; + + error_or<void> eov = json_codec.decode(json, native); + SAW_EXPECT(eov.is_error(), "Unknown members are not supported"); +} + SAW_TEST("Array Struct Empty read and write"){ using namespace saw; data<schema::TestArrayStruct, encode::Native> native{0u}; |
