Skip to content

Commit

Permalink
feat: add path exists assertion function (#18)
Browse files Browse the repository at this point in the history
* feat: add path exists assertion functions

* test: add test for testing path exists assertion functions
  • Loading branch information
threeal authored May 5, 2024
1 parent d672110 commit 2c8257a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ function(assert_not_defined VARIABLE)
endif()
endfunction()

# Asserts whether the given path exists.
#
# Arguments:
# - PATH: The path to assert.
function(assert_exists PATH)
if(NOT EXISTS "${PATH}")
message(FATAL_ERROR "expected path '${PATH}' to exist")
endif()
endfunction()

# Asserts whether the given path does not exist.
#
# Arguments:
# - PATH: The path to assert.
function(assert_not_exists PATH)
if(EXISTS "${PATH}")
message(FATAL_ERROR "expected path '${PATH}' not to exist")
endif()
endfunction()

# Asserts whether the given strings are equal.
#
# Arguments:
Expand Down
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function(add_cmake_test FILE)
foreach(NAME ${ARGN})
string(TOLOWER "${NAME}" TEST_COMMAND)
string(REPLACE " " "_" TEST_COMMAND "${TEST_COMMAND}")
string(REGEX REPLACE "( |-)" "_" TEST_COMMAND "${TEST_COMMAND}")
add_test(
NAME "${NAME}"
COMMAND "${CMAKE_COMMAND}"
Expand All @@ -18,6 +18,8 @@ add_cmake_test(
"Assert a false condition"
"Assert a defined variable"
"Assert an undefined variable"
"Assert an existing path"
"Assert a non-existing path"
"Assert equal strings"
"Assert unequal strings"
"Mock message"
Expand Down
22 changes: 22 additions & 0 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ function(test_assert_an_undefined_variable)
assert_message(FATAL_ERROR "expected variable 'SOME_VARIABLE' to be defined")
endfunction()

function(test_assert_an_existing_path)
file(TOUCH some-file)
assert_exists(some-file)

mock_message(ON)
assert_not_exists(some-file)

mock_message(OFF)
assert_message(FATAL_ERROR "expected path 'some-file' not to exist")
endfunction()

function(test_assert_a_non_existing_path)
file(REMOVE some-file)
assert_not_exists(some-file)

mock_message(ON)
assert_exists(some-file)

mock_message(OFF)
assert_message(FATAL_ERROR "expected path 'some-file' to exist")
endfunction()

function(test_assert_equal_strings)
assert_strequal("some string" "some string")

Expand Down

0 comments on commit 2c8257a

Please sign in to comment.