Skip to content

Commit 56be66b

Browse files
committed
Implement new matchers and not syntax for expects
- Add support for 'not to_*' syntax - Add new matchers: - to_contain/not to_contain - to_match/not to_match - to_be_true/not to_be_true - to_be_false/not to_be_false - to_be_defined/not to_be_defined - Improve error message formatting Part of #24
1 parent ee8ef8c commit 56be66b

File tree

1 file changed

+79
-36
lines changed

1 file changed

+79
-36
lines changed

bin/expects

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,104 @@
88
# Success
99
# ```
1010
# ```shell-session
11-
# $ x=42
12-
# $ expects "$x" to_be 10
13-
# FAIL: expected {actual} to_be 10, but {actual} is: 42
11+
# $ x=10
12+
# $ expects "$x" not to_be 42 && echo Success
13+
# Success
1414
# ```
1515

1616
actual_value=$1
17-
compare_method=$2
18-
expected_value=$3
17+
shift
18+
19+
# Parse "not to_*" or just "to_*"
20+
is_negated=false
21+
if [[ $1 == not ]] ; then
22+
is_negated=true
23+
shift
24+
fi
1925

20-
function get_compare_method () {
21-
local compare_method=$1
22-
case $compare_method in
23-
to_equal)
24-
printf '%s\n' '=='
26+
matcher=$1
27+
expected_value=$2
28+
29+
function test_value () {
30+
local actual=$1 matcher=$2 expected=$3
31+
case $matcher in
32+
to_be|to_equal)
33+
[[ $actual -eq $expected ]]
34+
return $?
2535
;;
26-
not_to_equal)
27-
printf '%s\n' '!='
36+
to_be_less_than)
37+
[[ $actual -lt $expected ]]
38+
return $?
2839
;;
29-
to_be)
30-
printf '%s\n' '-eq'
40+
to_be_greater_than)
41+
[[ $actual -gt $expected ]]
42+
return $?
3143
;;
32-
not_to_be)
33-
printf '%s\n' '-ne'
44+
to_contain)
45+
[[ $actual == *"$expected"* ]]
46+
return $?
3447
;;
35-
to_be_less_than)
36-
printf '%s\n' '-lt'
48+
to_match)
49+
[[ $actual =~ $expected ]]
50+
return $?
3751
;;
38-
to_be_less_than_or_equal)
39-
printf '%s\n' '-le'
52+
to_be_true)
53+
[[ $actual == true ]]
54+
return $?
4055
;;
41-
to_be_greater_than)
42-
printf '%s\n' '-gt'
56+
to_be_false)
57+
[[ $actual == false ]]
58+
return $?
59+
;;
60+
to_be_defined)
61+
[[ -n $actual ]]
62+
return $?
4363
;;
44-
to_be_greater_than_or_equal)
45-
printf '%s\n' '-ge'
64+
*)
65+
echo "Unknown matcher: $matcher" >&2
66+
exit 1
4667
;;
4768
esac
4869
}
4970

50-
compare_operator=$(get_compare_method "$compare_method")
51-
if [[ $compare_operator == '' ]] ; then
52-
echo "Unknown compare method: $compare_method" >&2
53-
exit 1
54-
fi
71+
function format_failure_message () {
72+
local actual=$1 not=$2 matcher=$3 expected=$4
73+
local visible_actual=$actual
74+
75+
if [[ -z $actual ]] ; then
76+
visible_actual=empty
77+
fi
5578

56-
if eval "[[ \"$actual_value\" $compare_operator \"$expected_value\" ]]" ; then
79+
# Special case for to_be_defined matcher
80+
if [[ $matcher == to_be_defined && -z $not ]] ; then
81+
printf "FAIL: expected {actual} %s%s, but {actual} is %s" \
82+
"$not" "$matcher" "$visible_actual"
83+
else
84+
if [[ -n $expected ]] ; then
85+
printf "FAIL: expected {actual} %s%s %s, but {actual} is: %s" \
86+
"$not" "$matcher" "$expected" "$visible_actual"
87+
else
88+
printf "FAIL: expected {actual} %s%s, but {actual} is: %s" \
89+
"$not" "$matcher" "$visible_actual"
90+
fi
91+
fi
92+
}
93+
94+
# Execute test and handle result
95+
if test_value "$actual_value" "$matcher" "$expected_value" ; then
96+
if $is_negated ; then
97+
format_failure_message "$actual_value" "not " "$matcher" "$expected_value" >&2
98+
exit 1
99+
fi
100+
exit 0
101+
else
102+
if ! $is_negated ; then
103+
format_failure_message "$actual_value" "" "$matcher" "$expected_value" >&2
104+
exit 1
105+
fi
57106
exit 0
58107
fi
59108

60-
[[ $actual_value == '' ]] \
61-
&& visible_actual_value='(empty or not defined)' \
62-
|| visible_actual_value=$actual_value
63-
echo "FAIL: expected {actual} $compare_method $expected_value, but {actual} is: $visible_actual_value" >&2
64-
exit 1
65-
66109
# The MIT License (MIT)
67110
#
68111
# Copyright (c) 2025- aiya000

0 commit comments

Comments
 (0)