Repository navigation
Expand file tree
/
Copy pathtest_enum_names_only.cpp
More file actions
152 lines (131 loc) · 5.56 KB
/
Copy pathtest_enum_names_only.cpp
File metadata and controls
152 lines (131 loc) · 5.56 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <gtest/gtest.h>
#include <cstdint>
#include <rfl.hpp>
#include <rfl/json.hpp>
namespace test_enum_names_only {
enum class Color { red = 1, green = 2, blue = 3 };
enum class Perm {
read = 1,
write = 2,
exec = 4
};
inline Perm operator|(Perm a, Perm b) noexcept {
return static_cast<Perm>(static_cast<int>(a) | static_cast<int>(b));
}
// An enum with a narrow underlying type.
enum class Narrow : uint8_t { low = 1, high = 2 };
struct Circle {
float radius;
Color color;
};
struct File {
int id;
Perm perm;
};
TEST(json, test_numeric_enum_allowed_by_default) {
// Legacy behavior: numeric values are accepted as cast values, even when
// they do not correspond to a declared enumerator.
const auto res = rfl::json::read<Circle>(R"({"radius":2.0,"color":"4"})");
ASSERT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().what();
EXPECT_EQ(static_cast<int>(res.value().color), 4);
}
TEST(json, test_enum_name_allowed_by_default) {
const auto res = rfl::json::read<Circle>(R"({"radius":2.0,"color":"red"})");
ASSERT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().what();
EXPECT_EQ(res.value().color, Color::red);
}
TEST(json, test_unknown_enum_name_rejected_by_default) {
const auto res = rfl::json::read<Circle>(R"({"radius":2.0,"color":"bart"})");
EXPECT_TRUE(!res.has_value());
EXPECT_EQ(res.error().what(),
R"(Failed to parse field 'color': Invalid enum value: 'bart'. Must be one of [red, green, blue].)");
}
TEST(json, test_flag_enum_numeric_allowed_by_default) {
const auto res = rfl::json::read<File>(R"({"id":1,"perm":"read|8"})");
ASSERT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().what();
EXPECT_EQ(static_cast<int>(res.value().perm), 9);
}
TEST(json, test_enum_name_allowed_with_processor) {
const auto res =
rfl::json::read<Circle, rfl::EnumNamesOnly>(
R"({"radius":2.0,"color":"red"})");
ASSERT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().what();
EXPECT_EQ(res.value().color, Color::red);
}
TEST(json, test_declared_numeric_value_rejected_with_processor) {
// Numeric values are rejected even when they correspond to a declared
// enumerator: only the declared enumerator names are accepted.
const auto res =
rfl::json::read<Circle, rfl::EnumNamesOnly>(
R"({"radius":2.0,"color":"2"})");
EXPECT_TRUE(!res.has_value());
EXPECT_EQ(res.error().what(),
R"(Failed to parse field 'color': Invalid enum value: '2'. Must be one of [red, green, blue].)");
}
TEST(json, test_undeclared_numeric_value_rejected_with_processor) {
const auto res =
rfl::json::read<Circle, rfl::EnumNamesOnly>(
R"({"radius":2.0,"color":"4"})");
EXPECT_TRUE(!res.has_value());
EXPECT_EQ(res.error().what(),
R"(Failed to parse field 'color': Invalid enum value: '4'. Must be one of [red, green, blue].)");
}
TEST(json, test_negative_numeric_value_rejected_with_processor) {
const auto res =
rfl::json::read<Circle, rfl::EnumNamesOnly>(
R"({"radius":2.0,"color":"-1"})");
EXPECT_TRUE(!res.has_value());
EXPECT_EQ(res.error().what(),
R"(Failed to parse field 'color': Invalid enum value: '-1'. Must be one of [red, green, blue].)");
}
TEST(json, test_unknown_enum_name_rejected_with_processor) {
const auto res =
rfl::json::read<Circle, rfl::EnumNamesOnly>(
R"({"radius":2.0,"color":"bart"})");
EXPECT_TRUE(!res.has_value());
EXPECT_EQ(res.error().what(),
R"(Failed to parse field 'color': Invalid enum value: 'bart'. Must be one of [red, green, blue].)");
}
TEST(json, test_flag_enum_names_allowed_with_processor) {
const auto res =
rfl::json::read<File, rfl::EnumNamesOnly>(
R"({"id":1,"perm":"read|write"})");
ASSERT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().what();
EXPECT_EQ(res.value().perm, Perm::read | Perm::write);
}
TEST(json, test_flag_enum_numeric_token_rejected_with_processor) {
const auto res =
rfl::json::read<File, rfl::EnumNamesOnly>(R"({"id":1,"perm":"read|8"})");
EXPECT_TRUE(!res.has_value());
EXPECT_EQ(res.error().what(),
R"(Failed to parse field 'perm': Invalid enum value: '8'. Must be one of [read, write, exec].)");
}
TEST(json, test_string_to_enum_direct) {
// With enum_names_only = true, only the declared names are accepted.
const auto name = rfl::string_to_enum<Color, true>("green");
ASSERT_TRUE(name.has_value());
EXPECT_EQ(*name, Color::green);
const auto declared_numeric = rfl::string_to_enum<Color, true>("2");
EXPECT_TRUE(!declared_numeric.has_value());
EXPECT_EQ(declared_numeric.error().what(),
R"(Invalid enum value: '2'. Must be one of [red, green, blue].)");
const auto undeclared_numeric = rfl::string_to_enum<Color, true>("4");
EXPECT_TRUE(!undeclared_numeric.has_value());
// Values that overflow the narrow underlying type are rejected as well.
const auto narrow = rfl::string_to_enum<Narrow, true>("300");
EXPECT_TRUE(!narrow.has_value());
// The default (permissive) behavior is unchanged: numeric values are
// accepted as cast values.
const auto permissive = rfl::string_to_enum<Color>("4");
ASSERT_TRUE(permissive.has_value());
EXPECT_EQ(static_cast<int>(*permissive), 4);
const auto permissive_narrow = rfl::string_to_enum<Narrow>("300");
ASSERT_TRUE(permissive_narrow.has_value());
EXPECT_EQ(static_cast<int>(*permissive_narrow), 44);
}
} // namespace test_enum_names_only