diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/codec-json.cpp | 63 |
1 files changed, 59 insertions, 4 deletions
diff --git a/tests/codec-json.cpp b/tests/codec-json.cpp index d82db14..b5caaa9 100644 --- a/tests/codec-json.cpp +++ b/tests/codec-json.cpp @@ -1,6 +1,8 @@ #include <forstio/test/suite.h> #include <forstio/codec/json/json.h> +#include <iostream> + namespace { SAW_TEST("Int32 write"){ using namespace saw; @@ -14,9 +16,62 @@ SAW_TEST("Int32 write"){ error_or<void> eov = json_codec.encode(native_int, json_int); SAW_EXPECT(eov.is_value(), "Encoding error"); - std::string_view str_view = "44123"; - for(std::size_t i = 0; i < str_view.size(); ++i){ - SAW_EXPECT( json_int.at(i) == str_view[i], "Value is not being encoded correctly" ); - } + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_v = "44123"; + std::string enc_val = convert_to_string(json_int.get_buffer()); + SAW_EXPECT( enc_val == str_v, std::string{"Value is not being encoded correctly. Encoded: "} + enc_val ); +} + +SAW_TEST("String write"){ + using namespace saw; + data<schema::String, encode::Native> nat_str; + data<schema::String, encode::Json> json_str; + + nat_str.set("foo"); + + codec<schema::String, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(nat_str, json_str); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + /** + * Currently doing this manually. Technically I should convert to std::string for tests + */ + std::string_view str_view = "\"foo\""; + std::string encoded_value = convert_to_string(json_str.get_buffer()); + + 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; + data<schema::TestTuple, encode::Native> native_tup; + data<schema::TestTuple, encode::Json> json_tup; + + auto& nat_zero = native_tup.template get<0>(); + auto& nat_one = native_tup.template get<1>(); + + nat_zero.set("bar"); + nat_one.set(34); + + codec<schema::TestTuple, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_tup, json_tup); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_v = "[\"bar\",34]"; + std::string enc_val = convert_to_string(json_tup.get_buffer()); + + SAW_EXPECT(enc_val == str_v, std::string{"Tuple not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v}); } } |