refute_output without args asserts output is empty
diff --git a/src/assert.bash b/src/assert.bash index b680fad..e1ffc4b 100644 --- a/src/assert.bash +++ b/src/assert.bash
@@ -293,9 +293,14 @@ refute_output() { local -i is_mode_partial=0 local -i is_mode_regexp=0 + local -i is_mode_empty=0 local -i use_stdin=0 # Handle options. + if (( $# == 0 )); then + is_mode_empty=1 + fi + while (( $# > 0 )); do case "$1" in -p|--partial) is_mode_partial=1; shift ;; @@ -329,7 +334,13 @@ fi # Matching. - if (( is_mode_regexp )); then + if (( is_mode_empty )); then + if [ -n "$output" ]; then + echo 'expected no output, but output was non-empty' \ + | batslib_decorate 'unexpected output' \ + | fail + fi + elif (( is_mode_regexp )); then if [[ $output =~ $unexpected ]] || (( $? == 0 )); then batslib_print_kv_single_or_multi 6 \ 'regexp' "$unexpected" \
diff --git a/test/50-assert-16-refute_output.bats b/test/50-assert-16-refute_output.bats index a289717..7983750 100755 --- a/test/50-assert-16-refute_output.bats +++ b/test/50-assert-16-refute_output.bats
@@ -25,6 +25,25 @@ [ "${lines[2]}" == '--' ] } +@test 'refute_output(): succeeds if output is empty' { + run echo '' + run refute_output +echo "$output" + [ "$status" -eq 0 ] + [ "${#lines[@]}" -eq 0 ] +} + +@test 'refute_output(): fails if output is non-empty' { + run echo 'a' + run refute_output +echo "$output" + [ "$status" -eq 1 ] + [ "${#lines[@]}" -eq 3 ] + [ "${lines[0]}" == '-- unexpected output --' ] + [ "${lines[1]}" == 'expected no output, but output was non-empty' ] + [ "${lines[2]}" == '--' ] +} + @test 'refute_output() - : reads <unexpected> from STDIN' { run echo '-' run refute_output - <<INPUT