From ee1290cf3fa900c0118ff881cf065c13182bf588 Mon Sep 17 00:00:00 2001 From: Juan Ibiapina Date: Thu, 11 Feb 2016 20:21:07 -0200 Subject: [PATCH] Allow heredoc on assert_output and refute_output --- README.md | 18 ++++++++++++++++++ src/assert.bash | 21 +++++++++++++++------ test/50-assert-15-assert_output.bats | 10 ++++++++++ test/50-assert-16-refute_output.bats | 9 +++++++++ 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b5f4250..c2ad3b3 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,15 @@ 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. ``` @@ -283,6 +292,15 @@ 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. ``` diff --git a/src/assert.bash b/src/assert.bash index 2e8015e..b2d1d2c 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -10,7 +10,7 @@ # # You should have received a copy of the CC0 Public Domain Dedication # along with this software. If not, see -# . +# . # # @@ -165,7 +165,8 @@ assert_failure() { } # Fail and display details if `$output' does not match the expected -# output. +# output. The expected output can be specified either by the first +# parameter or on the standard input. # # By default, literal matching is performed. The assertion fails if the # expected output does not equal `$output'. Details include both values. @@ -186,10 +187,12 @@ assert_failure() { # -p, --partial - partial matching # -e, --regexp - extended regular expression matching # Arguments: -# $1 - expected output +# $1 - [=STDIN] expected output # Returns: # 0 - expected matches the actual output # 1 - otherwise +# Inputs: +# STDIN - [=$1] expected output # Outputs: # STDERR - details, on failure # error message, on error @@ -215,7 +218,8 @@ assert_output() { fi # Arguments. - local -r expected="$1" + local expected + (( $# == 0 )) && expected="$(cat -)" || expected="$1" # Matching. if (( is_mode_regexp )); then @@ -252,6 +256,8 @@ assert_output() { } # Fail and display details if `$output' matches the unexpected output. +# The unexpected output can be specified either by the first parameter +# or on the standard input. # # By default, literal matching is performed. The assertion fails if the # unexpected output equals `$output'. Details include `$output'. @@ -274,10 +280,12 @@ assert_output() { # -p, --partial - partial matching # -e, --regexp - extended regular expression matching # Arguments: -# $1 - unexpected output +# $1 - [=STDIN] unexpected output # Returns: # 0 - unexpected matches the actual output # 1 - otherwise +# Inputs: +# STDIN - [=$1] unexpected output # Outputs: # STDERR - details, on failure # error message, on error @@ -303,7 +311,8 @@ refute_output() { fi # Arguments. - local -r unexpected="$1" + local unexpected + (( $# == 0 )) && unexpected="$(cat -)" || unexpected="$1" 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 2c6b750..de622f5 100755 --- a/test/50-assert-15-assert_output.bats +++ b/test/50-assert-15-assert_output.bats @@ -26,6 +26,16 @@ load test_helper [ "${lines[3]}" == '--' ] } +@test 'assert_output(): reads from STDIN' { + run echo 'a' + run assert_output <: displays details in multi-line format if \`\$output' is longer than one line" { run printf 'b 0\nb 1' diff --git a/test/50-assert-16-refute_output.bats b/test/50-assert-16-refute_output.bats index db7e6ed..2d47155 100755 --- a/test/50-assert-16-refute_output.bats +++ b/test/50-assert-16-refute_output.bats @@ -25,6 +25,15 @@ load test_helper [ "${lines[2]}" == '--' ] } +@test 'refute_output(): reads from STDIN' { + run echo 'a' + run refute_output <: displays details in multi-line format if necessary' { run printf 'a 0\na 1'