A CMake module containing a collection of assertion functions for testing purposes.
- Contains a collection of assertion functions for testing purposes.
- Supports asserting fatal error messages.
- Supports asserting process execution.
This module can be integrated into a CMake project in the following ways:
- Manually download the
Assertion.cmake
file and include it in the CMake project:include(path/to/Assertion.cmake)
- Use
file(DOWNLOAD)
to automatically download theAssertion.cmake
file:file( DOWNLOAD https://threeal.github.io/assertion-cmake/v0.3.0 ${CMAKE_BINARY_DIR}/Assertion.cmake ) include(${CMAKE_BINARY_DIR}/Assertion.cmake)
- Use CPM.cmake to add this package to the CMake project:
cpmaddpackage(gh:threeal/[email protected]) include(${Assertion_SOURCE_DIR}/cmake/Assertion.cmake)
This example demonstrates how to use the assert
function from this module to perform assertions:
assert(TRUE)
assert(NOT FALSE)
set(SOME_VARIABLE "some value")
assert(DEFINED SOME_VARIABLE)
assert("${SOME_VARIABLE}" STREQUAL "some value")
assert("some other string" MATCHES "some.*string")
file(TOUCH some_file)
assert(EXISTS some_file)
assert(NOT DIRECTORY some_file)
Use the assert_fatal_error
function to assert whether a call to the given function or macro throws the expected fatal error message:
function(some_function)
message(FATAL_ERROR "some fatal error message")
endfunction()
assert_fatal_error(CALL some_function MESSAGE "some fatal error message")
Use the assert_execute_process
function to assert whether the given command successfully executed a process:
assert_execute_process(COMMAND "${CMAKE_COMMAND}" -E true)
This function can also assert the standard output and error of the executed process:
assert_execute_process(
COMMAND "${CMAKE_COMMAND}" -E echo "Hello world!"
OUTPUT "Hello world!"
)
assert_execute_process(
COMMAND "${CMAKE_COMMAND}" invalid-dir
ERROR "CMake Error: The source directory .* does not exist."
)
This project is licensed under the terms of the MIT License.
Copyright © 2024 Alfi Maulana