diff --git a/tests/yaml/test_array.cpp b/tests/yaml/test_array.cpp new file mode 100644 index 00000000..a4586d3b --- /dev/null +++ b/tests/yaml/test_array.cpp @@ -0,0 +1,40 @@ +#include "test_array.hpp" + +#include +#include +#include +#include +#include +#include + +// Make sure things still compile when +// rfl.hpp is included after rfl/yaml.hpp. +#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); +} +} // namespace test_array diff --git a/tests/yaml/test_array.hpp b/tests/yaml/test_array.hpp new file mode 100644 index 00000000..502c3388 --- /dev/null +++ b/tests/yaml/test_array.hpp @@ -0,0 +1,4 @@ +namespace test_array{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_box.cpp b/tests/yaml/test_box.cpp new file mode 100644 index 00000000..04d88641 --- /dev/null +++ b/tests/yaml/test_box.cpp @@ -0,0 +1,50 @@ +#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); + +} +} // namespace test_box diff --git a/tests/yaml/test_box.hpp b/tests/yaml/test_box.hpp new file mode 100644 index 00000000..a564b9e1 --- /dev/null +++ b/tests/yaml/test_box.hpp @@ -0,0 +1,4 @@ +namespace test_box{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_custom_class1.cpp b/tests/yaml/test_custom_class1.cpp new file mode 100644 index 00000000..35f4a1da --- /dev/null +++ b/tests/yaml/test_custom_class1.cpp @@ -0,0 +1,42 @@ +#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); +} +} // namespace test_custom_class1 diff --git a/tests/yaml/test_custom_class1.hpp b/tests/yaml/test_custom_class1.hpp new file mode 100644 index 00000000..eafe6cd0 --- /dev/null +++ b/tests/yaml/test_custom_class1.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class1{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_custom_class3.cpp b/tests/yaml/test_custom_class3.cpp new file mode 100644 index 00000000..2ea105e4 --- /dev/null +++ b/tests/yaml/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); +} + +} // namespace test_custom_class3 diff --git a/tests/yaml/test_custom_class3.hpp b/tests/yaml/test_custom_class3.hpp new file mode 100644 index 00000000..9a6fdab4 --- /dev/null +++ b/tests/yaml/test_custom_class3.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class3{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_custom_class4.cpp b/tests/yaml/test_custom_class4.cpp new file mode 100644 index 00000000..e2d79a94 --- /dev/null +++ b/tests/yaml/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); +} +} // namespace test_custom_class4 diff --git a/tests/yaml/test_custom_class4.hpp b/tests/yaml/test_custom_class4.hpp new file mode 100644 index 00000000..2d3b151a --- /dev/null +++ b/tests/yaml/test_custom_class4.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class4{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_default_values.cpp b/tests/yaml/test_default_values.cpp new file mode 100644 index 00000000..f6af2352 --- /dev/null +++ b/tests/yaml/test_default_values.cpp @@ -0,0 +1,32 @@ +#include "test_default_values.hpp" + +#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); +} +} // namespace test_default_values diff --git a/tests/yaml/test_default_values.hpp b/tests/yaml/test_default_values.hpp new file mode 100644 index 00000000..c8f8360e --- /dev/null +++ b/tests/yaml/test_default_values.hpp @@ -0,0 +1,4 @@ +namespace test_default_values{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_deque.cpp b/tests/yaml/test_deque.cpp new file mode 100644 index 00000000..6dfa5803 --- /dev/null +++ b/tests/yaml/test_deque.cpp @@ -0,0 +1,33 @@ +#include "test_deque.hpp" + +#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); + +} +} // namespace test_deque diff --git a/tests/yaml/test_deque.hpp b/tests/yaml/test_deque.hpp new file mode 100644 index 00000000..6781e880 --- /dev/null +++ b/tests/yaml/test_deque.hpp @@ -0,0 +1,4 @@ +namespace test_deque{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_enum.cpp b/tests/yaml/test_enum.cpp new file mode 100644 index 00000000..d4d56a8b --- /dev/null +++ b/tests/yaml/test_enum.cpp @@ -0,0 +1,29 @@ +#include "test_enum.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum { + +enum class Color { red, green, blue, yellow }; + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = Circle{.radius = 2.0, .color = Color::green}; + + write_and_read(circle); +} + +} // namespace test_enum diff --git a/tests/yaml/test_enum.hpp b/tests/yaml/test_enum.hpp new file mode 100644 index 00000000..2e2e0b3d --- /dev/null +++ b/tests/yaml/test_enum.hpp @@ -0,0 +1,4 @@ +namespace test_enum { +void test(); +} + diff --git a/tests/yaml/test_field_variant.cpp b/tests/yaml/test_field_variant.cpp new file mode 100644 index 00000000..8b17665f --- /dev/null +++ b/tests/yaml/test_field_variant.cpp @@ -0,0 +1,39 @@ +#include "test_field_variant.hpp" + +#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); +} +} // namespace test_field_variant diff --git a/tests/yaml/test_field_variant.hpp b/tests/yaml/test_field_variant.hpp new file mode 100644 index 00000000..ba93e732 --- /dev/null +++ b/tests/yaml/test_field_variant.hpp @@ -0,0 +1,4 @@ +namespace test_field_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_flag_enum.cpp b/tests/yaml/test_flag_enum.cpp new file mode 100644 index 00000000..1d3f76af --- /dev/null +++ b/tests/yaml/test_flag_enum.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include + +#include "test_enum.hpp" +#include "write_and_read.hpp" + +namespace test_flag_enum { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = + Circle{.radius = 2.0, .color = Color::blue | Color::orange}; + + write_and_read(circle); +} + +} // namespace test_flag_enum diff --git a/tests/yaml/test_flag_enum.hpp b/tests/yaml/test_flag_enum.hpp new file mode 100644 index 00000000..2f4dc7a0 --- /dev/null +++ b/tests/yaml/test_flag_enum.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum { +void test(); +} + diff --git a/tests/yaml/test_flag_enum_with_int.cpp b/tests/yaml/test_flag_enum_with_int.cpp new file mode 100644 index 00000000..4c7deefe --- /dev/null +++ b/tests/yaml/test_flag_enum_with_int.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +#include "test_enum.hpp" +#include "write_and_read.hpp" + +namespace test_flag_enum_with_int { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; + + write_and_read(circle); +} + +} // namespace test_flag_enum_with_int diff --git a/tests/yaml/test_flag_enum_with_int.hpp b/tests/yaml/test_flag_enum_with_int.hpp new file mode 100644 index 00000000..a7512b60 --- /dev/null +++ b/tests/yaml/test_flag_enum_with_int.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum_with_int { +void test(); +} + diff --git a/tests/yaml/test_flatten.cpp b/tests/yaml/test_flatten.cpp new file mode 100644 index 00000000..c7f1d9be --- /dev/null +++ b/tests/yaml/test_flatten.cpp @@ -0,0 +1,38 @@ +#include "test_flatten.hpp" + +#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); +} +} // namespace test_flatten diff --git a/tests/yaml/test_flatten.hpp b/tests/yaml/test_flatten.hpp new file mode 100644 index 00000000..24d60e11 --- /dev/null +++ b/tests/yaml/test_flatten.hpp @@ -0,0 +1,4 @@ +namespace test_flatten{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_flatten_anonymous.cpp b/tests/yaml/test_flatten_anonymous.cpp new file mode 100644 index 00000000..05e61464 --- /dev/null +++ b/tests/yaml/test_flatten_anonymous.cpp @@ -0,0 +1,39 @@ +#include "test_flatten_anonymous.hpp" + +#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); +} + +} // namespace test_flatten_anonymous diff --git a/tests/yaml/test_flatten_anonymous.hpp b/tests/yaml/test_flatten_anonymous.hpp new file mode 100644 index 00000000..7ffa2785 --- /dev/null +++ b/tests/yaml/test_flatten_anonymous.hpp @@ -0,0 +1,4 @@ +namespace test_flatten_anonymous{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_forward_list.cpp b/tests/yaml/test_forward_list.cpp new file mode 100644 index 00000000..dd8d9e56 --- /dev/null +++ b/tests/yaml/test_forward_list.cpp @@ -0,0 +1,33 @@ +#include "test_forward_list.hpp" + +#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); + +} +} // namespace test_forward_list diff --git a/tests/yaml/test_forward_list.hpp b/tests/yaml/test_forward_list.hpp new file mode 100644 index 00000000..9784a0c4 --- /dev/null +++ b/tests/yaml/test_forward_list.hpp @@ -0,0 +1,4 @@ +namespace test_forward_list{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_literal.cpp b/tests/yaml/test_literal.cpp new file mode 100644 index 00000000..00578178 --- /dev/null +++ b/tests/yaml/test_literal.cpp @@ -0,0 +1,30 @@ +#include "test_literal.hpp" + +#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); +} +} // namespace test_literal diff --git a/tests/yaml/test_literal.hpp b/tests/yaml/test_literal.hpp new file mode 100644 index 00000000..ccd500ef --- /dev/null +++ b/tests/yaml/test_literal.hpp @@ -0,0 +1,4 @@ +namespace test_literal{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_literal_map.cpp b/tests/yaml/test_literal_map.cpp new file mode 100644 index 00000000..9bbebafb --- /dev/null +++ b/tests/yaml/test_literal_map.cpp @@ -0,0 +1,28 @@ +#include "test_literal_map.hpp" + +#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); +} +} // namespace test_literal_map diff --git a/tests/yaml/test_literal_map.hpp b/tests/yaml/test_literal_map.hpp new file mode 100644 index 00000000..cc05d0c8 --- /dev/null +++ b/tests/yaml/test_literal_map.hpp @@ -0,0 +1,4 @@ +namespace test_literal_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_map.cpp b/tests/yaml/test_map.cpp new file mode 100644 index 00000000..e37ce564 --- /dev/null +++ b/tests/yaml/test_map.cpp @@ -0,0 +1,32 @@ +#include "test_map.hpp" + +#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::map children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::map(); + children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); + children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); + children.insert(std::make_pair("child3", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map diff --git a/tests/yaml/test_map.hpp b/tests/yaml/test_map.hpp new file mode 100644 index 00000000..9ae49728 --- /dev/null +++ b/tests/yaml/test_map.hpp @@ -0,0 +1,4 @@ +namespace test_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_map_with_key_validation.cpp b/tests/yaml/test_map_with_key_validation.cpp new file mode 100644 index 00000000..e758219d --- /dev/null +++ b/tests/yaml/test_map_with_key_validation.cpp @@ -0,0 +1,33 @@ +#include "test_map_with_key_validation.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map_with_key_validation { + +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("Bart", Person{.first_name = "Bart"})); + children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"})); + children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map_with_key_validation diff --git a/tests/yaml/test_map_with_key_validation.hpp b/tests/yaml/test_map_with_key_validation.hpp new file mode 100644 index 00000000..1372f926 --- /dev/null +++ b/tests/yaml/test_map_with_key_validation.hpp @@ -0,0 +1,4 @@ +namespace test_map_with_key_validation{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_monster_example.cpp b/tests/yaml/test_monster_example.cpp new file mode 100644 index 00000000..0b9f0512 --- /dev/null +++ b/tests/yaml/test_monster_example.cpp @@ -0,0 +1,66 @@ +#include "test_monster_example.hpp" + +#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); + +} +} // namespace test_monster_example diff --git a/tests/yaml/test_monster_example.hpp b/tests/yaml/test_monster_example.hpp new file mode 100644 index 00000000..f2d959fc --- /dev/null +++ b/tests/yaml/test_monster_example.hpp @@ -0,0 +1,4 @@ +namespace test_monster_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_readme_example.cpp b/tests/yaml/test_readme_example.cpp new file mode 100644 index 00000000..4e6f8ee0 --- /dev/null +++ b/tests/yaml/test_readme_example.cpp @@ -0,0 +1,51 @@ +#include "test_readme_example.hpp" + +#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 child; +}; + +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", + .child = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_readme_example diff --git a/tests/yaml/test_readme_example.hpp b/tests/yaml/test_readme_example.hpp new file mode 100644 index 00000000..68c6cf81 --- /dev/null +++ b/tests/yaml/test_readme_example.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_ref.cpp b/tests/yaml/test_ref.cpp new file mode 100644 index 00000000..32a32e6c --- /dev/null +++ b/tests/yaml/test_ref.cpp @@ -0,0 +1,49 @@ +#include "test_ref.hpp" + +#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); + +} +} // namespace test_ref diff --git a/tests/yaml/test_ref.hpp b/tests/yaml/test_ref.hpp new file mode 100644 index 00000000..d289ba09 --- /dev/null +++ b/tests/yaml/test_ref.hpp @@ -0,0 +1,4 @@ +namespace test_ref{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_set.cpp b/tests/yaml/test_set.cpp new file mode 100644 index 00000000..9f2516ce --- /dev/null +++ b/tests/yaml/test_set.cpp @@ -0,0 +1,30 @@ +#include "test_set.hpp" + +#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); +} +} // namespace test_set diff --git a/tests/yaml/test_set.hpp b/tests/yaml/test_set.hpp new file mode 100644 index 00000000..142a663b --- /dev/null +++ b/tests/yaml/test_set.hpp @@ -0,0 +1,4 @@ +namespace test_set{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_size.cpp b/tests/yaml/test_size.cpp new file mode 100644 index 00000000..c37b9bd8 --- /dev/null +++ b/tests/yaml/test_size.cpp @@ -0,0 +1,42 @@ +#include "test_size.hpp" + +#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); +} +} // namespace test_size diff --git a/tests/yaml/test_size.hpp b/tests/yaml/test_size.hpp new file mode 100644 index 00000000..be330df0 --- /dev/null +++ b/tests/yaml/test_size.hpp @@ -0,0 +1,4 @@ +namespace test_size{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_string_map.cpp b/tests/yaml/test_string_map.cpp new file mode 100644 index 00000000..cb4fb903 --- /dev/null +++ b/tests/yaml/test_string_map.cpp @@ -0,0 +1,24 @@ +#include "test_string_map.hpp" + +#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); +} +} // namespace test_string_map diff --git a/tests/yaml/test_string_map.hpp b/tests/yaml/test_string_map.hpp new file mode 100644 index 00000000..94cb975a --- /dev/null +++ b/tests/yaml/test_string_map.hpp @@ -0,0 +1,4 @@ +namespace test_string_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_tagged_union.cpp b/tests/yaml/test_tagged_union.cpp new file mode 100644 index 00000000..a9dd1abc --- /dev/null +++ b/tests/yaml/test_tagged_union.cpp @@ -0,0 +1,36 @@ +#include "test_tagged_union.hpp" + +#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); +} +} // namespace test_tagged_union diff --git a/tests/yaml/test_tagged_union.hpp b/tests/yaml/test_tagged_union.hpp new file mode 100644 index 00000000..5d522ff9 --- /dev/null +++ b/tests/yaml/test_tagged_union.hpp @@ -0,0 +1,4 @@ +namespace test_tagged_union{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_timestamp.cpp b/tests/yaml/test_timestamp.cpp new file mode 100644 index 00000000..8af261d1 --- /dev/null +++ b/tests/yaml/test_timestamp.cpp @@ -0,0 +1,36 @@ +#include "test_timestamp.hpp" + +#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); +} +} // namespace test_timestamp diff --git a/tests/yaml/test_timestamp.hpp b/tests/yaml/test_timestamp.hpp new file mode 100644 index 00000000..891d89b9 --- /dev/null +++ b/tests/yaml/test_timestamp.hpp @@ -0,0 +1,4 @@ +namespace test_timestamp{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_unique_ptr.cpp b/tests/yaml/test_unique_ptr.cpp new file mode 100644 index 00000000..b1833533 --- /dev/null +++ b/tests/yaml/test_unique_ptr.cpp @@ -0,0 +1,33 @@ +#include "test_unique_ptr.hpp" + +#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); +} +} // namespace test_unique_ptr diff --git a/tests/yaml/test_unique_ptr.hpp b/tests/yaml/test_unique_ptr.hpp new file mode 100644 index 00000000..428ea2a2 --- /dev/null +++ b/tests/yaml/test_unique_ptr.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_unique_ptr2.cpp b/tests/yaml/test_unique_ptr2.cpp new file mode 100644 index 00000000..e3007267 --- /dev/null +++ b/tests/yaml/test_unique_ptr2.cpp @@ -0,0 +1,48 @@ +#include "test_unique_ptr2.hpp" + +#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); +} +} // namespace test_unique_ptr2 diff --git a/tests/yaml/test_unique_ptr2.hpp b/tests/yaml/test_unique_ptr2.hpp new file mode 100644 index 00000000..74adc170 --- /dev/null +++ b/tests/yaml/test_unique_ptr2.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr2{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_variant.cpp b/tests/yaml/test_variant.cpp new file mode 100644 index 00000000..cc3d6cbe --- /dev/null +++ b/tests/yaml/test_variant.cpp @@ -0,0 +1,36 @@ +#include "test_variant.hpp" + +#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); +} +} // namespace test_variant diff --git a/tests/yaml/test_variant.hpp b/tests/yaml/test_variant.hpp new file mode 100644 index 00000000..0e58ce71 --- /dev/null +++ b/tests/yaml/test_variant.hpp @@ -0,0 +1,4 @@ +namespace test_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/tests.cpp b/tests/yaml/tests.cpp index d5d7db2e..7f5a02d9 100644 --- a/tests/yaml/tests.cpp +++ b/tests/yaml/tests.cpp @@ -1,46 +1,61 @@ -#include -#include -#include -#include - -#include "write_and_read.hpp" - -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; -}; +#include "test_array.hpp" +#include "test_box.hpp" +#include "test_custom_class1.hpp" +#include "test_custom_class3.hpp" +#include "test_custom_class4.hpp" +#include "test_default_values.hpp" +#include "test_deque.hpp" +#include "test_enum.hpp" +#include "test_field_variant.hpp" +#include "test_flag_enum.hpp" +#include "test_flag_enum_with_int.hpp" +#include "test_flatten.hpp" +#include "test_flatten_anonymous.hpp" +#include "test_forward_list.hpp" +#include "test_literal.hpp" +#include "test_literal_map.hpp" +#include "test_map.hpp" +#include "test_map_with_key_validation.hpp" +#include "test_monster_example.hpp" +#include "test_readme_example.hpp" +#include "test_ref.hpp" +#include "test_set.hpp" +#include "test_size.hpp" +#include "test_tagged_union.hpp" +#include "test_timestamp.hpp" +#include "test_unique_ptr.hpp" +#include "test_unique_ptr2.hpp" +#include "test_variant.hpp" int main() { - 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); + test_readme_example::test(); + test_flatten::test(); + test_flatten_anonymous::test(); + test_enum::test(); + test_flag_enum::test(); + test_flag_enum_with_int::test(); + test_map::test(); + test_map_with_key_validation::test(); + test_variant::test(); + test_field_variant::test(); + test_tagged_union::test(); + test_deque::test(); + test_forward_list::test(); + test_literal_map::test(); + test_literal::test(); + test_monster_example::test(); + test_ref::test(); + test_set::test(); + test_size::test(); + test_timestamp::test(); + test_unique_ptr::test(); + test_unique_ptr2::test(); + test_array::test(); + test_box::test(); + test_custom_class1::test(); + test_custom_class3::test(); + test_custom_class4::test(); + test_default_values::test(); return 0; }