Skip to content

Add assert_line_count #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:]')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on my macos, wc -l returns a number prepended by whitespaces. therefore I used tr to strip them

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")"
}