2 Tests
just setupWe cannot tell whether count still works after a change, unless we try it by hand each time. Tests try it for
us. Two files of tests hold three tests, and one of them fails until counting ignores case. The fix is one word in
count, lower(). Read tests/test_count.py first, then the fix.
uv run python -m unittest discover -s tests -q
echo "The the THE" | uv run tallyThe tests pass, and the second command now prints 3 the.
A code block in the notes is code to read, not a command to run:
$ this line is not a buttonWhat changed
src/tally/__init__.py: the fix, one word. 1 line added, 1 removed. The file.- look at
for word in text.lower().split():
- look at
tests/test_count.py: two tests, andtest_caseis the one that failed. New, 11 lines. The file.tests/test_empty.py: an empty text counts nothing. New, 8 lines. The file.- look at
self.assertEqual(count(""), {})
- 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
The diff of tests/test_count.py
@@ -0,0 +1,11 @@
+import unittest
+
+from tally import count
+
+
+class Count(unittest.TestCase):
+ def test_words(self):
+ self.assertEqual(count("a b a"), {"a": 2, "b": 1})
+
+ def test_case(self):
+ self.assertEqual(count("A a"), {"a": 2})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(""), {})