diff --git a/README.md b/README.md index 2e60e92..4842e5b 100644 --- a/README.md +++ b/README.md @@ -189,15 +189,6 @@ By default, literal matching is performed. The assertion fails if } ``` -The expected output can be specified with a heredoc or standard input as well. - -```bash -@test 'assert_output() with pipe' { - run echo 'have' - echo 'want' | assert_output -} -``` - On failure, the expected and actual output are displayed. ``` @@ -266,6 +257,23 @@ invalid. This option and partial matching (`--partial` or `-p`) are mutually exclusive. An error is displayed when used simultaneously. +#### Standard Input, HereDocs and HereStrings + +The expected output can be specified via standard input (also +heredoc/herestring) with the `-`/`--stdin` option. + +```bash +@test 'assert_output() with pipe' { + run echo 'hello' + echo 'hello' | assert_output - +} + +@test 'assert_output() with herestring' { + run echo 'hello' + assert_output - <<< hello +} +``` + ### `refute_output` @@ -287,15 +295,6 @@ By default, literal matching is performed. The assertion fails if } ``` --The unexpected output can be specified with a heredoc or standard input as well. - -```bash -@test 'refute_output() with pipe' { - run echo 'want' - echo 'want' | refute_output -} -``` - On failure, the output is displayed. ``` @@ -363,6 +362,23 @@ invalid. This option and partial matching (`--partial` or `-p`) are mutually exclusive. An error is displayed when used simultaneously. +#### Standard Input, HereDocs and HereStrings + +The unexpected output can be specified via standard input (also +heredoc/herestring) with the `-`/`--stdin` option. + +```bash +@test 'refute_output() with pipe' { + run echo 'hello' + echo 'world' | refute_output - +} + +@test 'refute_output() with herestring' { + run echo 'hello' + refute_output - <<< world +} +``` + ### `assert_line` diff --git a/src/assert.bash b/src/assert.bash index 1194753..1aac74d 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -172,8 +172,9 @@ assert_failure() { # Options: # -p, --partial - partial matching # -e, --regexp - extended regular expression matching +# -, --stdin - read expected output from the standard input # Arguments: -# $1 - [=STDIN] expected output +# $1 - expected output # Returns: # 0 - expected matches the actual output # 1 - otherwise @@ -185,12 +186,14 @@ assert_failure() { assert_output() { local -i is_mode_partial=0 local -i is_mode_regexp=0 + local -i use_stdin=0 # Handle options. while (( $# > 0 )); do case "$1" in -p|--partial) is_mode_partial=1; shift ;; -e|--regexp) is_mode_regexp=1; shift ;; + -|--stdin) use_stdin=1; shift ;; --) shift; break ;; *) break ;; esac @@ -205,7 +208,11 @@ assert_output() { # Arguments. local expected - (( $# == 0 )) && expected="$(cat -)" || expected="$1" + if (( use_stdin )); then + expected="$(cat -)" + else + expected="$1" + fi # Matching. if (( is_mode_regexp )); then @@ -213,9 +220,7 @@ assert_output() { echo "Invalid extended regular expression: \`$expected'" \ | batslib_decorate 'ERROR: assert_output' \ | fail - return $? - fi - if ! [[ $output =~ $expected ]]; then + elif ! [[ $output =~ $expected ]]; then batslib_print_kv_single_or_multi 6 \ 'regexp' "$expected" \ 'output' "$output" \ @@ -265,8 +270,9 @@ assert_output() { # Options: # -p, --partial - partial matching # -e, --regexp - extended regular expression matching +# -, --stdin - read unexpected output from the standard input # Arguments: -# $1 - [=STDIN] unexpected output +# $1 - unexpected output # Returns: # 0 - unexpected matches the actual output # 1 - otherwise @@ -278,12 +284,14 @@ assert_output() { refute_output() { local -i is_mode_partial=0 local -i is_mode_regexp=0 + local -i use_stdin=0 # Handle options. while (( $# > 0 )); do case "$1" in -p|--partial) is_mode_partial=1; shift ;; -e|--regexp) is_mode_regexp=1; shift ;; + -|--stdin) use_stdin=1; shift ;; --) shift; break ;; *) break ;; esac @@ -298,7 +306,11 @@ refute_output() { # Arguments. local unexpected - (( $# == 0 )) && unexpected="$(cat -)" || unexpected="$1" + if (( use_stdin )); then + unexpected="$(cat -)" + else + unexpected="$1" + fi if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then echo "Invalid extended regular expression: \`$unexpected'" \ diff --git a/test/50-assert-15-assert_output.bats b/test/50-assert-15-assert_output.bats index cca79cc..90c2056 100755 --- a/test/50-assert-15-assert_output.bats +++ b/test/50-assert-15-assert_output.bats @@ -26,12 +26,20 @@ load test_helper [ "${lines[3]}" == '--' ] } -@test 'assert_output(): reads from STDIN' { +@test 'assert_output() - : reads from STDIN' { run echo 'a' - run assert_output < from STDIN' { + run echo 'a' + run assert_output --stdin < from STDIN' { - run echo 'a' - run refute_output < from STDIN' { + run echo '-' + run refute_output - < from STDIN' { + run echo '--stdin' + run refute_output --stdin <