Skip to content

Commit 907033a

Browse files
authored
Merge pull request #45 from michalmaj/test/add-whitespace-cases
test: add whitespace edge cases
2 parents 781aafa + 654e09a commit 907033a

3 files changed

Lines changed: 66 additions & 29 deletions

File tree

‎docs/04_pytest.md‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ Inside those files, test functions usually start with `test_`.
7979
Example:
8080

8181
```python
82-
def test_count_words_returns_zero_for_empty_text() -> None:
83-
result = count_words("")
82+
def test_count_characters_includes_whitespace_by_default() -> None:
83+
result = count_characters("hello world")
8484

85-
assert result == 0
85+
assert result == 11
8686
```
8787

8888
The function name describes the expected behavior.
@@ -108,15 +108,48 @@ This means:
108108
Example:
109109

110110
```python
111-
def test_normalize_whitespace_replaces_repeated_spaces() -> None:
112-
text = "Python is fun"
111+
def test_count_characters_can_ignore_whitespace() -> None:
112+
result = count_characters("hello world", include_whitespace=False)
113113

114+
assert result == 10
115+
```
116+
117+
This style makes tests easier to read and review.
118+
119+
## Parametrizing related cases
120+
121+
Sometimes several inputs should follow the same behavior. Copying the entire
122+
test for each input would make the test file repetitive.
123+
124+
pytest can run one test function with several sets of values:
125+
126+
```python
127+
import pytest
128+
129+
130+
@pytest.mark.parametrize(
131+
("text", "expected"),
132+
[
133+
("Python is fun", "Python is fun"),
134+
("Python\tis\nfun", "Python is fun"),
135+
(" \t\n ", ""),
136+
(" Python project ", "Python project"),
137+
],
138+
)
139+
def test_normalize_whitespace_handles_common_inputs(text: str, expected: str) -> None:
114140
result = normalize_whitespace(text)
115141

116-
assert result == "Python is fun"
142+
assert result == expected
117143
```
118144

119-
This style makes tests easier to read and review.
145+
The names `text` and `expected` match the two values in every tuple. pytest
146+
runs the function once for each tuple and reports every case separately.
147+
148+
This example covers repeated spaces, tabs and newlines, whitespace-only text,
149+
and leading or trailing spaces without duplicating the test logic.
150+
151+
Parametrization is useful when the action and assertion stay the same. Use
152+
separate tests when cases describe different behavior or need different setup.
120153

121154
## pytest configuration
122155

‎docs/ROADMAP.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ These parts are already included in the repository.
8383
- [x] Explain linting and formatting
8484
- [x] Explain when ignoring linting rules may be reasonable
8585
- [x] Check internal Markdown links with pytest
86+
- [x] Add whitespace edge cases to the example test suite
87+
- [x] Demonstrate pytest parametrization with related inputs
8688

8789
### Continuous integration
8890

‎tests/test_text_stats.py‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
"""Tests for basic text statistics utilities."""
22

3+
import pytest
4+
35
from text_toolkit import (
46
count_characters,
57
count_words,
68
normalize_whitespace,
79
)
810

911

10-
def test_normalize_whitespace_replaces_repeated_spaces() -> None:
11-
text = "Python is fun"
12-
13-
result = normalize_whitespace(text)
14-
15-
assert result == "Python is fun"
16-
17-
18-
def test_normalize_whitespace_handles_tabs_and_newlines() -> None:
19-
text = "Python\tis\nfun"
20-
12+
@pytest.mark.parametrize(
13+
("text", "expected"),
14+
[
15+
("Python is fun", "Python is fun"),
16+
("Python\tis\nfun", "Python is fun"),
17+
(" \t\n ", ""),
18+
(" Python project ", "Python project"),
19+
],
20+
)
21+
def test_normalize_whitespace_handles_common_inputs(text: str, expected: str) -> None:
2122
result = normalize_whitespace(text)
2223

23-
assert result == "Python is fun"
24-
25-
26-
def test_count_words_returns_zero_for_empty_text() -> None:
27-
result = count_words("")
24+
assert result == expected
2825

29-
assert result == 0
30-
31-
32-
def test_count_words_handles_repeated_whitespace() -> None:
33-
text = "Python project\nworkflow"
3426

27+
@pytest.mark.parametrize(
28+
("text", "expected"),
29+
[
30+
("", 0),
31+
(" \t\n ", 0),
32+
(" Python project ", 2),
33+
("Python project\nworkflow", 3),
34+
],
35+
)
36+
def test_count_words_handles_common_whitespace(text: str, expected: int) -> None:
3537
result = count_words(text)
3638

37-
assert result == 3
39+
assert result == expected
3840

3941

4042
def test_count_characters_includes_whitespace_by_default() -> None:

0 commit comments

Comments
 (0)