From 38bfbf61443c046acf91b678a5a8eb5a0ab13000 Mon Sep 17 00:00:00 2001 From: Josh Wheeler Date: Mon, 18 Oct 2021 22:02:34 -0600 Subject: [PATCH] Add support for 'assert_not_equal'. --- README.md | 23 ++++++++++++++++ load.bash | 1 + src/assert_not_equal.bash | 42 ++++++++++++++++++++++++++++ test/assert_not_equal.bats | 56 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 src/assert_not_equal.bash create mode 100644 test/assert_not_equal.bats diff --git a/README.md b/README.md index 2adbfd9..831374c 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ This project provides the following functions: - [assert](#assert) / [refute](#refute) Assert a given expression evaluates to `true` or `false`. - [assert_equal](#assert_equal) Assert two parameters are equal. + - [assert_not_equal](#assert_not_equal) Assert two parameters are not equal. - [assert_success](#assert_success) / [assert_failure](#assert_failure) Assert exit status is `0` or `1`. - [assert_output](#assert_output) / [refute_output](#refute_output) Assert output does (or does not) contain given content. - [assert_line](#assert_line) / [refute_line](#refute_line) Assert a specific line of output does (or does not) contain given content. @@ -127,6 +128,28 @@ actual : have If either value is longer than one line both are displayed in *multi-line* format. +### `assert_not_equal` + +Fail if the two parameters, actual and unexpected value respectively, are equal. + +```bash +@test 'assert_not_equal()' { + assert_not_equal 'foobar' 'foobar' +} +``` + +On failure, the expected and actual values are displayed. + +``` +-- values should not be equal -- +unexpected : foobar +actual : foobar +-- +``` + +If either value is longer than one line both are displayed in *multi-line* format. + + ### `assert_success` Fail if `$status` is not 0. diff --git a/load.bash b/load.bash index 38bffe0..1201df1 100644 --- a/load.bash +++ b/load.bash @@ -22,6 +22,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/src/assert.bash" source "$(dirname "${BASH_SOURCE[0]}")/src/refute.bash" source "$(dirname "${BASH_SOURCE[0]}")/src/assert_equal.bash" +source "$(dirname "${BASH_SOURCE[0]}")/src/assert_not_equal.bash" source "$(dirname "${BASH_SOURCE[0]}")/src/assert_success.bash" source "$(dirname "${BASH_SOURCE[0]}")/src/assert_failure.bash" source "$(dirname "${BASH_SOURCE[0]}")/src/assert_output.bash" diff --git a/src/assert_not_equal.bash b/src/assert_not_equal.bash new file mode 100644 index 0000000..933bb71 --- /dev/null +++ b/src/assert_not_equal.bash @@ -0,0 +1,42 @@ +# assert_not_equal +# ============ +# +# Summary: Fail if the actual and unexpected values are equal. +# +# Usage: assert_not_equal +# +# Options: +# The value being compared. +# The value to compare against. +# +# ```bash +# @test 'assert_not_equal()' { +# assert_not_equal 'foo' 'foo' +# } +# ``` +# +# IO: +# STDERR - expected and actual values, on failure +# Globals: +# none +# Returns: +# 0 - if actual does not equal unexpected +# 1 - otherwise +# +# On failure, the unexpected and actual values are displayed. +# +# ``` +# -- values should not be equal -- +# unexpected : foo +# actual : foo +# -- +# ``` +assert_not_equal() { + if [[ "$1" == "$2" ]]; then + batslib_print_kv_single_or_multi 10 \ + 'unexpected' "$2" \ + 'actual' "$1" \ + | batslib_decorate 'values should not be equal' \ + | fail + fi +} diff --git a/test/assert_not_equal.bats b/test/assert_not_equal.bats new file mode 100644 index 0000000..28f0142 --- /dev/null +++ b/test/assert_not_equal.bats @@ -0,0 +1,56 @@ +#!/usr/bin/env bats + +load test_helper + +@test 'assert_not_equal() : returns 0 if does not equal ' { + run assert_not_equal foo bar + assert_test_pass + + run assert_not_equal "foo" "bar" + assert_test_pass + + run assert_not_equal "foo" "" + assert_test_pass + + run assert_not_equal "" "foo" +} + +@test 'assert_not_equal() : returns 1 and displays details if equals ' { + run assert_not_equal 'foobar' 'foobar' + assert_test_fail <<'ERR_MSG' + +-- values should not be equal -- +unexpected : foobar +actual : foobar +-- +ERR_MSG + + run assert_not_equal 1 1 + assert_test_fail <<'ERR_MSG' + +-- values should not be equal -- +unexpected : 1 +actual : 1 +-- +ERR_MSG +} + +@test 'assert_not_equal() : displays details in multi-line format if and are longer than one line' { + run assert_not_equal $'foo\nbar' $'foo\nbar' + assert_test_fail <<'ERR_MSG' + +-- values should not be equal -- +unexpected (2 lines): + foo + bar +actual (2 lines): + foo + bar +-- +ERR_MSG +} + +@test 'assert_not_equal() : performs literal matching' { + run assert_not_equal 'a' '*' + assert_test_pass +} \ No newline at end of file