2 Tidy the project

Read the tutorial. Build it yourself is the other way to read this part.

just setup

The release is out. Now we tidy the project, so that it is easier to find your way in it. The top folder keeps the files that a user reads first. The tests, in two files, go into one file, named after the project.

Each file that moves or goes here moves by a command, mv, or goes by a command, rm.

The code before this part. The code after it.

2.1 A hidden file for the recipes

A user of tally reads README.md first, and has no use for the recipes of the justfile. They are for the people who work on tally. just also finds its recipes in a file named .justfile, a hidden file that ls does not list. mv gives the justfile that name.

This change came from running mv justfile .justfile.

The file justfile is now .justfile, with the same recipes.

just --list

It prints the same four recipes as before, default, run, setup and test.

What changed

  • .justfile: the recipes, in a hidden file. Renamed from justfile. It comes from mv justfile .justfile. The file.

This change on GitHub. The code after it.

2.2 A test of lines

A text often has more than one line. No test yet gives count a text with a newline in it. A new test, test_lines, counts a b, a newline, and a.

A patch to tests/test_count.py.

At the end of tests/test_count.py, the new test test_lines expects two of a and one of b, across two lines.

uv run python -m unittest discover -s tests -q

All five tests pass.

What changed

  • tests/test_count.py: a test of a text of two lines. 3 lines added. The file.
    • look at def test_lines(self):
The diff of tests/test_count.py
@@ -9,3 +9,6 @@ class Count(unittest.TestCase):
 
     def test_case(self):
         self.assertEqual(count("A a"), {"a": 2})
+
+    def test_lines(self):
+        self.assertEqual(count("a b\na"), {"a": 2, "b": 1})

This change on GitHub. The code after it.

2.3 One name for the tests

The tests are in two files, tests/test_count.py and tests/test_edge.py. One file is one place to look. It gets the name of the project, tests/test_tally.py. mv gives the file of edge cases that name.

This change came from running mv tests/test_edge.py tests/test_tally.py.

The file tests/test_edge.py is now tests/test_tally.py, with the same two tests.

uv run python -m unittest discover -s tests -q

All five tests pass, as before. unittest finds each file whose name starts with test.

What changed

  • tests/test_tally.py: the tests of edge cases, under the new name. Renamed from tests/test_edge.py. It comes from mv tests/test_edge.py tests/test_tally.py. The file.

This change on GitHub. The code after it.

2.4 Every test in one file

The class Count, with its three tests, goes into tests/test_tally.py too.

A patch to tests/test_tally.py.

At the end of tests/test_tally.py, the class Count has the three tests of tests/test_count.py.

uv run python -m unittest discover -s tests -q

All eight tests pass. The three tests of Count run twice, once from each file. The next change removes the old file.

What changed

  • tests/test_tally.py: every test of tally, in one file. 11 lines added. The file.
    • look at class Count(unittest.TestCase):
The diff of tests/test_tally.py
@@ -9,3 +9,14 @@ class Edge(unittest.TestCase):
 
     def test_spaces(self):
         self.assertEqual(count("  \n\t "), {})
+
+
+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})
+
+    def test_lines(self):
+        self.assertEqual(count("a b\na"), {"a": 2, "b": 1})

This change on GitHub. The code after it.

2.5 The old file of tests goes

Every test of tests/test_count.py is now in tests/test_tally.py too. rm removes the old file.

This change came from running rm tests/test_count.py.

The file tests/test_count.py is gone, and its tests stay in tests/test_tally.py.

uv run python -m unittest discover -s tests -q

Each test now runs one time only, and all five tests pass.

What changed

  • tests/test_count.py: its tests are in tests/test_tally.py. Deleted. It comes from rm tests/test_count.py.
The diff of tests/test_count.py
@@ -1,14 +0,0 @@
-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})
-
-    def test_lines(self):
-        self.assertEqual(count("a b\na"), {"a": 2, "b": 1})

This change on GitHub. The code after it.