Skip to content

Commit

Permalink
Merge pull request #95 from cmayo/main
Browse files Browse the repository at this point in the history
Add namespace function names
  • Loading branch information
Chemaclass authored Sep 15, 2023
2 parents 7afa0b7 + c6e68b6 commit f6771f0
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 112 deletions.
56 changes: 28 additions & 28 deletions src/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
function assertEquals() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" != "$actual" ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${expected}" "but got" "${actual}"
Console::printFailedTest "${label}" "${expected}" "but got" "${actual}"
return
fi

Expand All @@ -16,11 +16,11 @@ function assertEquals() {

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

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

Expand All @@ -29,11 +29,11 @@ function assertEmpty() {

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

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

Expand All @@ -43,11 +43,11 @@ function assertNotEmpty() {
function assertNotEquals() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" == "$actual" ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${expected}" "but got" "${actual}"
Console::printFailedTest "${label}" "${expected}" "but got" "${actual}"
return
fi

Expand All @@ -57,11 +57,11 @@ function assertNotEquals() {
function assertContains() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

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

Expand All @@ -71,11 +71,11 @@ function assertContains() {
function assertNotContains() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

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

Expand All @@ -85,11 +85,11 @@ function assertNotContains() {
function assertMatches() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if ! [[ $actual =~ $expected ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to match" "${expected}"
Console::printFailedTest "${label}" "${actual}" "to match" "${expected}"
return
fi

Expand All @@ -99,11 +99,11 @@ function assertMatches() {
function assertNotMatches() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ $actual =~ $expected ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to not match" "${expected}"
Console::printFailedTest "${label}" "${actual}" "to not match" "${expected}"
return
fi

Expand All @@ -113,11 +113,11 @@ function assertNotMatches() {
function assertExitCode() {
local actual_exit_code=$?
local expected_exit_code="$1"
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be" "${expected_exit_code}"
Console::printFailedTest "${label}" "${actual_exit_code}" "to be" "${expected_exit_code}"
return
fi

Expand All @@ -127,11 +127,11 @@ function assertExitCode() {
function assertSuccessfulCode() {
local actual_exit_code=$?
local expected_exit_code=0
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ $actual_exit_code != "$expected_exit_code" ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi

Expand All @@ -141,11 +141,11 @@ function assertSuccessfulCode() {
function assertGeneralError() {
local actual_exit_code=$?
local expected_exit_code=1
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi

Expand All @@ -156,11 +156,11 @@ function assertGeneralError() {
function assertCommandNotFound() {
local actual_exit_code=$?
local expected_exit_code=127
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi

Expand All @@ -169,13 +169,13 @@ function assertCommandNotFound() {

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

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

Expand All @@ -184,13 +184,13 @@ function assertArrayContains() {

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

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

Expand Down
14 changes: 7 additions & 7 deletions src/console_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ _TESTS_FAILED=0
_ASSERTIONS_PASSED=0
_ASSERTIONS_FAILED=0

function renderResult() {
function Console::renderResult() {
local tests_passed=$1
local tests_failed=$2
local assertions_passed=$3
Expand Down Expand Up @@ -36,28 +36,28 @@ function renderResult() {
printf " %s total\n" "$total_assertions"

if [[ "$tests_failed" -gt 0 ]]; then
printExecutionTime
Console::printExecutionTime
exit 1
fi

printf "%s%s%s\n" "$_COLOR_ALL_PASSED" "All tests passed" "$_COLOR_DEFAULT"
printExecutionTime
Console::printExecutionTime
exit 0
}

function printExecutionTime() {
function Console::printExecutionTime() {
if [ "$_OS" != "OSX" ]; then
_EXECUTION_TIME=$((($(date +%s%N) - "$_START_TIME") / 1000000))
printf "${_COLOR_BOLD}%s${_COLOR_DEFAULT}\n" "Time taken: ${_EXECUTION_TIME} ms"
fi
}

function printSuccessfulTest() {
function Console::printSuccessfulTest() {
local test_name=$1
printf "%s✓ Passed%s: %s\n" "$_COLOR_PASSED" "$_COLOR_DEFAULT" "${test_name}"
}

function printFailedTest() {
function Console::printFailedTest() {
local test_name=$1
local expected=$2
local failure_condition_message=$3
Expand All @@ -72,4 +72,4 @@ ${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
}

# Set a trap to call renderResult when the script exits
trap 'renderResult $_TESTS_PASSED $_TESTS_FAILED $_ASSERTIONS_PASSED $_ASSERTIONS_FAILED' EXIT
trap 'Console::renderResult $_TESTS_PASSED $_TESTS_FAILED $_ASSERTIONS_PASSED $_ASSERTIONS_FAILED' EXIT
8 changes: 4 additions & 4 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# @result string Eg: "Some logic"
#
function normalizeTestFunctionName() {
function Helper::normalizeTestFunctionName() {
local original_function_name="$1"
local result

Expand All @@ -21,7 +21,7 @@ function normalizeTestFunctionName() {
echo "$result"
}

function getFunctionsToRun() {
function Helper::getFunctionsToRun() {
local prefix=$1
local filter=$2
local function_names=$3
Expand Down Expand Up @@ -52,15 +52,15 @@ function getFunctionsToRun() {
echo "${functions_to_run[@]}"
}

function executeFunctionIfExists() {
function Helper::executeFunctionIfExists() {
local function_name=$1

if declare -F | awk '{print $3}' | grep -Eq "^${function_name}$"; then
"$function_name"
fi
}

function unsetIfExists() {
function Helper::unsetIfExists() {
local function_name=$1

if declare -F | awk '{print $3}' | grep -Eq "^${function_name}$"; then
Expand Down
Loading

0 comments on commit f6771f0

Please sign in to comment.