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 array contains #10

Open
wants to merge 3 commits into
base: main
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
21 changes: 21 additions & 0 deletions assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ assert_array_not_eq() {
return "$return_code"
}

assert_array_contains() {

declare -n map_name="$1"
local element="$2"
local msg="${3-}"
local contains="1"

for i in "${!map_name[@]}"
do
if [[ "${map_name[$i]}" = "$element" ]]; then
contains="0"
fi
done

if [ "$contains" != "0" ]; then
[ "${#msg}" -gt 0 ] && log_failure $msg || true
fi

return "$contains"
}

assert_empty() {
local actual=$1
local msg="${2-}"
Expand Down
24 changes: 24 additions & 0 deletions test_assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,29 @@ test_assert_array_not_eq() {

}

test_assert_array_contains() {
log_header "Test :: assert_array_contains"

input=("one" "tw oo" "333")
element="one"

assert_array_contains input $element # it can be an issue on other implementation of shell
if [ "$?" == 0 ]; then
log_success "assert_array_contains returns 0 if the arrays contains the element"
else
log_failure "assert_array_contains should return 0"
fi

element="onee"
assert_array_contains input $element # it can be an issue on other implementation of shell
if [ "$?" == 1 ]; then
log_success "assert_array_contains returns 1 if the arrays not contains the element"
else
log_failure "assert_array_contains should return 1"
fi

}

test_assert_empty() {
log_header "Test :: assert_empty"

Expand Down Expand Up @@ -479,6 +502,7 @@ test_assert_true
test_assert_false
test_assert_array_eq
test_assert_array_not_eq
test_assert_array_contains
test_assert_empty
test_assert_not_empty
test_assert_contain
Expand Down