diff --git a/tests/json/test_apply.cpp b/tests/json/test_apply.cpp new file mode 100644 index 00000000..3f80e843 --- /dev/null +++ b/tests/json/test_apply.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "test_replace.hpp" +#include "write_and_read.hpp" + +namespace test_apply { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; + + const auto view = rfl::to_view(lisa); + + *view.get<0>() = "Maggie"; + *std::get<1>(view.values()) = "Simpson"; + *view.get<"age">() = 0; + + view.apply([](const Field& field) { + using field_type = std::remove_pointer_t; + if constexpr (Field::name() == "age") { + static_assert(std::is_same_v); + } else { + static_assert(std::is_same_v); + } + }); + + rfl::to_view(lisa).apply([](auto field) { + if constexpr (decltype(field)::name() == "first_name") { + *field.value() = "Bart"; + } + }); + + write_and_read(lisa, + R"({"first_name":"Bart","last_name":"Simpson","age":0})"); +} +} // namespace test_apply diff --git a/tests/json/test_apply.hpp b/tests/json/test_apply.hpp new file mode 100644 index 00000000..cffdf39e --- /dev/null +++ b/tests/json/test_apply.hpp @@ -0,0 +1,4 @@ +namespace test_apply { +void test(); +} + diff --git a/tests/json/test_enum.hpp b/tests/json/test_enum.hpp deleted file mode 100644 index 2e2e0b3d..00000000 --- a/tests/json/test_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum { -void test(); -} - diff --git a/tests/json/test_enum1.cpp b/tests/json/test_enum1.cpp new file mode 100644 index 00000000..358a4eb1 --- /dev/null +++ b/tests/json/test_enum1.cpp @@ -0,0 +1,30 @@ +#include "test_enum1.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum1 { + +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, R"({"radius":2.0,"color":"green"})"); +} + +} // namespace test_enum1 diff --git a/tests/json/test_enum1.hpp b/tests/json/test_enum1.hpp new file mode 100644 index 00000000..97af2fb1 --- /dev/null +++ b/tests/json/test_enum1.hpp @@ -0,0 +1,4 @@ +namespace test_enum1 { +void test(); +} + diff --git a/tests/json/test_enum2.cpp b/tests/json/test_enum2.cpp new file mode 100644 index 00000000..7a9514bd --- /dev/null +++ b/tests/json/test_enum2.cpp @@ -0,0 +1,33 @@ +#include "test_enum2.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum2 { + +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; + + auto mutable_circle = Circle{.radius = 2.0, .color = Color::green}; + + if (auto color = rfl::string_to_enum("red"); color) { + mutable_circle.color = *color; + } + write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})"); +} + +} // namespace test_enum2 diff --git a/tests/json/test_enum2.hpp b/tests/json/test_enum2.hpp new file mode 100644 index 00000000..7941d5e9 --- /dev/null +++ b/tests/json/test_enum2.hpp @@ -0,0 +1,4 @@ +namespace test_enum2 { +void test(); +} + diff --git a/tests/json/test_enum3.cpp b/tests/json/test_enum3.cpp new file mode 100644 index 00000000..77b1ffff --- /dev/null +++ b/tests/json/test_enum3.cpp @@ -0,0 +1,33 @@ +#include "test_enum3.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum3 { + +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; + + auto mutable_circle = Circle{.radius = 2.0, .color = Color::red}; + + if (auto color = rfl::string_to_enum("bart"); color) { + mutable_circle.color = *color; + } + write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})"); +} + +} // namespace test_enum3 diff --git a/tests/json/test_enum3.hpp b/tests/json/test_enum3.hpp new file mode 100644 index 00000000..4bf7efab --- /dev/null +++ b/tests/json/test_enum3.hpp @@ -0,0 +1,4 @@ +namespace test_enum3 { +void test(); +} + diff --git a/tests/json/test_enum4.cpp b/tests/json/test_enum4.cpp new file mode 100644 index 00000000..7be74536 --- /dev/null +++ b/tests/json/test_enum4.cpp @@ -0,0 +1,35 @@ +#include "test_enum4.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum4 { + +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; + + auto mutable_circle = Circle{.radius = 2.0, .color = Color::red}; + + if (auto color = rfl::string_to_enum(rfl::enum_to_string(Color::blue)); + color) { + mutable_circle.color = *color; + } + + write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})"); +} + +} // namespace test_enum4 diff --git a/tests/json/test_enum4.hpp b/tests/json/test_enum4.hpp new file mode 100644 index 00000000..070d7b81 --- /dev/null +++ b/tests/json/test_enum4.hpp @@ -0,0 +1,4 @@ +namespace test_enum4 { +void test(); +} + diff --git a/tests/json/test_enum5.cpp b/tests/json/test_enum5.cpp new file mode 100644 index 00000000..3673011f --- /dev/null +++ b/tests/json/test_enum5.cpp @@ -0,0 +1,35 @@ +#include "test_enum5.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum5 { + +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; + + auto mutable_circle = Circle{.radius = 2.0, .color = Color::blue}; + + rfl::get_enumerators().apply([&](auto field) { + if constexpr (decltype(field)::name() == "green") { + mutable_circle.color = field.value(); + } + }); + write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})"); +} + +} // namespace test_enum5 diff --git a/tests/json/test_enum5.hpp b/tests/json/test_enum5.hpp new file mode 100644 index 00000000..1d7854eb --- /dev/null +++ b/tests/json/test_enum5.hpp @@ -0,0 +1,4 @@ +namespace test_enum5 { +void test(); +} + diff --git a/tests/json/test_enum6.cpp b/tests/json/test_enum6.cpp new file mode 100644 index 00000000..1afb8b0b --- /dev/null +++ b/tests/json/test_enum6.cpp @@ -0,0 +1,36 @@ +#include "test_enum6.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum6 { + +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; + + auto mutable_circle = Circle{.radius = 2.0, .color = Color::red}; + + rfl::get_underlying_enumerators().apply([&](const auto& field) { + if (field.name() == "blue") { + mutable_circle.color = static_cast(field.value()); + } + }); + + write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})"); +} + +} // namespace test_enum6 diff --git a/tests/json/test_enum6.hpp b/tests/json/test_enum6.hpp new file mode 100644 index 00000000..5c8023f4 --- /dev/null +++ b/tests/json/test_enum6.hpp @@ -0,0 +1,4 @@ +namespace test_enum6 { +void test(); +} + diff --git a/tests/json/test_enum.cpp b/tests/json/test_enum7.cpp similarity index 54% rename from tests/json/test_enum.cpp rename to tests/json/test_enum7.cpp index 8484e34a..0d78a754 100644 --- a/tests/json/test_enum.cpp +++ b/tests/json/test_enum7.cpp @@ -1,4 +1,4 @@ -#include "test_enum.hpp" +#include "test_enum7.hpp" #include #include @@ -10,7 +10,7 @@ #include "write_and_read.hpp" -namespace test_enum { +namespace test_enum7 { enum class Color { red, green, blue, yellow }; @@ -22,41 +22,6 @@ struct Circle { 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, R"({"radius":2.0,"color":"green"})"); - - auto mutable_circle = circle; - if (auto color = rfl::string_to_enum("red"); color) { - mutable_circle.color = *color; - } - write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})"); - - if (auto color = rfl::string_to_enum("bart"); color) { - mutable_circle.color = *color; - } - write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})"); - - if (auto color = rfl::string_to_enum(rfl::enum_to_string(Color::blue)); - color) { - mutable_circle.color = *color; - } - write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})"); - - rfl::get_enumerators().apply([&](auto field) { - if constexpr (decltype(field)::name() == "green") { - mutable_circle.color = field.value(); - } - }); - write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})"); - - rfl::get_underlying_enumerators().apply([&](const auto& field) { - if (field.name() == "blue") { - mutable_circle.color = static_cast(field.value()); - } - }); - write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})"); - constexpr auto enumerator_array = rfl::get_enumerator_array(); static_assert(enumerator_array[0].first == "red"); static_assert(enumerator_array[1].first == "green"); @@ -77,6 +42,9 @@ void test() { static_assert(enumerator_array_underlying[1].second == 1); static_assert(enumerator_array_underlying[2].second == 2); static_assert(enumerator_array_underlying[3].second == 3); + + // This is a compile-time test + std::cout << "OK" << std::endl << std::endl; } -} // namespace test_enum +} // namespace test_enum7 diff --git a/tests/json/test_enum7.hpp b/tests/json/test_enum7.hpp new file mode 100644 index 00000000..b8be5df5 --- /dev/null +++ b/tests/json/test_enum7.hpp @@ -0,0 +1,4 @@ +namespace test_enum7 { +void test(); +} + diff --git a/tests/json/test_flag_enum.hpp b/tests/json/test_flag_enum.hpp deleted file mode 100644 index 2f4dc7a0..00000000 --- a/tests/json/test_flag_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum { -void test(); -} - diff --git a/tests/json/test_flag_enum1.cpp b/tests/json/test_flag_enum1.cpp new file mode 100644 index 00000000..ad35b0bb --- /dev/null +++ b/tests/json/test_flag_enum1.cpp @@ -0,0 +1,41 @@ +#include "test_flag_enum1.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flag_enum1 { + +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, R"({"radius":2.0,"color":"red|blue|yellow"})"); +} + +} // namespace test_flag_enum1 diff --git a/tests/json/test_flag_enum1.hpp b/tests/json/test_flag_enum1.hpp new file mode 100644 index 00000000..cafcff27 --- /dev/null +++ b/tests/json/test_flag_enum1.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum1 { +void test(); +} + diff --git a/tests/json/test_flag_enum.cpp b/tests/json/test_flag_enum2.cpp similarity index 80% rename from tests/json/test_flag_enum.cpp rename to tests/json/test_flag_enum2.cpp index 2275b558..4b5fc1ab 100644 --- a/tests/json/test_flag_enum.cpp +++ b/tests/json/test_flag_enum2.cpp @@ -1,3 +1,5 @@ +#include "test_flag_enum2.hpp" + #include #include #include @@ -6,10 +8,9 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" -namespace test_flag_enum { +namespace test_flag_enum2 { enum class Color { red = 256, @@ -31,18 +32,16 @@ struct Circle { void test() { std::cout << std::source_location::current().function_name() << std::endl; - const auto circle = + auto mutable_circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; - write_and_read(circle, R"({"radius":2.0,"color":"red|blue|yellow"})"); - - auto mutable_circle = circle; rfl::get_enumerators().apply([&](auto field) { if constexpr (decltype(field)::name() == "green") { mutable_circle.color = field.value(); } }); + write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})"); } -} // namespace test_flag_enum +} // namespace test_flag_enum2 diff --git a/tests/json/test_flag_enum2.hpp b/tests/json/test_flag_enum2.hpp new file mode 100644 index 00000000..55a52008 --- /dev/null +++ b/tests/json/test_flag_enum2.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum2 { +void test(); +} + diff --git a/tests/json/test_flag_enum_with_int.cpp b/tests/json/test_flag_enum_with_int.cpp index 9c159289..63c1e4b6 100644 --- a/tests/json/test_flag_enum_with_int.cpp +++ b/tests/json/test_flag_enum_with_int.cpp @@ -1,3 +1,5 @@ +#include "test_flag_enum_with_int.hpp" + #include #include #include @@ -6,7 +8,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum_with_int { diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 9b51e510..5751f340 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -22,10 +22,17 @@ #include "test_default_values.hpp" #include "test_deque.hpp" #include "test_email.hpp" -#include "test_enum.hpp" +#include "test_enum1.hpp" +#include "test_enum2.hpp" +#include "test_enum3.hpp" +#include "test_enum4.hpp" +#include "test_enum5.hpp" +#include "test_enum6.hpp" +#include "test_enum7.hpp" #include "test_error_messages.hpp" #include "test_field_variant.hpp" -#include "test_flag_enum.hpp" +#include "test_flag_enum1.hpp" +#include "test_flag_enum2.hpp" #include "test_flag_enum_with_int.hpp" #include "test_flatten.hpp" #include "test_flatten_anonymous.hpp" @@ -80,9 +87,6 @@ int main() { test_unique_ptr::test(); test_unique_ptr2::test(); test_literal::test(); - test_enum::test(); - test_flag_enum::test(); - test_flag_enum_with_int::test(); test_variant::test(); test_tagged_union::test(); test_tagged_union2::test(); @@ -119,6 +123,17 @@ int main() { test_unnamed_namespace::test(); test_inside_function::test(); + test_enum1::test(); + test_enum2::test(); + test_enum3::test(); + test_enum4::test(); + test_enum5::test(); + test_enum6::test(); + test_enum7::test(); + test_flag_enum1::test(); + test_flag_enum2::test(); + test_flag_enum_with_int::test(); + test_custom_class1::test(); test_custom_class2::test(); test_custom_class3::test();