@@ -12,99 +12,85 @@ function fail() {
1212function assert_true() {
1313 local actual=" $1 "
1414
15- # First check if the actual value is one of the expected literal values
15+ # First check for expected literal values
1616 if [[ " $actual " == " true" || " $actual " == " 0" ]]; then
1717 state::add_assertions_passed
1818 return
1919 elif [[ " $actual " == " false" || " $actual " == " 1" ]]; then
20- # This is a literal false/1, so it should fail
21- local label
22- label=" $( helper::normalize_test_function_name " ${FUNCNAME[1]} " ) "
23-
24- state::add_assertions_failed
25- console_results::print_failed_test " ${label} " " true or 0" " but got " " ${actual} "
20+ handle_bool_assertion_failure " true or 0" " $actual "
2621 return
2722 fi
2823
29- if ! command -v " $actual " & > /dev/null ; then
30- # If actual is not a recognized command or function, it's an unknown input
31- local label
32- label= " $( helper::normalize_test_function_name " ${FUNCNAME[1]} " ) "
33-
34- state::add_assertions_failed
35- console_results::print_failed_test " ${label } " " valid command, function, or true/0 " \
36- " but got " " unknown input:$actual "
24+ local exit_code=0
25+ # Check if actual is a valid command, alias, or "eval"
26+ if [[ " $actual " =~ ^eval ]] ; then
27+ # Eval commands are valid, no need to check with command -v
28+ run_command_or_eval " $actual "
29+ exit_code= $?
30+ elif ! command -v " ${actual # eval } " & > /dev/null ; then
31+ handle_bool_assertion_failure " valid command, function, or true/0 " " unknown input: $actual "
3732 return
3833 fi
3934
40- # If it's an alias, use eval to execute it properly
41- if [[ " $( command -v " $actual " ) " =~ ^alias ]]; then
42- eval " $actual " & > /dev/null
43- else
44- " $actual " & > /dev/null
45- fi
46-
47- local exit_code=$?
4835 if [[ $exit_code -eq 0 ]]; then
4936 state::add_assertions_passed
50- return
37+ else
38+ handle_bool_assertion_failure " command or function with exit code 0" " exit code: $exit_code "
5139 fi
52-
53- local label
54- label=" $( helper::normalize_test_function_name " ${FUNCNAME[1]} " ) "
55-
56- state::add_assertions_failed
57- console_results::print_failed_test " ${label} " " command or function with exit code 0" \
58- " but got exit code " " $exit_code "
5940}
6041
6142function assert_false() {
6243 local actual=" $1 "
6344
64- # First check if the actual value is one of the expected literal values
45+ # First check for expected literal values
6546 if [[ " $actual " == " false" || " $actual " == " 1" ]]; then
6647 state::add_assertions_passed
6748 return
6849 elif [[ " $actual " == " true" || " $actual " == " 0" ]]; then
69- # This is a literal true/0, so it should fail
70- local label
71- label=" $( helper::normalize_test_function_name " ${FUNCNAME[1]} " ) "
72-
73- state::add_assertions_failed
74- console_results::print_failed_test " ${label} " " false or 1" " but got " " ${actual} "
50+ handle_bool_assertion_failure " false or 1" " $actual "
7551 return
7652 fi
7753
78- if ! command -v " $actual " & > /dev/null ; then
79- # If actual is not a recognized command or function, it's an unknown input
80- local label
81- label= " $( helper::normalize_test_function_name " ${FUNCNAME[1]} " ) "
82-
83- state::add_assertions_failed
84- console_results::print_failed_test " ${label } " " valid command, function, or false/1 " \
85- " but got " " unknown input:$actual "
54+ local exit_code=0
55+ # Check if actual is a valid command, alias, or "eval"
56+ if [[ " $actual " =~ ^eval ]] ; then
57+ # Eval commands are valid, no need to check with command -v
58+ run_command_or_eval " $actual "
59+ exit_code= $?
60+ elif ! command -v " ${actual # eval } " & > /dev/null ; then
61+ handle_bool_assertion_failure " valid command, function, or false/1 " " unknown input: $actual "
8662 return
8763 fi
8864
89- # If it's an alias, use eval to execute it properly
90- if [[ " $( command -v " $actual " ) " =~ ^alias ]]; then
91- eval " $actual " & > /dev/null
92- else
93- " $actual " & > /dev/null
94- fi
95-
96- local exit_code=$?
9765 if [[ $exit_code -ne 0 ]]; then
9866 state::add_assertions_passed
99- return
67+ else
68+ handle_bool_assertion_failure " command or function with non-zero exit code" " exit code: $exit_code "
10069 fi
70+ }
10171
72+ function handle_bool_assertion_failure() {
73+ local expected=" $1 "
74+ local got=" $2 "
10275 local label
103- label=" $( helper::normalize_test_function_name " ${FUNCNAME[1 ]} " ) "
76+ label=" $( helper::normalize_test_function_name " ${FUNCNAME[2 ]} " ) "
10477
10578 state::add_assertions_failed
106- console_results::print_failed_test " ${label} " " command or function with non-zero exit code" \
107- " but got" " exit_code:$exit_code "
79+ console_results::print_failed_test " $label " " $expected " " but got " " $got "
80+ }
81+
82+ function run_command_or_eval() {
83+ local cmd=" $1 "
84+
85+ # If it starts with "eval", handle it properly
86+ if [[ " $cmd " =~ ^eval ]]; then
87+ eval " ${cmd# eval } " & > /dev/null
88+ elif [[ " $( command -v " $cmd " ) " =~ ^alias ]]; then
89+ eval " $cmd " & > /dev/null
90+ else
91+ " $cmd " & > /dev/null
92+ fi
93+ return $?
10894}
10995
11096function assert_same() {
0 commit comments