From 323764a8c5daf3d56649636216ccb83e83737c75 Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 8 Feb 2015 16:24:38 +0100 Subject: [PATCH] more test cases --- .travis.yml | 2 +- test/unit.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 87c58d6a..3a808aac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,4 +24,4 @@ after_success: - make clean - make json_unit CXXFLAGS="-fprofile-arcs -ftest-coverage" - ./json_unit - - coveralls --exclude test/catch.hpp --exclude test/unit.cpp --include src/json.hpp --gcov-options '\-lp' --gcov 'gcov-4.8' + - coveralls --exclude test/catch.hpp --include src/json.hpp --gcov-options '\-lp' --gcov 'gcov-4.8' diff --git a/test/unit.cpp b/test/unit.cpp index c9e5fb7d..3996611f 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -1197,6 +1197,52 @@ TEST_CASE("value conversion") std::unordered_multimap o = j.get>(); CHECK(json(o) == j); } + + SECTION("exception in case of a non-object type") + { + CHECK_THROWS_AS(json(json::value_t::null).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::array).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::string).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::boolean).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::number_integer).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::number_float).get(), std::logic_error); + } + } + + SECTION("get an object (implicit)") + { + json::object_t o_reference = {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} }; + json j(o_reference); + + SECTION("json::object_t") + { + json::object_t o = j; + CHECK(json(o) == j); + } + + SECTION("std::map") + { + std::map o = j; + CHECK(json(o) == j); + } + + SECTION("std::multimap") + { + std::multimap o = j; + CHECK(json(o) == j); + } + + SECTION("std::unordered_map") + { + std::unordered_map o = j; + CHECK(json(o) == j); + } + + SECTION("std::unordered_multimap") + { + std::unordered_multimap o = j; + CHECK(json(o) == j); + } } SECTION("get an array (explicit)") @@ -1233,5 +1279,51 @@ TEST_CASE("value conversion") std::deque a = j.get>(); CHECK(json(a) == j); } + + SECTION("exception in case of a non-array type") + { + CHECK_THROWS_AS(json(json::value_t::null).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::object).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::string).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::boolean).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::number_integer).get(), std::logic_error); + CHECK_THROWS_AS(json(json::value_t::number_float).get(), std::logic_error); + } + } + + SECTION("get an array (implicit)") + { + json::array_t a_reference {json(1), json(2.2), json(false), json("string"), json()}; + json j(a_reference); + + SECTION("json::array_t") + { + json::array_t a = j; + CHECK(json(a) == j); + } + + SECTION("std::list") + { + std::list a = j; + CHECK(json(a) == j); + } + + SECTION("std::forward_list") + { + std::forward_list a = j; + CHECK(json(a) == j); + } + + SECTION("std::vector") + { + std::vector a = j; + CHECK(json(a) == j); + } + + SECTION("std::deque") + { + std::deque a = j; + CHECK(json(a) == j); + } } }