diff --git a/src/assert.sh b/src/assert.sh index 8d3bef10..8fa00ef7 100755 --- a/src/assert.sh +++ b/src/assert.sh @@ -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 diff --git a/tests/unit/assert_test.sh b/tests/unit/assert_test.sh index 2939caca..4b6d886e 100644 --- a/tests/unit/assert_test.sh +++ b/tests/unit/assert_test.sh @@ -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() {