Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/assert_equal.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Options:
# <actual> The value being compared.
# <expected> The value to compare against.
# -d, --diff Show diff between `expected` and `$output`
#
# ```bash
# @test 'assert_equal()' {
Expand All @@ -31,12 +32,29 @@
# actual : have
# --
# ```
#
# If the `--diff` option is set, a diff between the expected and actual output is shown.
assert_equal() {
local -i show_diff=0

while (( $# > 0 )); do
case "$1" in
-d|--diff) show_diff=1; shift ;;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a breaking change for some users that want to check if something is -d/--diff

Unfortunately, we don't have a -- interface established here. I think at minimum we should check that there are still (at least) two parameters left after the shift.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*) break ;;
esac
done

if [[ $1 != "$2" ]]; then
batslib_print_kv_single_or_multi 8 \
'expected' "$2" \
'actual' "$1" \
| batslib_decorate 'values do not equal' \
| fail
if (( show_diff )); then
diff <(echo "$2") <(echo "$1") \
| batslib_decorate 'values do not equal' \
| fail
else
batslib_print_kv_single_or_multi 8 \
'expected' "$2" \
'actual' "$1" \
| batslib_decorate 'values do not equal' \
| fail
fi
fi
}
21 changes: 16 additions & 5 deletions src/assert_output.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Options:
# -p, --partial Match if `expected` is a substring of `$output`
# -e, --regexp Treat `expected` as an extended regular expression
# -d, --diff Show diff between `expected` and `$output`
# -, --stdin Read `expected` value from STDIN
# <expected> The expected value, substring or regular expression
#
Expand Down Expand Up @@ -57,6 +58,8 @@
# --
# ```
#
# If the `--diff` option is set, a diff between the expected and actual output is shown.
#
# ## Existence
#
# To assert that any output exists at all, omit the `expected` argument.
Expand Down Expand Up @@ -126,6 +129,7 @@ assert_output() {
local -i is_mode_regexp=0
local -i is_mode_nonempty=0
local -i use_stdin=0
local -i show_diff=0
: "${output?}"

# Handle options.
Expand All @@ -137,6 +141,7 @@ assert_output() {
case "$1" in
-p|--partial) is_mode_partial=1; shift ;;
-e|--regexp) is_mode_regexp=1; shift ;;
-d|--diff) show_diff=1; shift ;;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, --diff might be a valid output to check for. At least, we have -- here to differentiate options from output but we should at least check that there still is a trailing output.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

-|--stdin) use_stdin=1; shift ;;
--) shift; break ;;
*) break ;;
Expand Down Expand Up @@ -187,11 +192,17 @@ assert_output() {
fi
else
if [[ $output != "$expected" ]]; then
batslib_print_kv_single_or_multi 8 \
'expected' "$expected" \
'actual' "$output" \
| batslib_decorate 'output differs' \
| fail
if (( show_diff )); then
diff <(echo "$expected") <(echo "$output") \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unified file format is more common than the so called normal format. It would be better to use diff -u instead of just diff here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, done

| batslib_decorate 'output differs' \
| fail
else
batslib_print_kv_single_or_multi 8 \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps batslib_print_kv_single_or_multi` could grow support for diffing expected and actual?

For instance it could use wdiff instead of diff if it detects single-line text.

'expected' "$expected" \
'actual' "$output" \
| batslib_decorate 'output differs' \
| fail
fi
fi
fi
}