From 892c1f6449ed8253e2a368cf5e6e221fdcc8699d Mon Sep 17 00:00:00 2001 From: "Jose M. Valera Reales" Date: Sat, 31 May 2025 01:10:34 +0200 Subject: [PATCH 1/2] feat: add assert_not_called test double --- docs/test-doubles.md | 23 +++++++++++++++++++++++ src/test_doubles.sh | 6 ++++++ tests/unit/test_doubles_test.sh | 16 ++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/docs/test-doubles.md b/docs/test-doubles.md index 5f12f69c..c7163a44 100644 --- a/docs/test-doubles.md +++ b/docs/test-doubles.md @@ -156,3 +156,26 @@ function test_failure() { } ``` ::: + +## assert_not_called +> `assert_not_called "spy"` + +Reports an error if `spy` has been executed at least once. + +::: code-group +```bash [Example] +function test_success() { + spy ps + + assert_not_called ps +} + +function test_failure() { + spy ps + + ps + + assert_not_called ps +} +``` +::: diff --git a/src/test_doubles.sh b/src/test_doubles.sh index c9c02e19..29ba8102 100644 --- a/src/test_doubles.sh +++ b/src/test_doubles.sh @@ -141,3 +141,9 @@ function assert_have_been_called_times() { state::add_assertions_passed } + +function assert_not_called() { + local command=$1 + local label="${2:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}" + assert_have_been_called_times 0 "$command" "$label" +} diff --git a/tests/unit/test_doubles_test.sh b/tests/unit/test_doubles_test.sh index 22040c67..ced4f821 100644 --- a/tests/unit/test_doubles_test.sh +++ b/tests/unit/test_doubles_test.sh @@ -147,3 +147,19 @@ function test_spy_called_with_different_arguments() { assert_have_been_called_with "first_a first_b" ps 1 assert_have_been_called_with "second" ps 2 } + +function test_successful_not_called() { + spy ps + + assert_not_called ps +} + +function test_unsuccessful_not_called() { + spy ps + + ps + + assert_same \ + "$(console_results::print_failed_test \"Unsuccessful not called\" \"ps\" \"to has been called\" \"0 times\" \"actual\" \"1 times\")" \ + "$(assert_not_called ps)" +} From d8ced706922ab915276c4f0ff00b3495c7fa5c06 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sat, 31 May 2025 01:12:08 +0200 Subject: [PATCH 2/2] fix: linter --- tests/unit/test_doubles_test.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_doubles_test.sh b/tests/unit/test_doubles_test.sh index ced4f821..e54d04c3 100644 --- a/tests/unit/test_doubles_test.sh +++ b/tests/unit/test_doubles_test.sh @@ -148,18 +148,20 @@ function test_spy_called_with_different_arguments() { assert_have_been_called_with "second" ps 2 } -function test_successful_not_called() { +function test_spy_successful_not_called() { spy ps assert_not_called ps } -function test_unsuccessful_not_called() { +function test_spy_unsuccessful_not_called() { spy ps ps assert_same \ - "$(console_results::print_failed_test \"Unsuccessful not called\" \"ps\" \"to has been called\" \"0 times\" \"actual\" \"1 times\")" \ + "$(console_results::print_failed_test "Unsuccessful not called" "ps" \ + "to has been called" "0 times" \ + "actual" "1 times")" \ "$(assert_not_called ps)" }