Skip to content

Commit ad7772a

Browse files
committed
refactor: assert_true/false simplify
1 parent 49ddbf2 commit ad7772a

File tree

2 files changed

+50
-64
lines changed

2 files changed

+50
-64
lines changed

src/assert.sh

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,99 +12,85 @@ function fail() {
1212
function 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

6142
function 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

11096
function assert_same() {

tests/unit/assert_test.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function provider_successful_assert_true() {
2424

2525
function test_unsuccessful_assert_true() {
2626
assert_same\
27-
"$(console_results::print_failed_test "Unsuccessful assert true" "true or 0" "but got " "false")"\
27+
"$(console_results::print_failed_test "Unsuccessful assert true" "true or 0" "but got" "false")"\
2828
"$(assert_true false)"
2929
}
3030

@@ -36,7 +36,7 @@ function test_unsuccessful_assert_true_on_function() {
3636
assert_same\
3737
"$(console_results::print_failed_test "Unsuccessful assert true on function"\
3838
"valid command, function, or true/0"\
39-
"but got" "unknown input:")" \
39+
"but got " "unknown input: ")" \
4040
"$(assert_true "$(return 1)")"
4141
}
4242

@@ -54,20 +54,20 @@ function provider_successful_assert_false() {
5454

5555
function test_unsuccessful_assert_false() {
5656
assert_same\
57-
"$(console_results::print_failed_test "Unsuccessful assert false" "false or 1" "but got " "true")"\
57+
"$(console_results::print_failed_test "Unsuccessful assert false" "false or 1" "but got" "true")"\
5858
"$(assert_false true)"
5959
}
6060

6161
function test_successful_assert_false_on_function() {
62-
assert_empty "$(assert_false mock_false)"
62+
assert_empty "$(assert_false "eval return 1")"
6363
}
6464

6565
function test_unsuccessful_assert_false_on_function() {
6666
assert_same\
6767
"$(console_results::print_failed_test "Unsuccessful assert false on function"\
6868
"command or function with non-zero exit code"\
69-
"but got" "exit_code:0")" \
70-
"$(assert_false ls)"
69+
"but got " "exit code: 0")" \
70+
"$(assert_false "eval return 0")"
7171
}
7272

7373
function test_successful_assert_same() {

0 commit comments

Comments
 (0)