From ca29a1e5e426904d9946fb646f0bfb2dcca5d7cc Mon Sep 17 00:00:00 2001 From: domsoll <76645975+domsoll@users.noreply.github.com> Date: Tue, 29 Dec 2020 19:22:43 +0100 Subject: [PATCH] CHG: expect strict size check in std::array test case ENH: test cases for std::pair and std::tuple added --- test/src/unit-conversions.cpp | 101 +++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 7 deletions(-) diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index de8040c87..eb20e832f 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -41,6 +41,8 @@ using nlohmann::json; #include #include #include +#include +#include #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 #define JSON_HAS_CPP_17 @@ -1488,7 +1490,7 @@ TEST_CASE("value conversion") SECTION("std::array") { j1.get>(); - j2.get>(); + j2.get>(); j3.get>(); j4.get>(); j5.get>(); @@ -1496,17 +1498,17 @@ TEST_CASE("value conversion") SECTION("std::array is larger than JSON") { std::array arr6 = {{1, 2, 3, 4, 5, 6}}; - CHECK_THROWS_AS(j1.get_to(arr6), json::out_of_range&); - CHECK_THROWS_WITH(j1.get_to(arr6), "[json.exception.out_of_range.401] " - "array index 4 is out of range"); + CHECK_THROWS_AS(j1.get_to(arr6), json::type_error&); + CHECK_THROWS_WITH(j1.get_to(arr6), "[json.exception.type_error.302] " + "array size must be 6, but is 4"); } SECTION("std::array is smaller than JSON") { std::array arr2 = {{8, 9}}; - j1.get_to(arr2); - CHECK(arr2[0] == 1); - CHECK(arr2[1] == 2); + CHECK_THROWS_AS(j1.get_to(arr2), json::type_error&); + CHECK_THROWS_WITH(j1.get_to(arr2), "[json.exception.type_error.302] " + "array size must be 2, but is 4"); } } @@ -1639,6 +1641,91 @@ TEST_CASE("value conversion") "[json.exception.type_error.302] type must be array, but is null"); } } + + SECTION("other STL containers") + { + json j1 = {1, 2}; + json j2 = {1u, 2u}; + json j3 = {1.2, 2.3}; + json j4 = {true, false}; + json j5 = {"one", "two"}; + json j6 = {1, "2"}; + json j7 = {1, 2.3}; + json j8 = {1, false}; + json j9 = {1, 2, 3}; + json j10 = {1}; + + SECTION("std::pair") + { + j1.get>(); + j2.get>(); + j3.get>(); + j4.get>(); + j5.get>(); + j6.get>(); + j7.get>(); + j8.get>(); + + SECTION("std::pair is larger than JSON") + { + std::pair p; + CHECK_THROWS_AS(j10.get_to(p), json::type_error&); + CHECK_THROWS_WITH(j10.get_to(p), "[json.exception.type_error.302] " + "array size must be 2, but is 1"); + } + + SECTION("std::pair is smaller than JSON") + { + std::pair p; + CHECK_THROWS_AS(j9.get_to(p), json::type_error&); + CHECK_THROWS_WITH(j9.get_to(p), "[json.exception.type_error.302] " + "array size must be 2, but is 3"); + } + } + + SECTION("std::tuple") + { + j1.get>(); + j2.get>(); + j3.get>(); + j4.get>(); + j5.get>(); + j6.get>(); + j7.get>(); + j8.get>(); + j9.get>(); + j10.get>(); + + SECTION("std::tuple is larger than JSON") + { + std::tuple t; + CHECK_THROWS_AS(j10.get_to(t), json::type_error&); + CHECK_THROWS_WITH(j10.get_to(t), "[json.exception.type_error.302] " + "array size must be 2, but is 1"); + } + + SECTION("std::tuple is smaller than JSON") + { + std::tuple t; + CHECK_THROWS_AS(j9.get_to(t), json::type_error&); + CHECK_THROWS_WITH(j9.get_to(t), "[json.exception.type_error.302] " + "array size must be 2, but is 3"); + } + } + + SECTION("exception in case of a non-object type") + { + CHECK_THROWS_AS((json().get>()), json::type_error&); + CHECK_THROWS_AS((json().get>()), json::type_error&); + + CHECK_THROWS_WITH( + (json().get>()), + "[json.exception.type_error.302] type must be array, but is null"); + CHECK_THROWS_WITH( + (json().get>()), + "[json.exception.type_error.302] type must be array, but is null"); + } + } } }