CHG: expect strict size check in std::array test case

ENH: test cases for std::pair and std::tuple added
This commit is contained in:
domsoll 2020-12-29 19:22:43 +01:00
parent ba239535a3
commit ca29a1e5e4

View File

@ -41,6 +41,8 @@ using nlohmann::json;
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <utility>
#include <tuple>
#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<std::array<int, 4>>();
j2.get<std::array<unsigned int, 3>>();
j2.get<std::array<unsigned int, 4>>();
j3.get<std::array<double, 4>>();
j4.get<std::array<bool, 3>>();
j5.get<std::array<std::string, 3>>();
@ -1496,17 +1498,17 @@ TEST_CASE("value conversion")
SECTION("std::array is larger than JSON")
{
std::array<int, 6> 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<int, 2> 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<std::pair<int, int>>();
j2.get<std::pair<unsigned int, unsigned int>>();
j3.get<std::pair<double, double>>();
j4.get<std::pair<bool, bool>>();
j5.get<std::pair<std::string, std::string>>();
j6.get<std::pair<int, std::string>>();
j7.get<std::pair<int, double>>();
j8.get<std::pair<int, bool>>();
SECTION("std::pair is larger than JSON")
{
std::pair<int, int> 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<int, int> 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<std::tuple<int, int>>();
j2.get<std::tuple<unsigned int, unsigned int>>();
j3.get<std::tuple<double, double>>();
j4.get<std::tuple<bool, bool>>();
j5.get<std::tuple<std::string, std::string>>();
j6.get<std::tuple<int, std::string>>();
j7.get<std::tuple<int, double>>();
j8.get<std::tuple<int, bool>>();
j9.get<std::tuple<int, int, int>>();
j10.get<std::tuple<int>>();
SECTION("std::tuple is larger than JSON")
{
std::tuple<int, int> 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<int, int> 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<std::pair<int, int>>()), json::type_error&);
CHECK_THROWS_AS((json().get<std::tuple<int, int>>()), json::type_error&);
CHECK_THROWS_WITH(
(json().get<std::pair<int, int>>()),
"[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH(
(json().get<std::tuple<int, int>>()),
"[json.exception.type_error.302] type must be array, but is null");
}
}
}
}