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

Handle String Assertion if Given Variable Instead of String #45

Merged
merged 2 commits into from
May 23, 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
9 changes: 9 additions & 0 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@ 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)
if(DEFINED "${LEFT_VALUE}")
set(LEFT_VALUE "${${LEFT_VALUE}}")
endif()
if(DEFINED "${RIGHT_VALUE}")
set(RIGHT_VALUE "${${RIGHT_VALUE}}")
endif()
set(MESSAGE "expected string '${LEFT_VALUE}'${NOT_WORD} to be equal to '${RIGHT_VALUE}'")
endif()
endif()
Expand Down
66 changes: 42 additions & 24 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,51 @@ function("Directory path assertions")
endfunction()

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

mock_message()
assert("some string" 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")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' not to match 'so.*ing'")
set(STRING_VAR "some string")

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

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

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

function("String equality assertions")
assert("some string" STREQUAL "some string")
assert(NOT "some string" STREQUAL "some other string")

mock_message()
assert("some string" STREQUAL "some other string")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' to be equal to 'some other string'")

mock_message()
assert(NOT "some string" STREQUAL "some string")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' not to be equal to 'some string'")
set(STRING_VAR "some string")
set(OTHER_STRING_VAR "some other string")

foreach(LEFT_VALUE STRING_VAR "${STRING_VAR}")
foreach(RIGHT_VALUE STRING_VAR "${STRING_VAR}")
assert("${LEFT_VALUE}" STREQUAL "${RIGHT_VALUE}")
endforeach()

foreach(RIGHT_VALUE OTHER_STRING_VAR "${OTHER_STRING_VAR}")
assert(NOT "${LEFT_VALUE}" STREQUAL "${RIGHT_VALUE}")
endforeach()

foreach(RIGHT_VALUE OTHER_STRING_VAR "${OTHER_STRING_VAR}")
mock_message()
assert("${LEFT_VALUE}" STREQUAL "${RIGHT_VALUE}")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' to be equal to 'some other string'")
endforeach()

foreach(RIGHT_VALUE STRING_VAR "${STRING_VAR}")
mock_message()
assert(NOT "${LEFT_VALUE}" STREQUAL "${RIGHT_VALUE}")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' not to be equal to 'some string'")
endforeach()
endforeach()
endfunction()

function(call_sample_messages)
Expand Down