Skip to content

Commit

Permalink
Add assert_line_count
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 17, 2024
1 parent 6651459 commit 8910ce8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions src/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions tests/unit/assert_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")"
}

0 comments on commit 8910ce8

Please sign in to comment.