2 Two tests
just setupA test gives count a text, and compares what it gives back with what we expect. tests/test_count.py holds two
tests, and tests/test_empty.py one. The test of case failed until count ignored case, with one word, lower().
uv run python -m unittest discover -s tests -v
echo "The the" | uv run tallyAll three tests pass, and the command now prints 2 the for "The the".
What changed
src/tally/__init__.py: the word that answers the test of case. 1 line added, 1 removed. The file.- look at
for word in text.lower().split():
- look at
tests/test_count.py: a test of counting, and a test of case. New, 11 lines. The file.- look at
self.assertEqual(count("A a"), {"a": 2})
- look at
tests/test_empty.py: an empty text counts nothing. New, 8 lines. The file.
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(""), {})