Skip to content

Commit

Permalink
Merge pull request #264 from TypedDevs/fix/assert-line-count
Browse files Browse the repository at this point in the history
Fix: assert_line_count
  • Loading branch information
Chemaclass authored Jun 17, 2024
2 parents cd28a3a + 50550c8 commit 775c8de
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
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:]')
((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
31 changes: 25 additions & 6 deletions tests/unit/assert_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,34 @@ 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
\n
this is line 7
this is \n line nine
"

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

function test_unsuccessful_assert_line_count() {
Expand Down

0 comments on commit 775c8de

Please sign in to comment.