Skip to content

Commit 77311c2

Browse files
committed
add option to show diffs in assert_output and assert_equal
1 parent e2d855b commit 77311c2

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/assert_equal.bash

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# Options:
99
# <actual> The value being compared.
1010
# <expected> The value to compare against.
11+
# -d, --diff Show diff between `expected` and `$output`
1112
#
1213
# ```bash
1314
# @test 'assert_equal()' {
@@ -31,12 +32,29 @@
3132
# actual : have
3233
# --
3334
# ```
35+
#
36+
# If the `--diff` option is set, a diff between the expected and actual output is shown.
3437
assert_equal() {
38+
local -i show_diff=0
39+
40+
while (( $# > 0 )); do
41+
case "$1" in
42+
-d|--diff) show_diff=1; shift ;;
43+
*) break ;;
44+
esac
45+
done
46+
3547
if [[ $1 != "$2" ]]; then
36-
batslib_print_kv_single_or_multi 8 \
37-
'expected' "$2" \
38-
'actual' "$1" \
39-
| batslib_decorate 'values do not equal' \
40-
| fail
48+
if (( show_diff )); then
49+
diff <(echo "$2") <(echo "$1") \
50+
| batslib_decorate 'values do not equal' \
51+
| fail
52+
else
53+
batslib_print_kv_single_or_multi 8 \
54+
'expected' "$2" \
55+
'actual' "$1" \
56+
| batslib_decorate 'values do not equal' \
57+
| fail
58+
fi
4159
fi
4260
}

src/assert_output.bash

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# Options:
99
# -p, --partial Match if `expected` is a substring of `$output`
1010
# -e, --regexp Treat `expected` as an extended regular expression
11+
# -d, --diff Show diff between `expected` and `$output`
1112
# -, --stdin Read `expected` value from STDIN
1213
# <expected> The expected value, substring or regular expression
1314
#
@@ -57,6 +58,8 @@
5758
# --
5859
# ```
5960
#
61+
# If the `--diff` option is set, a diff between the expected and actual output is shown.
62+
#
6063
# ## Existence
6164
#
6265
# To assert that any output exists at all, omit the `expected` argument.
@@ -126,6 +129,7 @@ assert_output() {
126129
local -i is_mode_regexp=0
127130
local -i is_mode_nonempty=0
128131
local -i use_stdin=0
132+
local -i show_diff=0
129133
: "${output?}"
130134

131135
# Handle options.
@@ -137,6 +141,7 @@ assert_output() {
137141
case "$1" in
138142
-p|--partial) is_mode_partial=1; shift ;;
139143
-e|--regexp) is_mode_regexp=1; shift ;;
144+
-d|--diff) show_diff=1; shift ;;
140145
-|--stdin) use_stdin=1; shift ;;
141146
--) shift; break ;;
142147
*) break ;;
@@ -187,11 +192,17 @@ assert_output() {
187192
fi
188193
else
189194
if [[ $output != "$expected" ]]; then
190-
batslib_print_kv_single_or_multi 8 \
191-
'expected' "$expected" \
192-
'actual' "$output" \
193-
| batslib_decorate 'output differs' \
194-
| fail
195+
if (( show_diff )); then
196+
diff <(echo "$expected") <(echo "$output") \
197+
| batslib_decorate 'output differs' \
198+
| fail
199+
else
200+
batslib_print_kv_single_or_multi 8 \
201+
'expected' "$expected" \
202+
'actual' "$output" \
203+
| batslib_decorate 'output differs' \
204+
| fail
205+
fi
195206
fi
196207
fi
197208
}

0 commit comments

Comments
 (0)