35
35
36
36
namespace fable {
37
37
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
+ */
38
44
inline void assert_eq (const Json& j, const Json& k) {
39
45
ASSERT_EQ (std::string (j.dump (2 )), std::string (k.dump (2 )));
40
46
}
41
47
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
+ */
42
56
inline void assert_eq (const Json& j, const char expect[]) {
43
57
assert_eq (j, parse_json (expect));
44
58
}
45
59
60
+ /* *
61
+ * Assert that both JSON values are NOT identical.
62
+ */
46
63
inline void assert_ne (const Json& j, const Json& k) {
47
64
ASSERT_NE (std::string (j.dump (2 )), std::string (k.dump (2 )));
48
65
}
49
66
67
+ /* *
68
+ * Assert that both JSON values are NOT identical.
69
+ *
70
+ * The second parameter is parsed to JSON and then dumped.
71
+ */
50
72
inline void assert_ne (const Json& j, const char expect[]) {
51
73
assert_ne (j, parse_json (expect));
52
74
}
53
75
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
+
54
84
inline void assert_schema_eq (const Confable& x, const Json& expect) {
55
85
assert_eq (x.schema ().json_schema (), expect);
56
86
}
@@ -59,32 +89,56 @@ inline void assert_schema_eq(const Confable& x, const char expect[]) {
59
89
assert_eq (x.schema ().json_schema (), parse_json (expect));
60
90
}
61
91
62
- inline void assert_validate (const Confable& x , const Conf& input) {
92
+ inline void assert_validate (const Schema& s , const Conf& input) {
63
93
try {
64
- x. schema () .validate (input);
94
+ s .validate (input);
65
95
} catch (SchemaError& e) {
66
96
pretty_print (e, std::cerr);
67
97
throw ;
68
98
};
69
99
}
70
100
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
+
71
109
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);
73
119
}
74
120
75
121
inline void assert_invalidate (const Confable& x, const Conf& input) {
76
- ASSERT_THROW (x.schema (). validate (input), SchemaError );
122
+ assert_invalidate (x.schema (), input );
77
123
}
78
124
79
125
inline void assert_invalidate (const Confable& x, const char json_input[]) {
80
126
assert_invalidate (x, Conf{parse_json (json_input)});
81
127
}
82
128
129
+ /* *
130
+ * Assert that the serialization is equal to the expected JSON.
131
+ */
83
132
template <typename T>
84
133
inline void assert_to_json (const T& x, const Json& expect) {
85
134
assert_eq (x.to_json (), expect);
86
135
}
87
136
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
+ */
88
142
template <typename T>
89
143
inline void assert_to_json (const T& x, const char json_expect[]) {
90
144
assert_to_json (x, parse_json (json_expect));
@@ -110,12 +164,24 @@ inline void assert_from_conf(T& x, const char json_input[]) {
110
164
assert_from_conf (x, Conf{parse_json (json_input)});
111
165
}
112
166
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
+ */
113
173
template <typename T>
114
174
inline void assert_from_eq_to (T& x, const Json& identity) {
115
175
assert_from_conf (x, Conf{identity});
116
176
assert_to_json (x, identity);
117
177
}
118
178
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
+ */
119
185
template <typename T>
120
186
inline void assert_from_eq_to (T& x, const char json_input[]) {
121
187
assert_from_eq_to (x, parse_json (json_input));
0 commit comments