commit 0b6fe9d2448b6c3d8888561cf9eeac4e62aba6cd Author: wralto-user-1 Date: Mon Nov 2 22:14:51 2026 +0100 feat: add stringutils module with word_count function and tests Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..c03ab12 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,17 @@ +# Agent Conventions + +## Language +- Python 3.12 + +## Git +- Follow [Conventional Commits](https://www.conventionalcommits.org/): `feat:`, `fix:`, `test:`, `chore:`, `docs:`, `refactor:`, etc. +- Commit messages must be concise and in lowercase, using imperative mood. + +## Testing +- All tests must be written with `pytest`. +- Test files must be in a `tests/` directory, mirroring the module structure. +- Test functions should be named `test_`. + +## Code Style +- Follow PEP 8 guidelines. +- Use type hints where appropriate. diff --git a/stringutils.py b/stringutils.py new file mode 100644 index 0000000..52e6ddc --- /dev/null +++ b/stringutils.py @@ -0,0 +1,14 @@ +def word_count(text: str) -> int: + """Count the number of words in a string. + + Words are defined as non-empty sequences separated by whitespace. + + Args: + text: The input string to count words in. + + Returns: + The number of words in the string. + """ + if not text.strip(): + return 0 + return len(text.split()) diff --git a/tests/test_stringutils.py b/tests/test_stringutils.py new file mode 100644 index 0000000..898cfb3 --- /dev/null +++ b/tests/test_stringutils.py @@ -0,0 +1,34 @@ +import pytest +from stringutils import word_count + + +def test_word_count_empty_string(): + assert word_count("") == 0 + + +def test_word_count_whitespace_only(): + assert word_count(" ") == 0 + + +def test_word_count_single_word(): + assert word_count("hello") == 1 + + +def test_word_count_multiple_words(): + assert word_count("hello world") == 2 + + +def test_word_count_multiple_spaces(): + assert word_count("hello world") == 2 + + +def test_word_count_leading_trailing_spaces(): + assert word_count(" hello world ") == 2 + + +def test_word_count_newlines_and_tabs(): + assert word_count("hello\nworld\t!") == 3 + + +def test_word_count_mixed_whitespace(): + assert word_count(" hello \t\n world ") == 2