diff --git a/README.md b/README.md index c2ad3b3..3fff359 100644 --- a/README.md +++ b/README.md @@ -54,30 +54,24 @@ this test always fails Fail if the given expression evaluates to false. ***Note:*** *The expression must be a simple command. [Compound -commands](https://www.gnu.org/software/bash/manual/bash.html#Compound-Commands), -such as `[[`, can be used only when executed with `bash -c`. +commands][bash-comp-cmd], such as `[[`, can be used only when executed +with `bash -c`. ```bash @test 'assert()' { - run touch '/var/log/test.log' + touch '/var/log/test.log' assert [ -e '/var/log/test.log' ] } ``` -On failure, the failed expression, `$status` and `$output` are -displayed. +On failure, the failed expression is displayed. ``` -- assertion failed -- expression : [ -e /var/log/test.log ] -status : 1 -output : touch: cannot touch ‘/var/log/test.log’: Permission denied -- ``` -If `$output` is longer than one line, it is displayed in *multi-line* -format. - ### `assert_equal` @@ -641,3 +635,4 @@ exclusive. An error is displayed when used simultaneously. [bats-core-output]: https://github.com/ztombol/bats-core#output-formatting [bats-core]: https://github.com/ztombol/bats-core [bats-docs]: https://github.com/ztombol/bats-docs +[bash-comp-cmd]: https://www.gnu.org/software/bash/manual/bash.html#Compound-Commands diff --git a/src/assert.bash b/src/assert.bash index b2d1d2c..7f87dc6 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -49,15 +49,13 @@ fail() { return 1 } -# Fail and display details if the expression evaluates to false. Details -# include the expression, `$status' and `$output'. +# Fail and display the expression if it evaluates to false. # # NOTE: The expression must be a simple command. Compound commands, such # as `[[', can be used only when executed with `bash -c'. # # Globals: -# status -# output +# none # Arguments: # $1 - expression # Returns: @@ -67,18 +65,8 @@ fail() { # STDERR - details, on failure assert() { if ! "$@"; then - { local -ar single=( - 'expression' "$*" - 'status' "$status" - ) - local -ar may_be_multi=( - 'output' "$output" - ) - local -ir width="$( batslib_get_max_single_line_key_width \ - "${single[@]}" "${may_be_multi[@]}" )" - batslib_print_kv_single "$width" "${single[@]}" - batslib_print_kv_single_or_multi "$width" "${may_be_multi[@]}" - } | batslib_decorate 'assertion failed' \ + batslib_print_kv_single 10 'expression' "$*" \ + | batslib_decorate 'assertion failed' \ | fail fi } diff --git a/test/50-assert-11-assert.bats b/test/50-assert-11-assert.bats index 5ada1c6..6b7606b 100755 --- a/test/50-assert-11-assert.bats +++ b/test/50-assert-11-assert.bats @@ -8,30 +8,11 @@ load test_helper [ "${#lines[@]}" -eq 0 ] } -@test 'assert() : returns 1 and displays details if evaluates to FALSE' { - run bash -c 'echo "a" - false' +@test 'assert() : returns 1 and displays if it evaluates to FALSE' { run assert false [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] + [ "${#lines[@]}" -eq 3 ] [ "${lines[0]}" == '-- assertion failed --' ] [ "${lines[1]}" == 'expression : false' ] - [ "${lines[2]}" == 'status : 1' ] - [ "${lines[3]}" == 'output : a' ] - [ "${lines[4]}" == '--' ] -} - -@test "assert() : displays \`\$output' in multi-line format if it is longer than one line" { - run bash -c 'printf "a 0\na 1" - false' - run assert false - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- assertion failed --' ] - [ "${lines[1]}" == 'expression : false' ] - [ "${lines[2]}" == 'status : 1' ] - [ "${lines[3]}" == 'output (2 lines):' ] - [ "${lines[4]}" == ' a 0' ] - [ "${lines[5]}" == ' a 1' ] - [ "${lines[6]}" == '--' ] + [ "${lines[2]}" == '--' ] }