mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
Split up the enum tests
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#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([]<typename Field>(const Field& field) {
|
||||
using field_type = std::remove_pointer_t<typename Field::Type>;
|
||||
if constexpr (Field::name() == "age") {
|
||||
static_assert(std::is_same_v<field_type, int>);
|
||||
} else {
|
||||
static_assert(std::is_same_v<field_type, std::string>);
|
||||
}
|
||||
});
|
||||
|
||||
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
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_apply {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
namespace test_enum {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "test_enum1.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum1 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "test_enum2.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Color>("red"); color) {
|
||||
mutable_circle.color = *color;
|
||||
}
|
||||
write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})");
|
||||
}
|
||||
|
||||
} // namespace test_enum2
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum2 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "test_enum3.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Color>("bart"); color) {
|
||||
mutable_circle.color = *color;
|
||||
}
|
||||
write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})");
|
||||
}
|
||||
|
||||
} // namespace test_enum3
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum3 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "test_enum4.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Color>(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
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum4 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "test_enum5.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Color>().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
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum5 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "test_enum6.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Color>().apply([&](const auto& field) {
|
||||
if (field.name() == "blue") {
|
||||
mutable_circle.color = static_cast<Color>(field.value());
|
||||
}
|
||||
});
|
||||
|
||||
write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})");
|
||||
}
|
||||
|
||||
} // namespace test_enum6
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum6 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "test_enum.hpp"
|
||||
#include "test_enum7.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
@@ -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<Color>("red"); color) {
|
||||
mutable_circle.color = *color;
|
||||
}
|
||||
write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})");
|
||||
|
||||
if (auto color = rfl::string_to_enum<Color>("bart"); color) {
|
||||
mutable_circle.color = *color;
|
||||
}
|
||||
write_and_read(mutable_circle, R"({"radius":2.0,"color":"red"})");
|
||||
|
||||
if (auto color = rfl::string_to_enum<Color>(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<Color>().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<Color>().apply([&](const auto& field) {
|
||||
if (field.name() == "blue") {
|
||||
mutable_circle.color = static_cast<Color>(field.value());
|
||||
}
|
||||
});
|
||||
write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})");
|
||||
|
||||
constexpr auto enumerator_array = rfl::get_enumerator_array<Color>();
|
||||
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
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum7 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
namespace test_flag_enum {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "test_flag_enum1.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Color>(static_cast<int>(c1) | static_cast<int>(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
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flag_enum1 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include "test_flag_enum2.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
@@ -6,10 +8,9 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Color>().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
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flag_enum2 {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include "test_flag_enum_with_int.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
@@ -6,7 +8,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_enum.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flag_enum_with_int {
|
||||
|
||||
+20
-5
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user