Skip to content

Commit cadf5cc

Browse files
committed
Migrate tests to Catch2
Signed-off-by: Cristian Le <[email protected]>
1 parent 23934ca commit cadf5cc

29 files changed

+369
-191
lines changed

test/CMakeLists.txt

+27-26
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ project(nlohmann_json_schema_validator_test
99
LANGUAGES CXX
1010
)
1111

12+
set(CMAKE_CXX_STANDARD 17)
13+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
14+
set(CMAKE_CXX_EXTENSIONS OFF)
15+
1216
#[==============================================================================================[
1317
# Options #
1418
]==============================================================================================]
@@ -66,12 +70,12 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
6670
FetchContent_Declare(Catch2
6771
GIT_REPOSITORY https://github.com/catchorg/Catch2
6872
GIT_TAG v3.4.0
69-
FIND_PACKAGE_ARGS CONFIG
73+
FIND_PACKAGE_ARGS 3.4.0 CONFIG
7074
)
7175
list(APPEND fetch_packages Catch2)
7276
else ()
7377
# Try to get system installed version
74-
find_package(Catch2 QUIET)
78+
find_package(Catch2 3.4.0 QUIET)
7579
if (NOT Catch2_FOUND)
7680
# If failed fetch the desired version
7781
FetchContent_Declare(Catch2
@@ -99,30 +103,27 @@ set_target_properties(json-schema-test-suite PROPERTIES
99103
target_link_libraries(json-schema-test-suite PRIVATE Catch2::Catch2WithMain nlohmann_json_schema_validator::validator)
100104
catch_discover_tests(json-schema-test-suite)
101105

102-
set(PIPE_IN_TEST_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/test-pipe-in.sh)
103-
104-
# built-in tests
105-
function(add_test_simple_schema name schema instance)
106-
add_test(
107-
NAME ${name}
108-
COMMAND ${PIPE_IN_TEST_SCRIPT}
109-
$<TARGET_FILE:json-schema-validate>
110-
${schema}
111-
${instance}
112-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
113-
endfunction()
114-
115-
file(GLOB TEST_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/*)
116-
117-
foreach(DIR ${TEST_DIRS})
118-
if(IS_DIRECTORY ${DIR})
119-
add_subdirectory(${DIR})
120-
endif()
121-
endforeach()
122-
123-
add_executable(uri uri.cpp)
124-
target_link_libraries(uri nlohmann_json_schema_validator)
125-
add_test(NAME uri COMMAND uri)
106+
target_sources(json-schema-test-suite PRIVATE
107+
test_uri.cpp
108+
utils.cpp
109+
)
110+
target_include_directories(json-schema-test-suite PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
111+
foreach (issue IN ITEMS
112+
issue-9
113+
issue-12
114+
issue-27
115+
issue-48
116+
issue-54
117+
issue-75
118+
issue-93
119+
issue-96
120+
issue-100
121+
issue-101
122+
issue-143
123+
issue-209
124+
)
125+
target_sources(json-schema-test-suite PRIVATE ${issue}/test_issue.cpp)
126+
endforeach ()
126127

127128
add_executable(errors errors.cpp)
128129
target_link_libraries(errors nlohmann_json_schema_validator)

test/issue-100/CMakeLists.txt

-6
This file was deleted.

test/issue-100/test_issue.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
#include <catch2/matchers/catch_matchers_all.hpp>
6+
7+
#include "utils.h"
8+
9+
using namespace std::literals;
10+
using Catch::Matchers::ContainsSubstring;
11+
using Catch::Matchers::MessageMatches;
12+
13+
TEST_CASE_METHOD(JsonValidateFixture, "issue-100")
14+
{
15+
// Change the working directory to the issue path
16+
auto path = std::filesystem::path(__FILE__).parent_path();
17+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
18+
REQUIRE_THROWS_MATCHES(validate("schema.json"sv, "instance.json"sv), std::invalid_argument, MessageMatches(ContainsSubstring("undefined references: [random_ref]")));
19+
}

test/issue-101/CMakeLists.txt

-6
This file was deleted.

test/issue-101/test_issue.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
#include <catch2/matchers/catch_matchers_all.hpp>
6+
7+
#include "utils.h"
8+
9+
using namespace std::literals;
10+
using Catch::Matchers::Matches;
11+
using Catch::Matchers::MessageMatches;
12+
13+
TEST_CASE_METHOD(JsonValidateFixture, "issue-101")
14+
{
15+
// Change the working directory to the issue path
16+
auto path = std::filesystem::path(__FILE__).parent_path();
17+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
18+
REQUIRE_THROWS_MATCHES(validate("schema.json"sv, "instance.json"sv), std::invalid_argument,
19+
MessageMatches(Matches("invalid JSON-type.*/properties/required.*expected: boolean or object.*")));
20+
}

test/issue-12/CMakeLists.txt

-3
This file was deleted.

test/issue-12/test_issue.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
6+
#include "utils.h"
7+
8+
using namespace std::literals;
9+
10+
TEST_CASE_METHOD(JsonValidateFixture, "issue-12")
11+
{
12+
// Change the working directory to the issue path
13+
auto path = std::filesystem::path(__FILE__).parent_path();
14+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
15+
REQUIRE_NOTHROW(validate("schema.json"sv, "instance.json"sv));
16+
}

test/issue-143/CMakeLists.txt

-13
This file was deleted.

test/issue-143/test_issue.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
#include <catch2/matchers/catch_matchers_all.hpp>
6+
7+
#include "utils.h"
8+
9+
using namespace std::literals;
10+
using Catch::Matchers::ContainsSubstring;
11+
using Catch::Matchers::MessageMatches;
12+
13+
TEST_CASE_METHOD(JsonValidateFixture, "issue-143")
14+
{
15+
// Change the working directory to the issue path
16+
auto path = std::filesystem::path(__FILE__).parent_path();
17+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
18+
CHECK_THROWS_MATCHES(validate("schema.json"sv, "instance-fail-1.json"sv), std::invalid_argument,
19+
MessageMatches(ContainsSubstring("At /ref1 of \"a\" - unexpected instance type")));
20+
CHECK_THROWS_MATCHES(validate("schema.json"sv, "instance-fail-a.json"sv), std::invalid_argument,
21+
MessageMatches(ContainsSubstring("At /refa of 12 - unexpected instance type")));
22+
CHECK_NOTHROW(validate("schema.json"sv, "instance.json"sv));
23+
}

test/issue-209/test_issue.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
6+
#include "utils.h"
7+
8+
using namespace std::literals;
9+
10+
TEST_CASE_METHOD(JsonValidateFixture, "issue-209")
11+
{
12+
// Change the working directory to the issue path
13+
auto path = std::filesystem::path(__FILE__).parent_path();
14+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
15+
REQUIRE_NOTHROW(validate("entities.schema.json"sv, "instance.json"sv));
16+
}

test/issue-27/CMakeLists.txt

-6
This file was deleted.

test/issue-27/test_issue.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
#include <catch2/matchers/catch_matchers_all.hpp>
6+
7+
#include "utils.h"
8+
9+
using namespace std::literals;
10+
using Catch::Matchers::ContainsSubstring;
11+
using Catch::Matchers::MessageMatches;
12+
13+
TEST_CASE_METHOD(JsonValidateFixture, "issue-27")
14+
{
15+
// Change the working directory to the issue path
16+
auto path = std::filesystem::path(__FILE__).parent_path();
17+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
18+
REQUIRE_THROWS_MATCHES(validate("schema.json"sv, "instance.json"sv), std::invalid_argument, MessageMatches(ContainsSubstring("instance exceeds maximum")));
19+
}

test/issue-48/CMakeLists.txt

-3
This file was deleted.

test/issue-48/test_issue.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
6+
#include "utils.h"
7+
8+
using namespace std::literals;
9+
10+
TEST_CASE_METHOD(JsonValidateFixture, "issue-48")
11+
{
12+
// Change the working directory to the issue path
13+
auto path = std::filesystem::path(__FILE__).parent_path();
14+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
15+
REQUIRE_NOTHROW(validate("schema.json"sv, "instance.json"sv));
16+
}

test/issue-54/CMakeLists.txt

-3
This file was deleted.

test/issue-54/test_issue.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
6+
#include "utils.h"
7+
8+
using namespace std::literals;
9+
10+
TEST_CASE_METHOD(JsonValidateFixture, "issue-54")
11+
{
12+
// Change the working directory to the issue path
13+
auto path = std::filesystem::path(__FILE__).parent_path();
14+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
15+
REQUIRE_NOTHROW(validate("schema.json"sv, "instance.json"sv));
16+
}

test/issue-75/CMakeLists.txt

-3
This file was deleted.

test/issue-75/test_issue.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
6+
#include "utils.h"
7+
8+
using namespace std::literals;
9+
10+
TEST_CASE_METHOD(JsonValidateFixture, "issue-75")
11+
{
12+
// Change the working directory to the issue path
13+
auto path = std::filesystem::path(__FILE__).parent_path();
14+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
15+
REQUIRE_NOTHROW(validate("schema.json"sv, "instance.json"sv));
16+
}

test/issue-9/CMakeLists.txt

-3
This file was deleted.

test/issue-9/test_issue.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <filesystem>
2+
#include <string_view>
3+
4+
#include <catch2/catch_test_macros.hpp>
5+
6+
#include "utils.h"
7+
8+
using namespace std::literals;
9+
10+
TEST_CASE_METHOD(JsonValidateFixture, "issue-9")
11+
{
12+
// Change the working directory to the issue path
13+
auto path = std::filesystem::path(__FILE__).parent_path();
14+
REQUIRE_NOTHROW(std::filesystem::current_path(path));
15+
REQUIRE_NOTHROW(validate("base.json"sv, "instance.json"sv));
16+
}

test/issue-93/CMakeLists.txt

-7
This file was deleted.

test/issue-93/issue-93.cpp

-55
This file was deleted.

0 commit comments

Comments
 (0)