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#
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