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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ assert_eq "hello" "world"
* `assert_ge` checks whether the first param is greator than or equal to the second one.
* `assert_lt` checks whether the first param is less than the second one.
* `assert_le` checks whether the first param is less than or equal to the second one.
* `assert_file_exist` checks whether the first param is an existing regular file.
* `assert_file_not_exist` checks whether the first param is not an existing regular file.

### How to write tests

Expand Down
50 changes: 48 additions & 2 deletions assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,15 @@ assert_contain() {
return 0;
fi

if [ -z "${haystack:+x}" ] && [ ! -z "${needle:+x}" ]; then
[ "${#msg}" -gt 0 ] && log_failure "'$haystack' doesn't contain '$needle' :: $msg" || true
return 1;
fi

if [ -z "${haystack##*$needle*}" ]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$haystack doesn't contain $needle :: $msg" || true
[ "${#msg}" -gt 0 ] && log_failure "'$haystack' doesn't contain '$needle' :: $msg" || true
return 1
fi
}
Expand All @@ -231,14 +236,23 @@ assert_not_contain() {
msg="$3"
fi

if [ -z "${haystack:+x}" ] && [ -z "${needle:+x}" ]; then
[ "${#msg}" -gt 0 ] && log_failure "'$haystack' contains '$needle' :: $msg" || true
return 1;
fi

if [ -z "${haystack:+x}" ]; then
return 0;
fi

if [ -z "${needle:+x}" ]; then
return 0;
fi

if [ "${haystack##*$needle*}" ]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$haystack contains $needle :: $msg" || true
[ "${#msg}" -gt 0 ] && log_failure "'$haystack' contains '$needle' :: $msg" || true
return 1
fi
}
Expand Down Expand Up @@ -310,3 +324,35 @@ assert_le() {
return 1
fi
}

assert_file_exist() {
local first="$1"
local msg

if [ "$#" -ge 2 ]; then
msg="$2"
fi

if [[ -f "$first" ]]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$first ! exist :: $msg" || true
return 1
fi
}

assert_file_not_exist() {
local first="$1"
local msg

if [ "$#" -ge 2 ]; then
msg="$2"
fi

if [[ ! -f "$first" ]]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$first exists :: $msg" || true
return 1
fi
}
117 changes: 116 additions & 1 deletion test_assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,27 @@ test_assert_contain() {
else
log_failure "assert_contain does not work"
fi

assert_contain "" "needle"
if [ "$?" == 1 ]; then
log_success "assert_contain returns 1 if the haystack is an empty string"
else
log_failure "assert_contain does not work"
fi

assert_contain "haystack" ""
if [ "$?" == 0 ]; then
log_success "assert_contain returns 0 if the needle is an empty string"
else
log_failure "assert_contain does not work"
fi

assert_contain "" ""
if [ "$?" == 0 ]; then
log_success "assert_contain returns 0 if the haystack and needle are empty strings"
else
log_failure "assert_contain does not work"
fi
}

test_assert_not_contain() {
Expand Down Expand Up @@ -359,6 +380,28 @@ test_assert_not_contain() {
else
log_failure "assert_not_contain does not work"
fi

# assert_not_contain haystack needle
assert_not_contain "" "needle"
if [ "$?" == 0 ]; then
log_success "assert_not_contain returns 0 if the haystack is an empty string"
else
log_failure "assert_not_contain does not work"
fi

assert_not_contain "haystack" ""
if [ "$?" == 0 ]; then
log_success "assert_not_contain returns 1 if the needle is an empty string"
else
log_failure "assert_not_contain does not work"
fi

assert_not_contain "" ""
if [ "$?" == 1 ]; then
log_success "assert_not_contain returns 1 if the haystack and needle are empty strings"
else
log_failure "assert_not_contain does not work"
fi
}

test_assert_gt() {
Expand Down Expand Up @@ -465,6 +508,77 @@ test_assert_le() {
fi
}

test_assert_file_exist() {
log_header "Test :: assert_file_exist"

local tmp_existing_file="tmp_test_file"
local tmp_non_existent_fle="tmp_should_not_exist_file"
local tmp_existing_dir="tmp_test_dir"

# ci-before
echo "A test file" > "$tmp_existing_file"
mkdir "$tmp_existing_dir"

assert_file_exist "$tmp_existing_file"
if [ "$?" == 0 ]; then
log_success "assert_file_exist returns 0 if param is existing file"
else
log_failure "assert_file_exist does not work"
fi

assert_file_exist "$tmp_non_existent_fle"
if [ "$?" == 1 ]; then
log_success "assert_file_exist returns 1 if param is not existing path"
else
log_failure "assert_file_exist does not work"
fi

assert_file_exist "$tmp_existing_dir"
if [ "$?" == 1 ]; then
log_success "assert_file_exist returns 1 if param is a existing directory"
else
log_failure "assert_file_exist does not work"
fi

# ci-after
rm -rf "$tmp_existing_file" "$tmp_existing_dir"
}

test_assert_file_not_exist() {
log_header "Test :: assert_file_not_exist"

local tmp_existing_file="tmp_test_file"
local tmp_non_existent_fle="tmp_should_not_exist_file"
local tmp_existing_dir="tmp_test_dir"

# ci-before
echo "A test file" > "$tmp_existing_file"
mkdir "$tmp_existing_dir"

assert_file_not_exist "$tmp_non_existent_fle"
if [ "$?" == 0 ]; then
log_success "assert_file_not_exist returns 0 if param is non existing file"
else
log_failure "assert_file_not_exist does not work"
fi

assert_file_not_exist "$tmp_existing_dir"
if [ "$?" == 0 ]; then
log_success "assert_file_not_exist returns 1 if param is a existing directory"
else
log_failure "assert_file_not_exist does not work"
fi

assert_file_not_exist "$tmp_existing_file"
if [ "$?" == 1 ]; then
log_success "assert_file_not_exist returns 1 if param is a existing file"
else
log_failure "assert_file_not_exist does not work"
fi

# ci-after
rm -rf "$tmp_existing_file" "$tmp_existing_dir"
}


# test calls
Expand All @@ -483,4 +597,5 @@ test_assert_gt
test_assert_ge
test_assert_lt
test_assert_le

test_assert_file_exist
test_assert_file_not_exist