@@ -729,3 +729,89 @@ function assert_line_count() {
729729
730730 bashunit::state::add_assertions_passed
731731}
732+
733+ function bashunit::format_to_regex() {
734+ local format=" $1 "
735+ local regex=" "
736+ local i=0
737+ local len=${# format}
738+
739+ while [ $i -lt " $len " ]; do
740+ local char=" ${format: $i : 1} "
741+ if [ " $char " = " %" ] && [ $(( i + 1 )) -lt " $len " ]; then
742+ local next=" ${format: $((i + 1)): 1} "
743+ case " $next " in
744+ d) regex=" ${regex} [0-9]+" ;;
745+ i) regex=" ${regex} [+-]?[0-9]+" ;;
746+ f) regex=" ${regex} [+-]?[0-9]*\\ .?[0-9]+" ;;
747+ s) regex=" ${regex} [^ ]+" ;;
748+ x) regex=" ${regex} [0-9a-fA-F]+" ;;
749+ e) regex=" ${regex} [+-]?[0-9]*\\ .?[0-9]+[eE][+-]?[0-9]+" ;;
750+ %) regex=" ${regex} %" ;;
751+ * )
752+ regex=" ${regex} %${next} "
753+ ;;
754+ esac
755+ i=$(( i + 2 ))
756+ else
757+ case " $char " in
758+ . | ' *' | ' +' | ' ?' | ' (' | ' )' | ' [' | ' ]' | ' {' | ' }' | ' |' | ' ^' | ' $' )
759+ regex=" ${regex} \\ ${char} "
760+ ;;
761+ \\ )
762+ regex=" ${regex} \\\\ "
763+ ;;
764+ * )
765+ regex=" ${regex}${char} "
766+ ;;
767+ esac
768+ i=$(( i + 1 ))
769+ fi
770+ done
771+
772+ printf ' %s' " ^${regex} $"
773+ }
774+
775+ function assert_string_matches_format() {
776+ bashunit::assert::should_skip && return 0
777+
778+ local format=" $1 "
779+ local actual=" $2 "
780+
781+ local regex
782+ regex=" $( bashunit::format_to_regex " $format " ) "
783+
784+ if ! [[ " $actual " =~ $regex ]]; then
785+ local test_fn
786+ test_fn=" $( bashunit::helper::find_test_function_name) "
787+ local label
788+ label=" $( bashunit::helper::normalize_test_function_name " $test_fn " ) "
789+ bashunit::assert::mark_failed
790+ bashunit::console_results::print_failed_test " ${label} " " ${actual} " " to match format" " ${format} "
791+ return
792+ fi
793+
794+ bashunit::state::add_assertions_passed
795+ }
796+
797+ function assert_string_not_matches_format() {
798+ bashunit::assert::should_skip && return 0
799+
800+ local format=" $1 "
801+ local actual=" $2 "
802+
803+ local regex
804+ regex=" $( bashunit::format_to_regex " $format " ) "
805+
806+ if [[ " $actual " =~ $regex ]]; then
807+ local test_fn
808+ test_fn=" $( bashunit::helper::find_test_function_name) "
809+ local label
810+ label=" $( bashunit::helper::normalize_test_function_name " $test_fn " ) "
811+ bashunit::assert::mark_failed
812+ bashunit::console_results::print_failed_test " ${label} " " ${actual} " " to not match format" " ${format} "
813+ return
814+ fi
815+
816+ bashunit::state::add_assertions_passed
817+ }
0 commit comments