Skip to content

Commit

Permalink
feat: handle regular expression match assertions if given var
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed May 22, 2024
1 parent ca23855 commit 09ca153
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ function(assert)
list(GET ARGUMENTS 2 RIGHT_VALUE)

if(OPERATOR STREQUAL MATCHES)
if(DEFINED "${LEFT_VALUE}")
set(LEFT_VALUE "${${LEFT_VALUE}}")
endif()
set(MESSAGE "expected string '${LEFT_VALUE}'${NOT_WORD} to match '${RIGHT_VALUE}'")
elseif(OPERATOR STREQUAL STREQUAL)
set(MESSAGE "expected string '${LEFT_VALUE}'${NOT_WORD} to be equal to '${RIGHT_VALUE}'")
Expand Down
23 changes: 19 additions & 4 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,31 @@ function("Directory path assertions")
endfunction()

function("Regular expression match assertions")
assert("some string" MATCHES "so.*ing")
assert(NOT "some string" MATCHES "so.*other.*ing")
set(STRING_VAR "some string")

assert(STRING_VAR MATCHES "so.*ing")
assert("${STRING_VAR}" MATCHES "so.*ing")

assert(NOT STRING_VAR MATCHES "so.*other.*ing")
assert(NOT "${STRING_VAR}" MATCHES "so.*other.*ing")

mock_message()
assert(STRING_VAR MATCHES "so.*other.*ing")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' to match 'so.*other.*ing'")

mock_message()
assert("some string" MATCHES "so.*other.*ing")
assert("${STRING_VAR}" MATCHES "so.*other.*ing")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' to match 'so.*other.*ing'")

mock_message()
assert(NOT "some string" MATCHES "so.*ing")
assert(NOT STRING_VAR MATCHES "so.*ing")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' not to match 'so.*ing'")

mock_message()
assert(NOT ${STRING_VAR} MATCHES "so.*ing")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' not to match 'so.*ing'")
endfunction()
Expand Down

0 comments on commit 09ca153

Please sign in to comment.