Repository navigation
Expand file tree
/
Copy pathtest_code_points.cpp
More file actions
140 lines (111 loc) · 4.84 KB
/
Copy pathtest_code_points.cpp
File metadata and controls
140 lines (111 loc) · 4.84 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
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include "write_and_read.hpp"
namespace test_code_points {
// UTF-8 escapes are used so the test does not depend on the source or
// execution character set.
// "héllo": 5 code points, 6 bytes.
constexpr const char* kHello = "h\xC3\xA9llo";
// "日本語": 3 code points, 9 bytes.
constexpr const char* kNihongo = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E";
// U+1F600 (grinning face): 1 code point, 4 bytes.
constexpr const char* kEmoji = "\xF0\x9F\x98\x80";
struct MaxFiveCodePoints {
rfl::Validator<std::string, rfl::CodePoints<rfl::Maximum<5>>> name;
};
struct MaxFiveBytes {
rfl::Validator<std::string, rfl::Size<rfl::Maximum<5>>> name;
};
struct ExactlyThreeCodePoints {
rfl::Validator<std::string, rfl::CodePoints<rfl::EqualTo<3>>> name;
};
struct ExactlyThreeBytes {
rfl::Validator<std::string, rfl::Size<rfl::EqualTo<3>>> name;
};
struct OneCodePoint {
rfl::Validator<std::string, rfl::CodePoints<rfl::EqualTo<1>>> name;
};
std::string to_json(const std::string& _name) {
return R"({"name":")" + _name + R"("})";
}
TEST(json, test_code_points_ascii_within_limit) {
write_and_read(MaxFiveCodePoints{.name = "hello"}, R"({"name":"hello"})");
}
TEST(json, test_code_points_ascii_over_limit) {
const auto res = rfl::json::read<MaxFiveCodePoints>(R"({"name":"hello!"})");
ASSERT_FALSE(res);
EXPECT_EQ(res.error().what(),
"Failed to parse field 'name': Code point count validation "
"failed: Value expected to be less than or equal to 5, but got "
"6.");
}
TEST(json, test_code_points_multibyte_latin) {
const auto json = to_json(kHello);
const auto res = rfl::json::read<MaxFiveCodePoints>(json);
ASSERT_TRUE(res) << res.error().what();
EXPECT_EQ(res.value().name.value(), kHello);
EXPECT_EQ(rfl::json::write(res.value()), json);
// The same limit expressed in bytes rejects the string.
const auto res_bytes = rfl::json::read<MaxFiveBytes>(json);
ASSERT_FALSE(res_bytes);
EXPECT_EQ(res_bytes.error().what(),
"Failed to parse field 'name': Size validation failed: Value "
"expected to be less than or equal to 5, but got 6.");
}
TEST(json, test_code_points_cjk) {
const auto json = to_json(kNihongo);
const auto res = rfl::json::read<ExactlyThreeCodePoints>(json);
ASSERT_TRUE(res) << res.error().what();
EXPECT_EQ(res.value().name.value(), kNihongo);
const auto res_bytes = rfl::json::read<ExactlyThreeBytes>(json);
ASSERT_FALSE(res_bytes);
EXPECT_EQ(res_bytes.error().what(),
"Failed to parse field 'name': Size validation failed: Value "
"expected to be equal to 3, but got 9.");
}
TEST(json, test_code_points_emoji) {
const auto res = rfl::json::read<OneCodePoint>(to_json(kEmoji));
ASSERT_TRUE(res) << res.error().what();
EXPECT_EQ(res.value().name.value(), kEmoji);
const auto res_two =
rfl::json::read<OneCodePoint>(to_json(std::string(kEmoji) + kEmoji));
ASSERT_FALSE(res_two);
EXPECT_EQ(res_two.error().what(),
"Failed to parse field 'name': Code point count validation "
"failed: Value expected to be equal to 1, but got 2.");
}
TEST(json, test_code_points_minimum) {
using AtLeastThree =
rfl::Validator<std::string, rfl::CodePoints<rfl::Minimum<3>>>;
// "日本" is 6 bytes but only 2 code points.
const auto res = AtLeastThree::from_value("\xE6\x97\xA5\xE6\x9C\xAC");
ASSERT_FALSE(res);
EXPECT_EQ(res.error().what(),
"Code point count validation failed: Value expected to be greater "
"than or equal to 3, but got 2.");
EXPECT_TRUE(AtLeastThree::from_value(kNihongo));
}
TEST(json, test_code_points_schema) {
constexpr auto expected =
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","allOf":[{"type":"string","minLength":4},{"type":"string","maxLength":6}],"$defs":{}})";
using MinMax = rfl::Validator<std::string, rfl::CodePoints<rfl::Minimum<4>>,
rfl::CodePoints<rfl::Maximum<6>>>;
EXPECT_EQ(rfl::json::to_schema<MinMax>(), expected);
using AllOf = rfl::Validator<
std::string,
rfl::CodePoints<rfl::AllOf<rfl::Minimum<4>, rfl::Maximum<6>>>>;
EXPECT_EQ(rfl::json::to_schema<AllOf>(), expected);
// Schema output is identical to the byte-based rfl::Size.
using SizeMinMax = rfl::Validator<std::string, rfl::Size<rfl::Minimum<4>>,
rfl::Size<rfl::Maximum<6>>>;
EXPECT_EQ(rfl::json::to_schema<MinMax>(),
rfl::json::to_schema<SizeMinMax>());
}
TEST(json, test_code_points_schema_exact) {
constexpr auto expected =
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","type":"string","minLength":3,"maxLength":3,"$defs":{}})";
using Exact = rfl::Validator<std::string, rfl::CodePoints<rfl::EqualTo<3>>>;
EXPECT_EQ(rfl::json::to_schema<Exact>(), expected);
}
} // namespace test_code_points