From b486fe99ac24807f722bef02c26eb13242129f4a Mon Sep 17 00:00:00 2001 From: "Ryan M. Richard" Date: Mon, 9 Jun 2025 12:22:33 -0500 Subject: [PATCH 1/3] adds regex_equal/regex_not_equal --- cmake/cmake_test/asserts/asserts.cmake | 1 + cmake/cmake_test/asserts/regex_equal.cmake | 79 ++++++++++++++++++++++ tests/cmake_test/asserts/regex_equal.cmake | 45 ++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 cmake/cmake_test/asserts/regex_equal.cmake create mode 100644 tests/cmake_test/asserts/regex_equal.cmake diff --git a/cmake/cmake_test/asserts/asserts.cmake b/cmake/cmake_test/asserts/asserts.cmake index 0a0d689..78a62b1 100644 --- a/cmake/cmake_test/asserts/asserts.cmake +++ b/cmake/cmake_test/asserts/asserts.cmake @@ -34,6 +34,7 @@ include(cmake_test/asserts/file_exists) include(cmake_test/asserts/library_exists) include(cmake_test/asserts/list) include(cmake_test/asserts/prints) +include(cmake_test/asserts/regex_equal) include(cmake_test/asserts/string) include(cmake_test/asserts/target_exists) include(cmake_test/asserts/target_has_property) diff --git a/cmake/cmake_test/asserts/regex_equal.cmake b/cmake/cmake_test/asserts/regex_equal.cmake new file mode 100644 index 0000000..103cfa6 --- /dev/null +++ b/cmake/cmake_test/asserts/regex_equal.cmake @@ -0,0 +1,79 @@ +# Copyright 2025 CMakePP +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include_guard() +include("cmakepp_lang/asserts/signature") + +#[[[ +# Asserts that the contents of the provided variable matches the given regular +# expression. +# +# :code:`var` must be a non-empty string, otherwise an :code:`ASSERTION_FAILED` +# exception is raised. +# +# :param var: The variable to apply the RegEx to. +# :type var: str* +# :param regex: The RegEx to apply to var. +# :type regex: str +#]] +function(ct_assert_regex_equal _are_var _are_regex) + cpp_assert_signature("${ARGV}" str* str) + + # Check separately for empty string + # since it will blow up the next if statement + # otherwise + if (_are_var STREQUAL "") + cpp_raise( + ASSERTION_FAILED + "ct_assert_regex_equal() given empty string as parameter" + ) + endif() + + if(NOT (${_are_var} MATCHES "${_are_regex}")) + cpp_raise( + ASSERTION_FAILED + "${_are_var} does not match RegEx ${_are_regex}." + ) + endif() +endfunction() + +#[[[ +# Asserts that the contents of the provided variable don't match the given +# regular expression. +# +# :param var: The variable to apply the regex to. +# :type var: str* +# :param regex: The regex to apply. +# :type regex: str +#]] +function(ct_assert_regex_not_equal _arne_var _arne_regex) + cpp_assert_signature("${ARGV}" str* str) + + # Check separately for empty string + # since it will blow up the next if statement + # otherwise + if (_arne_var STREQUAL "") + cpp_raise( + ASSERTION_FAILED + "ct_assert_regex_not_equal() given empty string as parameter" + ) + endif() + + if(${_arne_var} MATCHES "${_arne_regex}") + cpp_raise( + ASSERTION_FAILED + "${_arne_var} satisfies regex: ${_arne_regex}." + ) + endif() +endfunction() \ No newline at end of file diff --git a/tests/cmake_test/asserts/regex_equal.cmake b/tests/cmake_test/asserts/regex_equal.cmake new file mode 100644 index 0000000..9c6b41b --- /dev/null +++ b/tests/cmake_test/asserts/regex_equal.cmake @@ -0,0 +1,45 @@ +include(cmake_test/cmake_test) + +ct_add_test(NAME "assert_regex_equal") +function(${assert_regex_equal}) + include(cmake_test/asserts/regex_equal) + + set(is_true "TRUE") + set(contains_a_number "Pi is 3.14") + set(no_number "Pi is a Greek letter.") + + ct_add_section(NAME truthy_value_match) + function(${truthy_value_match}) + ct_assert_regex_equal(is_true "TRUE") + ct_assert_regex_equal(contains_a_number "[0-9]+\.[0-9]+") + endfunction() + + ct_add_section(NAME truthy_value_no_match EXPECTFAIL) + function(${truthy_value_no_match}) + ct_assert_regex_equal(is_true "FALSE") + ct_assert_regex_equal(no_number "[0-9]+\.[0-9]+") + endfunction() + +endfunction() + +ct_add_test(NAME "assert_regex_not_equal") +function(${assert_regex_not_equal}) + include(cmake_test/asserts/regex_equal) + + set(is_true "TRUE") + set(contains_a_number "Pi is 3.14") + set(no_number "Pi is a Greek letter.") + + ct_add_section(NAME truthy_value_no_match) + function(${truthy_value_no_match}) + ct_assert_regex_not_equal(is_true "FALSE") + ct_assert_regex_not_equal(no_number "[0-9]+\.[0-9]+") + endfunction() + + ct_add_section(NAME truthy_value EXPECTFAIL) + function(${truthy_value}) + ct_assert_regex_not_equal(is_true "TRUE") + ct_assert_regex_not_equal(contains_a_number "[0-9]+\.[0-9]+") + endfunction() + +endfunction() \ No newline at end of file From 19a3d62c7bec4bc3102ce93b5c770d76428ea9fc Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Mon, 9 Jun 2025 12:51:49 -0500 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Zachery Crandall --- cmake/cmake_test/asserts/regex_equal.cmake | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmake/cmake_test/asserts/regex_equal.cmake b/cmake/cmake_test/asserts/regex_equal.cmake index 103cfa6..d4b825f 100644 --- a/cmake/cmake_test/asserts/regex_equal.cmake +++ b/cmake/cmake_test/asserts/regex_equal.cmake @@ -25,10 +25,10 @@ include("cmakepp_lang/asserts/signature") # :param var: The variable to apply the RegEx to. # :type var: str* # :param regex: The RegEx to apply to var. -# :type regex: str +# :type regex: desc #]] function(ct_assert_regex_equal _are_var _are_regex) - cpp_assert_signature("${ARGV}" str* str) + cpp_assert_signature("${ARGV}" str* desc) # Check separately for empty string # since it will blow up the next if statement @@ -52,13 +52,16 @@ endfunction() # Asserts that the contents of the provided variable don't match the given # regular expression. # +# :code:`var` must be a non-empty string, otherwise an :code:`ASSERTION_FAILED` +# exception is raised. +# # :param var: The variable to apply the regex to. # :type var: str* # :param regex: The regex to apply. -# :type regex: str +# :type regex: desc #]] function(ct_assert_regex_not_equal _arne_var _arne_regex) - cpp_assert_signature("${ARGV}" str* str) + cpp_assert_signature("${ARGV}" str* desc) # Check separately for empty string # since it will blow up the next if statement From 536d0d96293aeb6a90e9efa56af591d5a3e49d9d Mon Sep 17 00:00:00 2001 From: "Ryan M. Richard" Date: Mon, 9 Jun 2025 15:18:49 -0500 Subject: [PATCH 3/3] revert desc to str --- cmake/cmake_test/asserts/regex_equal.cmake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmake/cmake_test/asserts/regex_equal.cmake b/cmake/cmake_test/asserts/regex_equal.cmake index d4b825f..373c49a 100644 --- a/cmake/cmake_test/asserts/regex_equal.cmake +++ b/cmake/cmake_test/asserts/regex_equal.cmake @@ -25,10 +25,10 @@ include("cmakepp_lang/asserts/signature") # :param var: The variable to apply the RegEx to. # :type var: str* # :param regex: The RegEx to apply to var. -# :type regex: desc +# :type regex: str #]] function(ct_assert_regex_equal _are_var _are_regex) - cpp_assert_signature("${ARGV}" str* desc) + cpp_assert_signature("${ARGV}" str* str) # Check separately for empty string # since it will blow up the next if statement @@ -58,10 +58,10 @@ endfunction() # :param var: The variable to apply the regex to. # :type var: str* # :param regex: The regex to apply. -# :type regex: desc +# :type regex: str #]] function(ct_assert_regex_not_equal _arne_var _arne_regex) - cpp_assert_signature("${ARGV}" str* desc) + cpp_assert_signature("${ARGV}" str* str) # Check separately for empty string # since it will blow up the next if statement