Skip to content

Commit

Permalink
feat: handle string assertion if given variable instead of string (#45)
Browse files Browse the repository at this point in the history
* feat: handle regular expression match assertions if given var

* feat: handle string equality assertions if given var
  • Loading branch information
threeal authored May 23, 2024
1 parent ca23855 commit 731063d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
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

0 comments on commit 731063d

Please sign in to comment.