Skip to content
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

Fix: assert_line_count #264

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 10 additions & 8 deletions src/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,23 @@ function assert_greater_or_equal_than() {

function assert_line_count() {
local expected="$1"
local actual="$2"
local input_str="$2"
local label="${3:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"

if [ -z "$actual" ]; then
local actual_line_count=0
if [ -z "$input_str" ]; then
local actual=0
else
local actual_line_count
actual_line_count=$(echo "$actual" | wc -l | tr -d '[:blank:]')
local actual
actual=$(echo "$input_str" | wc -l | tr -d '[:blank:]')
additional_new_lines=$(grep -o '\\n' <<< "$input_str" | wc -l | tr -d '[:blank:]')
Comment on lines +328 to +329
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't get why/how bash represents the newline in 2 different characters.. but the tests seem to show it works.

I don't know why 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Same here... I just got to this solution driven by the tests, I don't understand why this works... so it's bash... 🤣

And here, yet, another proof that TDD works 😝

((actual+=additional_new_lines))
fi

if [[ "$expected" != "$actual_line_count" ]]; then
if [[ "$expected" != "$actual" ]]; then
state::add_assertions_failed
console_results::print_failed_test "${label}" "${actual}"\
console_results::print_failed_test "${label}" "${input_str}"\
"to contain number of lines equal to" "${expected}"\
"but found" "${actual_line_count}"
"but found" "${actual}"
return
fi

Expand Down
29 changes: 23 additions & 6 deletions tests/unit/assert_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,32 @@ function test_unsuccessful_assert_equals_ignore_colors() {
"$(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
function test_successful_assert_line_count_empty_str() {
assert_empty "$(assert_line_count 0 "")"
}

function test_successful_assert_line_count_one_line() {
assert_empty "$(assert_line_count 1 "one line")"
}

function test_successful_assert_count_multiline() {
local multiline_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")"
assert_empty "$(assert_line_count 3 "$multiline_string")"
}

function test_successful_assert_line_count_multiline_string_in_one_line() {
assert_empty "$(assert_line_count 4 "one\ntwo\nthree\nfour")"
}

function test_successful_assert_line_count_multiline_with_new_lines() {
local multiline_str="this \n is \n a multiline \n in one
this is line 5
this is \n line seven"

assert_empty "$(assert_line_count 7 "$multiline_str")"
}

function test_unsuccessful_assert_line_count() {
Expand Down