Skip to content

Commit

Permalink
test: refactor test to be declared as functions (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal authored Apr 29, 2024
1 parent 790aee8 commit 27f650f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function(add_cmake_test FILE)
foreach(NAME ${ARGN})
string(TOLOWER ${NAME} TEST_COMMAND)
string(REPLACE " " "_" TEST_COMMAND ${TEST_COMMAND})
add_test(
NAME ${NAME}
COMMAND ${CMAKE_COMMAND}
-D CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
-D TEST_MATCHES=^${NAME}$
-D TEST_COMMAND=${TEST_COMMAND}
-P ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
)
endforeach()
Expand Down
27 changes: 10 additions & 17 deletions test/cmake/MkdirRecursiveTest.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
# Matches everything if not defined
if(NOT TEST_MATCHES)
set(TEST_MATCHES ".*")
endif()

set(TEST_COUNT 0)

if("Create directory recursively" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

function(create_directory_recursively)
if(EXISTS parent)
message(STATUS "Removing test directory")
file(REMOVE_RECURSE parent)
Expand All @@ -24,15 +15,17 @@ if("Create directory recursively" MATCHES ${TEST_MATCHES})

message(STATUS "Removing test directory")
file(REMOVE_RECURSE parent)
endif()
endfunction()

# Add more test cases here.
if("Test name" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

function(test_name)
# Do something.
endif()
endfunction()

if(TEST_COUNT LESS_EQUAL 0)
message(FATAL_ERROR "Nothing to test with: ${TEST_MATCHES}")
if(NOT DEFINED TEST_COMMAND)
message(FATAL_ERROR "The 'TEST_COMMAND' variable should be defined")
elseif(NOT COMMAND ${TEST_COMMAND})
message(FATAL_ERROR "Unable to find a command named '${TEST_COMMAND}'")
endif()

cmake_language(CALL ${TEST_COMMAND})

0 comments on commit 27f650f

Please sign in to comment.