Skip to content

A CMake module containing a collection of assertion functions for testing purposes

License

Notifications You must be signed in to change notification settings

threeal/assertion-cmake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Assertion.cmake

A CMake module containing a collection of assertion functions for testing purposes.

Key Features

  • Contains a collection of assertion functions for testing purposes.
  • Supports asserting fatal error messages.
  • Supports asserting process execution.

Integration

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 the Assertion.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)

Example Usages

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)

Assert Fatal Errors

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")

Assert Process Execution

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."
)

License

This project is licensed under the terms of the MIT License.

Copyright © 2024 Alfi Maulana