Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert/Refute against nonempty/empty output #15

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,13 @@ By default, literal matching is performed. The assertion fails if
}
```

The expected output can be specified with a heredoc or standard input as well.
The expected output can be specified with a heredoc or standard input as well,
by providing `-` as an option.

```bash
@test 'assert_output() with pipe' {
run echo 'have'
echo 'want' | assert_output
echo 'want' | assert_output -
}
```

Expand All @@ -210,6 +211,26 @@ actual : have
If either value is longer than one line both are displayed in
*multi-line* format.

#### Existence

To assert that any (non-empty) output exists at all, simply omit the matching
argument.

```bash
@test 'assert_output()' {
run echo 'have'
assert_output
}
```

On failure, an error message is displayed.

```
-- no output --
expected non-empty output, but output was empty
--
```

#### Partial matching

Partial matching can be enabled with the `--partial` option (`-p` for
Expand Down Expand Up @@ -287,12 +308,13 @@ By default, literal matching is performed. The assertion fails if
}
```

-The unexpected output can be specified with a heredoc or standard input as well.
The unexpected output can be specified with a heredoc or standard input as well,
by providing `-` as an option.

```bash
@test 'refute_output() with pipe' {
run echo 'want'
echo 'want' | refute_output
echo 'want' | refute_output -
}
```

Expand All @@ -307,6 +329,25 @@ output : want
If output is longer than one line it is displayed in *multi-line*
format.

#### Existence

To assert that there is no output at all, simply omit the matching argument.

```bash
@test 'refute_output()' {
run foo --silent
refute_output
}
```

On failure, an error message is displayed.

```
-- unexpected output --
expected no output, but output was non-empty
--
```

#### Partial matching

Partial matching can be enabled with the `--partial` option (`-p` for
Expand Down
47 changes: 40 additions & 7 deletions src/assert.bash
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,19 @@ assert_failure() {
assert_output() {
local -i is_mode_partial=0
local -i is_mode_regexp=0
local -i is_mode_nonempty=0
local -i use_stdin=0

# Handle options.
if (( $# == 0 )); then
is_mode_nonempty=1
fi

while (( $# > 0 )); do
case "$1" in
-p|--partial) is_mode_partial=1; shift ;;
-e|--regexp) is_mode_regexp=1; shift ;;
-) use_stdin=1; shift ;;
--) shift; break ;;
*) break ;;
esac
Expand All @@ -205,17 +212,25 @@ 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
if (( is_mode_nonempty )); then
if [ -z "$output" ]; then
echo 'expected non-empty output, but output was empty' \
| batslib_decorate 'no output' \
| fail
fi
elif (( is_mode_regexp )); then
if [[ '' =~ $expected ]] || (( $? == 2 )); then
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" \
Expand Down Expand Up @@ -278,12 +293,19 @@ assert_output() {
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 ;;
-e|--regexp) is_mode_regexp=1; shift ;;
-) use_stdin=1; shift ;;
--) shift; break ;;
*) break ;;
esac
Expand All @@ -298,7 +320,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'" \
Expand All @@ -308,7 +334,14 @@ refute_output() {
fi

# Matching.
if (( is_mode_regexp )); then
if (( is_mode_empty )); then
if [ -n "$output" ]; then
batslib_print_kv_single_or_multi 6 \
'output' "$output" \
| batslib_decorate 'output non-empty, but expected no output' \
| fail
fi
elif (( is_mode_regexp )); then
if [[ $output =~ $unexpected ]] || (( $? == 0 )); then
batslib_print_kv_single_or_multi 6 \
'regexp' "$unexpected" \
Expand Down
22 changes: 19 additions & 3 deletions test/50-assert-15-assert_output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,28 @@ load test_helper
[ "${lines[3]}" == '--' ]
}

@test 'assert_output(): reads <expected> from STDIN' {
@test 'assert_output(): succeeds if output is non-empty' {
run echo 'a'
run assert_output <<STDIN
run assert_output
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}

@test 'assert_output(): fails if output is empty' {
run echo ''
run assert_output
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- no output --' ]
[ "${lines[1]}" == 'expected non-empty output, but output was empty' ]
[ "${lines[2]}" == '--' ]
}

@test 'assert_output() - : reads <expected> from STDIN' {
run echo 'a'
run assert_output - <<STDIN
a
STDIN
echo "$output"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
Expand Down
21 changes: 19 additions & 2 deletions test/50-assert-16-refute_output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,26 @@ load test_helper
[ "${lines[2]}" == '--' ]
}

@test 'refute_output(): reads <unexpected> from STDIN' {
@test 'refute_output(): succeeds if output is empty' {
run echo ''
run refute_output
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}

@test 'refute_output(): fails if output is non-empty' {
run echo 'a'
run refute_output <<INPUT
run refute_output
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- output non-empty, but expected no output --' ]
[ "${lines[1]}" == 'output : a' ]
[ "${lines[2]}" == '--' ]
}

@test 'refute_output() - : reads <unexpected> from STDIN' {
run echo '-'
run refute_output - <<INPUT
b
INPUT
[ "$status" -eq 0 ]
Expand Down