From f698610949d30274e91ab507c7c2dcf18b685075 Mon Sep 17 00:00:00 2001 From: MOHAMMED-FAIZAN-KHAN Date: Fri, 23 Oct 2026 09:50:15 +0530 Subject: [PATCH] Fix word count edge cases Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- fixit/wordcount.py | 6 ++---- tests/test_textutils.py | 2 -- tests/test_wordcount.py | 11 ++++++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/fixit/wordcount.py b/fixit/wordcount.py index ede6f0d..b898f34 100644 --- a/fixit/wordcount.py +++ b/fixit/wordcount.py @@ -1,7 +1,5 @@ """Word, line and character counting.""" -import re - def count_words(text: str) -> int: """Count the words in the given text. @@ -10,7 +8,7 @@ def count_words(text: str) -> int: count_words("hello world") returns 2. count_words("one two three") returns 3. """ - return len(re.split(r'\s+', text.strip())) + return len(text.split()) def count_lines(text: str) -> int: @@ -20,7 +18,7 @@ def count_lines(text: str) -> int: count_lines("hello\nworld") returns 2. count_lines("hello") returns 1. """ - return len(text.split("\n")) + return len(text.splitlines()) def count_chars(text: str) -> int: diff --git a/tests/test_textutils.py b/tests/test_textutils.py index 9ebd1a7..1d86d07 100644 --- a/tests/test_textutils.py +++ b/tests/test_textutils.py @@ -1,4 +1,3 @@ -import pytest from fixit.textutils import is_palindrome, reverse_words, slugify @@ -6,7 +5,6 @@ def test_simple_palindrome(): assert is_palindrome("level") -@pytest.mark.xfail(reason="Known bug: case-sensitive") def test_palindrome_ignores_case(): assert is_palindrome("Level") diff --git a/tests/test_wordcount.py b/tests/test_wordcount.py index d6a9607..332065b 100644 --- a/tests/test_wordcount.py +++ b/tests/test_wordcount.py @@ -1,4 +1,3 @@ -import pytest from fixit.wordcount import count_chars, count_lines, count_words @@ -10,15 +9,21 @@ def test_count_words_extra_spaces(): assert count_words("hello world") == 2 -@pytest.mark.xfail(reason="Known bug: empty string returns 1") def test_count_words_empty(): assert count_words("") == 0 -@pytest.mark.xfail(reason="Known bug: trailing newline adds a phantom line") def test_count_lines_trailing_newline(): assert count_lines("a\nb\n") == 2 +def test_count_lines_empty(): + assert count_lines("") == 0 + + +def test_count_words_whitespace_only(): + assert count_words(" \t\n ") == 0 + + def test_count_chars(): assert count_chars("abc") == 3