diff --git a/.env.example b/.env.example index 78c6bcdd..164a2c57 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ PARALLEL_RUN= + diff --git a/CHANGELOG.md b/CHANGELOG.md index 30cd0af5..6e21fab1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/assert.sh b/src/assert.sh index d2293b3a..5ca40b22 100755 --- a/src/assert.sh +++ b/src/assert.sh @@ -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" diff --git a/tests/unit/assert_test.sh b/tests/unit/assert_test.sh index cab21412..b4cc3a1c 100644 --- a/tests/unit/assert_test.sh +++ b/tests/unit/assert_test.sh @@ -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")" }