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

Add support for 'assert_not_equal'. #37

Merged
merged 2 commits into from
Nov 4, 2021
Merged
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
martin-schulze-vireso marked this conversation as resolved.
Show resolved Hide resolved
--
```

If either value is longer than one line both are displayed in *multi-line* format.


### `assert_success`

Fail if `$status` is not 0.
Expand Down
1 change: 1 addition & 0 deletions load.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions src/assert_not_equal.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# assert_not_equal
# ============
#
# Summary: Fail if the actual and unexpected values are equal.
#
# Usage: assert_not_equal <actual> <unexpected>
#
# Options:
# <actual> The value being compared.
# <unexpected> 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
}
57 changes: 57 additions & 0 deletions test/assert_not_equal.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bats

load test_helper

@test 'assert_not_equal() <actual> <unexpected>: returns 0 if <actual> does not equal <unexpected>' {
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"
martin-schulze-vireso marked this conversation as resolved.
Show resolved Hide resolved
assert_test_pass
}

@test 'assert_not_equal() <actual> <unexpected>: returns 1 and displays details if <actual> equals <unexpected>' {
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() <actual> <unexpected>: displays details in multi-line format if <actual> and <unexpected> 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() <actual> <unexpected>: performs literal matching' {
run assert_not_equal 'a' '*'
assert_test_pass
}