✅ add tests for diagnostics
This commit is contained in:
parent
e9d641130d
commit
aeecc09ba1
@ -110,6 +110,7 @@ set(files
|
|||||||
src/unit-convenience.cpp
|
src/unit-convenience.cpp
|
||||||
src/unit-conversions.cpp
|
src/unit-conversions.cpp
|
||||||
src/unit-deserialization.cpp
|
src/unit-deserialization.cpp
|
||||||
|
src/unit-diagnostics.cpp
|
||||||
src/unit-element_access1.cpp
|
src/unit-element_access1.cpp
|
||||||
src/unit-element_access2.cpp
|
src/unit-element_access2.cpp
|
||||||
src/unit-hash.cpp
|
src/unit-hash.cpp
|
||||||
|
|||||||
81
test/src/unit-diagnostics.cpp
Normal file
81
test/src/unit-diagnostics.cpp
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
__ _____ _____ _____
|
||||||
|
__| | __| | | | JSON for Modern C++ (test suite)
|
||||||
|
| | |__ | | | | | | version 3.9.1
|
||||||
|
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
|
||||||
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||||
|
SPDX-License-Identifier: MIT
|
||||||
|
Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "doctest_compatibility.h"
|
||||||
|
|
||||||
|
#define JSON_DIAGNOSTICS 1
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
TEST_CASE("Better diagnostics")
|
||||||
|
{
|
||||||
|
SECTION("invalid type")
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j["a"]["b"]["c"] = 1;
|
||||||
|
std::string s;
|
||||||
|
CHECK_THROWS_WITH_AS(s = j["a"]["b"]["c"], "[json.exception.type_error.302] (/a/b/c) type must be string, but is number", json::type_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("missing key")
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j["object"]["object"] = true;
|
||||||
|
CHECK_THROWS_WITH_AS(j["object"].at("not_found"), "[json.exception.out_of_range.403] (/object) key 'not_found' not found", json::out_of_range);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("array index out of range")
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j["array"][4] = true;
|
||||||
|
CHECK_THROWS_WITH_AS(j["array"].at(5), "[json.exception.out_of_range.401] (/array) array index 5 is out of range", json::out_of_range);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("array index at wrong type")
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j["array"][4] = true;
|
||||||
|
CHECK_THROWS_WITH_AS(j["array"][4][5], "[json.exception.type_error.305] (/array/4) cannot use operator[] with a numeric argument with boolean", json::type_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("wrong iterator")
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j["array"] = json::array();
|
||||||
|
CHECK_THROWS_WITH_AS(j["array"].erase(j.begin()), "[json.exception.invalid_iterator.202] (/array) iterator does not fit current value", json::invalid_iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("JSON Pointer escaping")
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j["a/b"]["m~n"] = 1;
|
||||||
|
std::string s;
|
||||||
|
CHECK_THROWS_WITH_AS(s = j["a/b"]["m~n"], "[json.exception.type_error.302] (/a~1b/m~0n) type must be string, but is number", json::type_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1181,8 +1181,8 @@ TEST_CASE("Unicode" * doctest::skip())
|
|||||||
CHECK_NOTHROW(json::json_pointer("/" + ptr));
|
CHECK_NOTHROW(json::json_pointer("/" + ptr));
|
||||||
|
|
||||||
// check escape/unescape roundtrip
|
// check escape/unescape roundtrip
|
||||||
auto escaped = json::json_pointer::escape(ptr);
|
auto escaped = nlohmann::detail::escape(ptr);
|
||||||
json::json_pointer::unescape(escaped);
|
nlohmann::detail::unescape(escaped);
|
||||||
CHECK(escaped == ptr);
|
CHECK(escaped == ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user