Skip to content
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
1 change: 1 addition & 0 deletions cmake/cmake_test/asserts/asserts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
82 changes: 82 additions & 0 deletions cmake/cmake_test/asserts/regex_equal.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 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.
#
# :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
#]]
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()
45 changes: 45 additions & 0 deletions tests/cmake_test/asserts/regex_equal.cmake
Original file line number Diff line number Diff line change
@@ -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()
Loading