diff 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
line wrap: on
line diff
--- a/tests/libcommon/util/main.cpp	Fri Jun 16 12:38:04 2017 +0200
+++ b/tests/libcommon/util/main.cpp	Tue Jul 04 13:23:15 2017 +0200
@@ -20,6 +20,7 @@
 #include <boost/test/unit_test.hpp>
 
 #include <malikania/util.hpp>
+#include <malikania/size.hpp>
 
 using namespace mlk;
 using namespace nlohmann;
@@ -335,3 +336,136 @@
 }
 
 BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(json_require_size)
+
+/*
+ * util::json::require_size
+ * ------------------------------------------------------------------
+ */
+
+BOOST_AUTO_TEST_CASE(simple)
+{
+    json object{
+        { "size", {
+                { "width", 10 },
+                { "height", 20 }
+            }
+        }
+    };
+
+    BOOST_REQUIRE_EQUAL(util::json::require_size(object, "/size"_json_pointer), mlk::size(10, 20));
+}
+
+BOOST_AUTO_TEST_CASE(not_object)
+{
+    json object{
+        { "size", false }
+    };
+
+    try {
+        util::json::require_size(object, "/size"_json_pointer);
+        BOOST_FAIL("exception expected");
+    } catch (const util::json::property_error& ex) {
+        BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::null);
+        BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
+    }
+}
+
+BOOST_AUTO_TEST_CASE(not_int_width)
+{
+    json object{
+        { "size", {
+                { "width", "10" },
+                { "height", 20 }
+            }
+        }
+    };
+
+    try {
+        util::json::require_size(object, "/size"_json_pointer);
+        BOOST_FAIL("exception expected");
+    } catch (const util::json::property_error& ex) {
+        BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::string);
+        BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
+    }
+}
+
+BOOST_AUTO_TEST_CASE(not_int_height)
+{
+    json object{
+        { "size", {
+                { "width", 10 },
+                { "height", "20" }
+            }
+        }
+    };
+
+    try {
+        util::json::require_size(object, "/size"_json_pointer);
+        BOOST_FAIL("exception expected");
+    } catch (const util::json::property_error& ex) {
+        BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::string);
+        BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
+    }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(json_get_size)
+
+BOOST_AUTO_TEST_CASE(simple)
+{
+    json object{
+        { "size", {
+                { "width", 10 },
+                { "height", 20 }
+            }
+        }
+    };
+
+    BOOST_REQUIRE_EQUAL(util::json::get_size(object, "/size"_json_pointer), mlk::size(10, 20));
+}
+
+BOOST_AUTO_TEST_CASE(not_object)
+{
+    json object{
+        { "size", false }
+    };
+
+    mlk::size def(10, 20);
+
+    BOOST_REQUIRE_EQUAL(util::json::get_size(object, "/size"_json_pointer, def), def);
+}
+
+BOOST_AUTO_TEST_CASE(not_int_width)
+{
+    json object{
+        { "size", {
+                { "width", "10" },
+                { "height", 20 }
+            }
+        }
+    };
+
+    mlk::size def(10, 20);
+
+    BOOST_REQUIRE_EQUAL(util::json::get_size(object, "/size"_json_pointer, def), def);
+}
+
+BOOST_AUTO_TEST_CASE(not_int_height)
+{
+    json object{
+        { "size", {
+                { "width", 10 },
+                { "height", "20" }
+            }
+        }
+    };
+
+    mlk::size def(10, 20);
+
+    BOOST_REQUIRE_EQUAL(util::json::get_size(object, "/size"_json_pointer, def), def);
+}
+
+BOOST_AUTO_TEST_SUITE_END()