From 1382a2a505edfd6529b1d9e36dde2513ff3289a7 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 3 Dec 2023 16:52:24 +0100 Subject: [PATCH] Compile the JSON tests separately --- include/rfl/internal/get_struct_name.hpp | 2 +- include/rfl/io/load_bytes.hpp | 2 +- include/rfl/io/load_string.hpp | 2 +- tests/json/CMakeLists.txt | 55 +++++++++++++++ tests/json/test_all_of.cpp | 31 ++++++++ tests/json/test_all_of.hpp | 31 +------- tests/json/test_alphanumeric_map.cpp | 27 +++++++ tests/json/test_alphanumeric_map.hpp | 27 +------ .../json/test_alphanumeric_unordered_map.cpp | 31 ++++++++ .../json/test_alphanumeric_unordered_map.hpp | 31 +------- tests/json/test_anonymous_fields.cpp | 59 ++++++++++++++++ tests/json/test_anonymous_fields.hpp | 59 +--------------- tests/json/test_any_of.cpp | 35 ++++++++++ tests/json/test_any_of.hpp | 35 +--------- tests/json/test_array.cpp | 42 +++++++++++ tests/json/test_array.hpp | 42 +---------- tests/json/test_as.cpp | 43 ++++++++++++ tests/json/test_as.hpp | 43 +----------- tests/json/test_as_flatten.cpp | 44 ++++++++++++ tests/json/test_as_flatten.hpp | 44 +----------- tests/json/test_box.cpp | 51 ++++++++++++++ tests/json/test_box.hpp | 51 +------------- tests/json/test_custom_class1.cpp | 43 ++++++++++++ tests/json/test_custom_class1.hpp | 43 +----------- tests/json/test_custom_class2.cpp | 48 +++++++++++++ tests/json/test_custom_class2.hpp | 48 +------------ tests/json/test_custom_class3.cpp | 68 ++++++++++++++++++ tests/json/test_custom_class3.hpp | 68 +----------------- tests/json/test_custom_class4.cpp | 69 ++++++++++++++++++ tests/json/test_custom_class4.hpp | 69 +----------------- tests/json/test_custom_constructor.cpp | 55 +++++++++++++++ tests/json/test_custom_constructor.hpp | 55 +-------------- tests/json/test_default_values.cpp | 35 ++++++++++ tests/json/test_default_values.hpp | 35 +--------- tests/json/test_deque.cpp | 35 ++++++++++ tests/json/test_deque.hpp | 35 +--------- tests/json/test_email.cpp | 31 ++++++++ tests/json/test_email.hpp | 31 +------- tests/json/test_error_messages.cpp | 48 +++++++++++++ tests/json/test_error_messages.hpp | 48 +------------ tests/json/test_field_variant.cpp | 40 +++++++++++ tests/json/test_field_variant.hpp | 40 +---------- tests/json/test_flatten.cpp | 41 +++++++++++ tests/json/test_flatten.hpp | 41 +---------- tests/json/test_flatten_anonymous.cpp | 42 +++++++++++ tests/json/test_flatten_anonymous.hpp | 42 +---------- tests/json/test_flatten_tuple.hpp | 28 -------- tests/json/test_forward_list.cpp | 35 ++++++++++ tests/json/test_forward_list.hpp | 35 +--------- tests/json/test_list.cpp | 35 ++++++++++ tests/json/test_list.hpp | 35 +--------- tests/json/test_literal.cpp | 32 +++++++++ tests/json/test_literal.hpp | 32 +-------- tests/json/test_literal_map.cpp | 29 ++++++++ tests/json/test_literal_map.hpp | 29 +------- tests/json/test_literal_unordered_map.cpp | 37 ++++++++++ tests/json/test_literal_unordered_map.hpp | 37 +--------- tests/json/test_map.cpp | 35 ++++++++++ tests/json/test_map.hpp | 35 +--------- tests/json/test_map_with_key_validation.cpp | 38 ++++++++++ tests/json/test_map_with_key_validation.hpp | 38 +--------- tests/json/test_monster_example.cpp | 68 ++++++++++++++++++ tests/json/test_monster_example.hpp | 68 +----------------- tests/json/test_move_replace.cpp | 34 +++++++++ tests/json/test_move_replace.hpp | 34 +-------- tests/json/test_multimap.cpp | 35 ++++++++++ tests/json/test_multimap.hpp | 35 +--------- tests/json/test_multiset.cpp | 33 +++++++++ tests/json/test_multiset.hpp | 33 +-------- tests/json/test_one_of.cpp | 34 +++++++++ tests/json/test_one_of.hpp | 34 +-------- tests/json/test_optional_fields.cpp | 37 ++++++++++ tests/json/test_optional_fields.hpp | 37 +--------- tests/json/test_readme_example.cpp | 55 +++++++++++++++ tests/json/test_readme_example.hpp | 55 +-------------- tests/json/test_ref.cpp | 51 ++++++++++++++ tests/json/test_ref.hpp | 51 +------------- tests/json/test_replace.cpp | 34 +++++++++ tests/json/test_replace.hpp | 34 +-------- tests/json/test_replace_flatten.cpp | 45 ++++++++++++ tests/json/test_replace_flatten.hpp | 45 +----------- tests/json/test_replace_with_other_struct.cpp | 44 ++++++++++++ tests/json/test_replace_with_other_struct.hpp | 44 +----------- tests/json/test_result.cpp | 37 ++++++++++ tests/json/test_result.hpp | 37 +--------- tests/json/test_save_load.cpp | 70 +++++++++++++++++++ tests/json/test_save_load.hpp | 70 +------------------ tests/json/test_set.cpp | 33 +++++++++ tests/json/test_set.hpp | 33 +-------- tests/json/test_size.cpp | 45 ++++++++++++ tests/json/test_size.hpp | 45 +----------- tests/json/test_string_map.cpp | 25 +++++++ tests/json/test_string_map.hpp | 25 +------ tests/json/test_string_unordered_map.cpp | 30 ++++++++ tests/json/test_string_unordered_map.hpp | 30 +------- tests/json/test_tagged_union.cpp | 37 ++++++++++ tests/json/test_tagged_union.hpp | 37 +--------- tests/json/test_tagged_union2.cpp | 40 +++++++++++ tests/json/test_tagged_union2.hpp | 40 +---------- tests/json/test_timestamp.cpp | 39 +++++++++++ tests/json/test_timestamp.hpp | 39 +---------- tests/json/test_unique_ptr.cpp | 36 ++++++++++ tests/json/test_unique_ptr.hpp | 36 +--------- tests/json/test_unique_ptr2.cpp | 51 ++++++++++++++ tests/json/test_unique_ptr2.hpp | 51 +------------- tests/json/test_unordered_map.cpp | 37 ++++++++++ tests/json/test_unordered_map.hpp | 37 +--------- tests/json/test_unordered_multimap.cpp | 37 ++++++++++ tests/json/test_unordered_multimap.hpp | 37 +--------- tests/json/test_unordered_multiset.cpp | 35 ++++++++++ tests/json/test_unordered_multiset.hpp | 35 +--------- tests/json/test_unordered_set.cpp | 35 ++++++++++ tests/json/test_unordered_set.hpp | 35 +--------- tests/json/test_variant.cpp | 37 ++++++++++ tests/json/test_variant.hpp | 37 +--------- 115 files changed, 2476 insertions(+), 2119 deletions(-) create mode 100644 tests/json/test_all_of.cpp create mode 100644 tests/json/test_alphanumeric_map.cpp create mode 100644 tests/json/test_alphanumeric_unordered_map.cpp create mode 100644 tests/json/test_anonymous_fields.cpp create mode 100644 tests/json/test_any_of.cpp create mode 100644 tests/json/test_array.cpp create mode 100644 tests/json/test_as.cpp create mode 100644 tests/json/test_as_flatten.cpp create mode 100644 tests/json/test_box.cpp create mode 100644 tests/json/test_custom_class1.cpp create mode 100644 tests/json/test_custom_class2.cpp create mode 100644 tests/json/test_custom_class3.cpp create mode 100644 tests/json/test_custom_class4.cpp create mode 100644 tests/json/test_custom_constructor.cpp create mode 100644 tests/json/test_default_values.cpp create mode 100644 tests/json/test_deque.cpp create mode 100644 tests/json/test_email.cpp create mode 100644 tests/json/test_error_messages.cpp create mode 100644 tests/json/test_field_variant.cpp create mode 100644 tests/json/test_flatten.cpp create mode 100644 tests/json/test_flatten_anonymous.cpp delete mode 100644 tests/json/test_flatten_tuple.hpp create mode 100644 tests/json/test_forward_list.cpp create mode 100644 tests/json/test_list.cpp create mode 100644 tests/json/test_literal.cpp create mode 100644 tests/json/test_literal_map.cpp create mode 100644 tests/json/test_literal_unordered_map.cpp create mode 100644 tests/json/test_map.cpp create mode 100644 tests/json/test_map_with_key_validation.cpp create mode 100644 tests/json/test_monster_example.cpp create mode 100644 tests/json/test_move_replace.cpp create mode 100644 tests/json/test_multimap.cpp create mode 100644 tests/json/test_multiset.cpp create mode 100644 tests/json/test_one_of.cpp create mode 100644 tests/json/test_optional_fields.cpp create mode 100644 tests/json/test_readme_example.cpp create mode 100644 tests/json/test_ref.cpp create mode 100644 tests/json/test_replace.cpp create mode 100644 tests/json/test_replace_flatten.cpp create mode 100644 tests/json/test_replace_with_other_struct.cpp create mode 100644 tests/json/test_result.cpp create mode 100644 tests/json/test_save_load.cpp create mode 100644 tests/json/test_set.cpp create mode 100644 tests/json/test_size.cpp create mode 100644 tests/json/test_string_map.cpp create mode 100644 tests/json/test_string_unordered_map.cpp create mode 100644 tests/json/test_tagged_union.cpp create mode 100644 tests/json/test_tagged_union2.cpp create mode 100644 tests/json/test_timestamp.cpp create mode 100644 tests/json/test_unique_ptr.cpp create mode 100644 tests/json/test_unique_ptr2.cpp create mode 100644 tests/json/test_unordered_map.cpp create mode 100644 tests/json/test_unordered_multimap.cpp create mode 100644 tests/json/test_unordered_multiset.cpp create mode 100644 tests/json/test_unordered_set.cpp create mode 100644 tests/json/test_variant.cpp diff --git a/include/rfl/internal/get_struct_name.hpp b/include/rfl/internal/get_struct_name.hpp index 64c7d0bb..e8eb486b 100644 --- a/include/rfl/internal/get_struct_name.hpp +++ b/include/rfl/internal/get_struct_name.hpp @@ -8,7 +8,7 @@ namespace rfl { namespace internal { -std::string remove_namespaces(const std::string& _name) { +inline std::string remove_namespaces(const std::string& _name) { const size_t pos = _name.find_last_of(":"); if (pos == std::string::npos) { return _name; diff --git a/include/rfl/io/load_bytes.hpp b/include/rfl/io/load_bytes.hpp index 7a3415b8..2b78ebca 100644 --- a/include/rfl/io/load_bytes.hpp +++ b/include/rfl/io/load_bytes.hpp @@ -11,7 +11,7 @@ namespace rfl { namespace io { -Result> load_bytes(const std::string& _fname) { +inline Result> load_bytes(const std::string& _fname) { std::ifstream input(_fname, std::ios::binary); if (input.is_open()) { std::istreambuf_iterator begin(input), end; diff --git a/include/rfl/io/load_string.hpp b/include/rfl/io/load_string.hpp index ecb2c0c0..9c816a27 100644 --- a/include/rfl/io/load_string.hpp +++ b/include/rfl/io/load_string.hpp @@ -10,7 +10,7 @@ namespace rfl { namespace io { -Result load_string(const std::string& _fname) { +inline Result load_string(const std::string& _fname) { std::ifstream infile(_fname); if (infile.is_open()) { auto r = std::string(std::istreambuf_iterator(infile), diff --git a/tests/json/CMakeLists.txt b/tests/json/CMakeLists.txt index 72068363..d0604d43 100644 --- a/tests/json/CMakeLists.txt +++ b/tests/json/CMakeLists.txt @@ -9,6 +9,61 @@ include_directories( add_executable( reflect-cpp-tests "tests.cpp" + "test_all_of.cpp" + "test_alphanumeric_map.cpp" + "test_alphanumeric_unordered_map.cpp" + "test_anonymous_fields.cpp" + "test_any_of.cpp" + "test_array.cpp" + "test_as.cpp" + "test_as_flatten.cpp" + "test_box.cpp" + "test_custom_class1.cpp" + "test_custom_class2.cpp" + "test_custom_class3.cpp" + "test_custom_class4.cpp" + "test_custom_constructor.cpp" + "test_default_values.cpp" + "test_deque.cpp" + "test_email.cpp" + "test_error_messages.cpp" + "test_field_variant.cpp" + "test_flatten.cpp" + "test_flatten_anonymous.cpp" + "test_forward_list.cpp" + "test_list.cpp" + "test_literal.cpp" + "test_literal_map.cpp" + "test_literal_unordered_map.cpp" + "test_map.cpp" + "test_map_with_key_validation.cpp" + "test_monster_example.cpp" + "test_move_replace.cpp" + "test_multimap.cpp" + "test_multiset.cpp" + "test_one_of.cpp" + "test_optional_fields.cpp" + "test_readme_example.cpp" + "test_ref.cpp" + "test_replace.cpp" + "test_replace_flatten.cpp" + "test_replace_with_other_struct.cpp" + "test_result.cpp" + "test_save_load.cpp" + "test_set.cpp" + "test_size.cpp" + "test_string_map.cpp" + "test_string_unordered_map.cpp" + "test_tagged_union.cpp" + "test_tagged_union2.cpp" + "test_timestamp.cpp" + "test_unique_ptr.cpp" + "test_unique_ptr2.cpp" + "test_unordered_map.cpp" + "test_unordered_multimap.cpp" + "test_unordered_multiset.cpp" + "test_unordered_set.cpp" + "test_variant.cpp" "../../src/yyjson.c" ) diff --git a/tests/json/test_all_of.cpp b/tests/json/test_all_of.cpp new file mode 100644 index 00000000..87501b0c --- /dev/null +++ b/tests/json/test_all_of.cpp @@ -0,0 +1,31 @@ +#include "test_all_of.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_all_of { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + Age age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer, + R"({"firstName":"Homer","lastName":"Simpson","age":45})"); +} +} // namespace test_all_of diff --git a/tests/json/test_all_of.hpp b/tests/json/test_all_of.hpp index eae5fe93..082d508a 100644 --- a/tests/json/test_all_of.hpp +++ b/tests/json/test_all_of.hpp @@ -1,29 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_all_of { - -using Age = rfl::Validator, rfl::Maximum<130>>; - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - Age age; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto homer = - Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; - - write_and_read(homer, - R"({"firstName":"Homer","lastName":"Simpson","age":45})"); +namespace test_all_of{ + void test(); } -} // namespace test_all_of + \ No newline at end of file diff --git a/tests/json/test_alphanumeric_map.cpp b/tests/json/test_alphanumeric_map.cpp new file mode 100644 index 00000000..803a30fc --- /dev/null +++ b/tests/json/test_alphanumeric_map.cpp @@ -0,0 +1,27 @@ +#include "test_alphanumeric_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_alphanumeric_map { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})"); +} + +} // namespace test_alphanumeric_map diff --git a/tests/json/test_alphanumeric_map.hpp b/tests/json/test_alphanumeric_map.hpp index a2fca5ec..389fdc7b 100644 --- a/tests/json/test_alphanumeric_map.hpp +++ b/tests/json/test_alphanumeric_map.hpp @@ -1,25 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_alphanumeric_map { - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - std::map> homer; - homer.insert( - std::make_pair("firstName", std::make_unique("Homer"))); - homer.insert( - std::make_pair("lastName", std::make_unique("Simpson"))); - - write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})"); +namespace test_alphanumeric_map{ + void test(); } - -} // namespace test_alphanumeric_map + \ No newline at end of file diff --git a/tests/json/test_alphanumeric_unordered_map.cpp b/tests/json/test_alphanumeric_unordered_map.cpp new file mode 100644 index 00000000..3f8bc467 --- /dev/null +++ b/tests/json/test_alphanumeric_unordered_map.cpp @@ -0,0 +1,31 @@ +#include "test_alphanumeric_unordered_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_alphanumeric_unordered_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + using Map = + std::unordered_map>; + Map homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_alphanumeric_unordered_map diff --git a/tests/json/test_alphanumeric_unordered_map.hpp b/tests/json/test_alphanumeric_unordered_map.hpp index 9dd15a19..d9f85cd2 100644 --- a/tests/json/test_alphanumeric_unordered_map.hpp +++ b/tests/json/test_alphanumeric_unordered_map.hpp @@ -1,29 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_alphanumeric_unordered_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Map = - std::unordered_map>; - Map homer; - homer.insert( - std::make_pair("firstName", std::make_unique("Homer"))); - homer.insert( - std::make_pair("lastName", std::make_unique("Simpson"))); - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::json::write(homer); - const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_alphanumeric_unordered_map{ + void test(); } -} // namespace test_alphanumeric_unordered_map + \ No newline at end of file diff --git a/tests/json/test_anonymous_fields.cpp b/tests/json/test_anonymous_fields.cpp new file mode 100644 index 00000000..60f98841 --- /dev/null +++ b/tests/json/test_anonymous_fields.cpp @@ -0,0 +1,59 @@ +#include "test_anonymous_fields.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_anonymous_fields { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + std::string first_name; + std::string last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"first_name":"Homer","last_name":"Simpson","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"first_name":"Bart","last_name":"Simpson","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"first_name":"Lisa","last_name":"Simpson","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"first_name":"Maggie","last_name":"Simpson","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})"); +} +} // namespace test_anonymous_fields diff --git a/tests/json/test_anonymous_fields.hpp b/tests/json/test_anonymous_fields.hpp index f13aedcc..16c47d36 100644 --- a/tests/json/test_anonymous_fields.hpp +++ b/tests/json/test_anonymous_fields.hpp @@ -1,57 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_anonymous_fields { - -using Age = rfl::Validator, rfl::Maximum<130>>>; - -struct Person { - std::string first_name; - std::string last_name; - rfl::Timestamp<"%Y-%m-%d"> birthday; - Age age; - rfl::Email email; - std::vector children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 10, - .email = "bart@simpson.com"}; - - const auto lisa = Person{.first_name = "Lisa", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com"}; - - const auto maggie = Person{.first_name = "Maggie", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 0, - .email = "maggie@simpson.com"}; - - const auto homer = - Person{.first_name = "Homer", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read( - homer, - R"({"first_name":"Homer","last_name":"Simpson","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"first_name":"Bart","last_name":"Simpson","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"first_name":"Lisa","last_name":"Simpson","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"first_name":"Maggie","last_name":"Simpson","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})"); +namespace test_anonymous_fields{ + void test(); } -} // namespace test_anonymous_fields + \ No newline at end of file diff --git a/tests/json/test_any_of.cpp b/tests/json/test_any_of.cpp new file mode 100644 index 00000000..f0c259f2 --- /dev/null +++ b/tests/json/test_any_of.cpp @@ -0,0 +1,35 @@ +#include "test_any_of.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_any_of { + +using Age = + rfl::Validator, rfl::Maximum<10>>, + rfl::AllOf, rfl::Maximum<130>>>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + Age age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer, + R"({"firstName":"Homer","lastName":"Simpson","age":45})"); +} + +} // namespace test_any_of diff --git a/tests/json/test_any_of.hpp b/tests/json/test_any_of.hpp index 390724f1..128c8ebc 100644 --- a/tests/json/test_any_of.hpp +++ b/tests/json/test_any_of.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_any_of { - -using Age = - rfl::Validator, rfl::Maximum<10>>, - rfl::AllOf, rfl::Maximum<130>>>>; - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - Age age; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto homer = - Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; - - write_and_read(homer, - R"({"firstName":"Homer","lastName":"Simpson","age":45})"); +namespace test_any_of{ + void test(); } - -} // namespace test_any_of + \ No newline at end of file diff --git a/tests/json/test_array.cpp b/tests/json/test_array.cpp new file mode 100644 index 00000000..54706e42 --- /dev/null +++ b/tests/json/test_array.cpp @@ -0,0 +1,42 @@ +#include "test_array.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_array { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children = nullptr; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto bart = Person{.first_name = "Bart"}; + + auto lisa = Person{.first_name = "Lisa"}; + + auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = Person{ + .first_name = "Homer", + .children = std::make_unique>(std::array{ + std::move(bart), std::move(lisa), std::move(maggie)})}; + + write_and_read( + homer, + "{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{" + "\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{" + "\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{" + "\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}"); +} +} // namespace test_array diff --git a/tests/json/test_array.hpp b/tests/json/test_array.hpp index de51ddc8..502c3388 100644 --- a/tests/json/test_array.hpp +++ b/tests/json/test_array.hpp @@ -1,40 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_array { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children = nullptr; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto bart = Person{.first_name = "Bart"}; - - auto lisa = Person{.first_name = "Lisa"}; - - auto maggie = Person{.first_name = "Maggie"}; - - const auto homer = Person{ - .first_name = "Homer", - .children = std::make_unique>(std::array{ - std::move(bart), std::move(lisa), std::move(maggie)})}; - - write_and_read( - homer, - "{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{" - "\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{" - "\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{" - "\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}"); +namespace test_array{ + void test(); } -} // namespace test_array + \ No newline at end of file diff --git a/tests/json/test_as.cpp b/tests/json/test_as.cpp new file mode 100644 index 00000000..a5a2d96d --- /dev/null +++ b/tests/json/test_as.cpp @@ -0,0 +1,43 @@ +#include "test_as.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_as { + +struct A { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", rfl::Box> f2; +}; + +struct B { + rfl::Field<"f3", std::string> f3; + rfl::Field<"f4", rfl::Box> f4; +}; + +struct C { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", rfl::Box> f2; + rfl::Field<"f4", rfl::Box> f4; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; + + auto b = B{.f3 = "Hello", .f4 = rfl::make_box("World")}; + + const auto c = rfl::as(std::move(a), std::move(b)); + + write_and_read(c, R"({"f1":"Hello","f2":"World","f4":"World"})"); +} + +} // namespace test_as diff --git a/tests/json/test_as.hpp b/tests/json/test_as.hpp index c392c009..a2a8e1ab 100644 --- a/tests/json/test_as.hpp +++ b/tests/json/test_as.hpp @@ -1,41 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_as { - -struct A { - rfl::Field<"f1", std::string> f1; - rfl::Field<"f2", rfl::Box> f2; -}; - -struct B { - rfl::Field<"f3", std::string> f3; - rfl::Field<"f4", rfl::Box> f4; -}; - -struct C { - rfl::Field<"f1", std::string> f1; - rfl::Field<"f2", rfl::Box> f2; - rfl::Field<"f4", rfl::Box> f4; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; - - auto b = B{.f3 = "Hello", .f4 = rfl::make_box("World")}; - - const auto c = rfl::as(std::move(a), std::move(b)); - - write_and_read(c, R"({"f1":"Hello","f2":"World","f4":"World"})"); +namespace test_as{ + void test(); } - -} // namespace test_as + \ No newline at end of file diff --git a/tests/json/test_as_flatten.cpp b/tests/json/test_as_flatten.cpp new file mode 100644 index 00000000..d5faa405 --- /dev/null +++ b/tests/json/test_as_flatten.cpp @@ -0,0 +1,44 @@ +#include "test_as_flatten.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_as_flatten { + +struct A { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", rfl::Box> f2; +}; + +struct B { + rfl::Field<"f3", std::string> f3; + rfl::Field<"f4", rfl::Box> f4; +}; + +struct C { + rfl::Flatten a; + rfl::Flatten b; + rfl::Field<"f5", int> f5; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; + + auto b = B{.f3 = "Hello", .f4 = rfl::make_box("World")}; + + const auto c = + rfl::as(std::move(a), std::move(b), rfl::make_field<"f5">(5)); + + write_and_read( + c, R"({"f1":"Hello","f2":"World","f3":"Hello","f4":"World","f5":5})"); +} +} // namespace test_as_flatten diff --git a/tests/json/test_as_flatten.hpp b/tests/json/test_as_flatten.hpp index 69cda447..9b7df9d4 100644 --- a/tests/json/test_as_flatten.hpp +++ b/tests/json/test_as_flatten.hpp @@ -1,42 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_as_flatten { - -struct A { - rfl::Field<"f1", std::string> f1; - rfl::Field<"f2", rfl::Box> f2; -}; - -struct B { - rfl::Field<"f3", std::string> f3; - rfl::Field<"f4", rfl::Box> f4; -}; - -struct C { - rfl::Flatten a; - rfl::Flatten b; - rfl::Field<"f5", int> f5; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; - - auto b = B{.f3 = "Hello", .f4 = rfl::make_box("World")}; - - const auto c = - rfl::as(std::move(a), std::move(b), rfl::make_field<"f5">(5)); - - write_and_read( - c, R"({"f1":"Hello","f2":"World","f3":"Hello","f4":"World","f5":5})"); +namespace test_as_flatten{ + void test(); } -} // namespace test_as_flatten + \ No newline at end of file diff --git a/tests/json/test_box.cpp b/tests/json/test_box.cpp new file mode 100644 index 00000000..a7c9090e --- /dev/null +++ b/tests/json/test_box.cpp @@ -0,0 +1,51 @@ +#include "test_box.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_box { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Box lesser; + rfl::Box greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_box(DecisionTree{leaf1}), + .greater = rfl::make_box(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read( + tree, + R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})"); +} +} // namespace test_box diff --git a/tests/json/test_box.hpp b/tests/json/test_box.hpp index 1e08f80c..a564b9e1 100644 --- a/tests/json/test_box.hpp +++ b/tests/json/test_box.hpp @@ -1,49 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_box { - -struct DecisionTree { - struct Leaf { - using Tag = rfl::Literal<"Leaf">; - double value; - }; - - struct Node { - using Tag = rfl::Literal<"Node">; - rfl::Rename<"criticalValue", double> critical_value; - rfl::Box lesser; - rfl::Box greater; - }; - - using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; - - rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto leaf1 = DecisionTree::Leaf{.value = 3.0}; - - auto leaf2 = DecisionTree::Leaf{.value = 5.0}; - - auto node = DecisionTree::Node{ - .critical_value = 10.0, - .lesser = rfl::make_box(DecisionTree{leaf1}), - .greater = rfl::make_box(DecisionTree{leaf2})}; - - const DecisionTree tree{.leaf_or_node = std::move(node)}; - - write_and_read( - tree, - R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})"); +namespace test_box{ + void test(); } -} // namespace test_box + \ No newline at end of file diff --git a/tests/json/test_custom_class1.cpp b/tests/json/test_custom_class1.cpp new file mode 100644 index 00000000..2e14e575 --- /dev/null +++ b/tests/json/test_custom_class1.cpp @@ -0,0 +1,43 @@ +#include "test_custom_class1.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class1 { + +struct Person { + struct PersonImpl { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; + }; + + using ReflectionType = PersonImpl; + + Person(const PersonImpl& _impl) : impl(_impl) {} + + Person(const std::string& _first_name) + : impl(PersonImpl{.first_name = _first_name}) {} + + const ReflectionType& reflection() const { return impl; }; + + private: + PersonImpl impl; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart"); + + write_and_read(bart, + R"({"firstName":"Bart","lastName":"Simpson","children":[]})"); +} +} // namespace test_custom_class1 diff --git a/tests/json/test_custom_class1.hpp b/tests/json/test_custom_class1.hpp index 468752b0..eafe6cd0 100644 --- a/tests/json/test_custom_class1.hpp +++ b/tests/json/test_custom_class1.hpp @@ -1,41 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_class1 { - -struct Person { - struct PersonImpl { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::vector children; - }; - - using ReflectionType = PersonImpl; - - Person(const PersonImpl& _impl) : impl(_impl) {} - - Person(const std::string& _first_name) - : impl(PersonImpl{.first_name = _first_name}) {} - - const ReflectionType& reflection() const { return impl; }; - - private: - PersonImpl impl; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person("Bart"); - - write_and_read(bart, - R"({"firstName":"Bart","lastName":"Simpson","children":[]})"); +namespace test_custom_class1{ + void test(); } -} // namespace test_custom_class1 + \ No newline at end of file diff --git a/tests/json/test_custom_class2.cpp b/tests/json/test_custom_class2.cpp new file mode 100644 index 00000000..adfa1021 --- /dev/null +++ b/tests/json/test_custom_class2.cpp @@ -0,0 +1,48 @@ +#include "test_custom_class2.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class2 { + +struct FiveDigitCode { + using ReflectionType = int; + + FiveDigitCode(const int _code) : code(_code) { + if (_code < 10000) { + throw std::runtime_error("Less than five digits."); + } + if (_code > 99999) { + throw std::runtime_error("More than five digits."); + } + } + + ReflectionType reflection() const { return code; }; + + private: + int code; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto result = rfl::json::read("123"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto c = FiveDigitCode(12345); + + write_and_read(c, "12345"); +} +} // namespace test_custom_class2 diff --git a/tests/json/test_custom_class2.hpp b/tests/json/test_custom_class2.hpp index aff5b697..21cfbd51 100644 --- a/tests/json/test_custom_class2.hpp +++ b/tests/json/test_custom_class2.hpp @@ -1,46 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_class2 { - -struct FiveDigitCode { - using ReflectionType = int; - - FiveDigitCode(const int _code) : code(_code) { - if (_code < 10000) { - throw std::runtime_error("Less than five digits."); - } - if (_code > 99999) { - throw std::runtime_error("More than five digits."); - } - } - - ReflectionType reflection() const { return code; }; - - private: - int code; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto result = rfl::json::read("123"); - - if (result) { - std::cout << "Failed: Expected an error, but got none." << std::endl; - return; - } - - const auto c = FiveDigitCode(12345); - - write_and_read(c, "12345"); +namespace test_custom_class2{ + void test(); } -} // namespace test_custom_class2 + \ No newline at end of file diff --git a/tests/json/test_custom_class3.cpp b/tests/json/test_custom_class3.cpp new file mode 100644 index 00000000..5da2c207 --- /dev/null +++ b/tests/json/test_custom_class3.cpp @@ -0,0 +1,68 @@ +#include "test_custom_class3.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class3 { + +struct Person { + Person(const std::string& _first_name, const std::string& _last_name, + const int _age) + : first_name_(_first_name), last_name_(_last_name), age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + std::string last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + int age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = _p.last_name(), + .age = _p.age()}; + } + + Person to_class() const { return Person(first_name(), last_name(), age); } +}; +} // namespace test_custom_class3 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class3 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart", "Simpson", 10); + + write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})"); +} + +} // namespace test_custom_class3 diff --git a/tests/json/test_custom_class3.hpp b/tests/json/test_custom_class3.hpp index f65e22c1..9a6fdab4 100644 --- a/tests/json/test_custom_class3.hpp +++ b/tests/json/test_custom_class3.hpp @@ -1,66 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_class3 { - -struct Person { - Person(const std::string& _first_name, const std::string& _last_name, - const int _age) - : first_name_(_first_name), last_name_(_last_name), age_(_age) {} - - const auto& first_name() const { return first_name_; } - - const auto& last_name() const { return last_name_; } - - auto age() const { return age_; } - - private: - std::string first_name_; - std::string last_name_; - int age_; -}; - -struct PersonImpl { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - int age; - - static PersonImpl from_class(const Person& _p) noexcept { - return PersonImpl{.first_name = _p.first_name(), - .last_name = _p.last_name(), - .age = _p.age()}; - } - - Person to_class() const { return Person(first_name(), last_name(), age); } -}; -} // namespace test_custom_class3 - -namespace rfl { -namespace parsing { - -template -struct Parser - : public CustomParser {}; - -} // namespace parsing -} // namespace rfl - -namespace test_custom_class3 { - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person("Bart", "Simpson", 10); - - write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})"); +namespace test_custom_class3{ + void test(); } - -} // namespace test_custom_class3 + \ No newline at end of file diff --git a/tests/json/test_custom_class4.cpp b/tests/json/test_custom_class4.cpp new file mode 100644 index 00000000..7057cbdf --- /dev/null +++ b/tests/json/test_custom_class4.cpp @@ -0,0 +1,69 @@ +#include "test_custom_class4.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class4 { + +struct Person { + Person(const std::string& _first_name, + const rfl::Box& _last_name, int _age) + : first_name_(_first_name), + last_name_(rfl::make_box(*_last_name)), + age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + rfl::Box last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = rfl::make_box(*_p.last_name()), + .age = _p.age()}; + } +}; + +} // namespace test_custom_class4 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class4 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = test_custom_class4::Person( + "Bart", rfl::make_box("Simpson"), 10); + + write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})"); +} +} // namespace test_custom_class4 diff --git a/tests/json/test_custom_class4.hpp b/tests/json/test_custom_class4.hpp index 5178acb4..2d3b151a 100644 --- a/tests/json/test_custom_class4.hpp +++ b/tests/json/test_custom_class4.hpp @@ -1,67 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_class4 { - -struct Person { - Person(const std::string& _first_name, - const rfl::Box& _last_name, int _age) - : first_name_(_first_name), - last_name_(rfl::make_box(*_last_name)), - age_(_age) {} - - const auto& first_name() const { return first_name_; } - - const auto& last_name() const { return last_name_; } - - auto age() const { return age_; } - - private: - std::string first_name_; - rfl::Box last_name_; - int age_; -}; - -struct PersonImpl { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", rfl::Box> last_name; - rfl::Field<"age", int> age; - - static PersonImpl from_class(const Person& _p) noexcept { - return PersonImpl{.first_name = _p.first_name(), - .last_name = rfl::make_box(*_p.last_name()), - .age = _p.age()}; - } -}; - -} // namespace test_custom_class4 - -namespace rfl { -namespace parsing { - -template -struct Parser - : public CustomParser {}; - -} // namespace parsing -} // namespace rfl - -namespace test_custom_class4 { - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = test_custom_class4::Person( - "Bart", rfl::make_box("Simpson"), 10); - - write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})"); +namespace test_custom_class4{ + void test(); } -} // namespace test_custom_class4 + \ No newline at end of file diff --git a/tests/json/test_custom_constructor.cpp b/tests/json/test_custom_constructor.cpp new file mode 100644 index 00000000..28ec28dc --- /dev/null +++ b/tests/json/test_custom_constructor.cpp @@ -0,0 +1,55 @@ +#include "test_custom_constructor.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_constructor { + +struct Person { + static rfl::Result from_json_obj( + typename rfl::json::Reader::InputVarType _obj) { + /// This only exists for the purpose of the test. + const auto change_first_name = [](auto&& _person) { + return rfl::replace(std::move(_person), + rfl::Field<"firstName", std::string>("Bart")); + }; + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::json::read>(_obj) + .transform(from_nt) + .transform(change_first_name); + } + + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto res = rfl::json::read( + R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})"); + + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl; + return; + } + + if (res.value().first_name() != "Bart") { + std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" + << std::endl + << std::endl; + } else { + std::cout << "OK" << std::endl << std::endl; + } +} +} // namespace test_custom_constructor diff --git a/tests/json/test_custom_constructor.hpp b/tests/json/test_custom_constructor.hpp index 3ccd0c1a..013721d0 100644 --- a/tests/json/test_custom_constructor.hpp +++ b/tests/json/test_custom_constructor.hpp @@ -1,53 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_constructor { - -struct Person { - static rfl::Result from_json_obj( - typename rfl::json::Reader::InputVarType _obj) { - /// This only exists for the purpose of the test. - const auto change_first_name = [](auto&& _person) { - return rfl::replace(std::move(_person), - rfl::Field<"firstName", std::string>("Bart")); - }; - const auto from_nt = [](auto&& _nt) { - return rfl::from_named_tuple(std::move(_nt)); - }; - return rfl::json::read>(_obj) - .transform(from_nt) - .transform(change_first_name); - } - - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto res = rfl::json::read( - R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})"); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl; - return; - } - - if (res.value().first_name() != "Bart") { - std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" - << std::endl - << std::endl; - } else { - std::cout << "OK" << std::endl << std::endl; - } +namespace test_custom_constructor{ + void test(); } -} // namespace test_custom_constructor + \ No newline at end of file diff --git a/tests/json/test_default_values.cpp b/tests/json/test_default_values.cpp new file mode 100644 index 00000000..a8105197 --- /dev/null +++ b/tests/json/test_default_values.cpp @@ -0,0 +1,35 @@ +#include "test_default_values.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_default_values { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart"}; + const auto lisa = Person{.first_name = "Lisa"}; + const auto maggie = Person{.first_name = "Maggie"}; + const auto homer = + Person{.first_name = "Homer", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]})"); +} +} // namespace test_default_values diff --git a/tests/json/test_default_values.hpp b/tests/json/test_default_values.hpp index b2854f48..c8f8360e 100644 --- a/tests/json/test_default_values.hpp +++ b/tests/json/test_default_values.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_default_values { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::vector children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart"}; - const auto lisa = Person{.first_name = "Lisa"}; - const auto maggie = Person{.first_name = "Maggie"}; - const auto homer = - Person{.first_name = "Homer", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]})"); +namespace test_default_values{ + void test(); } -} // namespace test_default_values + \ No newline at end of file diff --git a/tests/json/test_deque.cpp b/tests/json/test_deque.cpp new file mode 100644 index 00000000..059a9a7f --- /dev/null +++ b/tests/json/test_deque.cpp @@ -0,0 +1,35 @@ +#include "test_deque.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_deque { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} +} // namespace test_deque diff --git a/tests/json/test_deque.hpp b/tests/json/test_deque.hpp index 66540ef2..6781e880 100644 --- a/tests/json/test_deque.hpp +++ b/tests/json/test_deque.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_deque { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->emplace_back(Person{.first_name = "Bart"}); - children->emplace_back(Person{.first_name = "Lisa"}); - children->emplace_back(Person{.first_name = "Maggie"}); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +namespace test_deque{ + void test(); } -} // namespace test_deque + \ No newline at end of file diff --git a/tests/json/test_email.cpp b/tests/json/test_email.cpp new file mode 100644 index 00000000..861963e2 --- /dev/null +++ b/tests/json/test_email.cpp @@ -0,0 +1,31 @@ +#include "test_email.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_email { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Email email; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto homer = Person{.first_name = "Homer", + .last_name = "Simpson", + .email = "homer@simpson.com"}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","email":"homer@simpson.com"})"); +} +} // namespace test_email diff --git a/tests/json/test_email.hpp b/tests/json/test_email.hpp index 74d3c90b..3d9e15a0 100644 --- a/tests/json/test_email.hpp +++ b/tests/json/test_email.hpp @@ -1,29 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_email { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - rfl::Email email; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto homer = Person{.first_name = "Homer", - .last_name = "Simpson", - .email = "homer@simpson.com"}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","email":"homer@simpson.com"})"); +namespace test_email{ + void test(); } -} // namespace test_email + \ No newline at end of file diff --git a/tests/json/test_error_messages.cpp b/tests/json/test_error_messages.cpp new file mode 100644 index 00000000..aa475a1d --- /dev/null +++ b/tests/json/test_error_messages.cpp @@ -0,0 +1,48 @@ +#include "test_error_messages.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_error_messages { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const std::string faulty_string = + R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})"; + + const auto result = rfl::json::read(faulty_string); + + const std::string expected = R"(Found 3 errors: +1) Failed to parse field 'lastName': Could not cast to string. +2) Failed to parse field 'birthday': String '04/19/1987' did not match format '%Y-%m-%d'. +3) Field named 'children' not found.)"; + + if (!result.error()) { + std::cout << "Expected an error, got none." << std::endl << std::endl; + } + + if (result.error().value().what() != expected) { + std::cout << "Expected: " << std::endl + << expected << std::endl + << "Got:" << std::endl + << result.error().value().what() << std::endl + << std::endl; + } + + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_error_messages diff --git a/tests/json/test_error_messages.hpp b/tests/json/test_error_messages.hpp index adba716b..726d24a3 100644 --- a/tests/json/test_error_messages.hpp +++ b/tests/json/test_error_messages.hpp @@ -1,46 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_error_messages { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - rfl::Timestamp<"%Y-%m-%d"> birthday; - std::vector children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const std::string faulty_string = - R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})"; - - const auto result = rfl::json::read(faulty_string); - - const std::string expected = R"(Found 3 errors: -1) Failed to parse field 'lastName': Could not cast to string. -2) Failed to parse field 'birthday': String '04/19/1987' did not match format '%Y-%m-%d'. -3) Field named 'children' not found.)"; - - if (!result.error()) { - std::cout << "Expected an error, got none." << std::endl << std::endl; - } - - if (result.error().value().what() != expected) { - std::cout << "Expected: " << std::endl - << expected << std::endl - << "Got:" << std::endl - << result.error().value().what() << std::endl - << std::endl; - } - - std::cout << "OK" << std::endl << std::endl; +namespace test_error_messages{ + void test(); } -} // namespace test_error_messages + \ No newline at end of file diff --git a/tests/json/test_field_variant.cpp b/tests/json/test_field_variant.cpp new file mode 100644 index 00000000..75543dcb --- /dev/null +++ b/tests/json/test_field_variant.cpp @@ -0,0 +1,40 @@ +#include "test_field_variant.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_field_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::Variant, + rfl::Field<"rectangle", Rectangle>, + rfl::Field<"square", rfl::Box>>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = + rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); + + write_and_read(r, R"({"rectangle":{"height":10.0,"width":5.0}})"); +} +} // namespace test_field_variant diff --git a/tests/json/test_field_variant.hpp b/tests/json/test_field_variant.hpp index 4b541ffc..ba93e732 100644 --- a/tests/json/test_field_variant.hpp +++ b/tests/json/test_field_variant.hpp @@ -1,38 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_field_variant { - -struct Circle { - double radius; -}; - -struct Rectangle { - double height; - double width; -}; - -struct Square { - double width; -}; - -using Shapes = rfl::Variant, - rfl::Field<"rectangle", Rectangle>, - rfl::Field<"square", rfl::Box>>; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Shapes r = - rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); - - write_and_read(r, R"({"rectangle":{"height":10.0,"width":5.0}})"); +namespace test_field_variant{ + void test(); } -} // namespace test_field_variant + \ No newline at end of file diff --git a/tests/json/test_flatten.cpp b/tests/json/test_flatten.cpp new file mode 100644 index 00000000..e4437ba1 --- /dev/null +++ b/tests/json/test_flatten.cpp @@ -0,0 +1,41 @@ +#include "test_flatten.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", rfl::Box> employer; + rfl::Field<"salary", float> salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read( + employee, + R"({"firstName":"Homer","lastName":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})"); +} +} // namespace test_flatten diff --git a/tests/json/test_flatten.hpp b/tests/json/test_flatten.hpp index c2485755..24d60e11 100644 --- a/tests/json/test_flatten.hpp +++ b/tests/json/test_flatten.hpp @@ -1,39 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_flatten { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", rfl::Box> last_name; - rfl::Field<"age", int> age; -}; - -struct Employee { - rfl::Flatten person; - rfl::Field<"employer", rfl::Box> employer; - rfl::Field<"salary", float> salary; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto employee = Employee{ - .person = Person{.first_name = "Homer", - .last_name = rfl::make_box("Simpson"), - .age = 45}, - .employer = rfl::make_box("Mr. Burns"), - .salary = 60000.0}; - - write_and_read( - employee, - R"({"firstName":"Homer","lastName":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})"); +namespace test_flatten{ + void test(); } -} // namespace test_flatten + \ No newline at end of file diff --git a/tests/json/test_flatten_anonymous.cpp b/tests/json/test_flatten_anonymous.cpp new file mode 100644 index 00000000..21c7c809 --- /dev/null +++ b/tests/json/test_flatten_anonymous.cpp @@ -0,0 +1,42 @@ +#include "test_flatten_anonymous.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten_anonymous { + +struct Person { + std::string first_name; + rfl::Box last_name; + int age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Box employer; + float salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read( + employee, + R"({"first_name":"Homer","last_name":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})"); +} + +} // namespace test_flatten_anonymous diff --git a/tests/json/test_flatten_anonymous.hpp b/tests/json/test_flatten_anonymous.hpp index 32dcdc07..7ffa2785 100644 --- a/tests/json/test_flatten_anonymous.hpp +++ b/tests/json/test_flatten_anonymous.hpp @@ -1,40 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_flatten_anonymous { - -struct Person { - std::string first_name; - rfl::Box last_name; - int age; -}; - -struct Employee { - rfl::Flatten person; - rfl::Box employer; - float salary; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto employee = Employee{ - .person = Person{.first_name = "Homer", - .last_name = rfl::make_box("Simpson"), - .age = 45}, - .employer = rfl::make_box("Mr. Burns"), - .salary = 60000.0}; - - write_and_read( - employee, - R"({"first_name":"Homer","last_name":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})"); +namespace test_flatten_anonymous{ + void test(); } - -} // namespace test_flatten_anonymous + \ No newline at end of file diff --git a/tests/json/test_flatten_tuple.hpp b/tests/json/test_flatten_tuple.hpp deleted file mode 100644 index ea640024..00000000 --- a/tests/json/test_flatten_tuple.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -void test_flatten_tuple() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Person = std::tuple, int>; - - struct Employee { - rfl::Flatten person; - rfl::Box employer; - float salary; - }; - - const auto employee = Employee{ - .person = Person{"Homer", rfl::make_box("Simpson"), 45}, - .employer = rfl::make_box("Mr. Burns"), - .salary = 60000.0}; - - write_and_read(employee, R"(["Homer","Simpson",45,"Mr. Burns",60000.0])"); -} diff --git a/tests/json/test_forward_list.cpp b/tests/json/test_forward_list.cpp new file mode 100644 index 00000000..d46ef96f --- /dev/null +++ b/tests/json/test_forward_list.cpp @@ -0,0 +1,35 @@ +#include "test_forward_list.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_forward_list { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_front(Person{.first_name = "Maggie"}); + children->emplace_front(Person{.first_name = "Lisa"}); + children->emplace_front(Person{.first_name = "Bart"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} +} // namespace test_forward_list diff --git a/tests/json/test_forward_list.hpp b/tests/json/test_forward_list.hpp index b4b87090..9784a0c4 100644 --- a/tests/json/test_forward_list.hpp +++ b/tests/json/test_forward_list.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_forward_list { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->emplace_front(Person{.first_name = "Maggie"}); - children->emplace_front(Person{.first_name = "Lisa"}); - children->emplace_front(Person{.first_name = "Bart"}); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +namespace test_forward_list{ + void test(); } -} // namespace test_forward_list + \ No newline at end of file diff --git a/tests/json/test_list.cpp b/tests/json/test_list.cpp new file mode 100644 index 00000000..cffb8f52 --- /dev/null +++ b/tests/json/test_list.cpp @@ -0,0 +1,35 @@ +#include "test_list.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_list { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} +} // namespace test_list diff --git a/tests/json/test_list.hpp b/tests/json/test_list.hpp index d80efb65..956d36cc 100644 --- a/tests/json/test_list.hpp +++ b/tests/json/test_list.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_list { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->emplace_back(Person{.first_name = "Bart"}); - children->emplace_back(Person{.first_name = "Lisa"}); - children->emplace_back(Person{.first_name = "Maggie"}); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +namespace test_list{ + void test(); } -} // namespace test_list + \ No newline at end of file diff --git a/tests/json/test_literal.cpp b/tests/json/test_literal.cpp new file mode 100644 index 00000000..c695c552 --- /dev/null +++ b/tests/json/test_literal.cpp @@ -0,0 +1,32 @@ +#include "test_literal.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal { + +using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; +using LastName = rfl::Literal<"Simpson">; + +struct Person { + rfl::Rename<"firstName", FirstName> first_name; + rfl::Rename<"lastName", LastName> last_name; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = FirstName::make<"Bart">()}; + + write_and_read(bart, + R"({"firstName":"Bart","lastName":"Simpson","children":[]})"); +} +} // namespace test_literal diff --git a/tests/json/test_literal.hpp b/tests/json/test_literal.hpp index 2a666633..ccd500ef 100644 --- a/tests/json/test_literal.hpp +++ b/tests/json/test_literal.hpp @@ -1,30 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_literal { - -using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; -using LastName = rfl::Literal<"Simpson">; - -struct Person { - rfl::Rename<"firstName", FirstName> first_name; - rfl::Rename<"lastName", LastName> last_name; - std::vector children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = FirstName::make<"Bart">()}; - - write_and_read(bart, - R"({"firstName":"Bart","lastName":"Simpson","children":[]})"); +namespace test_literal{ + void test(); } -} // namespace test_literal + \ No newline at end of file diff --git a/tests/json/test_literal_map.cpp b/tests/json/test_literal_map.cpp new file mode 100644 index 00000000..5f6afa8f --- /dev/null +++ b/tests/json/test_literal_map.cpp @@ -0,0 +1,29 @@ +#include "test_literal_map.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal_map { + +using FieldName = rfl::Literal<"firstName", "lastName">; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert(std::make_pair(FieldName::make<"firstName">(), + std::make_unique("Homer"))); + homer.insert(std::make_pair(FieldName::make<"lastName">(), + std::make_unique("Simpson"))); + + write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})"); +} +} // namespace test_literal_map diff --git a/tests/json/test_literal_map.hpp b/tests/json/test_literal_map.hpp index 72281f50..cc05d0c8 100644 --- a/tests/json/test_literal_map.hpp +++ b/tests/json/test_literal_map.hpp @@ -1,27 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_literal_map { - -using FieldName = rfl::Literal<"firstName", "lastName">; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - std::map> homer; - homer.insert(std::make_pair(FieldName::make<"firstName">(), - std::make_unique("Homer"))); - homer.insert(std::make_pair(FieldName::make<"lastName">(), - std::make_unique("Simpson"))); - - write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})"); +namespace test_literal_map{ + void test(); } -} // namespace test_literal_map + \ No newline at end of file diff --git a/tests/json/test_literal_unordered_map.cpp b/tests/json/test_literal_unordered_map.cpp new file mode 100644 index 00000000..9dfb6334 --- /dev/null +++ b/tests/json/test_literal_unordered_map.cpp @@ -0,0 +1,37 @@ +#include "test_literal_unordered_map.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal_unordered_map { + +using FieldName = rfl::Literal<"firstName", "lastName">; + +using Map = std::unordered_map>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + Map homer; + + homer.insert(std::make_pair(FieldName::make<"firstName">(), + std::make_unique("Homer"))); + homer.insert(std::make_pair(FieldName::make<"lastName">(), + std::make_unique("Simpson"))); + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_literal_unordered_map diff --git a/tests/json/test_literal_unordered_map.hpp b/tests/json/test_literal_unordered_map.hpp index 8a41e494..870a82c3 100644 --- a/tests/json/test_literal_unordered_map.hpp +++ b/tests/json/test_literal_unordered_map.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_literal_unordered_map { - -using FieldName = rfl::Literal<"firstName", "lastName">; - -using Map = std::unordered_map>; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - Map homer; - - homer.insert(std::make_pair(FieldName::make<"firstName">(), - std::make_unique("Homer"))); - homer.insert(std::make_pair(FieldName::make<"lastName">(), - std::make_unique("Simpson"))); - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::json::write(homer); - const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_literal_unordered_map{ + void test(); } -} // namespace test_literal_unordered_map + \ No newline at end of file diff --git a/tests/json/test_map.cpp b/tests/json/test_map.cpp new file mode 100644 index 00000000..9f4a619d --- /dev/null +++ b/tests/json/test_map.cpp @@ -0,0 +1,35 @@ +#include "test_map.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})"); +} +} // namespace test_map diff --git a/tests/json/test_map.hpp b/tests/json/test_map.hpp index bb96439c..9ae49728 100644 --- a/tests/json/test_map.hpp +++ b/tests/json/test_map.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_map { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})"); +namespace test_map{ + void test(); } -} // namespace test_map + \ No newline at end of file diff --git a/tests/json/test_map_with_key_validation.cpp b/tests/json/test_map_with_key_validation.cpp new file mode 100644 index 00000000..994cfddc --- /dev/null +++ b/tests/json/test_map_with_key_validation.cpp @@ -0,0 +1,38 @@ +#include "test_map_with_key_validation.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map_with_key_validation { + +using Between1and3 = rfl::Validator, rfl::Maximum<3>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})"); +} +} // namespace test_map_with_key_validation diff --git a/tests/json/test_map_with_key_validation.hpp b/tests/json/test_map_with_key_validation.hpp index ddd46c38..1372f926 100644 --- a/tests/json/test_map_with_key_validation.hpp +++ b/tests/json/test_map_with_key_validation.hpp @@ -1,36 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_map_with_key_validation { - -using Between1and3 = rfl::Validator, rfl::Maximum<3>>; - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})"); +namespace test_map_with_key_validation{ + void test(); } -} // namespace test_map_with_key_validation + \ No newline at end of file diff --git a/tests/json/test_monster_example.cpp b/tests/json/test_monster_example.cpp new file mode 100644 index 00000000..8887e249 --- /dev/null +++ b/tests/json/test_monster_example.cpp @@ -0,0 +1,68 @@ +#include "test_monster_example.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_monster_example { + +using Color = rfl::Literal<"Red", "Green", "Blue">; + +struct Weapon { + std::string name; + short damage; +}; + +using Equipment = rfl::Variant>; + +struct Vec3 { + float x; + float y; + float z; +}; + +struct Monster { + Vec3 pos; + short mana = 150; + short hp = 100; + std::string name; + bool friendly = false; + std::vector inventory; + Color color = Color::make<"Blue">(); + std::vector weapons; + Equipment equipped; + std::vector path; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto sword = Weapon{.name = "Sword", .damage = 3}; + const auto axe = Weapon{.name = "Axe", .damage = 5}; + + const auto weapons = std::vector({sword, axe}); + + const auto position = Vec3{1.0f, 2.0f, 3.0f}; + + const auto inventory = + std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + + const auto orc = Monster{.pos = position, + .mana = 150, + .hp = 80, + .name = "MyMonster", + .inventory = inventory, + .color = Color::make<"Red">(), + .weapons = weapons, + .equipped = rfl::make_field<"weapon">(axe)}; + + write_and_read( + orc, + R"({"pos":{"x":1.0,"y":2.0,"z":3.0},"mana":150,"hp":80,"name":"MyMonster","friendly":false,"inventory":[0,1,2,3,4,5,6,7,8,9],"color":"Red","weapons":[{"name":"Sword","damage":3},{"name":"Axe","damage":5}],"equipped":{"weapon":{"name":"Axe","damage":5}},"path":[]})"); +} +} // namespace test_monster_example diff --git a/tests/json/test_monster_example.hpp b/tests/json/test_monster_example.hpp index 85d5a758..f2d959fc 100644 --- a/tests/json/test_monster_example.hpp +++ b/tests/json/test_monster_example.hpp @@ -1,66 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_monster_example { - -using Color = rfl::Literal<"Red", "Green", "Blue">; - -struct Weapon { - std::string name; - short damage; -}; - -using Equipment = rfl::Variant>; - -struct Vec3 { - float x; - float y; - float z; -}; - -struct Monster { - Vec3 pos; - short mana = 150; - short hp = 100; - std::string name; - bool friendly = false; - std::vector inventory; - Color color = Color::make<"Blue">(); - std::vector weapons; - Equipment equipped; - std::vector path; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto sword = Weapon{.name = "Sword", .damage = 3}; - const auto axe = Weapon{.name = "Axe", .damage = 5}; - - const auto weapons = std::vector({sword, axe}); - - const auto position = Vec3{1.0f, 2.0f, 3.0f}; - - const auto inventory = - std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); - - const auto orc = Monster{.pos = position, - .mana = 150, - .hp = 80, - .name = "MyMonster", - .inventory = inventory, - .color = Color::make<"Red">(), - .weapons = weapons, - .equipped = rfl::make_field<"weapon">(axe)}; - - write_and_read( - orc, - R"({"pos":{"x":1.0,"y":2.0,"z":3.0},"mana":150,"hp":80,"name":"MyMonster","friendly":false,"inventory":[0,1,2,3,4,5,6,7,8,9],"color":"Red","weapons":[{"name":"Sword","damage":3},{"name":"Axe","damage":5}],"equipped":{"weapon":{"name":"Axe","damage":5}},"path":[]})"); +namespace test_monster_example{ + void test(); } -} // namespace test_monster_example + \ No newline at end of file diff --git a/tests/json/test_move_replace.cpp b/tests/json/test_move_replace.cpp new file mode 100644 index 00000000..e4e15ba2 --- /dev/null +++ b/tests/json/test_move_replace.cpp @@ -0,0 +1,34 @@ +#include "test_move_replace.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_move_replace { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"children", rfl::Box>> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .children = rfl::make_box>()}; + + const auto maggie = rfl::replace( + std::move(lisa), rfl::make_field<"firstName">(std::string("Maggie"))); + + write_and_read( + maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})"); +} +} // namespace test_move_replace diff --git a/tests/json/test_move_replace.hpp b/tests/json/test_move_replace.hpp index fae44c5c..07704631 100644 --- a/tests/json/test_move_replace.hpp +++ b/tests/json/test_move_replace.hpp @@ -1,32 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_move_replace { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - rfl::Box> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto lisa = Person{.first_name = "Lisa", - .last_name = "Simpson", - .children = rfl::make_box>()}; - - const auto maggie = - rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie"))); - - write_and_read( - maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})"); +namespace test_move_replace{ + void test(); } -} // namespace test_move_replace + \ No newline at end of file diff --git a/tests/json/test_multimap.cpp b/tests/json/test_multimap.cpp new file mode 100644 index 00000000..cb74d75b --- /dev/null +++ b/tests/json/test_multimap.cpp @@ -0,0 +1,35 @@ +#include "test_multimap.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_multimap { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[[1,{"firstName":"Bart","lastName":"Simpson"}],[2,{"firstName":"Lisa","lastName":"Simpson"}],[3,{"firstName":"Maggie","lastName":"Simpson"}]]})"); +} +} // namespace test_multimap diff --git a/tests/json/test_multimap.hpp b/tests/json/test_multimap.hpp index 0f875fd4..81845572 100644 --- a/tests/json/test_multimap.hpp +++ b/tests/json/test_multimap.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_multimap { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":[[1,{"firstName":"Bart","lastName":"Simpson"}],[2,{"firstName":"Lisa","lastName":"Simpson"}],[3,{"firstName":"Maggie","lastName":"Simpson"}]]})"); +namespace test_multimap{ + void test(); } -} // namespace test_multimap + \ No newline at end of file diff --git a/tests/json/test_multiset.cpp b/tests/json/test_multiset.cpp new file mode 100644 index 00000000..923144f0 --- /dev/null +++ b/tests/json/test_multiset.cpp @@ -0,0 +1,33 @@ +#include "test_multiset.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_multiset { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::multiset({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})"); +} +} // namespace test_multiset diff --git a/tests/json/test_multiset.hpp b/tests/json/test_multiset.hpp index 1335ee02..adfcf186 100644 --- a/tests/json/test_multiset.hpp +++ b/tests/json/test_multiset.hpp @@ -1,31 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_multiset { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::multiset({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})"); +namespace test_multiset{ + void test(); } -} // namespace test_multiset + \ No newline at end of file diff --git a/tests/json/test_one_of.cpp b/tests/json/test_one_of.cpp new file mode 100644 index 00000000..d554d37d --- /dev/null +++ b/tests/json/test_one_of.cpp @@ -0,0 +1,34 @@ +#include "test_one_of.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_one_of { + +using Age = + rfl::Validator, rfl::Maximum<10>>, + rfl::AllOf, rfl::Maximum<130>>>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + Age age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer, + R"({"firstName":"Homer","lastName":"Simpson","age":45})"); +} +} // namespace test_one_of diff --git a/tests/json/test_one_of.hpp b/tests/json/test_one_of.hpp index 51be48c7..47b4e262 100644 --- a/tests/json/test_one_of.hpp +++ b/tests/json/test_one_of.hpp @@ -1,32 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_one_of { - -using Age = - rfl::Validator, rfl::Maximum<10>>, - rfl::AllOf, rfl::Maximum<130>>>>; - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - Age age; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto homer = - Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; - - write_and_read(homer, - R"({"firstName":"Homer","lastName":"Simpson","age":45})"); +namespace test_one_of{ + void test(); } -} // namespace test_one_of + \ No newline at end of file diff --git a/tests/json/test_optional_fields.cpp b/tests/json/test_optional_fields.cpp new file mode 100644 index 00000000..dfedddbf --- /dev/null +++ b/tests/json/test_optional_fields.cpp @@ -0,0 +1,37 @@ +#include "test_optional_fields.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_optional_fields { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + rfl::Rename<"children", std::optional>> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart"}; + + const auto lisa = Person{.first_name = "Lisa"}; + + const auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = + Person{.first_name = "Homer", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} +} // namespace test_optional_fields diff --git a/tests/json/test_optional_fields.hpp b/tests/json/test_optional_fields.hpp index 3d93bfe5..640cab95 100644 --- a/tests/json/test_optional_fields.hpp +++ b/tests/json/test_optional_fields.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_optional_fields { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - rfl::Rename<"children", std::optional>> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart"}; - - const auto lisa = Person{.first_name = "Lisa"}; - - const auto maggie = Person{.first_name = "Maggie"}; - - const auto homer = - Person{.first_name = "Homer", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +namespace test_optional_fields{ + void test(); } -} // namespace test_optional_fields + \ No newline at end of file diff --git a/tests/json/test_readme_example.cpp b/tests/json/test_readme_example.cpp new file mode 100644 index 00000000..3eef7acc --- /dev/null +++ b/tests/json/test_readme_example.cpp @@ -0,0 +1,55 @@ +#include "test_readme_example.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_readme_example { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::string town = "Springfield"; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = + Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"firstName":"Bart","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"firstName":"Lisa","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"firstName":"Maggie","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})"); +} +} // namespace test_readme_example diff --git a/tests/json/test_readme_example.hpp b/tests/json/test_readme_example.hpp index 649e9077..68c6cf81 100644 --- a/tests/json/test_readme_example.hpp +++ b/tests/json/test_readme_example.hpp @@ -1,53 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_readme_example { - -using Age = rfl::Validator, rfl::Maximum<130>>; - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::string town = "Springfield"; - rfl::Timestamp<"%Y-%m-%d"> birthday; - Age age; - rfl::Email email; - std::vector children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart", - .birthday = "1987-04-19", - .age = 10, - .email = "bart@simpson.com"}; - - const auto lisa = Person{.first_name = "Lisa", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com"}; - - const auto maggie = Person{.first_name = "Maggie", - .birthday = "1987-04-19", - .age = 0, - .email = "maggie@simpson.com"}; - - const auto homer = - Person{.first_name = "Homer", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"firstName":"Bart","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"firstName":"Lisa","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"firstName":"Maggie","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})"); +namespace test_readme_example{ + void test(); } -} // namespace test_readme_example + \ No newline at end of file diff --git a/tests/json/test_ref.cpp b/tests/json/test_ref.cpp new file mode 100644 index 00000000..e3d3d23c --- /dev/null +++ b/tests/json/test_ref.cpp @@ -0,0 +1,51 @@ +#include "test_ref.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_ref { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Ref lesser; + rfl::Ref greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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(DecisionTree{leaf1}), + .greater = rfl::make_ref(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read( + tree, + R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})"); +} +} // namespace test_ref diff --git a/tests/json/test_ref.hpp b/tests/json/test_ref.hpp index b85e4d2b..d289ba09 100644 --- a/tests/json/test_ref.hpp +++ b/tests/json/test_ref.hpp @@ -1,49 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_ref { - -struct DecisionTree { - struct Leaf { - using Tag = rfl::Literal<"Leaf">; - double value; - }; - - struct Node { - using Tag = rfl::Literal<"Node">; - rfl::Rename<"criticalValue", double> critical_value; - rfl::Ref lesser; - rfl::Ref greater; - }; - - using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; - - rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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(DecisionTree{leaf1}), - .greater = rfl::make_ref(DecisionTree{leaf2})}; - - const DecisionTree tree{.leaf_or_node = std::move(node)}; - - write_and_read( - tree, - R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})"); +namespace test_ref{ + void test(); } -} // namespace test_ref + \ No newline at end of file diff --git a/tests/json/test_replace.cpp b/tests/json/test_replace.cpp new file mode 100644 index 00000000..7c555e8d --- /dev/null +++ b/tests/json/test_replace.cpp @@ -0,0 +1,34 @@ +#include "test_replace.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_replace { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"children", rfl::Box>> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .children = rfl::make_box>()}; + + const auto maggie = rfl::replace( + std::move(lisa), rfl::make_field<"firstName">(std::string("Maggie"))); + + write_and_read( + maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})"); +} +} // namespace test_replace diff --git a/tests/json/test_replace.hpp b/tests/json/test_replace.hpp index d69eca37..9d421218 100644 --- a/tests/json/test_replace.hpp +++ b/tests/json/test_replace.hpp @@ -1,32 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_replace { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"children", rfl::Box>> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto lisa = Person{.first_name = "Lisa", - .last_name = "Simpson", - .children = rfl::make_box>()}; - - const auto maggie = rfl::replace( - std::move(lisa), rfl::make_field<"firstName">(std::string("Maggie"))); - - write_and_read( - maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})"); +namespace test_replace{ + void test(); } -} // namespace test_replace + \ No newline at end of file diff --git a/tests/json/test_replace_flatten.cpp b/tests/json/test_replace_flatten.cpp new file mode 100644 index 00000000..e57e8b88 --- /dev/null +++ b/tests/json/test_replace_flatten.cpp @@ -0,0 +1,45 @@ +#include "test_replace_flatten.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_replace_flatten { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", rfl::Box> employer; + rfl::Field<"salary", float> salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + auto employee2 = + rfl::replace(std::move(employee), rfl::make_field<"salary">(70000.0), + rfl::make_field<"age">(46)); + + write_and_read( + employee2, + R"({"firstName":"Homer","lastName":"Simpson","age":46,"employer":"Mr. Burns","salary":70000.0})"); +} +} // namespace test_replace_flatten diff --git a/tests/json/test_replace_flatten.hpp b/tests/json/test_replace_flatten.hpp index 9f22f8a3..5bc5f716 100644 --- a/tests/json/test_replace_flatten.hpp +++ b/tests/json/test_replace_flatten.hpp @@ -1,43 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_replace_flatten { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", rfl::Box> last_name; - rfl::Field<"age", int> age; -}; - -struct Employee { - rfl::Flatten person; - rfl::Field<"employer", rfl::Box> employer; - rfl::Field<"salary", float> salary; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto employee = Employee{ - .person = Person{.first_name = "Homer", - .last_name = rfl::make_box("Simpson"), - .age = 45}, - .employer = rfl::make_box("Mr. Burns"), - .salary = 60000.0}; - - auto employee2 = - rfl::replace(std::move(employee), rfl::make_field<"salary">(70000.0), - rfl::make_field<"age">(46)); - - write_and_read( - employee2, - R"({"firstName":"Homer","lastName":"Simpson","age":46,"employer":"Mr. Burns","salary":70000.0})"); +namespace test_replace_flatten{ + void test(); } -} // namespace test_replace_flatten + \ No newline at end of file diff --git a/tests/json/test_replace_with_other_struct.cpp b/tests/json/test_replace_with_other_struct.cpp new file mode 100644 index 00000000..eb9e8f78 --- /dev/null +++ b/tests/json/test_replace_with_other_struct.cpp @@ -0,0 +1,44 @@ +#include "test_replace_with_other_struct.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_replace_with_other_struct { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", std::string> employer; + rfl::Field<"salary", float> salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}, + .employer = std::string("Mr. Burns"), + .salary = 60000.0}; + + const auto carl = Person{.first_name = "Carl", .last_name = "", .age = 45}; + + const auto employee2 = rfl::replace(employee, carl); + + write_and_read( + employee2, + R"({"firstName":"Carl","lastName":"","age":45,"employer":"Mr. Burns","salary":60000.0})"); +} +} // namespace test_replace_with_other_struct diff --git a/tests/json/test_replace_with_other_struct.hpp b/tests/json/test_replace_with_other_struct.hpp index 8f946a3f..1315603a 100644 --- a/tests/json/test_replace_with_other_struct.hpp +++ b/tests/json/test_replace_with_other_struct.hpp @@ -1,42 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_replace_with_other_struct { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"age", int> age; -}; - -struct Employee { - rfl::Flatten person; - rfl::Field<"employer", std::string> employer; - rfl::Field<"salary", float> salary; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto employee = Employee{ - .person = - Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}, - .employer = std::string("Mr. Burns"), - .salary = 60000.0}; - - const auto carl = Person{.first_name = "Carl", .last_name = "", .age = 45}; - - const auto employee2 = rfl::replace(employee, carl); - - write_and_read( - employee2, - R"({"firstName":"Carl","lastName":"","age":45,"employer":"Mr. Burns","salary":60000.0})"); +namespace test_replace_with_other_struct{ + void test(); } -} // namespace test_replace_with_other_struct + \ No newline at end of file diff --git a/tests/json/test_result.cpp b/tests/json/test_result.cpp new file mode 100644 index 00000000..1db7f4c7 --- /dev/null +++ b/tests/json/test_result.cpp @@ -0,0 +1,37 @@ +#include "test_result.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_result { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const rfl::Result bart = Person{.first_name = "Bart"}; + + const rfl::Result lisa = Person{.first_name = "Lisa"}; + + const rfl::Result maggie = rfl::Error("Some error occurred."); + + const auto homer = Person{ + .first_name = "Homer", + .children = std::vector>({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"error":"Some error occurred."}]})"); +} +} // namespace test_result diff --git a/tests/json/test_result.hpp b/tests/json/test_result.hpp index dddc6576..170943c5 100644 --- a/tests/json/test_result.hpp +++ b/tests/json/test_result.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_result { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::vector> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const rfl::Result bart = Person{.first_name = "Bart"}; - - const rfl::Result lisa = Person{.first_name = "Lisa"}; - - const rfl::Result maggie = rfl::Error("Some error occurred."); - - const auto homer = Person{ - .first_name = "Homer", - .children = std::vector>({bart, lisa, maggie})}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"error":"Some error occurred."}]})"); +namespace test_result{ + void test(); } -} // namespace test_result + \ No newline at end of file diff --git a/tests/json/test_save_load.cpp b/tests/json/test_save_load.cpp new file mode 100644 index 00000000..43f36a70 --- /dev/null +++ b/tests/json/test_save_load.cpp @@ -0,0 +1,70 @@ +#include "test_save_load.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace test_save_load { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer1 = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + rfl::json::save("homer.json", homer1); + + const auto homer2 = rfl::json::load("homer.json").value(); + + const auto string1 = rfl::json::write(homer1); + const auto string2 = rfl::json::write(homer2); + + if (string1 != string2) { + std::cout << "Test failed. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_save_load diff --git a/tests/json/test_save_load.hpp b/tests/json/test_save_load.hpp index 72130d94..a8829fd1 100644 --- a/tests/json/test_save_load.hpp +++ b/tests/json/test_save_load.hpp @@ -1,68 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -namespace test_save_load { - -using Age = rfl::Validator, rfl::Maximum<130>>>; - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - rfl::Timestamp<"%Y-%m-%d"> birthday; - Age age; - rfl::Email email; - std::vector children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 10, - .email = "bart@simpson.com", - .children = std::vector()}; - - const auto lisa = Person{.first_name = "Lisa", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com"}; - - const auto maggie = Person{.first_name = "Maggie", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 0, - .email = "maggie@simpson.com"}; - - const auto homer1 = - Person{.first_name = "Homer", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .children = std::vector({bart, lisa, maggie})}; - - rfl::json::save("homer.json", homer1); - - const auto homer2 = rfl::json::load("homer.json").value(); - - const auto string1 = rfl::json::write(homer1); - const auto string2 = rfl::json::write(homer2); - - if (string1 != string2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; +namespace test_save_load{ + void test(); } -} // namespace test_save_load + \ No newline at end of file diff --git a/tests/json/test_set.cpp b/tests/json/test_set.cpp new file mode 100644 index 00000000..19600193 --- /dev/null +++ b/tests/json/test_set.cpp @@ -0,0 +1,33 @@ +#include "test_set.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_set { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})"); +} +} // namespace test_set diff --git a/tests/json/test_set.hpp b/tests/json/test_set.hpp index 572f3f73..142a663b 100644 --- a/tests/json/test_set.hpp +++ b/tests/json/test_set.hpp @@ -1,31 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_set { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::set({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})"); +namespace test_set{ + void test(); } -} // namespace test_set + \ No newline at end of file diff --git a/tests/json/test_size.cpp b/tests/json/test_size.cpp new file mode 100644 index 00000000..f4935f9d --- /dev/null +++ b/tests/json/test_size.cpp @@ -0,0 +1,45 @@ +#include "test_size.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_size { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + rfl::Validator, + rfl::Size, rfl::EqualTo<3>>>> + children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{ + .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto lisa = Person{ + .first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto maggie = Person{ + .first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19","children":[{"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Lisa","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Maggie","lastName":"Simpson","birthday":"1987-04-19","children":[]}]})"); +} +} // namespace test_size diff --git a/tests/json/test_size.hpp b/tests/json/test_size.hpp index cc5c9c3b..be330df0 100644 --- a/tests/json/test_size.hpp +++ b/tests/json/test_size.hpp @@ -1,43 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_size { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name; - rfl::Timestamp<"%Y-%m-%d"> birthday; - rfl::Validator, - rfl::Size, rfl::EqualTo<3>>>> - children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{ - .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; - - const auto lisa = Person{ - .first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"}; - - const auto maggie = Person{ - .first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"}; - - const auto homer = - Person{.first_name = "Homer", - .last_name = "Simpson", - .birthday = "1987-04-19", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19","children":[{"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Lisa","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Maggie","lastName":"Simpson","birthday":"1987-04-19","children":[]}]})"); +namespace test_size{ + void test(); } -} // namespace test_size + \ No newline at end of file diff --git a/tests/json/test_string_map.cpp b/tests/json/test_string_map.cpp new file mode 100644 index 00000000..41dc61d9 --- /dev/null +++ b/tests/json/test_string_map.cpp @@ -0,0 +1,25 @@ +#include "test_string_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_string_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})"); +} +} // namespace test_string_map diff --git a/tests/json/test_string_map.hpp b/tests/json/test_string_map.hpp index 628c5f7d..94cb975a 100644 --- a/tests/json/test_string_map.hpp +++ b/tests/json/test_string_map.hpp @@ -1,23 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - std::map> homer; - homer.insert( - std::make_pair("firstName", std::make_unique("Homer"))); - homer.insert( - std::make_pair("lastName", std::make_unique("Simpson"))); - - write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})"); +namespace test_string_map{ + void test(); } -} // namespace test_string_map + \ No newline at end of file diff --git a/tests/json/test_string_unordered_map.cpp b/tests/json/test_string_unordered_map.cpp new file mode 100644 index 00000000..69a72829 --- /dev/null +++ b/tests/json/test_string_unordered_map.cpp @@ -0,0 +1,30 @@ +#include "test_string_unordered_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_string_unordered_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + using Map = std::unordered_map>; + Map homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_string_unordered_map diff --git a/tests/json/test_string_unordered_map.hpp b/tests/json/test_string_unordered_map.hpp index d3796093..268057a3 100644 --- a/tests/json/test_string_unordered_map.hpp +++ b/tests/json/test_string_unordered_map.hpp @@ -1,28 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_string_unordered_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Map = std::unordered_map>; - Map homer; - homer.insert( - std::make_pair("firstName", std::make_unique("Homer"))); - homer.insert( - std::make_pair("lastName", std::make_unique("Simpson"))); - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::json::write(homer); - const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_string_unordered_map{ + void test(); } -} // namespace test_string_unordered_map + \ No newline at end of file diff --git a/tests/json/test_tagged_union.cpp b/tests/json/test_tagged_union.cpp new file mode 100644 index 00000000..2727e443 --- /dev/null +++ b/tests/json/test_tagged_union.cpp @@ -0,0 +1,37 @@ +#include "test_tagged_union.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r, R"({"shape":"Rectangle","height":10.0,"width":5.0})"); +} +} // namespace test_tagged_union diff --git a/tests/json/test_tagged_union.hpp b/tests/json/test_tagged_union.hpp index c8cf5f78..5d522ff9 100644 --- a/tests/json/test_tagged_union.hpp +++ b/tests/json/test_tagged_union.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_tagged_union { - -struct Circle { - double radius; -}; - -struct Rectangle { - double height; - double width; -}; - -struct Square { - double width; -}; - -using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Shapes r = Rectangle{.height = 10, .width = 5}; - - write_and_read(r, R"({"shape":"Rectangle","height":10.0,"width":5.0})"); +namespace test_tagged_union{ + void test(); } -} // namespace test_tagged_union + \ No newline at end of file diff --git a/tests/json/test_tagged_union2.cpp b/tests/json/test_tagged_union2.cpp new file mode 100644 index 00000000..0fa84d9f --- /dev/null +++ b/tests/json/test_tagged_union2.cpp @@ -0,0 +1,40 @@ +#include "test_tagged_union2.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union2 { + +struct Circle { + using Tag = rfl::Literal<"circle", "Circle">; + rfl::Field<"radius", double> radius; +}; + +struct Rectangle { + using Tag = rfl::Literal<"rectangle", "Rectangle">; + rfl::Field<"height", double> height; + rfl::Field<"width", double> width; +}; + +struct Square { + using Tag = rfl::Literal<"square", "Square">; + rfl::Field<"width", double> width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r, R"({"shape":"rectangle","height":10.0,"width":5.0})"); +} +} // namespace test_tagged_union2 diff --git a/tests/json/test_tagged_union2.hpp b/tests/json/test_tagged_union2.hpp index 597016c5..05bce109 100644 --- a/tests/json/test_tagged_union2.hpp +++ b/tests/json/test_tagged_union2.hpp @@ -1,38 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_tagged_union2 { - -struct Circle { - using Tag = rfl::Literal<"circle", "Circle">; - rfl::Field<"radius", double> radius; -}; - -struct Rectangle { - using Tag = rfl::Literal<"rectangle", "Rectangle">; - rfl::Field<"height", double> height; - rfl::Field<"width", double> width; -}; - -struct Square { - using Tag = rfl::Literal<"square", "Square">; - rfl::Field<"width", double> width; -}; - -using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Shapes r = Rectangle{.height = 10, .width = 5}; - - write_and_read(r, R"({"shape":"rectangle","height":10.0,"width":5.0})"); +namespace test_tagged_union2{ + void test(); } -} // namespace test_tagged_union2 + \ No newline at end of file diff --git a/tests/json/test_timestamp.cpp b/tests/json/test_timestamp.cpp new file mode 100644 index 00000000..1adfb911 --- /dev/null +++ b/tests/json/test_timestamp.cpp @@ -0,0 +1,39 @@ +#include "test_timestamp.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_timestamp { + +using TS = rfl::Timestamp<"%Y-%m-%d">; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + TS birthday; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto result = TS::from_string("nonsense"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; + + write_and_read( + bart, + R"({"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19"})"); +} +} // namespace test_timestamp diff --git a/tests/json/test_timestamp.hpp b/tests/json/test_timestamp.hpp index 62f5a0c6..891d89b9 100644 --- a/tests/json/test_timestamp.hpp +++ b/tests/json/test_timestamp.hpp @@ -1,37 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_timestamp { - -using TS = rfl::Timestamp<"%Y-%m-%d">; - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - TS birthday; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto result = TS::from_string("nonsense"); - - if (result) { - std::cout << "Failed: Expected an error, but got none." << std::endl; - return; - } - - const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; - - write_and_read( - bart, - R"({"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19"})"); +namespace test_timestamp{ + void test(); } -} // namespace test_timestamp + \ No newline at end of file diff --git a/tests/json/test_unique_ptr.cpp b/tests/json/test_unique_ptr.cpp new file mode 100644 index 00000000..e354862d --- /dev/null +++ b/tests/json/test_unique_ptr.cpp @@ -0,0 +1,36 @@ +#include "test_unique_ptr.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} +} // namespace test_unique_ptr diff --git a/tests/json/test_unique_ptr.hpp b/tests/json/test_unique_ptr.hpp index c36e2d92..428ea2a2 100644 --- a/tests/json/test_unique_ptr.hpp +++ b/tests/json/test_unique_ptr.hpp @@ -1,34 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unique_ptr { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->emplace_back(Person{.first_name = "Bart"}); - children->emplace_back(Person{.first_name = "Lisa"}); - children->emplace_back(Person{.first_name = "Maggie"}); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read( - homer, - R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +namespace test_unique_ptr{ + void test(); } -} // namespace test_unique_ptr + \ No newline at end of file diff --git a/tests/json/test_unique_ptr2.cpp b/tests/json/test_unique_ptr2.cpp new file mode 100644 index 00000000..4f62b94e --- /dev/null +++ b/tests/json/test_unique_ptr2.cpp @@ -0,0 +1,51 @@ +#include "test_unique_ptr2.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr2 { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + std::unique_ptr lesser; + std::unique_ptr greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = std::make_unique(DecisionTree{leaf1}), + .greater = std::make_unique(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read( + tree, + R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})"); +} +} // namespace test_unique_ptr2 diff --git a/tests/json/test_unique_ptr2.hpp b/tests/json/test_unique_ptr2.hpp index 085db6aa..74adc170 100644 --- a/tests/json/test_unique_ptr2.hpp +++ b/tests/json/test_unique_ptr2.hpp @@ -1,49 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unique_ptr2 { - -struct DecisionTree { - struct Leaf { - using Tag = rfl::Literal<"Leaf">; - double value; - }; - - struct Node { - using Tag = rfl::Literal<"Node">; - rfl::Rename<"criticalValue", double> critical_value; - std::unique_ptr lesser; - std::unique_ptr greater; - }; - - using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; - - rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto leaf1 = DecisionTree::Leaf{.value = 3.0}; - - auto leaf2 = DecisionTree::Leaf{.value = 5.0}; - - auto node = DecisionTree::Node{ - .critical_value = 10.0, - .lesser = std::make_unique(DecisionTree{leaf1}), - .greater = std::make_unique(DecisionTree{leaf2})}; - - const DecisionTree tree{.leaf_or_node = std::move(node)}; - - write_and_read( - tree, - R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})"); +namespace test_unique_ptr2{ + void test(); } -} // namespace test_unique_ptr2 + \ No newline at end of file diff --git a/tests/json/test_unordered_map.cpp b/tests/json/test_unordered_map.cpp new file mode 100644 index 00000000..8876dce8 --- /dev/null +++ b/tests/json/test_unordered_map.cpp @@ -0,0 +1,37 @@ +#include "test_unordered_map.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unordered_map { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_unordered_map diff --git a/tests/json/test_unordered_map.hpp b/tests/json/test_unordered_map.hpp index bc2fccd0..9b059a96 100644 --- a/tests/json/test_unordered_map.hpp +++ b/tests/json/test_unordered_map.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_map { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::json::write(homer); - const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_unordered_map{ + void test(); } -} // namespace test_unordered_map + \ No newline at end of file diff --git a/tests/json/test_unordered_multimap.cpp b/tests/json/test_unordered_multimap.cpp new file mode 100644 index 00000000..b9020abe --- /dev/null +++ b/tests/json/test_unordered_multimap.cpp @@ -0,0 +1,37 @@ +#include "test_unordered_multimap.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unordered_multimap { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_unordered_multimap diff --git a/tests/json/test_unordered_multimap.hpp b/tests/json/test_unordered_multimap.hpp index 7662d0ee..0f36f705 100644 --- a/tests/json/test_unordered_multimap.hpp +++ b/tests/json/test_unordered_multimap.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_multimap { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::json::write(homer); - const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_unordered_multimap{ + void test(); } -} // namespace test_unordered_multimap + \ No newline at end of file diff --git a/tests/json/test_unordered_multiset.cpp b/tests/json/test_unordered_multiset.cpp new file mode 100644 index 00000000..8cf957d7 --- /dev/null +++ b/tests/json/test_unordered_multiset.cpp @@ -0,0 +1,35 @@ +#include "test_unordered_multiset.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unordered_multiset { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::unordered_multiset({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered multisets are unpredictable. We therefore only make sure that + // this compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_unordered_multiset diff --git a/tests/json/test_unordered_multiset.hpp b/tests/json/test_unordered_multiset.hpp index 40ce4401..491ff7ba 100644 --- a/tests/json/test_unordered_multiset.hpp +++ b/tests/json/test_unordered_multiset.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_multiset { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::unordered_multiset({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered multisets are unpredictable. We therefore only make sure that - // this compiles. - const auto json_string = rfl::json::write(homer); - const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_unordered_multiset{ + void test(); } -} // namespace test_unordered_multiset + \ No newline at end of file diff --git a/tests/json/test_unordered_set.cpp b/tests/json/test_unordered_set.cpp new file mode 100644 index 00000000..0bfe06c5 --- /dev/null +++ b/tests/json/test_unordered_set.cpp @@ -0,0 +1,35 @@ +#include "test_unordered_set.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unordered_set { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::unordered_set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered sets are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_unordered_set diff --git a/tests/json/test_unordered_set.hpp b/tests/json/test_unordered_set.hpp index 0cea2752..be2b257f 100644 --- a/tests/json/test_unordered_set.hpp +++ b/tests/json/test_unordered_set.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_set { - -struct Person { - rfl::Rename<"firstName", std::string> first_name; - rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::unordered_set({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered sets are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::json::write(homer); - const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_unordered_set{ + void test(); } -} // namespace test_unordered_set + \ No newline at end of file diff --git a/tests/json/test_variant.cpp b/tests/json/test_variant.cpp new file mode 100644 index 00000000..18d6a056 --- /dev/null +++ b/tests/json/test_variant.cpp @@ -0,0 +1,37 @@ +#include "test_variant.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = std::variant>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r, R"({"height":10.0,"width":5.0})"); +} +} // namespace test_variant diff --git a/tests/json/test_variant.hpp b/tests/json/test_variant.hpp index 24bdabc1..0e58ce71 100644 --- a/tests/json/test_variant.hpp +++ b/tests/json/test_variant.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_variant { - -struct Circle { - double radius; -}; - -struct Rectangle { - double height; - double width; -}; - -struct Square { - double width; -}; - -using Shapes = std::variant>; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Shapes r = Rectangle{.height = 10, .width = 5}; - - write_and_read(r, R"({"height":10.0,"width":5.0})"); +namespace test_variant{ + void test(); } -} // namespace test_variant + \ No newline at end of file