From 3d0306df4cddbfc2821967f2505045fdafdfa04b Mon Sep 17 00:00:00 2001 From: "Roscoe A. Bartlett" Date: Fri, 3 Jun 2022 13:23:29 -0600 Subject: [PATCH] Add function unittest_string_var_regex() (#483) I also updated the documentation some for unittest_string_regex(). I do not run the new function unittest_string_var_regex() in this commit but will in a later commit where I manually tested as part of work for #483 to ensure it passes and fails when it should. --- tribits/core/utils/UnitTestHelpers.cmake | 62 +++++++++++++++++-- .../guides/UtilsMacroFunctionDocTemplate.rst | 1 + 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/tribits/core/utils/UnitTestHelpers.cmake b/tribits/core/utils/UnitTestHelpers.cmake index 0345f07e8..912efb0a5 100644 --- a/tribits/core/utils/UnitTestHelpers.cmake +++ b/tribits/core/utils/UnitTestHelpers.cmake @@ -240,12 +240,13 @@ endfunction() # @FUNCTION: unittest_string_regex() # -# Perform a series regexes of given strings and update overall test statistics. +# Perform a series of regexes on a given string and update overall test +# statistics. # # Usage:: # # unittest_string_regex( -# +# "" # REGEX_STRINGS "" "" ... # ) # @@ -279,12 +280,65 @@ function(unittest_string_regex INPUT_STRING) string(REGEX MATCH "${REGEX}" REGEX_MATCH_RESULT "${INPUT_STRING}") + message(" Searching string:") + message(" '${INPUT_STRING}'") + if (REGEX_MATCH_RESULT) + message(" for REGEX {${REGEX}}: [PASSED]\n") + math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 ) + global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED}) + else() + message(" for REGEX {${REGEX}}: [FAILED]\n") + global_set(UNITTEST_OVERALL_PASS FALSE) + message(WARNING "Stack trace for failed unit test") + endif() + + endforeach() + +endfunction() + + +# @FUNCTION: unittest_string_var_regex() +# +# Perform a series of regexes on a given string variable and update overall +# test statistics. +# +# Usage:: +# +# unittest_string_var_regex( +# +# REGEX_STRINGS "" "" ... +# ) +# +# If the ``"${}"`` matches all of the of the regexs +# ``""``, ``""``, ..., then the test passes. Otherwise it fails. +# +# This updates the global variables ``UNITTEST_OVERALL_NUMRUN``, +# ``UNITTEST_OVERALL_NUMPASSED``, and ``UNITTEST_OVERALL_PASS`` which are used +# by the unit test harness system to assess overall pass/fail. +# +function(unittest_string_var_regex inputStringVar) + + cmake_parse_arguments(PARSE_ARGV 1 + PARSE "" "" # prefix, options, one_value_keywords + "REGEX_STRINGS" #multi_value_keywords + ) + tribits_check_for_unparsed_arguments(PARSE) + + foreach(REGEX ${PARSE_REGEX_STRINGS}) + + math( EXPR NUMRUN ${UNITTEST_OVERALL_NUMRUN}+1 ) + global_set(UNITTEST_OVERALL_NUMRUN ${NUMRUN}) + + string(REGEX MATCH "${REGEX}" REGEX_MATCH_RESULT "${${inputStringVar}}") + + message("Searching string variable value '${inputStringVar}':") + message(" '${${inputStringVar}}'") if (REGEX_MATCH_RESULT) - message(" Searching for REGEX {${REGEX}}: [PASSED]\n") + message(" for REGEX {${REGEX}}: [PASSED]\n") math( EXPR NUMPASSED ${UNITTEST_OVERALL_NUMPASSED}+1 ) global_set(UNITTEST_OVERALL_NUMPASSED ${NUMPASSED}) else() - message(" Searching for REGEX {${REGEX}}: [FAILED]\n") + message(" for REGEX {${REGEX}}: [FAILED]\n") global_set(UNITTEST_OVERALL_PASS FALSE) message(WARNING "Stack trace for failed unit test") endif() diff --git a/tribits/doc/guides/UtilsMacroFunctionDocTemplate.rst b/tribits/doc/guides/UtilsMacroFunctionDocTemplate.rst index e75e63e38..f5c4cf397 100644 --- a/tribits/doc/guides/UtilsMacroFunctionDocTemplate.rst +++ b/tribits/doc/guides/UtilsMacroFunctionDocTemplate.rst @@ -44,5 +44,6 @@ @FUNCTION: unittest_has_substr_const() + @FUNCTION: unittest_not_has_substr_const() + @FUNCTION: unittest_string_regex() + +@FUNCTION: unittest_string_var_regex() + @FUNCTION: unittest_file_regex() + @FUNCTION: unittest_final_result() +