diff options
author | Zoltán Tömböl <zoltan.tombol@gmail.com> | 2016-02-25 03:48:09 +0300 |
---|---|---|
committer | Zoltán Tömböl <zoltan.tombol@gmail.com> | 2016-02-25 03:48:09 +0300 |
commit | c529cbe996cc01d19d000048ff691014d150f7c2 (patch) | |
tree | ae2042be421308f9be8e2da89713b9987e06bbe1 | |
parent | 2e00005b36fce799d4be0777fe82de812dcf4c0e (diff) | |
parent | 94988734ca1d4debbc72ec6c13337860846710bd (diff) |
Merge pull request #4 from jasonkarns/refute
Add refute function
-rw-r--r-- | README.md | 24 | ||||
-rw-r--r-- | src/assert.bash | 22 | ||||
-rwxr-xr-x | test/50-assert-19-refute.bats | 18 |
3 files changed, 64 insertions, 0 deletions
@@ -73,6 +73,30 @@ expression : [ -e /var/log/test.log ] ``` +### `refute` + +Fail if the given expression evaluates to true. + +***Note:*** *The expression must be a simple command. [Compound +commands][bash-comp-cmd], such as `[[`, can be used only when executed +with `bash -c`.* + +```bash +@test 'refute()' { + rm -f '/var/log/test.log' + refute [ -e '/var/log/test.log' ] +} +``` + +On failure, the successful expression is displayed. + +``` +-- assertion succeeded, but it was expected to fail -- +expression : [ -e /var/log/test.log ] +-- +``` + + ### `assert_equal` Fail if the two parameters, actual and expected value respectively, do diff --git a/src/assert.bash b/src/assert.bash index 7f87dc6..78b8416 100644 --- a/src/assert.bash +++ b/src/assert.bash @@ -71,6 +71,28 @@ assert() { fi } +# Fail and display the expression if it evaluates to true. +# +# NOTE: The expression must be a simple command. Compound commands, such +# as `[[', can be used only when executed with `bash -c'. +# +# Globals: +# none +# Arguments: +# $1 - expression +# Returns: +# 0 - expression evaluates to FALSE +# 1 - otherwise +# Outputs: +# STDERR - details, on failure +refute() { + if "$@"; then + batslib_print_kv_single 10 'expression' "$*" \ + | batslib_decorate 'assertion succeeded, but it was expected to fail' \ + | fail + fi +} + # Fail and display details if the expected and actual values do not # equal. Details include both values. # diff --git a/test/50-assert-19-refute.bats b/test/50-assert-19-refute.bats new file mode 100755 index 0000000..191dc73 --- /dev/null +++ b/test/50-assert-19-refute.bats @@ -0,0 +1,18 @@ +#!/usr/bin/env bats + +load test_helper + +@test 'refute() <expression>: returns 0 if <expression> evaluates to FALSE' { + run refute false + [ "$status" -eq 0 ] + [ "${#lines[@]}" -eq 0 ] +} + +@test 'refute() <expression>: returns 1 and displays <expression> if it evaluates to TRUE' { + run refute true + [ "$status" -eq 1 ] + [ "${#lines[@]}" -eq 3 ] + [ "${lines[0]}" == '-- assertion succeeded, but it was expected to fail --' ] + [ "${lines[1]}" == 'expression : true' ] + [ "${lines[2]}" == '--' ] +} |