Migrate tests to Catch2
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
This commit is contained in:
parent
23934ca763
commit
cadf5cc226
@ -9,6 +9,10 @@ project(nlohmann_json_schema_validator_test
|
|||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
#[==============================================================================================[
|
#[==============================================================================================[
|
||||||
# Options #
|
# Options #
|
||||||
]==============================================================================================]
|
]==============================================================================================]
|
||||||
@ -66,12 +70,12 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
|
|||||||
FetchContent_Declare(Catch2
|
FetchContent_Declare(Catch2
|
||||||
GIT_REPOSITORY https://github.com/catchorg/Catch2
|
GIT_REPOSITORY https://github.com/catchorg/Catch2
|
||||||
GIT_TAG v3.4.0
|
GIT_TAG v3.4.0
|
||||||
FIND_PACKAGE_ARGS CONFIG
|
FIND_PACKAGE_ARGS 3.4.0 CONFIG
|
||||||
)
|
)
|
||||||
list(APPEND fetch_packages Catch2)
|
list(APPEND fetch_packages Catch2)
|
||||||
else ()
|
else ()
|
||||||
# Try to get system installed version
|
# Try to get system installed version
|
||||||
find_package(Catch2 QUIET)
|
find_package(Catch2 3.4.0 QUIET)
|
||||||
if (NOT Catch2_FOUND)
|
if (NOT Catch2_FOUND)
|
||||||
# If failed fetch the desired version
|
# If failed fetch the desired version
|
||||||
FetchContent_Declare(Catch2
|
FetchContent_Declare(Catch2
|
||||||
@ -99,31 +103,28 @@ set_target_properties(json-schema-test-suite PROPERTIES
|
|||||||
target_link_libraries(json-schema-test-suite PRIVATE Catch2::Catch2WithMain nlohmann_json_schema_validator::validator)
|
target_link_libraries(json-schema-test-suite PRIVATE Catch2::Catch2WithMain nlohmann_json_schema_validator::validator)
|
||||||
catch_discover_tests(json-schema-test-suite)
|
catch_discover_tests(json-schema-test-suite)
|
||||||
|
|
||||||
set(PIPE_IN_TEST_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/test-pipe-in.sh)
|
target_sources(json-schema-test-suite PRIVATE
|
||||||
|
test_uri.cpp
|
||||||
# built-in tests
|
utils.cpp
|
||||||
function(add_test_simple_schema name schema instance)
|
)
|
||||||
add_test(
|
target_include_directories(json-schema-test-suite PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
NAME ${name}
|
foreach (issue IN ITEMS
|
||||||
COMMAND ${PIPE_IN_TEST_SCRIPT}
|
issue-9
|
||||||
$<TARGET_FILE:json-schema-validate>
|
issue-12
|
||||||
${schema}
|
issue-27
|
||||||
${instance}
|
issue-48
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
issue-54
|
||||||
endfunction()
|
issue-75
|
||||||
|
issue-93
|
||||||
file(GLOB TEST_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/*)
|
issue-96
|
||||||
|
issue-100
|
||||||
foreach(DIR ${TEST_DIRS})
|
issue-101
|
||||||
if(IS_DIRECTORY ${DIR})
|
issue-143
|
||||||
add_subdirectory(${DIR})
|
issue-209
|
||||||
endif()
|
)
|
||||||
|
target_sources(json-schema-test-suite PRIVATE ${issue}/test_issue.cpp)
|
||||||
endforeach ()
|
endforeach ()
|
||||||
|
|
||||||
add_executable(uri uri.cpp)
|
|
||||||
target_link_libraries(uri nlohmann_json_schema_validator)
|
|
||||||
add_test(NAME uri COMMAND uri)
|
|
||||||
|
|
||||||
add_executable(errors errors.cpp)
|
add_executable(errors errors.cpp)
|
||||||
target_link_libraries(errors nlohmann_json_schema_validator)
|
target_link_libraries(errors nlohmann_json_schema_validator)
|
||||||
add_test(NAME errors COMMAND errors)
|
add_test(NAME errors COMMAND errors)
|
||||||
|
|||||||
@ -1,6 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::100
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
set_tests_properties(Issue::100
|
|
||||||
PROPERTIES
|
|
||||||
WILL_FAIL 1)
|
|
||||||
19
test/issue-100/test_issue.cpp
Normal file
19
test/issue-100/test_issue.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <catch2/matchers/catch_matchers_all.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
using Catch::Matchers::ContainsSubstring;
|
||||||
|
using Catch::Matchers::MessageMatches;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-100")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_THROWS_MATCHES(validate("schema.json"sv, "instance.json"sv), std::invalid_argument, MessageMatches(ContainsSubstring("undefined references: [random_ref]")));
|
||||||
|
}
|
||||||
@ -1,6 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::101
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
set_tests_properties(Issue::101
|
|
||||||
PROPERTIES
|
|
||||||
WILL_FAIL 1)
|
|
||||||
20
test/issue-101/test_issue.cpp
Normal file
20
test/issue-101/test_issue.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <catch2/matchers/catch_matchers_all.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
using Catch::Matchers::Matches;
|
||||||
|
using Catch::Matchers::MessageMatches;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-101")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_THROWS_MATCHES(validate("schema.json"sv, "instance.json"sv), std::invalid_argument,
|
||||||
|
MessageMatches(Matches("invalid JSON-type.*/properties/required.*expected: boolean or object.*")));
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::12
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
16
test/issue-12/test_issue.cpp
Normal file
16
test/issue-12/test_issue.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-12")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_NOTHROW(validate("schema.json"sv, "instance.json"sv));
|
||||||
|
}
|
||||||
@ -1,13 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::143-1
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance-fail-1.json)
|
|
||||||
add_test_simple_schema(Issue::143-a
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance-fail-a.json)
|
|
||||||
add_test_simple_schema(Issue::143-ok
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
|
|
||||||
set_tests_properties(Issue::143-1 Issue::143-a
|
|
||||||
PROPERTIES
|
|
||||||
WILL_FAIL 1)
|
|
||||||
23
test/issue-143/test_issue.cpp
Normal file
23
test/issue-143/test_issue.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <catch2/matchers/catch_matchers_all.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
using Catch::Matchers::ContainsSubstring;
|
||||||
|
using Catch::Matchers::MessageMatches;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-143")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
CHECK_THROWS_MATCHES(validate("schema.json"sv, "instance-fail-1.json"sv), std::invalid_argument,
|
||||||
|
MessageMatches(ContainsSubstring("At /ref1 of \"a\" - unexpected instance type")));
|
||||||
|
CHECK_THROWS_MATCHES(validate("schema.json"sv, "instance-fail-a.json"sv), std::invalid_argument,
|
||||||
|
MessageMatches(ContainsSubstring("At /refa of 12 - unexpected instance type")));
|
||||||
|
CHECK_NOTHROW(validate("schema.json"sv, "instance.json"sv));
|
||||||
|
}
|
||||||
16
test/issue-209/test_issue.cpp
Normal file
16
test/issue-209/test_issue.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-209")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_NOTHROW(validate("entities.schema.json"sv, "instance.json"sv));
|
||||||
|
}
|
||||||
@ -1,6 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::27
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
set_tests_properties(Issue::27
|
|
||||||
PROPERTIES
|
|
||||||
WILL_FAIL 1)
|
|
||||||
19
test/issue-27/test_issue.cpp
Normal file
19
test/issue-27/test_issue.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <catch2/matchers/catch_matchers_all.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
using Catch::Matchers::ContainsSubstring;
|
||||||
|
using Catch::Matchers::MessageMatches;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-27")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_THROWS_MATCHES(validate("schema.json"sv, "instance.json"sv), std::invalid_argument, MessageMatches(ContainsSubstring("instance exceeds maximum")));
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::48
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
16
test/issue-48/test_issue.cpp
Normal file
16
test/issue-48/test_issue.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-48")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_NOTHROW(validate("schema.json"sv, "instance.json"sv));
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::54
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
16
test/issue-54/test_issue.cpp
Normal file
16
test/issue-54/test_issue.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-54")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_NOTHROW(validate("schema.json"sv, "instance.json"sv));
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::75
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
16
test/issue-75/test_issue.cpp
Normal file
16
test/issue-75/test_issue.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-75")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_NOTHROW(validate("schema.json"sv, "instance.json"sv));
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::9
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/base.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
16
test/issue-9/test_issue.cpp
Normal file
16
test/issue-9/test_issue.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-9")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_NOTHROW(validate("base.json"sv, "instance.json"sv));
|
||||||
|
}
|
||||||
@ -1,7 +0,0 @@
|
|||||||
|
|
||||||
add_executable(issue-93 issue-93.cpp)
|
|
||||||
target_link_libraries(issue-93 nlohmann_json_schema_validator)
|
|
||||||
|
|
||||||
add_test(NAME issue-93
|
|
||||||
COMMAND issue-93
|
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
|
||||||
@ -1,55 +0,0 @@
|
|||||||
#include <nlohmann/json-schema.hpp>
|
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
using nlohmann::json;
|
|
||||||
using nlohmann::json_uri;
|
|
||||||
using nlohmann::json_schema::json_validator;
|
|
||||||
|
|
||||||
static const auto expected_patch = R"(
|
|
||||||
[{"op":"add","path":"/0/renderable/bg","value":"Black"}]
|
|
||||||
)"_json;
|
|
||||||
|
|
||||||
static const auto instance = R"(
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"name":"player",
|
|
||||||
"renderable": {
|
|
||||||
"fg":"White"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
)"_json;
|
|
||||||
|
|
||||||
static void loader(const json_uri &uri, json &schema)
|
|
||||||
{
|
|
||||||
std::string filename = "./" + uri.path();
|
|
||||||
std::ifstream lf(filename);
|
|
||||||
if (!lf.good())
|
|
||||||
throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename);
|
|
||||||
try {
|
|
||||||
lf >> schema;
|
|
||||||
} catch (const std::exception &e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
json_validator validator(loader);
|
|
||||||
|
|
||||||
std::fstream f("blueprints.schema.json");
|
|
||||||
|
|
||||||
json schema;
|
|
||||||
f >> schema;
|
|
||||||
|
|
||||||
validator.set_root_schema(schema);
|
|
||||||
|
|
||||||
auto missing_default_patch = validator.validate(instance);
|
|
||||||
|
|
||||||
std::cerr << missing_default_patch << "\n";
|
|
||||||
std::cerr << expected_patch << "\n";
|
|
||||||
|
|
||||||
return missing_default_patch != expected_patch;
|
|
||||||
}
|
|
||||||
37
test/issue-93/test_issue.cpp
Normal file
37
test/issue-93/test_issue.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <catch2/matchers/catch_matchers_all.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
using Catch::Matchers::ContainsSubstring;
|
||||||
|
using Catch::Matchers::MessageMatches;
|
||||||
|
|
||||||
|
static const auto instance = R"(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name":"player",
|
||||||
|
"renderable": {
|
||||||
|
"fg":"White"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)"_json;
|
||||||
|
|
||||||
|
static const auto expected_patch = R"(
|
||||||
|
[{"op":"add","path":"/0/renderable/bg","value":"Black"}]
|
||||||
|
)"_json;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-93")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
nlohmann::json patch;
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
auto schema_path = path / "blueprints.schema.json";
|
||||||
|
REQUIRE_NOTHROW(patch = validate(schema_path, instance));
|
||||||
|
REQUIRE(patch == expected_patch);
|
||||||
|
}
|
||||||
@ -1,6 +0,0 @@
|
|||||||
add_test_simple_schema(Issue::96
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/schema.json
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/instance.json)
|
|
||||||
set_tests_properties(Issue::96
|
|
||||||
PROPERTIES
|
|
||||||
WILL_FAIL 1)
|
|
||||||
19
test/issue-96/test_issue.cpp
Normal file
19
test/issue-96/test_issue.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <catch2/matchers/catch_matchers_all.hpp>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
using Catch::Matchers::ContainsSubstring;
|
||||||
|
using Catch::Matchers::MessageMatches;
|
||||||
|
|
||||||
|
TEST_CASE_METHOD(JsonValidateFixture, "issue-96")
|
||||||
|
{
|
||||||
|
// Change the working directory to the issue path
|
||||||
|
auto path = std::filesystem::path(__FILE__).parent_path();
|
||||||
|
REQUIRE_NOTHROW(std::filesystem::current_path(path));
|
||||||
|
REQUIRE_THROWS_MATCHES(validate("schema.json"sv, "instance.json"sv), std::invalid_argument, MessageMatches(ContainsSubstring("instance exceeds maximum")));
|
||||||
|
}
|
||||||
@ -1,13 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# all argument are considered as a program to call (with its arguments),
|
|
||||||
# the last argument is read from stdin via '<'
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
arr=( "$@" )
|
|
||||||
|
|
||||||
input=${arr[${#arr[@]}-1]}
|
|
||||||
unset 'arr[${#arr[@]}-1]'
|
|
||||||
|
|
||||||
${arr[@]} < $input
|
|
||||||
@ -6,56 +6,49 @@
|
|||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include <cstdlib>
|
|
||||||
#include <nlohmann/json-schema.hpp>
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include <nlohmann/json-schema.hpp>
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
using nlohmann::json_uri;
|
using nlohmann::json_uri;
|
||||||
|
|
||||||
static int errors;
|
namespace nlohmann
|
||||||
|
|
||||||
static void EXPECT_EQ(const std::string &a, const std::string &b)
|
|
||||||
{
|
{
|
||||||
if (a != b) {
|
bool operator==(const json_uri &a, const std::string &b)
|
||||||
std::cerr << "Failed: '" << a << "' != '" << b << "'\n";
|
|
||||||
errors++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void EXPECT_EQ(const nlohmann::json_uri &a, const std::string &b)
|
|
||||||
{
|
{
|
||||||
EXPECT_EQ(a.to_string(), b);
|
return a.to_string() == b;
|
||||||
}
|
}
|
||||||
|
} // namespace nlohmann
|
||||||
|
|
||||||
static void paths(json_uri start,
|
void paths(json_uri start,
|
||||||
const std::string &full,
|
const std::string &full,
|
||||||
const std::string &full_path,
|
const std::string &full_path,
|
||||||
const std::string &no_path)
|
const std::string &no_path)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(start, full + " # ");
|
CHECK(start == full + " # ");
|
||||||
|
|
||||||
auto a = start.derive("other.json");
|
auto a = start.derive("other.json");
|
||||||
EXPECT_EQ(a, full_path + "/other.json # ");
|
CHECK(a == full_path + "/other.json # ");
|
||||||
|
|
||||||
auto b = a.derive("base.json");
|
auto b = a.derive("base.json");
|
||||||
EXPECT_EQ(b, full_path + "/base.json # ");
|
CHECK(b == full_path + "/base.json # ");
|
||||||
|
|
||||||
auto c = b.derive("subdir/base.json");
|
auto c = b.derive("subdir/base.json");
|
||||||
EXPECT_EQ(c, full_path + "/subdir/base.json # ");
|
CHECK(c == full_path + "/subdir/base.json # ");
|
||||||
|
|
||||||
auto d = c.derive("subdir2/base.json");
|
auto d = c.derive("subdir2/base.json");
|
||||||
EXPECT_EQ(d, full_path + "/subdir/subdir2/base.json # ");
|
CHECK(d == full_path + "/subdir/subdir2/base.json # ");
|
||||||
|
|
||||||
auto e = c.derive("/subdir2/base.json");
|
auto e = c.derive("/subdir2/base.json");
|
||||||
EXPECT_EQ(e, no_path + "/subdir2/base.json # ");
|
CHECK(e == no_path + "/subdir2/base.json # ");
|
||||||
|
|
||||||
auto f = c.derive("new.json");
|
auto f = c.derive("new.json");
|
||||||
EXPECT_EQ(f, full_path + "/subdir/new.json # ");
|
CHECK(f == full_path + "/subdir/new.json # ");
|
||||||
|
|
||||||
auto g = c.derive("/new.json");
|
auto g = c.derive("/new.json");
|
||||||
EXPECT_EQ(g, no_path + "/new.json # ");
|
CHECK(g == no_path + "/new.json # ");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pointer_plain_name(json_uri start,
|
static void pointer_plain_name(json_uri start,
|
||||||
@ -64,26 +57,26 @@ static void pointer_plain_name(json_uri start,
|
|||||||
const std::string &no_path)
|
const std::string &no_path)
|
||||||
{
|
{
|
||||||
auto a = start.derive("#/json/path");
|
auto a = start.derive("#/json/path");
|
||||||
EXPECT_EQ(a, full + " # /json/path");
|
CHECK(a == full + " # /json/path");
|
||||||
|
|
||||||
a = start.derive("#/json/special_%22");
|
a = start.derive("#/json/special_%22");
|
||||||
EXPECT_EQ(a, full + " # /json/special_\"");
|
CHECK(a == full + " # /json/special_\"");
|
||||||
|
|
||||||
a = a.derive("#foo");
|
a = a.derive("#foo");
|
||||||
EXPECT_EQ(a, full + " # foo");
|
CHECK(a == full + " # foo");
|
||||||
|
|
||||||
a = a.derive("#foo/looks_like_json/poiner/but/isnt");
|
a = a.derive("#foo/looks_like_json/poiner/but/isnt");
|
||||||
EXPECT_EQ(a, full + " # foo/looks_like_json/poiner/but/isnt");
|
CHECK(a == full + " # foo/looks_like_json/poiner/but/isnt");
|
||||||
EXPECT_EQ(a.identifier(), "foo/looks_like_json/poiner/but/isnt");
|
CHECK(a.identifier() == "foo/looks_like_json/poiner/but/isnt");
|
||||||
EXPECT_EQ(a.pointer().to_string(), "");
|
CHECK(a.pointer().to_string().empty());
|
||||||
|
|
||||||
a = a.derive("#/looks_like_json/poiner/and/it/is");
|
a = a.derive("#/looks_like_json/poiner/and/it/is");
|
||||||
EXPECT_EQ(a, full + " # /looks_like_json/poiner/and/it/is");
|
CHECK(a == full + " # /looks_like_json/poiner/and/it/is");
|
||||||
EXPECT_EQ(a.pointer().to_string(), "/looks_like_json/poiner/and/it/is");
|
CHECK(a.pointer().to_string() == "/looks_like_json/poiner/and/it/is");
|
||||||
EXPECT_EQ(a.identifier(), "");
|
CHECK(a.identifier().empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
TEST_CASE("uri")
|
||||||
{
|
{
|
||||||
json_uri empty("");
|
json_uri empty("");
|
||||||
paths(empty,
|
paths(empty,
|
||||||
@ -101,6 +94,4 @@ int main(void)
|
|||||||
"http://json-schema.org/draft-07/schema",
|
"http://json-schema.org/draft-07/schema",
|
||||||
"http://json-schema.org/draft-07",
|
"http://json-schema.org/draft-07",
|
||||||
"http://json-schema.org");
|
"http://json-schema.org");
|
||||||
|
|
||||||
return errors;
|
|
||||||
}
|
}
|
||||||
60
test/utils.cpp
Normal file
60
test/utils.cpp
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
using nlohmann::json_uri;
|
||||||
|
using nlohmann::json_schema::default_string_format_check;
|
||||||
|
using nlohmann::json_schema::json_validator;
|
||||||
|
|
||||||
|
static void loader(const json_uri &uri, json &schema)
|
||||||
|
{
|
||||||
|
std::string filename = "./" + uri.path();
|
||||||
|
std::ifstream fstream(filename);
|
||||||
|
if (!fstream.good())
|
||||||
|
throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename);
|
||||||
|
try {
|
||||||
|
fstream >> schema;
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonValidateFixture::JsonValidateFixture()
|
||||||
|
: validator(loader, default_string_format_check) {}
|
||||||
|
json JsonValidateFixture::validate(const std::filesystem::path &schema_path, const std::filesystem::path &instance_path)
|
||||||
|
{
|
||||||
|
json schema;
|
||||||
|
std::ifstream fstream{absolute(schema_path).string()};
|
||||||
|
fstream >> schema;
|
||||||
|
return validate(schema, instance_path);
|
||||||
|
}
|
||||||
|
json JsonValidateFixture::validate(const json &schema, const json &instance)
|
||||||
|
{
|
||||||
|
validator.set_root_schema(schema);
|
||||||
|
return validator.validate(instance);
|
||||||
|
}
|
||||||
|
json JsonValidateFixture::validate(const json &schema, const std::filesystem::path &instance_path)
|
||||||
|
{
|
||||||
|
json instance;
|
||||||
|
std::ifstream fstream{absolute(instance_path).string()};
|
||||||
|
fstream >> instance;
|
||||||
|
return validate(schema, instance);
|
||||||
|
}
|
||||||
|
json JsonValidateFixture::validate(std::string_view schema_path, std::string_view instance_path)
|
||||||
|
{
|
||||||
|
auto path = std::filesystem::current_path() / schema_path;
|
||||||
|
return validate(path, instance_path);
|
||||||
|
}
|
||||||
|
json JsonValidateFixture::validate(const std::filesystem::path &schema_path, std::string_view instance_path)
|
||||||
|
{
|
||||||
|
auto path = std::filesystem::current_path() / instance_path;
|
||||||
|
return validate(schema_path, path);
|
||||||
|
}
|
||||||
|
json JsonValidateFixture::validate(const std::filesystem::path &schema_path, const json &instance)
|
||||||
|
{
|
||||||
|
json schema;
|
||||||
|
std::ifstream fstream{absolute(schema_path).string()};
|
||||||
|
fstream >> schema;
|
||||||
|
return validate(schema, instance);
|
||||||
|
}
|
||||||
20
test/utils.h
Normal file
20
test/utils.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <nlohmann/json-schema.hpp>
|
||||||
|
|
||||||
|
class JsonValidateFixture
|
||||||
|
{
|
||||||
|
nlohmann::json_schema::json_validator validator;
|
||||||
|
|
||||||
|
public:
|
||||||
|
JsonValidateFixture();
|
||||||
|
nlohmann::json validate(std::string_view schema_path, std::string_view instance_path);
|
||||||
|
nlohmann::json validate(const std::filesystem::path &schema_path, std::string_view instance_path);
|
||||||
|
nlohmann::json validate(const std::filesystem::path &schema_path, const std::filesystem::path &instance_path);
|
||||||
|
nlohmann::json validate(const std::filesystem::path &schema_path, const nlohmann::json &instance);
|
||||||
|
nlohmann::json validate(const nlohmann::json &schema, const std::filesystem::path &instance_path);
|
||||||
|
nlohmann::json validate(const nlohmann::json &schema, const nlohmann::json &instance);
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user