Traffic Light: tests and solutions


makePythonTdd TrafficLight

the code in makePythonTdd.sh from Traffic Light

 1#!/bin/bash
 2uv init atm
 3cd atm
 4mkdir src
 5mv main.py src/atm.py
 6mkdir tests
 7touch tests/__init__.py
 8
 9echo "import unittest
10
11
12class TestATM(unittest.TestCase):
13
14    def test_failure(self):
15        self.assertFalse(True)
16
17
18# Exceptions seen
19# AssertionError
20" > tests/test_atm.py
21
22echo "pytest" > requirements.txt
23echo "pytest-watcher" >> requirements.txt
24uv add --requirement requirements.txt
25uv run pytest-watcher . --now

the code in makePythonTdd.ps1 from Traffic Light

 1uv init atm
 2cd atm
 3mkdir src
 4Move-Item "main.py" "src/atm.py"
 5mkdir tests
 6New-Item tests/__init__.py
 7
 8"import unittest
 9
10
11class TestATM(unittest.TestCase):
12
13    def test_failure(self):
14        self.assertFalse(True)
15
16
17# Exceptions seen
18# AssertionError
19" | Out-File "tests/test_atm.py" -Encoding UTF8
20
21"pytest" | Out-File requirements.txt -Encoding UTF8
22"pytest-watcher" >> requirements.txt
23uv add --requirement requirements.txt
24uv run pytest-watcher . --now

Traffic Light: tests

the code in traffic_light/tests/test_traffic_light.py from Traffic Light

  1import src.traffic_light
  2import unittest
  3
  4
  5RED, YELLOW, GREEN = 'RED', 'YELLOW', 'GREEN'
  6NO_WALK = 'NO WALK'
  7WALK = (RED, 'WALK')
  8YELLOW_NO_WALK = (YELLOW, NO_WALK)
  9GREEN_NO_WALK = (GREEN, NO_WALK)
 10
 11
 12class TestTrafficLight(unittest.TestCase):
 13
 14    def test_red_traffic_light_w_walk(self):
 15        self.assertEqual(
 16            src.traffic_light.show(
 17                current_light=RED,
 18                timer_done=True,
 19                walk_button=True,
 20            ),
 21            WALK
 22        )
 23
 24        self.assertEqual(
 25            src.traffic_light.show(
 26                current_light=RED,
 27                timer_done=True,
 28                walk_button=False,
 29            ),
 30            GREEN_NO_WALK
 31        )
 32
 33        self.assertEqual(
 34            src.traffic_light.show(
 35                current_light=RED,
 36                timer_done=False,
 37                walk_button=True,
 38            ),
 39            WALK
 40        )
 41
 42        self.assertEqual(
 43            src.traffic_light.show(
 44                current_light=RED,
 45                timer_done=False,
 46                walk_button=False,
 47            ),
 48            WALK
 49        )
 50
 51    def test_yellow_traffic_light_w_walk(self):
 52        self.assertEqual(
 53            src.traffic_light.show(
 54                current_light=YELLOW,
 55                timer_done=True,
 56                walk_button=True,
 57            ),
 58            WALK
 59        )
 60
 61        self.assertEqual(
 62            src.traffic_light.show(
 63                current_light=YELLOW,
 64                timer_done=True,
 65                walk_button=False,
 66            ),
 67            WALK
 68        )
 69
 70        self.assertEqual(
 71            src.traffic_light.show(
 72                current_light=YELLOW,
 73                timer_done=False,
 74                walk_button=True,
 75            ),
 76            YELLOW_NO_WALK
 77        )
 78
 79        self.assertEqual(
 80            src.traffic_light.show(
 81                current_light=YELLOW,
 82                timer_done=False,
 83                walk_button=False,
 84            ),
 85            YELLOW_NO_WALK
 86        )
 87
 88    def test_green_traffic_light_w_walk(self):
 89        self.assertEqual(
 90            src.traffic_light.show(
 91                current_light=GREEN,
 92                timer_done=True,
 93                walk_button=True,
 94            ),
 95            YELLOW_NO_WALK
 96        )
 97
 98        self.assertEqual(
 99            src.traffic_light.show(
100                current_light=GREEN,
101                timer_done=True,
102                walk_button=False,
103            ),
104            YELLOW_NO_WALK
105        )
106
107        self.assertEqual(
108            src.traffic_light.show(
109                current_light=GREEN,
110                timer_done=False,
111                walk_button=True,
112            ),
113            GREEN_NO_WALK
114        )
115
116        self.assertEqual(
117            src.traffic_light.show(
118                current_light=GREEN,
119                timer_done=False,
120                walk_button=False,
121            ),
122            GREEN_NO_WALK
123        )
124
125
126# Exceptions seen
127# AssertionError
128# NameError
129# AttributeError
130# TypeError
131# SyntaxError

Traffic Light: solution

the code in traffic_light/src/traffic_light.py from Traffic Light

 1def show(
 2        current_light='RED', timer_done=False,
 3        walk_button=False,
 4    ):
 5    red, yellow, green = 'RED', 'YELLOW', 'GREEN'
 6    walk = (red, 'WALK')
 7    no_walk = 'NO WALK'
 8
 9    if not timer_done and current_light != red:
10        return current_light, no_walk
11
12    if timer_done:
13        if current_light == green:
14            return yellow, no_walk
15        if current_light == red and not walk_button:
16            return green, no_walk
17
18    return walk