Skip to content

Commit

Permalink
Merge pull request #259 from staabm/line-count
Browse files Browse the repository at this point in the history
Add `assert_line_count`
  • Loading branch information
Chemaclass authored Jun 17, 2024
2 parents a6dd402 + 9f0a777 commit b7114c4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions docs/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
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 b7114c4

Please sign in to comment.