From c1a568511968e33ed9b401293cbfe9de5c58fcb0 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 3 Jul 2024 22:01:02 +0700 Subject: [PATCH 1/2] feat: add `assertion_add_test` function --- CMakeLists.txt | 40 +++++++++++----------------------------- cmake/Assertion.cmake | 13 +++++++++++++ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9df5c4b..75dbed1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,35 +15,17 @@ include(cmake/Assertion.cmake) if(ASSERT_ENABLE_TESTS) enable_testing() - add_test( - NAME "inclusion of other modules" - COMMAND "${CMAKE_COMMAND}" -P "${ASSERTION_LIST_FILE}" - -- ${CMAKE_CURRENT_SOURCE_DIR}/test/IncludeOtherModules.cmake) - - add_test( - NAME "failure invocation" - COMMAND "${CMAKE_COMMAND}" -P "${ASSERTION_LIST_FILE}" - -- ${CMAKE_CURRENT_SOURCE_DIR}/test/Fail.cmake) - - add_test( - NAME "condition assertions" - COMMAND "${CMAKE_COMMAND}" -P "${ASSERTION_LIST_FILE}" - -- ${CMAKE_CURRENT_SOURCE_DIR}/test/Assert.cmake) - - add_test( - NAME "fatal error assertions" - COMMAND "${CMAKE_COMMAND}" -P "${ASSERTION_LIST_FILE}" - -- ${CMAKE_CURRENT_SOURCE_DIR}/test/AssertFatalError.cmake) - - add_test( - NAME "execute process assertions" - COMMAND "${CMAKE_COMMAND}" -P "${ASSERTION_LIST_FILE}" - -- ${CMAKE_CURRENT_SOURCE_DIR}/test/AssertExecuteProcess.cmake) - - add_test( - NAME "section creation" - COMMAND "${CMAKE_COMMAND}" -P "${ASSERTION_LIST_FILE}" - -- ${CMAKE_CURRENT_SOURCE_DIR}/test/SectionCreation.cmake) + assertion_add_test( + "inclusion of other modules" test/IncludeOtherModules.cmake) + + assertion_add_test("failure invocation" test/Fail.cmake) + assertion_add_test("condition assertions" test/Assert.cmake) + assertion_add_test("fatal error assertions" test/AssertFatalError.cmake) + + assertion_add_test( + "execute process assertions" test/AssertExecuteProcess.cmake) + + assertion_add_test("section creation" test/SectionCreation.cmake) endif() if(ASSERT_ENABLE_INSTALL) diff --git a/cmake/Assertion.cmake b/cmake/Assertion.cmake index 34f8a4e..a533d74 100644 --- a/cmake/Assertion.cmake +++ b/cmake/Assertion.cmake @@ -6,6 +6,19 @@ cmake_minimum_required(VERSION 3.17) # This variable contains the path to the included `Assertion.cmake` module. set(ASSERTION_LIST_FILE "${CMAKE_CURRENT_LIST_FILE}") +# Add a new test that runs a CMake file in script mode. +# +# assertion_add_test( ) +# +# This function adds a new test named `` that will run the specified +# `` CMake file in script mode. +function(assertion_add_test NAME FILE) + add_test( + NAME "${NAME}" + COMMAND "${CMAKE_COMMAND}" -P "${ASSERTION_LIST_FILE}" + -- ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}) +endfunction() + # Throws a formatted fatal error message. # # fail(...) From 44e5f786a86f286a6ab587dde34e6ebb455e0444 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 3 Jul 2024 22:05:59 +0700 Subject: [PATCH 2/2] test: add a test creation test --- CMakeLists.txt | 1 + test/TestCreation.cmake | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 test/TestCreation.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 75dbed1..e23ea1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ if(ASSERT_ENABLE_TESTS) assertion_add_test( "inclusion of other modules" test/IncludeOtherModules.cmake) + assertion_add_test("test creation" test/TestCreation.cmake) assertion_add_test("failure invocation" test/Fail.cmake) assertion_add_test("condition assertions" test/Assert.cmake) assertion_add_test("fatal error assertions" test/AssertFatalError.cmake) diff --git a/test/TestCreation.cmake b/test/TestCreation.cmake new file mode 100644 index 0000000..d0810dd --- /dev/null +++ b/test/TestCreation.cmake @@ -0,0 +1,12 @@ +section("it should correctly create a new test") + function(add_test) + cmake_parse_arguments(PARSE_ARGV 0 ARG "" "NAME" "COMMAND") + assert(ARG_NAME STREQUAL "some test") + + set(EXPECTED_COMMAND "${CMAKE_COMMAND}" -P "${ASSERTION_LIST_FILE}" + -- ${CMAKE_CURRENT_SOURCE_DIR}/some_test.cmake) + assert(ARG_COMMAND STREQUAL EXPECTED_COMMAND) + endfunction() + + assertion_add_test("some test" some_test.cmake) +endsection()