comparison tests/libcommon/util/main.cpp @ 98:f4d23ad4aa27

Common: refactoring class mlk::size 1. Make uniform size representation in JSON format: { "width": 100, "height": 200 } The Javascript API was using this form while an array was used in loaders. 2. Create brand new functions in util::json to parse mlk::size objects, Updated util::json::require_(u)int as well. 3. Add new tests for mlk::size object, 4. Get rid of utilities in loader as they are in util::json instead.
author David Demelier <markand@malikania.fr>
date Tue, 04 Jul 2017 13:23:15 +0200
parents 469b6d558ab0
children 119bcc5a727e
comparison
equal deleted inserted replaced
97:7377a3d8600d 98:f4d23ad4aa27
18 18
19 #define BOOST_TEST_MODULE "Util" 19 #define BOOST_TEST_MODULE "Util"
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 #include <malikania/size.hpp>
23 24
24 using namespace mlk; 25 using namespace mlk;
25 using namespace nlohmann; 26 using namespace nlohmann;
26 27
27 namespace nlohmann { 28 namespace nlohmann {
333 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned); 334 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
334 } 335 }
335 } 336 }
336 337
337 BOOST_AUTO_TEST_SUITE_END() 338 BOOST_AUTO_TEST_SUITE_END()
339
340 BOOST_AUTO_TEST_SUITE(json_require_size)
341
342 /*
343 * util::json::require_size
344 * ------------------------------------------------------------------
345 */
346
347 BOOST_AUTO_TEST_CASE(simple)
348 {
349 json object{
350 { "size", {
351 { "width", 10 },
352 { "height", 20 }
353 }
354 }
355 };
356
357 BOOST_REQUIRE_EQUAL(util::json::require_size(object, "/size"_json_pointer), mlk::size(10, 20));
358 }
359
360 BOOST_AUTO_TEST_CASE(not_object)
361 {
362 json object{
363 { "size", false }
364 };
365
366 try {
367 util::json::require_size(object, "/size"_json_pointer);
368 BOOST_FAIL("exception expected");
369 } catch (const util::json::property_error& ex) {
370 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::null);
371 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
372 }
373 }
374
375 BOOST_AUTO_TEST_CASE(not_int_width)
376 {
377 json object{
378 { "size", {
379 { "width", "10" },
380 { "height", 20 }
381 }
382 }
383 };
384
385 try {
386 util::json::require_size(object, "/size"_json_pointer);
387 BOOST_FAIL("exception expected");
388 } catch (const util::json::property_error& ex) {
389 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::string);
390 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
391 }
392 }
393
394 BOOST_AUTO_TEST_CASE(not_int_height)
395 {
396 json object{
397 { "size", {
398 { "width", 10 },
399 { "height", "20" }
400 }
401 }
402 };
403
404 try {
405 util::json::require_size(object, "/size"_json_pointer);
406 BOOST_FAIL("exception expected");
407 } catch (const util::json::property_error& ex) {
408 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::string);
409 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
410 }
411 }
412
413 BOOST_AUTO_TEST_SUITE_END()
414
415 BOOST_AUTO_TEST_SUITE(json_get_size)
416
417 BOOST_AUTO_TEST_CASE(simple)
418 {
419 json object{
420 { "size", {
421 { "width", 10 },
422 { "height", 20 }
423 }
424 }
425 };
426
427 BOOST_REQUIRE_EQUAL(util::json::get_size(object, "/size"_json_pointer), mlk::size(10, 20));
428 }
429
430 BOOST_AUTO_TEST_CASE(not_object)
431 {
432 json object{
433 { "size", false }
434 };
435
436 mlk::size def(10, 20);
437
438 BOOST_REQUIRE_EQUAL(util::json::get_size(object, "/size"_json_pointer, def), def);
439 }
440
441 BOOST_AUTO_TEST_CASE(not_int_width)
442 {
443 json object{
444 { "size", {
445 { "width", "10" },
446 { "height", 20 }
447 }
448 }
449 };
450
451 mlk::size def(10, 20);
452
453 BOOST_REQUIRE_EQUAL(util::json::get_size(object, "/size"_json_pointer, def), def);
454 }
455
456 BOOST_AUTO_TEST_CASE(not_int_height)
457 {
458 json object{
459 { "size", {
460 { "width", 10 },
461 { "height", "20" }
462 }
463 }
464 };
465
466 mlk::size def(10, 20);
467
468 BOOST_REQUIRE_EQUAL(util::json::get_size(object, "/size"_json_pointer, def), def);
469 }
470
471 BOOST_AUTO_TEST_SUITE_END()