Skip to content

Commit a8128d9

Browse files
Simon Kraemerarnemertz
authored andcommitted
Added GTest (based on existing Catch2 tests)
1 parent 85b62c5 commit a8128d9

10 files changed

Lines changed: 134 additions & 64 deletions

File tree

cmake/Conan.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ macro(run_conan)
1717
REQUIRES
1818
${CONAN_EXTRA_REQUIRES}
1919
catch2/2.13.3
20+
gtest/1.11.0
2021
docopt.cpp/0.6.2
2122
fmt/6.2.0
2223
spdlog/1.5.0
2324
sml/1.1.4
2425
OPTIONS
2526
${CONAN_EXTRA_OPTIONS}
27+
gtest:build_gmock=True
2628
BASIC_SETUP
2729
CMAKE_TARGETS # individual targets to link to
2830
BUILD

test/CMakeLists.txt

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,2 @@
1-
# Automatically enable catch2 to generate ctest targets
2-
if(CONAN_CATCH2_ROOT_DEBUG)
3-
include(${CONAN_CATCH2_ROOT_DEBUG}/lib/cmake/Catch2/Catch.cmake)
4-
else()
5-
include(${CONAN_CATCH2_ROOT}/lib/cmake/Catch2/Catch.cmake)
6-
endif()
7-
8-
add_library(catch_main STATIC catch_main.cpp)
9-
target_link_libraries(catch_main PUBLIC CONAN_PKG::catch2)
10-
target_link_libraries(catch_main PRIVATE project_options)
11-
12-
add_executable(tests tests.cpp)
13-
target_link_libraries(tests PRIVATE project_warnings project_options catch_main)
14-
15-
# automatically discover tests that are defined in catch based test files you can modify the unittests. Set TEST_PREFIX
16-
# to whatever you want, or use different for different binaries
17-
catch_discover_tests(
18-
tests
19-
TEST_PREFIX
20-
"unittests."
21-
REPORTER
22-
xml
23-
OUTPUT_DIR
24-
.
25-
OUTPUT_PREFIX
26-
"unittests."
27-
OUTPUT_SUFFIX
28-
.xml)
29-
30-
# Add a file containing a set of constexpr tests
31-
add_executable(constexpr_tests constexpr_tests.cpp)
32-
target_link_libraries(constexpr_tests PRIVATE project_options project_warnings catch_main)
33-
34-
catch_discover_tests(
35-
constexpr_tests
36-
TEST_PREFIX
37-
"constexpr."
38-
REPORTER
39-
xml
40-
OUTPUT_DIR
41-
.
42-
OUTPUT_PREFIX
43-
"constexpr."
44-
OUTPUT_SUFFIX
45-
.xml)
46-
47-
# Disable the constexpr portion of the test, and build again this allows us to have an executable that we can debug when
48-
# things go wrong with the constexpr testing
49-
add_executable(relaxed_constexpr_tests constexpr_tests.cpp)
50-
target_link_libraries(relaxed_constexpr_tests PRIVATE project_options project_warnings catch_main)
51-
target_compile_definitions(relaxed_constexpr_tests PRIVATE -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE)
52-
53-
catch_discover_tests(
54-
relaxed_constexpr_tests
55-
TEST_PREFIX
56-
"relaxed_constexpr."
57-
REPORTER
58-
xml
59-
OUTPUT_DIR
60-
.
61-
OUTPUT_PREFIX
62-
"relaxed_constexpr."
63-
OUTPUT_SUFFIX
64-
.xml)
1+
add_subdirectory(catch2)
2+
add_subdirectory(gtest)

test/catch2/CMakeLists.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
include(CTest)
2+
include(Catch)
3+
4+
add_library(catch_main STATIC catch_main.cpp)
5+
target_link_libraries(catch_main PUBLIC CONAN_PKG::catch2)
6+
target_link_libraries(catch_main PRIVATE project_options)
7+
8+
add_executable(catch_tests tests.cpp)
9+
target_link_libraries(catch_tests PRIVATE project_warnings project_options catch_main)
10+
11+
# automatically discover tests that are defined in catch based test files you can modify the unittests. Set TEST_PREFIX
12+
# to whatever you want, or use different for different binaries
13+
catch_discover_tests(
14+
catch_tests
15+
TEST_PREFIX
16+
"catch_unittests."
17+
REPORTER
18+
xml
19+
OUTPUT_DIR
20+
.
21+
OUTPUT_PREFIX
22+
"catch_unittests."
23+
OUTPUT_SUFFIX
24+
.xml)
25+
26+
# Add a file containing a set of constexpr tests
27+
add_executable(catch_constexpr_tests constexpr_tests.cpp)
28+
target_link_libraries(catch_constexpr_tests PRIVATE project_options project_warnings catch_main)
29+
30+
catch_discover_tests(
31+
catch_constexpr_tests
32+
TEST_PREFIX
33+
"catch_constexpr."
34+
REPORTER
35+
xml
36+
OUTPUT_DIR
37+
.
38+
OUTPUT_PREFIX
39+
"catch_constexpr."
40+
OUTPUT_SUFFIX
41+
.xml)
42+
43+
# Disable the constexpr portion of the test, and build again this allows us to have an executable that we can debug when
44+
# things go wrong with the constexpr testing
45+
add_executable(catch_relaxed_constexpr_tests constexpr_tests.cpp)
46+
target_link_libraries(catch_relaxed_constexpr_tests PRIVATE project_options project_warnings catch_main)
47+
target_compile_definitions(catch_relaxed_constexpr_tests PRIVATE -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE)
48+
49+
catch_discover_tests(
50+
catch_relaxed_constexpr_tests
51+
TEST_PREFIX
52+
"catch_relaxed_constexpr."
53+
REPORTER
54+
xml
55+
OUTPUT_DIR
56+
.
57+
OUTPUT_PREFIX
58+
"catch_relaxed_constexpr."
59+
OUTPUT_SUFFIX
60+
.xml)
File renamed without changes.

test/gtest/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
include(CTest)
2+
include(GoogleTest)
3+
4+
add_library(gtest_main STATIC gtest_main.cpp)
5+
target_link_libraries(gtest_main PUBLIC CONAN_PKG::gtest)
6+
target_link_libraries(gtest_main PRIVATE project_options)
7+
8+
add_executable(gtest_tests tests.cpp)
9+
target_link_libraries(gtest_tests PRIVATE project_warnings project_options gtest_main)
10+
11+
# automatically discover tests that are defined in gtest based test files you can modify the unittests. Set TEST_PREFIX
12+
# to whatever you want, or use different for different binaries
13+
gtest_discover_tests(
14+
gtest_tests
15+
TEST_PREFIX
16+
"gtest_unittests."
17+
XML_OUTPUT_DIR
18+
.)
19+
20+
# Add a file containing a set of constexpr tests
21+
add_executable(gtest_constexpr_tests constexpr_tests.cpp)
22+
target_link_libraries(gtest_constexpr_tests PRIVATE project_options project_warnings gtest_main)
23+
24+
gtest_discover_tests(
25+
gtest_constexpr_tests
26+
TEST_PREFIX
27+
"gtest_constexpr."
28+
XML_OUTPUT_DIR
29+
.)
30+
31+
# GTest doesn't support switching between constexpr and non-constexpr versions of tests.
32+

test/gtest/constexpr_tests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <gtest/gtest.h>
2+
3+
constexpr unsigned int Factorial(unsigned int number)
4+
{
5+
return number <= 1 ? number : Factorial(number - 1) * number;
6+
}
7+
8+
// NOLINTNEXTLINE(*)
9+
TEST(factorial, Factorials_are_computed_with_constexpr)
10+
{
11+
static_assert(Factorial(1) == 1);
12+
static_assert(Factorial(2) == 2);
13+
static_assert(Factorial(3) == 6);//NOLINT(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers)
14+
static_assert(Factorial(10) == 3628800);//NOLINT(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers)
15+
}

test/gtest/gtest_main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <gtest/gtest.h>
2+
3+
int main(int argc, char **argv)
4+
{
5+
::testing::InitGoogleTest(&argc, argv);
6+
return RUN_ALL_TESTS();
7+
}
8+

test/gtest/tests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <gtest/gtest.h>
2+
3+
unsigned int Factorial(unsigned int number)
4+
{
5+
return number <= 1 ? number : Factorial(number - 1) * number;
6+
}
7+
8+
// NOLINTNEXTLINE(*)
9+
TEST(factorial, Factorials_are_computed)
10+
{
11+
ASSERT_EQ(Factorial(1), 1);
12+
ASSERT_EQ(Factorial(2), 2);
13+
ASSERT_EQ(Factorial(3), 6);
14+
ASSERT_EQ(Factorial(10), 3628800);
15+
}

0 commit comments

Comments
 (0)