Skip to content

Commit

Permalink
feat: add variable defined assertion functions (#10)
Browse files Browse the repository at this point in the history
* feat: add variable defined assertion functions

* test: add test for testing variable defined assertion functions
  • Loading branch information
threeal authored May 3, 2024
1 parent 92f3bf7 commit 64f31e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,23 @@ function(assert_false CONDITION)
message(FATAL_ERROR "expected the condition to be false")
endif()
endfunction()

# Asserts whether the given variable is defined.
#
# Arguments:
# - VARIABLE: The variable to assert.
function(assert_defined VARIABLE)
if(NOT DEFINED "${VARIABLE}")
message(FATAL_ERROR "expected variable '${VARIABLE}' to be defined")
endif()
endfunction()

# Asserts whether the given variable is not defined.
#
# Arguments:
# - VARIABLE: The variable to assert.
function(assert_not_defined VARIABLE)
if(DEFINED "${VARIABLE}")
message(FATAL_ERROR "expected variable '${VARIABLE}' not to be defined")
endif()
endfunction()
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ add_cmake_test(
cmake/AssertionTest.cmake
"Assert a true condition"
"Assert a false condition"
"Assert a defined variable"
"Assert an undefined variable"
)
9 changes: 9 additions & 0 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ function(test_assert_a_false_condition)
assert_false(FALSE)
endfunction()

function(test_assert_a_defined_variable)
set(SOME_VARIABLE "some value")
assert_defined(SOME_VARIABLE)
endfunction()

function(test_assert_an_undefined_variable)
assert_not_defined(SOME_VARIABLE)
endfunction()

if(NOT DEFINED TEST_COMMAND)
message(FATAL_ERROR "The 'TEST_COMMAND' variable should be defined")
elseif(NOT COMMAND test_${TEST_COMMAND})
Expand Down

0 comments on commit 64f31e8

Please sign in to comment.