Skip to content

Commit 1a97427

Browse files
committed
fable: Extend gtest.hpp utility header
1 parent 90bbf58 commit 1a97427

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

fable/include/fable/utility/gtest.hpp

+70-4
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,52 @@
3535

3636
namespace fable {
3737

38+
/**
39+
* Assert that both JSON values are identical.
40+
*
41+
* This function dumps both JSON with indentation so that failing
42+
* tests can nicely format the diff between strings.
43+
*/
3844
inline void assert_eq(const Json& j, const Json& k) {
3945
ASSERT_EQ(std::string(j.dump(2)), std::string(k.dump(2)));
4046
}
4147

48+
/**
49+
* Assert that both JSON values are identical.
50+
*
51+
* The second parameter is parsed to JSON and then dumped.
52+
*
53+
* This function dumps both JSON with indentation so that failing
54+
* tests can nicely format the diff between strings.
55+
*/
4256
inline void assert_eq(const Json& j, const char expect[]) {
4357
assert_eq(j, parse_json(expect));
4458
}
4559

60+
/**
61+
* Assert that both JSON values are NOT identical.
62+
*/
4663
inline void assert_ne(const Json& j, const Json& k) {
4764
ASSERT_NE(std::string(j.dump(2)), std::string(k.dump(2)));
4865
}
4966

67+
/**
68+
* Assert that both JSON values are NOT identical.
69+
*
70+
* The second parameter is parsed to JSON and then dumped.
71+
*/
5072
inline void assert_ne(const Json& j, const char expect[]) {
5173
assert_ne(j, parse_json(expect));
5274
}
5375

76+
inline void assert_schema_eq(const Schema& s, const Json& expect) {
77+
assert_eq(s.json_schema(), expect);
78+
}
79+
80+
inline void assert_schema_eq(const Schema& s, const char expect[]) {
81+
assert_eq(s.json_schema(), parse_json(expect));
82+
}
83+
5484
inline void assert_schema_eq(const Confable& x, const Json& expect) {
5585
assert_eq(x.schema().json_schema(), expect);
5686
}
@@ -59,32 +89,56 @@ inline void assert_schema_eq(const Confable& x, const char expect[]) {
5989
assert_eq(x.schema().json_schema(), parse_json(expect));
6090
}
6191

62-
inline void assert_validate(const Confable& x, const Conf& input) {
92+
inline void assert_validate(const Schema& s, const Conf& input) {
6393
try {
64-
x.schema().validate(input);
94+
s.validate(input);
6595
} catch (SchemaError& e) {
6696
pretty_print(e, std::cerr);
6797
throw;
6898
};
6999
}
70100

101+
inline void assert_validate(const Schema& s, const char json_input[]) {
102+
assert_validate(s, Conf{parse_json(json_input)});
103+
}
104+
105+
inline void assert_validate(const Confable& x, const Conf& input) {
106+
assert_validate(x.schema(), input);
107+
}
108+
71109
inline void assert_validate(const Confable& x, const char json_input[]) {
72-
assert_validate(x, Conf{parse_json(json_input)});
110+
assert_validate(x.schema(), Conf{parse_json(json_input)});
111+
}
112+
113+
inline void assert_invalidate(const Schema& s, const Conf& input) {
114+
ASSERT_THROW(s.validate(input), SchemaError);
115+
}
116+
117+
inline void assert_invalidate(const Schema& s, const char json_input[]) {
118+
ASSERT_THROW(s.validate(Conf{parse_json(json_input)}), SchemaError);
73119
}
74120

75121
inline void assert_invalidate(const Confable& x, const Conf& input) {
76-
ASSERT_THROW(x.schema().validate(input), SchemaError);
122+
assert_invalidate(x.schema(), input);
77123
}
78124

79125
inline void assert_invalidate(const Confable& x, const char json_input[]) {
80126
assert_invalidate(x, Conf{parse_json(json_input)});
81127
}
82128

129+
/**
130+
* Assert that the serialization is equal to the expected JSON.
131+
*/
83132
template <typename T>
84133
inline void assert_to_json(const T& x, const Json& expect) {
85134
assert_eq(x.to_json(), expect);
86135
}
87136

137+
/**
138+
* Assert that the serialization is equal to the expected JSON string.
139+
*
140+
* The string is parsed to Json, so that field order is not important.
141+
*/
88142
template <typename T>
89143
inline void assert_to_json(const T& x, const char json_expect[]) {
90144
assert_to_json(x, parse_json(json_expect));
@@ -110,12 +164,24 @@ inline void assert_from_conf(T& x, const char json_input[]) {
110164
assert_from_conf(x, Conf{parse_json(json_input)});
111165
}
112166

167+
/**
168+
* Assert that deserializing the input works and serializes to the same input.
169+
*
170+
* This asserts that the type supports the identity function. This does not need
171+
* to hold for any type, but you may want it to hold for your type.
172+
*/
113173
template <typename T>
114174
inline void assert_from_eq_to(T& x, const Json& identity) {
115175
assert_from_conf(x, Conf{identity});
116176
assert_to_json(x, identity);
117177
}
118178

179+
/**
180+
* Assert that deserializing the input works and serializes to the same input.
181+
*
182+
* This asserts that the type supports the identity function. This does not need
183+
* to hold for any type, but you may want it to hold for your type.
184+
*/
119185
template <typename T>
120186
inline void assert_from_eq_to(T& x, const char json_input[]) {
121187
assert_from_eq_to(x, parse_json(json_input));

0 commit comments

Comments
 (0)