Skip to content
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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not need quotation marks around 1


for i in "${!map_name[@]}"
do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place do at the end of for header.

if [[ "${map_name[$i]}" = "$element" ]]; then
contains="0"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can return here.

fi
done

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place quotation marks around $msg.

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