2.2 to 2.3 An empty text, and case
Read the tutorial. Build it yourself is the other way to read this part.
just setupThe code before this part. The code after it.
2.2 A test of an empty text
An empty file is a text with no words. count must give an empty dictionary for it, and not fail. A test in a
file of its own, tests/test_empty.py, says so, and keeps it true after later changes.
A patch to tests/test_empty.py.
The new file tests/test_empty.py holds one test, test_empty. It gives count the empty text "", and
expects the empty dictionary {}.
uv run python -m unittest discover -s tests -qThe new test passes at once, because split() finds no words in an empty text. test_case still fails.
What changed
tests/test_empty.py: an empty text counts nothing. New, 8 lines. The file.- look at
self.assertEqual(count(""), {})
- look at
The diff of tests/test_empty.py
@@ -0,0 +1,8 @@
+import unittest
+
+from tally import count
+
+
+class Empty(unittest.TestCase):
+ def test_empty(self):
+ self.assertEqual(count(""), {})2.3 Case does not matter
test_case fails, because count counts A and a as two words. When count puts the text in lower case before
it splits it, every word is counted in lower case. One word in the loop of count, lower(), does it.
A patch to src/tally/__init__.py.
In count, in src/tally/__init__.py, the loop now splits text.lower(), and not text. So A and a are
the same word.
uv run python -m unittest discover -s tests -q
echo "The the" | uv run tallyAll three tests pass, and the command prints 2 the for "The the".
What changed
src/tally/__init__.py: one word,lower(), answers the failing test. 1 line added, 1 removed. The file.- look at
for word in text.lower().split():
- look at
The diff of src/tally/__init__.py
@@ -5,7 +5,7 @@ import sys
def count(text):
counts = {}
- for word in text.split():
+ for word in text.lower().split():
counts[word] = counts.get(word, 0) + 1
return counts