Skip to content

Commit 0ba3e11

Browse files
committed
🐛 Fix test machinery
1 parent a4c4034 commit 0ba3e11

File tree

5 files changed

+59
-38
lines changed

5 files changed

+59
-38
lines changed

.github/workflows/unit_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ jobs:
176176
- name: Test
177177
working-directory: ${{github.workspace}}/build
178178
run: |
179-
ctest -j $(nproc) -T memcheck
179+
ctest -j $(nproc) -E EXPECT_FAIL -T memcheck
180180
181181
LOGFILE=$(ls ./Testing/Temporary/LastDynamicAnalysis_*.log)
182182
FAILSIZE=$(du -c ./Testing/Temporary/MemoryChecker.* | tail -1 | cut -f1)

cmake/test.cmake

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ macro(add_rapidcheck)
7979
endif()
8080
endmacro()
8181

82-
function(add_unit_test name)
82+
function(add_unit_test_target name)
8383
set(options CATCH2 GTEST GUNIT)
8484
set(multiValueArgs FILES INCLUDE_DIRECTORIES LIBRARIES SYSTEM_LIBRARIES)
8585
cmake_parse_arguments(UNIT "${options}" "" "${multiValueArgs}" ${ARGN})
@@ -92,15 +92,11 @@ function(add_unit_test name)
9292
add_dependencies(build_unit_tests ${name})
9393

9494
if(UNIT_CATCH2)
95-
get_catch2()
96-
add_rapidcheck()
9795
catch_discover_tests(${name})
9896
target_link_libraries_system(${name} PRIVATE Catch2::Catch2WithMain
9997
rapidcheck rapidcheck_catch)
10098
set(target_test_command $<TARGET_FILE:${name}> "--order" "rand")
10199
elseif(UNIT_GTEST)
102-
get_gtest()
103-
add_rapidcheck()
104100
gtest_discover_tests(${name})
105101
target_link_libraries_system(
106102
${name}
@@ -113,9 +109,6 @@ function(add_unit_test name)
113109
rapidcheck_gmock)
114110
set(target_test_command $<TARGET_FILE:${name}> "--gtest_shuffle")
115111
elseif(UNIT_GUNIT)
116-
get_gunit()
117-
add_boost_di()
118-
add_rapidcheck()
119112
target_include_directories(
120113
${name} SYSTEM
121114
PRIVATE ${gunit_SOURCE_DIR}/include
@@ -147,7 +140,38 @@ function(add_unit_test name)
147140
add_dependencies(unit_tests "run_${name}")
148141
endfunction()
149142

150-
function(add_feature_test name)
143+
function(detect_test_framework)
144+
set(options CATCH2 GTEST GUNIT)
145+
cmake_parse_arguments(TF "${options}" "" "" ${ARGN})
146+
if(TF_CATCH)
147+
return(PROPAGATE TF_CATCH)
148+
elseif(TF_GTEST)
149+
return(PROPAGATE TF_GTEST)
150+
elseif(TF_GUNIT)
151+
return(PROPAGATE TF_GUNIT)
152+
endif()
153+
endfunction()
154+
155+
macro(add_unit_test)
156+
detect_test_framework(${ARGN})
157+
if(TF_CATCH)
158+
get_catch2()
159+
add_rapidcheck()
160+
unset(TF_CATCH)
161+
elseif(TF_GTEST)
162+
get_gtest()
163+
add_rapidcheck()
164+
unset(TF_GTEST)
165+
elseif(TF_GUNIT)
166+
get_gunit()
167+
add_boost_di()
168+
add_rapidcheck()
169+
unset(TF_GUNIT)
170+
endif()
171+
add_unit_test_target(${ARGN})
172+
endmacro()
173+
174+
function(add_feature_test_target name)
151175
set(singleValueArgs FEATURE)
152176
set(multiValueArgs FILES INCLUDE_DIRECTORIES LIBRARIES SYSTEM_LIBRARIES)
153177
cmake_parse_arguments(FEAT "${options}" "${singleValueArgs}"
@@ -160,10 +184,6 @@ function(add_feature_test name)
160184
target_link_libraries(${name} PRIVATE sanitizers)
161185
add_dependencies(build_unit_tests ${name})
162186

163-
get_gunit()
164-
add_gherkin()
165-
add_boost_di()
166-
add_rapidcheck()
167187
target_include_directories(
168188
${name} SYSTEM
169189
PRIVATE ${gunit_SOURCE_DIR}/include
@@ -195,3 +215,11 @@ function(add_feature_test name)
195215
set_property(TEST ${name} PROPERTY ENVIRONMENT "SCENARIO=${FEATURE_FILE}")
196216
add_dependencies(unit_tests "run_${name}")
197217
endfunction()
218+
219+
macro(add_feature_test)
220+
get_gunit()
221+
add_gherkin()
222+
add_boost_di()
223+
add_rapidcheck()
224+
add_feature_test_target(${ARGN})
225+
endmacro()

test/CMakeLists.txt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
# Test that code is NOT able to compile
2-
function(test_static_assert_fail TEST_NAME)
3-
add_executable(${TEST_NAME}_t EXCLUDE_FROM_ALL ${TEST_NAME}.cpp)
4-
target_link_libraries(${TEST_NAME}_t PRIVATE safe_arithmetic)
5-
add_test(NAME ${TEST_NAME}
2+
function(test_static_assert_fail test_file)
3+
string(REPLACE "/" "_" test_name "${test_file}")
4+
string(PREPEND test_name "EXPECT_FAIL.")
5+
add_executable(${test_name} EXCLUDE_FROM_ALL ${test_file})
6+
target_link_libraries(${test_name} PRIVATE safe_arithmetic)
7+
add_test(NAME ${test_name}
68
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
7-
${TEST_NAME}_t)
9+
${test_name})
810
set_tests_properties(
9-
${TEST_NAME} PROPERTIES PASS_REGULAR_EXPRESSION
11+
${test_name} PROPERTIES PASS_REGULAR_EXPRESSION
1012
"(static_assert)|(static assertion failed)")
1113
endfunction()
1214

13-
# Test that code that must compile
14-
function(test_static_assert_pass TEST_NAME)
15-
add_executable(${TEST_NAME}_t EXCLUDE_FROM_ALL ${TEST_NAME}.cpp)
16-
target_link_libraries(${TEST_NAME}_t PRIVATE safe_arithmetic)
17-
add_test(NAME ${TEST_NAME}
18-
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
19-
${TEST_NAME}_t)
20-
endfunction()
21-
2215
add_subdirectory(safe)

test/safe/array/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
test_static_assert_fail(array_constant_access_max_violation)
1+
test_static_assert_fail(array_constant_access_max_violation.cpp)

test/safe/var/CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
test_static_assert_fail(assign_constant_max_violation)
2-
test_static_assert_fail(assign_constant_min_violation)
3-
test_static_assert_fail(construct_constant_max_violation)
4-
test_static_assert_fail(construct_constant_min_violation)
5-
test_static_assert_fail(interval_larger_than_type)
6-
test_static_assert_fail(assign_lhs_rhs_union)
7-
test_static_assert_fail(assign_lhs_union)
8-
test_static_assert_fail(assign_rhs_union)
1+
test_static_assert_fail(assign_constant_max_violation.cpp)
2+
test_static_assert_fail(assign_constant_min_violation.cpp)
3+
test_static_assert_fail(construct_constant_max_violation.cpp)
4+
test_static_assert_fail(construct_constant_min_violation.cpp)
5+
test_static_assert_fail(interval_larger_than_type.cpp)
6+
test_static_assert_fail(assign_lhs_rhs_union.cpp)
7+
test_static_assert_fail(assign_lhs_union.cpp)
8+
test_static_assert_fail(assign_rhs_union.cpp)

0 commit comments

Comments
 (0)