diff --git a/src/assert_equal.bash b/src/assert_equal.bash index 4ef1297..c6e5cc0 100644 --- a/src/assert_equal.bash +++ b/src/assert_equal.bash @@ -3,11 +3,12 @@ # # Summary: Fail if the actual and expected values are not equal. # -# Usage: assert_equal +# Usage: assert_equal [-d] # # Options: # The value being compared. # The value to compare against. +# -d, --diff Show diff between `expected` and `$output` # # ```bash # @test 'assert_equal()' { @@ -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 } diff --git a/src/assert_output.bash b/src/assert_output.bash index 82d0a0c..8e9739c 100644 --- a/src/assert_output.bash +++ b/src/assert_output.bash @@ -3,11 +3,12 @@ # # Summary: Fail if `$output' does not match the expected output. # -# Usage: assert_output [-p | -e] [- | [--] ] +# Usage: assert_output [-p | -e | -d] [- | [--] ] # # 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 # The expected value, substring or regular expression # @@ -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. @@ -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. @@ -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 ;; -|--stdin) use_stdin=1; shift ;; --) shift; break ;; *) break ;; @@ -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 \ diff --git a/test/assert_equal.bats b/test/assert_equal.bats index 7d3fc86..f4ab3b5 100755 --- a/test/assert_equal.bats +++ b/test/assert_equal.bats @@ -60,3 +60,21 @@ actual : a -- ERR_MSG } + +@test 'assert_equal() -d : returns 0 if equals ' { + run assert_equal -d 'a' 'a' + assert_test_pass +} + +@test 'assert_equal() -d : returns 1 and displays details if does not equal ' { + run assert_equal -d 'a' 'b' + + assert_test_fail <<'ERR_MSG' + +-- values do not equal -- +@@ -1 +1 @@ +-a ++b +-- +ERR_MSG +} diff --git a/test/assert_output.bats b/test/assert_output.bats index e9609e0..8c79342 100755 --- a/test/assert_output.bats +++ b/test/assert_output.bats @@ -13,6 +13,12 @@ load test_helper assert_test_pass } +@test "assert_output() -d : returns 0 if equals \`\$output'" { + run echo 'a' + run assert_output -d 'a' + assert_test_pass +} + @test "assert_output() : returns 1 and displays details if does not equal \`\$output'" { run echo 'b' run assert_output 'a' @@ -26,6 +32,20 @@ actual : b ERR_MSG } +@test "assert_output() -d : returns 1 and displays details if 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 @@ -262,7 +282,6 @@ Invalid extended regular expression: `[.*' ERR_MSG } - # # Common #