Skip to content

Commit

Permalink
Merge pull request #93 from TypedDevs/assert-empty
Browse files Browse the repository at this point in the history
Assert empty
  • Loading branch information
Chemaclass authored Sep 15, 2023
2 parents 1d32599 + d823964 commit fbcf09b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PARALLEL_RUN=

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Added `assertCommandNotFound`
- Added `assertArrayContains`
- Added `assertArrayNotContains`
- Added `assertEmpty`
- Added `assertNotEmpty`
- Improved the readability of the assert by using guard clause
- Update documentation
- Add support for the static analysis on MacOS
Expand Down
26 changes: 26 additions & 0 deletions src/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ function assertEquals() {
((_ASSERTIONS_PASSED++))
}

function assertEmpty() {
local expected="$1"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" != "" ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "to be empty" "but got" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
}

function assertNotEmpty() {
local expected="$1"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" == "" ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "to not be empty" "but got" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
}

function assertNotEquals() {
local expected="$1"
local actual="$2"
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/assert_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ function test_unsuccessful_assertEquals() {
"$(assertEquals "1" "2")"
}

function test_successful_assertEmpty() {
assertEquals "$SUCCESSFUL_EMPTY_MESSAGE" "$(assertEmpty "")"
}

function test_unsuccessful_assertEmpty() {
assertEquals\
"$(printFailedTest "Unsuccessful assertEmpty" "to be empty" "but got" "1")"\
"$(assertEmpty "1")"
}

function test_successful_assertNotEmpty() {
assertEquals "$SUCCESSFUL_EMPTY_MESSAGE" "$(assertNotEmpty "a_random_string")"
}

function test_unsuccessful_assertNotEmpty() {
assertEquals\
"$(printFailedTest "Unsuccessful assertNotEmpty" "to not be empty" "but got" "")"\
"$(assertNotEmpty "")"
}

function test_successful_assertNotEquals() {
assertEquals "$SUCCESSFUL_EMPTY_MESSAGE" "$(assertNotEquals "1" "2")"
}
Expand Down

0 comments on commit fbcf09b

Please sign in to comment.