Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Regular Expression Match Assertion Function #35

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ function(assert_not_directory PATH)
endif()
endfunction()

# Asserts whether the given string matches the given regular expression.
#
# Arguments:
# - STRING: The string to assert.
# - REGEX: The regular expression to match against the string.
function(assert_matches STRING REGEX)
if(NOT "${STRING}" MATCHES "${REGEX}")
message(FATAL_ERROR "expected string '${STRING}' to match '${REGEX}'")
endif()
endfunction()

# Asserts whether the given string does not match the given regular expression.
#
# Arguments:
# - STRING: The string to assert.
# - REGEX: The regular expression to match against the string.
function(assert_not_matches STRING REGEX)
if("${STRING}" MATCHES "${REGEX}")
message(FATAL_ERROR "expected string '${STRING}' not to match '${REGEX}'")
endif()
endfunction()

# Asserts whether the given strings are equal.
#
# Arguments:
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ add_cmake_test(
"Assert a file path"
"Assert a directory path"
"Assert a non-existing path"
"Assert a matching regular expression"
"Assert an unmatching regular expression"
"Assert equal strings"
"Assert unequal strings"
"Mock message"
Expand Down
18 changes: 18 additions & 0 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ function("Assert a non-existing path")
assert_message(FATAL_ERROR "expected path 'some-non-existing-file' to exist")
endfunction()

function("Assert a matching regular expression")
assert_matches("some string" "so.*ing")

mock_message()
assert_not_matches("some string" "so.*ing")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' not to match 'so.*ing'")
endfunction()

function("Assert an unmatching regular expression")
mock_message()
assert_matches("some string" "so.*other.*ing")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' to match 'so.*other.*ing'")

assert_not_matches("some string" "so.*other.*ing")
endfunction()

function("Assert equal strings")
assert_strequal("some string" "some string")

Expand Down