Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to show diff in assert_output and assert_equal #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 24 additions & 6 deletions src/assert_equal.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#
# Summary: Fail if the actual and expected values are not equal.
#
# Usage: assert_equal <actual> <expected>
# Usage: assert_equal [-d] <actual> <expected>
#
# 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 ;;
*) 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 -u <(echo "$1") <(echo "$2") | tail -n +3 \
| 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
}
17 changes: 16 additions & 1 deletion src/assert_output.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#
# Summary: Fail if `$output' does not match the expected output.
#
# Usage: assert_output [-p | -e] [- | [--] <expected>]
# Usage: assert_output [-p | -e | -d] [- | [--] <expected>]
#
# 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 @@ -185,6 +190,16 @@ assert_output() {
| batslib_decorate 'output does not contain substring' \
| fail
fi
elif (( show_diff )); then
if (( $# == 0 )); then
echo "Missing expected output" \
| batslib_decorate 'ERROR: assert_output' \
| fail
elif [[ $output != "$expected" ]]; then
diff -u <(echo "$output") <(echo "$expected") | tail -n +3 \
| batslib_decorate 'output differs' \
| fail
fi
else
if [[ $output != "$expected" ]]; then
batslib_print_kv_single_or_multi 8 \
Expand Down
18 changes: 18 additions & 0 deletions test/assert_equal.bats
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,21 @@ actual : a
--
ERR_MSG
}

@test 'assert_equal() -d <actual> <expected>: returns 0 if <actual> equals <expected>' {
run assert_equal -d 'a' 'a'
assert_test_pass
}

@test 'assert_equal() -d <actual> <expected>: returns 1 and displays details if <actual> does not equal <expected>' {
run assert_equal -d 'a' 'b'

assert_test_fail <<'ERR_MSG'

-- values do not equal --
@@ -1 +1 @@
-a
+b
--
ERR_MSG
}
21 changes: 20 additions & 1 deletion test/assert_output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ load test_helper
assert_test_pass
}

@test "assert_output() -d <expected>: returns 0 if <expected> equals \`\$output'" {
run echo 'a'
run assert_output -d 'a'
assert_test_pass
}

@test "assert_output() <expected>: returns 1 and displays details if <expected> does not equal \`\$output'" {
run echo 'b'
run assert_output 'a'
Expand All @@ -26,6 +32,20 @@ actual : b
ERR_MSG
}

@test "assert_output() -d <expected>: returns 1 and displays details if <expected> does not equal \`\$output'" {
run echo 'b'
run assert_output -d 'a'

assert_test_fail <<'ERR_MSG'

-- output differs --
@@ -1 +1 @@
-b
+a
--
ERR_MSG
}

@test 'assert_output(): succeeds if output is non-empty' {
run echo 'a'
run assert_output
Expand Down Expand Up @@ -262,7 +282,6 @@ Invalid extended regular expression: `[.*'
ERR_MSG
}


#
# Common
#
Expand Down