From 731063d5a064be5b0aa7c180ab0ab40d6147a049 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Thu, 23 May 2024 21:40:26 +0700 Subject: [PATCH] feat: handle string assertion if given variable instead of string (#45) * feat: handle regular expression match assertions if given var * feat: handle string equality assertions if given var --- cmake/Assertion.cmake | 9 +++++ test/cmake/AssertionTest.cmake | 66 +++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/cmake/Assertion.cmake b/cmake/Assertion.cmake index 54484b2..88039f7 100644 --- a/cmake/Assertion.cmake +++ b/cmake/Assertion.cmake @@ -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() diff --git a/test/cmake/AssertionTest.cmake b/test/cmake/AssertionTest.cmake index f9b8e2e..3392937 100644 --- a/test/cmake/AssertionTest.cmake +++ b/test/cmake/AssertionTest.cmake @@ -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)