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 assert_contains() as a new assertion #19

Open
wants to merge 3 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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ If either value is longer than one line both are displayed in
*multi-line* format.


### `assert_contains`

Fail and display details if the expected value is not contained
in the provided array. Details include both values.

```bash
@test 'assert_contains()' {
local items=(one two three)
assert_contains "four" "${items[@]}"
}
```

On failure, the expected and array values are displayed.

```
-- item was not found in the array --
expected : four
actual : one two three
--
```

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
28 changes: 28 additions & 0 deletions src/assert.bash
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ assert_equal() {
fi
}

# Fail and display details if the expected value is not contained
# in the provided array. Details include both values.
#
# Globals:
# none
# Arguments:
# $1 - expected value
# ${@:2} - array of values to search
# Returns:
# 0 - value is found in array
# 1 - otherwise
# Outputs:
# STDERR - details, on failure
assert_contains() {
local item
for item in "${@:2}"; do
if [[ "$item" == "$1" ]]; then
return 0
fi
done

batslib_print_kv_single_or_multi 8 \
'expected' "$1" \
'actual' "$(echo ${@:2})" \
| batslib_decorate 'item was not found in the array' \
| fail
}

# Fail and display details if `$status' is not 0. Details include
# `$status' and `$output'.
#
Expand Down
31 changes: 31 additions & 0 deletions test/50-assert-20-contains.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bats

load test_helper

@test 'assert_contains() <item> <items>: returns 0 if <item> is in <items>' {
local items=(one two three)
run assert_contains two "${items[@]}"

[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}

@test 'assert_contains() <item> <items>: returns 0 if <item> containing spaces is in <items>' {
local items=("one two" three)
run assert_contains "one two" "${items[@]}"

[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}

@test 'assert_contains() <item> <items>: returns 1 and displays details if <item> is not in <items>' {
local items=(one two three)
run assert_contains four "${items[@]}"

[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- item was not found in the array --' ]
[ "${lines[1]}" == 'expected : four' ]
[ "${lines[2]}" == 'actual : one two three' ]
[ "${lines[3]}" == '--' ]
}