From 21565589e4714c5194363f67dc600ad3db9d7e86 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 17 Jun 2024 09:07:05 +0200 Subject: [PATCH 1/3] feat: Add `assert_line_count` --- CHANGELOG.md | 1 + docs/assertions.md | 21 +++++++++++++++++++++ src/assert.sh | 20 ++++++++++++++++++++ tests/unit/assert_test.sh | 18 ++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d58f0c2..d43b1504 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Add multi-invokers; consolidate parameterized-testing documentation - Add `fail()` function - Remove all test mocks after each test case +- Add `assert_line_count` ## [0.11.0](https://github.com/TypedDevs/bashunit/compare/0.10.1...0.11.0) - 2024-03-02 diff --git a/docs/assertions.md b/docs/assertions.md index ee9990c1..beeb5bd8 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -153,6 +153,27 @@ function test_failure() { ``` ::: +## assert_line_count +> `assert_line_count "count" "haystack"` + +Reports an error if `haystack` does not contain `count` lines. + +::: code-group +```bash [Example] +function test_success() { + local string="this is line one +this is line two +this is line three" + + assert_line_count "3" "$string" +} + +function test_failure() { + assert_line_count "2" "foobar" +} +``` +::: + ## assert_less_than > `assert_less_than "expected" "actual"` diff --git a/src/assert.sh b/src/assert.sh index c3448352..b96fb239 100755 --- a/src/assert.sh +++ b/src/assert.sh @@ -315,3 +315,23 @@ function assert_greater_or_equal_than() { state::add_assertions_passed } + +function assert_line_count() { + local expected="$1" + local actual="$2" + local label="${3:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}" + + if [ -z "$actual" ]; then + local actual_line_count=0 + else + local actual_line_count=$(echo "$actual" | wc -l | tr -d '[:blank:]') + fi + + if [[ "$expected" != "$actual_line_count" ]]; then + state::add_assertions_failed + console_results::print_failed_test "${label}" "${actual}" "to contain number of lines equal to" "${expected}" + return + fi + + state::add_assertions_passed +} diff --git a/tests/unit/assert_test.sh b/tests/unit/assert_test.sh index f55466ed..d82b8c82 100644 --- a/tests/unit/assert_test.sh +++ b/tests/unit/assert_test.sh @@ -333,3 +333,21 @@ function test_unsuccessful_assert_equals_ignore_colors() { "✗ Failed foo")"\ "$(assert_equals_ignore_colors "$string" "$string")" } + +function test_successful_assert_line_count() { + local one_line_string="one line" + local multi_line_string="this is line one + this is line two + this is line three" + + assert_empty "$(assert_line_count "0" "")" + assert_empty "$(assert_line_count "1" "$one_line_string")" + assert_empty "$(assert_line_count "3" "$multi_line_string")" +} + +function test_unsuccessful_assert_line_count() { + assert_equals\ + "$(console_results::print_failed_test\ + "Unsuccessful assert line count" "one_line_string" "to contain number of lines equal to" "10")"\ + "$(assert_line_count "10" "one_line_string")" +} From fcc99f0d33f854c0a4a1cebaab86daeaf9a0c849 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 17 Jun 2024 13:05:49 +0200 Subject: [PATCH 2/3] chore: fix changelog. use numbers over strings. --- CHANGELOG.md | 2 +- docs/assertions.md | 4 ++-- tests/unit/assert_test.sh | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d43b1504..04370c8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased](https://github.com/TypedDevs/bashunit/compare/0.12.0...main) - Allow calling assertions standalone outside tests +- Add `assert_line_count` ## [0.12.0](https://github.com/TypedDevs/bashunit/compare/0.11.0...0.12.0) - 2024-06-11 @@ -11,7 +12,6 @@ - Add multi-invokers; consolidate parameterized-testing documentation - Add `fail()` function - Remove all test mocks after each test case -- Add `assert_line_count` ## [0.11.0](https://github.com/TypedDevs/bashunit/compare/0.10.1...0.11.0) - 2024-03-02 diff --git a/docs/assertions.md b/docs/assertions.md index beeb5bd8..321d7f5d 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -165,11 +165,11 @@ function test_success() { this is line two this is line three" - assert_line_count "3" "$string" + assert_line_count 3 "$string" } function test_failure() { - assert_line_count "2" "foobar" + assert_line_count 2 "foobar" } ``` ::: diff --git a/tests/unit/assert_test.sh b/tests/unit/assert_test.sh index d82b8c82..382728df 100644 --- a/tests/unit/assert_test.sh +++ b/tests/unit/assert_test.sh @@ -340,14 +340,14 @@ function test_successful_assert_line_count() { this is line two this is line three" - assert_empty "$(assert_line_count "0" "")" - assert_empty "$(assert_line_count "1" "$one_line_string")" - assert_empty "$(assert_line_count "3" "$multi_line_string")" + assert_empty "$(assert_line_count 0 "")" + assert_empty "$(assert_line_count 1" "$one_line_string")" + assert_empty "$(assert_line_count 3 "$multi_line_string")" } function test_unsuccessful_assert_line_count() { assert_equals\ "$(console_results::print_failed_test\ "Unsuccessful assert line count" "one_line_string" "to contain number of lines equal to" "10")"\ - "$(assert_line_count "10" "one_line_string")" + "$(assert_line_count 10 "one_line_string")" } From 9f0a777bb90d16856adef37c37e61be907efc43f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 17 Jun 2024 13:15:19 +0200 Subject: [PATCH 3/3] chore: fix typo Co-authored-by: Jose Maria Valera Reales --- tests/unit/assert_test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/assert_test.sh b/tests/unit/assert_test.sh index 382728df..81e3acfe 100644 --- a/tests/unit/assert_test.sh +++ b/tests/unit/assert_test.sh @@ -341,7 +341,7 @@ function test_successful_assert_line_count() { this is line three" assert_empty "$(assert_line_count 0 "")" - assert_empty "$(assert_line_count 1" "$one_line_string")" + assert_empty "$(assert_line_count 1 "$one_line_string")" assert_empty "$(assert_line_count 3 "$multi_line_string")" }