Skip to content

Commit a879359

Browse files
committed
Compile tests with Catch2
1 parent 649ce7e commit a879359

File tree

5 files changed

+70
-31
lines changed

5 files changed

+70
-31
lines changed

oup.sublime-project

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757
"name": "oup_runtime_tests_run",
5858
"shell_cmd": "make -j12 oup_runtime_tests_run",
5959
},
60+
{
61+
"name": "Catch2",
62+
"shell_cmd": "make -j12 Catch2",
63+
},
64+
{
65+
"name": "Catch2WithMain",
66+
"shell_cmd": "make -j12 Catch2WithMain",
67+
},
6068
{
6169
"name": "oup_runtime_tests",
6270
"shell_cmd": "make -j12 oup_runtime_tests",
@@ -67,11 +75,7 @@
6775
},
6876
{
6977
"name": "oup_speed_benchmark",
70-
"shell_cmd": "make -j12 oup_speed_benchmark",
71-
},
72-
{
73-
"name": "snatch",
74-
"shell_cmd": "make -j12 snatch",
78+
"shell_cmd": "make -j12 oup_speed_benchmark"
7579
},
7680
],
7781
"working_dir": "$folder/build",

tests/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function(add_platform_definitions TARGET)
1717
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
1818
target_compile_options(${TARGET} PRIVATE -Wall)
1919
target_compile_options(${TARGET} PRIVATE -Wextra)
20+
target_compile_options(${TARGET} PRIVATE -Wno-nonnull)
2021
target_compile_options(${TARGET} PRIVATE -Werror)
2122
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
2223
target_compile_options(${TARGET} PRIVATE -Wall)
@@ -31,10 +32,10 @@ endfunction()
3132

3233
include(FetchContent)
3334

34-
FetchContent_Declare(snatch
35-
GIT_REPOSITORY https://github.com/cschreib/snatch.git
36-
GIT_TAG v0.1.3)
37-
FetchContent_MakeAvailable(snatch)
35+
FetchContent_Declare(catch2
36+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
37+
GIT_TAG v3.2.0)
38+
FetchContent_MakeAvailable(catch2)
3839

3940
set(RUNTIME_TEST_FILES
4041
${PROJECT_SOURCE_DIR}/tests/tests_common.cpp
@@ -61,7 +62,7 @@ set(RUNTIME_TEST_FILES
6162

6263
add_executable(oup_runtime_tests ${RUNTIME_TEST_FILES})
6364
target_link_libraries(oup_runtime_tests PRIVATE oup::oup)
64-
target_link_libraries(oup_runtime_tests PRIVATE snatch::snatch)
65+
target_link_libraries(oup_runtime_tests PRIVATE Catch2::Catch2WithMain)
6566
add_platform_definitions(oup_runtime_tests)
6667

6768
add_custom_target(oup_runtime_tests_run

tests/runtime_tests_observer_assignment_copy.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ TEMPLATE_LIST_TEST_CASE(
217217
{
218218
TestType ptr = make_pointer_deleter_1<TestType>();
219219
observer_ptr<TestType> optr{ptr};
220-
SNATCH_WARNING_PUSH;
221-
SNATCH_WARNING_DISABLE_SELF_ASSIGN;
220+
// SNATCH_WARNING_PUSH;
221+
// SNATCH_WARNING_DISABLE_SELF_ASSIGN;
222222
optr = optr;
223-
SNATCH_WARNING_POP;
223+
// SNATCH_WARNING_POP;
224224

225225
CHECK(optr.get() == ptr.get());
226226
CHECK(optr.expired() == false);
@@ -236,10 +236,10 @@ TEMPLATE_LIST_TEST_CASE(
236236

237237
{
238238
observer_ptr<TestType> optr;
239-
SNATCH_WARNING_PUSH;
240-
SNATCH_WARNING_DISABLE_SELF_ASSIGN;
239+
// SNATCH_WARNING_PUSH;
240+
// SNATCH_WARNING_DISABLE_SELF_ASSIGN;
241241
optr = optr;
242-
SNATCH_WARNING_POP;
242+
// SNATCH_WARNING_POP;
243243

244244
CHECK(optr.get() == nullptr);
245245
CHECK(optr.expired() == true);

tests/runtime_tests_observer_from_this.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ TEMPLATE_LIST_TEST_CASE(
7474
} else {
7575
REQUIRE_THROWS_MATCHES(
7676
(make_observer_from_this<TestType>(orig_ptr)), oup::bad_observer_from_this,
77-
snatch::matchers::with_what_contains{
78-
"observer_from_this() called with uninitialized control block"});
77+
Catch::Matchers::Message(
78+
"observer_from_this() called with uninitialized control block"));
7979
REQUIRE_THROWS_MATCHES(
8080
(make_const_observer_from_this<TestType>(orig_ptr)),
8181
oup::bad_observer_from_this,
82-
snatch::matchers::with_what_contains{
83-
"observer_from_this() called with uninitialized control block"});
82+
Catch::Matchers::Message(
83+
"observer_from_this() called with uninitialized control block"));
8484
}
8585

8686
CHECK_INSTANCES(1, 0);
@@ -110,12 +110,12 @@ TEMPLATE_LIST_TEST_CASE("observer from this no owner stack", "[observer_from_thi
110110
} else {
111111
REQUIRE_THROWS_MATCHES(
112112
(make_observer_from_this<TestType>(&obj)), oup::bad_observer_from_this,
113-
snatch::matchers::with_what_contains{
114-
"observer_from_this() called with uninitialized control block"});
113+
Catch::Matchers::Message(
114+
"observer_from_this() called with uninitialized control block"));
115115
REQUIRE_THROWS_MATCHES(
116116
(make_const_observer_from_this<TestType>(&obj)), oup::bad_observer_from_this,
117-
snatch::matchers::with_what_contains{
118-
"observer_from_this() called with uninitialized control block"});
117+
Catch::Matchers::Message(
118+
"observer_from_this() called with uninitialized control block"));
119119
}
120120

121121
CHECK_INSTANCES(1, 0);
@@ -147,13 +147,13 @@ TEMPLATE_LIST_TEST_CASE(
147147
} else {
148148
REQUIRE_THROWS_MATCHES(
149149
(make_observer_from_this<TestType>(orig_ptr)), oup::bad_observer_from_this,
150-
snatch::matchers::with_what_contains{
151-
"observer_from_this() called with uninitialized control block"});
150+
Catch::Matchers::Message(
151+
"observer_from_this() called with uninitialized control block"));
152152
REQUIRE_THROWS_MATCHES(
153153
(make_const_observer_from_this<TestType>(orig_ptr)),
154154
oup::bad_observer_from_this,
155-
snatch::matchers::with_what_contains{
156-
"observer_from_this() called with uninitialized control block"});
155+
Catch::Matchers::Message(
156+
"observer_from_this() called with uninitialized control block"));
157157
}
158158
}
159159

@@ -388,8 +388,8 @@ TEMPLATE_LIST_TEST_CASE("observer from this in constructor", "[observer_from_thi
388388
next_test_object_constructor_calls_observer_from_this = true;
389389
REQUIRE_THROWS_MATCHES(
390390
(make_pointer_deleter_1<TestType>()), oup::bad_observer_from_this,
391-
snatch::matchers::with_what_contains{
392-
"observer_from_this() called with uninitialized control block"});
391+
Catch::Matchers::Message(
392+
"observer_from_this() called with uninitialized control block"));
393393
next_test_object_constructor_calls_observer_from_this = false;
394394
}
395395

tests/testing.hpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
#include "snatch/snatch.hpp"
1+
#include "catch2/catch_template_test_macros.hpp"
2+
#include "catch2/catch_test_macros.hpp"
3+
#include "catch2/matchers/catch_matchers_exception.hpp"
4+
5+
namespace snatch::impl {
6+
template<typename T>
7+
constexpr std::string_view get_type_name() noexcept {
8+
#if defined(__clang__)
9+
constexpr auto prefix = std::string_view{"[T = "};
10+
constexpr auto suffix = "]";
11+
constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
12+
#elif defined(__GNUC__)
13+
constexpr auto prefix = std::string_view{"with T = "};
14+
constexpr auto suffix = "; ";
15+
constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
16+
#elif defined(_MSC_VER)
17+
constexpr auto prefix = std::string_view{"get_type_name<"};
18+
constexpr auto suffix = ">(void)";
19+
constexpr auto function = std::string_view{__FUNCSIG__};
20+
#else
21+
# error Unsupported compiler
22+
#endif
23+
24+
const auto start = function.find(prefix) + prefix.size();
25+
const auto end = function.find(suffix);
26+
const auto size = end - start;
27+
28+
return function.substr(start, size);
29+
}
30+
} // namespace snatch::impl
31+
32+
namespace snatch {
33+
template<typename T>
34+
constexpr std::string_view type_name = impl::get_type_name<T>();
35+
} // namespace snatch

0 commit comments

Comments
 (0)