1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-27 00:41:05 +03:00

🔨 added user-defined exceptions 305

This commit is contained in:
Niels Lohmann
2017-03-05 19:08:12 +01:00
parent f473f74fb0
commit aa842b4a27
4 changed files with 124 additions and 112 deletions

View File

@ -447,106 +447,118 @@ TEST_CASE("element access 2")
const json j_const_nonobject(j_nonobject);
CHECK_NOTHROW(j_nonobject["foo"]);
CHECK_NOTHROW(j_nonobject2[json::object_t::key_type("foo")]);
CHECK_THROWS_AS(j_const_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_WITH(j_const_nonobject["foo"], "cannot use operator[] with null");
CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_WITH(j_const_nonobject["foo"], "[json.exception.type_error.305] cannot use operator[] with null");
CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with null");
"[json.exception.type_error.305] cannot use operator[] with null");
}
SECTION("boolean")
{
json j_nonobject(json::value_t::boolean);
const json j_const_nonobject(j_nonobject);
CHECK_THROWS_AS(j_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_WITH(j_nonobject["foo"], "cannot use operator[] with boolean");
CHECK_THROWS_AS(j_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_WITH(j_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with boolean");
CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with boolean");
CHECK_THROWS_WITH(j_const_nonobject["foo"], "cannot use operator[] with boolean");
"[json.exception.type_error.305] cannot use operator[] with boolean");
CHECK_THROWS_WITH(j_const_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with boolean");
CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with boolean");
"[json.exception.type_error.305] cannot use operator[] with boolean");
}
SECTION("string")
{
json j_nonobject(json::value_t::string);
const json j_const_nonobject(j_nonobject);
CHECK_THROWS_AS(j_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_WITH(j_nonobject["foo"], "cannot use operator[] with string");
CHECK_THROWS_AS(j_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_WITH(j_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with string");
CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with string");
CHECK_THROWS_WITH(j_const_nonobject["foo"], "cannot use operator[] with string");
"[json.exception.type_error.305] cannot use operator[] with string");
CHECK_THROWS_WITH(j_const_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with string");
CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with string");
"[json.exception.type_error.305] cannot use operator[] with string");
}
SECTION("array")
{
json j_nonobject(json::value_t::array);
const json j_const_nonobject(j_nonobject);
CHECK_THROWS_AS(j_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_WITH(j_nonobject["foo"], "cannot use operator[] with array");
CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], "cannot use operator[] with array");
CHECK_THROWS_WITH(j_const_nonobject["foo"], "cannot use operator[] with array");
CHECK_THROWS_AS(j_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_WITH(j_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with array");
CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], "[json.exception.type_error.305] cannot use operator[] with array");
CHECK_THROWS_WITH(j_const_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with array");
CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with array");
"[json.exception.type_error.305] cannot use operator[] with array");
}
SECTION("number (integer)")
{
json j_nonobject(json::value_t::number_integer);
const json j_const_nonobject(j_nonobject);
CHECK_THROWS_AS(j_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_WITH(j_nonobject["foo"], "cannot use operator[] with number");
CHECK_THROWS_AS(j_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_WITH(j_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject["foo"], "cannot use operator[] with number");
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with number");
"[json.exception.type_error.305] cannot use operator[] with number");
}
SECTION("number (unsigned)")
{
json j_nonobject(json::value_t::number_unsigned);
const json j_const_nonobject(j_nonobject);
CHECK_THROWS_AS(j_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_WITH(j_nonobject["foo"], "cannot use operator[] with number");
CHECK_THROWS_AS(j_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_WITH(j_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject["foo"], "cannot use operator[] with number");
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with number");
"[json.exception.type_error.305] cannot use operator[] with number");
}
SECTION("number (floating-point)")
{
json j_nonobject(json::value_t::number_float);
const json j_const_nonobject(j_nonobject);
CHECK_THROWS_AS(j_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], std::domain_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], std::domain_error);
CHECK_THROWS_WITH(j_nonobject["foo"], "cannot use operator[] with number");
CHECK_THROWS_AS(j_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error);
CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error);
CHECK_THROWS_WITH(j_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject["foo"], "cannot use operator[] with number");
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject["foo"],
"[json.exception.type_error.305] cannot use operator[] with number");
CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")],
"cannot use operator[] with number");
"[json.exception.type_error.305] cannot use operator[] with number");
}
}
}