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