From 0abbc0fbdf04ba1c6771a1219b9b590b4d596bb9 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Mon, 2 Oct 2023 21:53:28 +0200 Subject: [PATCH] Added tests --- tests/test_default_values.hpp | 2 +- tests/test_literal.hpp | 29 +++++++++++++++++++++ tests/test_readme_example.hpp | 27 ++++++++------------ tests/test_ref.hpp | 47 +++++++++++++++++++++++++++++++++++ tests/test_tagged_union.hpp | 37 +++++++++++++++++++++++++++ tests/test_variant.hpp | 32 ++++++++++++++++++++++++ tests/tests.cpp | 8 ++++++ tests/write_and_read.hpp | 6 +++-- 8 files changed, 168 insertions(+), 20 deletions(-) create mode 100644 tests/test_literal.hpp create mode 100644 tests/test_ref.hpp create mode 100644 tests/test_tagged_union.hpp create mode 100644 tests/test_variant.hpp diff --git a/tests/test_default_values.hpp b/tests/test_default_values.hpp index 25c1384f..8125aa13 100644 --- a/tests/test_default_values.hpp +++ b/tests/test_default_values.hpp @@ -9,7 +9,7 @@ // Makes sure that we can set default values. void test_default_values() { - std::cout << "Testing default values..." << std::endl; + std::cout << "test_default_values" << std::endl; struct Person { rfl::Field<"firstName", std::string> first_name; diff --git a/tests/test_literal.hpp b/tests/test_literal.hpp new file mode 100644 index 00000000..425af72d --- /dev/null +++ b/tests/test_literal.hpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that we can set default values. +void test_literal() { + std::cout << "test_literal" << std::endl; + + using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; + using LastName = rfl::Literal<"Simpson">; + + struct Person { + rfl::Field<"firstName", FirstName> first_name; + rfl::Field<"lastName", LastName> last_name = rfl::default_value; + rfl::Field<"children", std::vector> children = + rfl::default_value; + }; + + const auto bart = Person{.first_name = FirstName::make<"Bart">()}; + + write_and_read( + bart, + "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\",\"children\":[]}"); +} diff --git a/tests/test_readme_example.hpp b/tests/test_readme_example.hpp index edbef115..02f9db7b 100644 --- a/tests/test_readme_example.hpp +++ b/tests/test_readme_example.hpp @@ -4,9 +4,13 @@ #include #include +#include "write_and_read.hpp" + // Makes sure that the example in the README works as expected. // It would be embarrassing if it didn't... void test_readme_example() { + std::cout << "test_readme_example" << std::endl; + // "firstName", "lastName" and "children" are the field names // as they will appear in the JSON. The C++ standard is // snake case, the JSON standard is camel case, so the names @@ -37,21 +41,10 @@ void test_readme_example() { .last_name = "Simpson", .children = std::vector({bart, lisa, maggie})}; - // We can now transform this into a JSON string. - const std::string json_string = rfl::json::write(homer); - - std::cout << json_string << std::endl; - - // And we can parse the string to recreate the struct. - auto homer2 = rfl::json::read(json_string).value(); - - // Fields can be accessed like this: - std::cout << "Hello, my name is " << homer.first_name() << " " - << homer.last_name() << "." << std::endl; - - // Since homer2 is mutable, we can also change the values like this: - homer2.first_name = "Marge"; - - std::cout << "Hello, my name is " << homer2.first_name() << " " - << homer2.last_name() << "." << std::endl; + write_and_read( + homer, + "{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{" + "\"firstName\":\"Bart\",\"lastName\":\"Simpson\",\"children\":[]},{" + "\"firstName\":\"Lisa\",\"lastName\":\"Simpson\",\"children\":[]},{" + "\"firstName\":\"Maggie\",\"lastName\":\"Simpson\",\"children\":[]}]}"); } diff --git a/tests/test_ref.hpp b/tests/test_ref.hpp new file mode 100644 index 00000000..8f1fb894 --- /dev/null +++ b/tests/test_ref.hpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_ref() { + std::cout << "test_ref" << std::endl; + + struct DecisionTree { + struct Leaf { + rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value; + rfl::Field<"value", double> value; + }; + + struct Node { + rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value; + rfl::Field<"criticalValue", double> critical_value; + rfl::Field<"left", rfl::Ref> lesser; + rfl::Field<"right", rfl::Ref> greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; + }; + + const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = + DecisionTree::Node{.critical_value = 10.0, + .lesser = rfl::make_ref(leaf1), + .greater = rfl::make_ref(leaf2)}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read( + tree, + "{\"leafOrNode\":{\"type\":\"Node\",\"criticalValue\":10.0,\"left\":{" + "\"leafOrNode\":{\"type\":\"Leaf\",\"value\":3.0}},\"right\":{" + "\"leafOrNode\":{\"type\":\"Leaf\",\"value\":5.0}}}}"); +} diff --git a/tests/test_tagged_union.hpp b/tests/test_tagged_union.hpp new file mode 100644 index 00000000..fb2aaac9 --- /dev/null +++ b/tests/test_tagged_union.hpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that we can set default values. +void test_tagged_union() { + std::cout << "test_tagged_union" << std::endl; + + struct Circle { + rfl::Field<"shape", rfl::Literal<"Circle">> shape = rfl::default_value; + rfl::Field<"radius", double> radius; + }; + + struct Rectangle { + rfl::Field<"shape", rfl::Literal<"Rectangle">> shape = + rfl::default_value; + rfl::Field<"height", double> height; + rfl::Field<"width", double> width; + }; + + struct Square { + rfl::Field<"shape", rfl::Literal<"Square">> shape = rfl::default_value; + rfl::Field<"width", double> width; + }; + + using Shapes = std::variant; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r, + "{\"shape\":\"Rectangle\",\"height\":10.0,\"width\":5.0}"); +} diff --git a/tests/test_variant.hpp b/tests/test_variant.hpp new file mode 100644 index 00000000..a71aaf0c --- /dev/null +++ b/tests/test_variant.hpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that we can set default values. +void test_variant() { + std::cout << "test_variant" << std::endl; + + struct Circle { + rfl::Field<"radius", double> radius; + }; + + struct Rectangle { + rfl::Field<"height", double> height; + rfl::Field<"width", double> width; + }; + + struct Square { + rfl::Field<"width", double> width; + }; + + using Shapes = std::variant; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r, "{\"height\":10.0,\"width\":5.0}"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 8153d4e0..f8c38d28 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,9 +1,17 @@ #include "test_default_values.hpp" +#include "test_literal.hpp" #include "test_readme_example.hpp" +#include "test_ref.hpp" +#include "test_tagged_union.hpp" +#include "test_variant.hpp" int main() { test_readme_example(); test_default_values(); + test_literal(); + test_variant(); + test_tagged_union(); + test_ref(); return 0; } diff --git a/tests/write_and_read.hpp b/tests/write_and_read.hpp index 29d88c78..ece468c5 100644 --- a/tests/write_and_read.hpp +++ b/tests/write_and_read.hpp @@ -12,7 +12,8 @@ void write_and_read(const T& _struct, const std::string& _expected) { std::cout << "Test failed on write. Expected:" << std::endl << _expected << std::endl << "Got: " << std::endl - << json_string1 << std::endl; + << json_string1 << std::endl + << std::endl; return; } const auto struct2 = rfl::json::read(json_string1).value(); @@ -21,7 +22,8 @@ void write_and_read(const T& _struct, const std::string& _expected) { std::cout << "Test failed on read. Expected:" << std::endl << _expected << std::endl << "Got: " << std::endl - << json_string2 << std::endl; + << json_string2 << std::endl + << std::endl; return; } std::cout << "OK" << std::endl << std::endl;