Repository navigation
Expand file tree
/
Copy pathtest_enum_descriptions.cpp
More file actions
76 lines (62 loc) · 2.66 KB
/
Copy pathtest_enum_descriptions.cpp
File metadata and controls
76 lines (62 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
namespace test_enum_descriptions {
// Define an enum with descriptions
enum class Color { red, green, blue };
// An enum without descriptions for comparison
enum class Size { small, medium, large };
struct Config {
Color color;
Size size;
};
} // namespace test_enum_descriptions
// Specialize enum_descriptions to provide descriptions for Color values
template <>
struct rfl::config::enum_descriptions<test_enum_descriptions::Color> {
static constexpr bool has_descriptions = true;
static constexpr std::string_view get(test_enum_descriptions::Color value) {
switch (value) {
case test_enum_descriptions::Color::red:
return "The color red";
case test_enum_descriptions::Color::green:
return "The color green";
case test_enum_descriptions::Color::blue:
return "The color blue";
default:
return "";
}
}
};
namespace test_enum_descriptions {
TEST(json, test_enum_descriptions_schema) {
const auto json_schema = rfl::json::to_schema<Config>();
// The schema should contain oneOf with const/description for Color
EXPECT_TRUE(json_schema.find("\"oneOf\"") != std::string::npos)
<< "Expected oneOf for described enum. Schema: " << json_schema;
EXPECT_TRUE(json_schema.find("\"const\":\"red\"") != std::string::npos)
<< "Expected const for red. Schema: " << json_schema;
EXPECT_TRUE(json_schema.find("\"description\":\"The color red\"") !=
std::string::npos)
<< "Expected description for red. Schema: " << json_schema;
EXPECT_TRUE(json_schema.find("\"const\":\"green\"") != std::string::npos)
<< "Expected const for green. Schema: " << json_schema;
EXPECT_TRUE(json_schema.find("\"const\":\"blue\"") != std::string::npos)
<< "Expected const for blue. Schema: " << json_schema;
// Size should still use regular enum format
EXPECT_TRUE(json_schema.find("\"enum\":[\"small\",\"medium\",\"large\"]") !=
std::string::npos)
<< "Expected regular enum for Size. Schema: " << json_schema;
}
TEST(json, test_enum_descriptions_read_write) {
// Verify that read/write still works correctly with described enums
const Config config{.color = Color::green, .size = Size::medium};
const auto json_string = rfl::json::write(config);
EXPECT_EQ(json_string, R"({"color":"green","size":"medium"})");
const auto parsed = rfl::json::read<Config>(json_string);
EXPECT_TRUE(parsed.has_value()) << "Failed to parse: " << parsed.error().what();
EXPECT_EQ(parsed.value().color, Color::green);
EXPECT_EQ(parsed.value().size, Size::medium);
}
} // namespace test_enum_descriptions