Skip to content

Commit

Permalink
Merge pull request #79 from TypedDevs/remove-asserts-returns
Browse files Browse the repository at this point in the history
Remove asserts returns
  • Loading branch information
khru authored Sep 14, 2023
2 parents 663b20b + 49d9eba commit 78b71d1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 106 deletions.
46 changes: 18 additions & 28 deletions src/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ function assertEquals() {
if [[ "$expected" != "$actual" ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${expected}" "but got" "${actual}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertNotEquals() {
Expand All @@ -23,11 +22,10 @@ function assertNotEquals() {
if [[ "$expected" == "$actual" ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${expected}" "but got" "${actual}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertContains() {
Expand All @@ -36,13 +34,12 @@ function assertContains() {
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"

if ! [[ $actual == *"$expected"* ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to contain" "${expected}"
return 1
fi
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to contain" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
return 0
((_ASSERTIONS_PASSED++))
}

function assertNotContains() {
Expand All @@ -53,11 +50,10 @@ function assertNotContains() {
if [[ $actual == *"$expected"* ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to not contain" "${expected}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertMatches() {
Expand All @@ -68,11 +64,10 @@ function assertMatches() {
if ! [[ $actual =~ $expected ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to match" "${expected}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertNotMatches() {
Expand All @@ -83,11 +78,10 @@ function assertNotMatches() {
if [[ $actual =~ $expected ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to not match" "${expected}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertExitCode() {
Expand All @@ -98,11 +92,10 @@ function assertExitCode() {
if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be" "${expected_exit_code}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertSuccessfulCode() {
Expand All @@ -113,11 +106,10 @@ function assertSuccessfulCode() {
if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertGeneralError() {
Expand All @@ -128,11 +120,10 @@ function assertGeneralError() {
if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}


Expand All @@ -144,39 +135,38 @@ function assertCommandNotFound() {
if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertArrayContains() {
local expected="$1"
label="$(normalizeTestFunctionName "${FUNCNAME[1]}")"
shift
local actual=("$@")

if ! [[ "${actual[*]}" == *"$expected"* ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual[*]}" "to contain" "${expected}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}

function assertArrayNotContains() {
local expected="$1"
label="$(normalizeTestFunctionName "${FUNCNAME[1]}")"
shift
local actual=("$@")

if [[ "${actual[*]}" == *"$expected"* ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual[*]}" "to not contain" "${expected}"
return 1
return
fi

((_ASSERTIONS_PASSED++))
return 0
}
12 changes: 8 additions & 4 deletions tests/functional/logic_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ function test_should_validate_an_ok_exit_code() {
function fake_function() {
return 0
}

fake_function

assertExitCode "0"
}

function test_should_validate_a_non_ok_exit_code() {
function fake_function() {
return 1
}

fake_function

assertExitCode "1"
}

function test_other_way_of_using_the_exit_code() {
function fake_function() {
return 1
}
assertExitCode "1" "$(fake_function)"
}
return 1
}

assertExitCode "1" "$(fake_function)"
}
Loading

0 comments on commit 78b71d1

Please sign in to comment.