comparison tests/libcommon/util/main.cpp @ 88:469b6d558ab0

Common: add util::json functions, closes #628
author David Demelier <markand@malikania.fr>
date Sat, 04 Feb 2017 16:15:20 +0100
parents 858621081b95
children f4d23ad4aa27
comparison
equal deleted inserted replaced
87:955a9409ab5f 88:469b6d558ab0
20 #include <boost/test/unit_test.hpp> 20 #include <boost/test/unit_test.hpp>
21 21
22 #include <malikania/util.hpp> 22 #include <malikania/util.hpp>
23 23
24 using namespace mlk; 24 using namespace mlk;
25 using namespace nlohmann;
26
27 namespace nlohmann {
28
29 namespace detail {
30
31 std::ostream& operator<<(std::ostream& out, json::value_t type)
32 {
33 out << json(type).type_name();
34 return out;
35 }
36
37 } // !detail
38
39 } // !nlohmann
25 40
26 /* 41 /*
27 * util::clamp 42 * util::clamp
28 * ------------------------------------------------------------------ 43 * ------------------------------------------------------------------
29 */ 44 */
54 { 69 {
55 BOOST_REQUIRE_EQUAL(10, util::clamp(20, 0, 10)); 70 BOOST_REQUIRE_EQUAL(10, util::clamp(20, 0, 10));
56 } 71 }
57 72
58 BOOST_AUTO_TEST_SUITE_END() 73 BOOST_AUTO_TEST_SUITE_END()
74
75 /*
76 * util::json::require
77 * ------------------------------------------------------------------
78 */
79
80 BOOST_AUTO_TEST_SUITE(json_require)
81
82 BOOST_AUTO_TEST_CASE(simple)
83 {
84 json object{
85 { "b", true },
86 { "i", 123 },
87 { "s", "blabla" }
88 };
89
90 BOOST_REQUIRE_EQUAL(util::json::require(
91 object, "/b"_json_pointer, json::value_t::boolean).type(), json::value_t::boolean);
92 BOOST_REQUIRE_EQUAL(util::json::require(
93 object, "/i"_json_pointer, json::value_t::number_integer).type(), json::value_t::number_integer);
94 BOOST_REQUIRE_EQUAL(util::json::require(
95 object, "/s"_json_pointer, json::value_t::string).type(), json::value_t::string);
96 }
97
98 BOOST_AUTO_TEST_CASE(nonexistent)
99 {
100 auto json = json::object();
101
102 try {
103 util::json::require(json, "/non-existent"_json_pointer, json::value_t::string);
104 BOOST_FAIL("exception expected");
105 } catch (const util::json::property_error& ex) {
106 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::null);
107 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string);
108 }
109 }
110
111 BOOST_AUTO_TEST_CASE(invalid)
112 {
113 json object{
114 { "not-string", 123 }
115 };
116
117 try {
118 util::json::require(object, "/not-string"_json_pointer, json::value_t::string);
119 BOOST_FAIL("exception expected");
120 } catch (const util::json::property_error& ex) {
121 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
122 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string);
123 }
124 }
125
126 BOOST_AUTO_TEST_SUITE_END()
127
128 /*
129 * util::json::require_array
130 * ------------------------------------------------------------------
131 */
132
133 BOOST_AUTO_TEST_SUITE(json_require_array)
134
135 BOOST_AUTO_TEST_CASE(simple)
136 {
137 json object{
138 { "a", { 1, 2, 3 } },
139 { "l1", {
140 { "l2", { 4, 5, 6 } }
141 }
142 }
143 };
144
145 auto a = util::json::require_array(object, "/a"_json_pointer);
146 auto l2 = util::json::require_array(object, "/l1/l2"_json_pointer);
147
148 BOOST_REQUIRE(a.is_array());
149 BOOST_REQUIRE(l2.is_array());
150 }
151
152 BOOST_AUTO_TEST_CASE(invalid)
153 {
154 json object{
155 { "not-array", 123 }
156 };
157
158 try {
159 util::json::require_array(object, "/not-array"_json_pointer);
160 BOOST_FAIL("exception expected");
161 } catch (const util::json::property_error& ex) {
162 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
163 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::array);
164 }
165 }
166
167 BOOST_AUTO_TEST_SUITE_END()
168
169 /*
170 * util::json::require_bool
171 * ------------------------------------------------------------------
172 */
173
174 BOOST_AUTO_TEST_SUITE(json_require_bool)
175
176 BOOST_AUTO_TEST_CASE(simple)
177 {
178 json object{
179 { "b", true }
180 };
181
182 BOOST_REQUIRE_EQUAL(util::json::require_bool(object, "/b"_json_pointer), true);
183 }
184
185 BOOST_AUTO_TEST_CASE(invalid)
186 {
187 json object{
188 { "not-bool", 123 }
189 };
190
191 try {
192 util::json::require_bool(object, "/not-bool"_json_pointer);
193 BOOST_FAIL("exception expected");
194 } catch (const util::json::property_error& ex) {
195 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
196 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::boolean);
197 }
198 }
199
200 BOOST_AUTO_TEST_SUITE_END()
201
202 /*
203 * util::json::require_int
204 * ------------------------------------------------------------------
205 */
206
207 BOOST_AUTO_TEST_SUITE(json_require_int)
208
209 BOOST_AUTO_TEST_CASE(simple)
210 {
211 json object{
212 { "i", 123 }
213 };
214
215 BOOST_REQUIRE_EQUAL(util::json::require_int(object, "/i"_json_pointer), 123);
216 }
217
218 BOOST_AUTO_TEST_CASE(invalid)
219 {
220 json object{
221 { "not-int", true }
222 };
223
224 try {
225 util::json::require_int(object, "/not-int"_json_pointer);
226 BOOST_FAIL("exception expected");
227 } catch (const util::json::property_error& ex) {
228 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::boolean);
229 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_integer);
230 }
231 }
232
233 BOOST_AUTO_TEST_SUITE_END()
234
235 /*
236 * util::json::require_object
237 * ------------------------------------------------------------------
238 */
239
240 BOOST_AUTO_TEST_SUITE(json_require_object)
241
242 BOOST_AUTO_TEST_CASE(simple)
243 {
244 json object{
245 {
246 "network", json::object({
247 { "host", "localhost" },
248 { "port", 9090 }
249 })
250 }
251 };
252
253 BOOST_REQUIRE(util::json::require_object(object, "/network"_json_pointer).is_object());
254 }
255
256 BOOST_AUTO_TEST_CASE(invalid)
257 {
258 json object{
259 { "not-object", 123 }
260 };
261
262 try {
263 util::json::require_object(object, "/not-object"_json_pointer);
264 BOOST_FAIL("exception expected");
265 } catch (const util::json::property_error& ex) {
266 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
267 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::object);
268 }
269 }
270
271 BOOST_AUTO_TEST_SUITE_END()
272
273 /*
274 * util::json::require_string
275 * ------------------------------------------------------------------
276 */
277
278 BOOST_AUTO_TEST_SUITE(json_require_string)
279
280 BOOST_AUTO_TEST_CASE(simple)
281 {
282 json object{
283 { "s", "hello" }
284 };
285
286 BOOST_REQUIRE_EQUAL(util::json::require_string(object, "/s"_json_pointer), "hello");
287 }
288
289 BOOST_AUTO_TEST_CASE(invalid)
290 {
291 json object{
292 { "not-string", 123 }
293 };
294
295 try {
296 util::json::require_string(object, "/not-string"_json_pointer);
297 BOOST_FAIL("exception expected");
298 } catch (const util::json::property_error& ex) {
299 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
300 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string);
301 }
302 }
303
304 BOOST_AUTO_TEST_SUITE_END()
305
306 /*
307 * util::json::require_uint
308 * ------------------------------------------------------------------
309 */
310
311 BOOST_AUTO_TEST_SUITE(json_require_uint)
312
313 BOOST_AUTO_TEST_CASE(simple)
314 {
315 json object{
316 { "u1", 123U }
317 };
318
319 BOOST_REQUIRE_EQUAL(util::json::require_uint(object, "/u1"_json_pointer), 123U);
320 }
321
322 BOOST_AUTO_TEST_CASE(invalid)
323 {
324 json object{
325 { "not-uint", true }
326 };
327
328 try {
329 util::json::require_uint(object, "/not-uint"_json_pointer);
330 BOOST_FAIL("exception expected");
331 } catch (const util::json::property_error& ex) {
332 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::boolean);
333 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
334 }
335 }
336
337 BOOST_AUTO_TEST_SUITE_END()