summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/codec-json.cpp64
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});
+}
}