Microwave: tests and solutions


makePythonTdd Microwave

the code in makePythonTdd.sh from Microwave

 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 Microwave

 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

Microwave: tests

the code in microwave/tests/test_microwave.py from Microwave

  1import src.microwave
  2import unittest
  3
  4
  5OFF = 'OFF'
  6
  7
  8class TestMicrowave(unittest.TestCase):
  9
 10    def test_too_hot_open_door_timer_set(self):
 11        self.assertEqual(
 12            src.microwave.microwave(
 13                door_is_open=True,
 14                timer_is_set=True,
 15                start_is_pushed=True,
 16                too_hot=True,
 17            ),
 18            OFF
 19        )
 20
 21        self.assertEqual(
 22            src.microwave.microwave(
 23                door_is_open=True,
 24                timer_is_set=True,
 25                start_is_pushed=True,
 26                too_hot=False,
 27            ),
 28            OFF
 29        )
 30
 31        self.assertEqual(
 32            src.microwave.microwave(
 33                door_is_open=True,
 34                timer_is_set=True,
 35                start_is_pushed=False,
 36                too_hot=True,
 37            ),
 38            OFF
 39        )
 40
 41        self.assertEqual(
 42            src.microwave.microwave(
 43                door_is_open=True,
 44                timer_is_set=True,
 45                start_is_pushed=False,
 46                too_hot=False,
 47            ),
 48            OFF
 49        )
 50
 51    def test_too_hot_open_door_timer_not_set(self):
 52        self.assertEqual(
 53            src.microwave.microwave(
 54                door_is_open=True,
 55                timer_is_set=False,
 56                start_is_pushed=True,
 57                too_hot=True,
 58            ),
 59            OFF
 60        )
 61
 62        self.assertEqual(
 63            src.microwave.microwave(
 64                door_is_open=True,
 65                timer_is_set=False,
 66                start_is_pushed=True,
 67                too_hot=False,
 68            ),
 69            OFF
 70        )
 71
 72        self.assertEqual(
 73            src.microwave.microwave(
 74                door_is_open=True,
 75                timer_is_set=False,
 76                start_is_pushed=False,
 77                too_hot=True,
 78            ),
 79            OFF
 80        )
 81
 82        self.assertEqual(
 83            src.microwave.microwave(
 84                door_is_open=True,
 85                timer_is_set=False,
 86                start_is_pushed=False,
 87                too_hot=False,
 88            ),
 89            OFF
 90        )
 91
 92    def test_too_hot_closed_door_timer_set(self):
 93        self.assertEqual(
 94            src.microwave.microwave(
 95                door_is_open=False,
 96                timer_is_set=True,
 97                start_is_pushed=True,
 98                too_hot=True,
 99            ),
100            OFF
101        )
102
103        self.assertEqual(
104            src.microwave.microwave(
105                door_is_open=False,
106                timer_is_set=True,
107                start_is_pushed=True,
108                too_hot=False,
109            ),
110            'HEATING'
111        )
112
113        self.assertEqual(
114            src.microwave.microwave(
115                door_is_open=False,
116                timer_is_set=True,
117                start_is_pushed=False,
118                too_hot=True,
119            ),
120            OFF
121        )
122
123        self.assertEqual(
124            src.microwave.microwave(
125                door_is_open=False,
126                timer_is_set=True,
127                start_is_pushed=False,
128                too_hot=False,
129            ),
130            OFF
131        )
132
133    def test_too_hot_closed_door_timer_not_set(self):
134        self.assertEqual(
135            src.microwave.microwave(
136                door_is_open=False,
137                timer_is_set=False,
138                start_is_pushed=True,
139                too_hot=True,
140            ),
141            OFF
142        )
143
144        self.assertEqual(
145            src.microwave.microwave(
146                door_is_open=False,
147                timer_is_set=False,
148                start_is_pushed=True,
149                too_hot=False,
150            ),
151            OFF
152        )
153
154        self.assertEqual(
155            src.microwave.microwave(
156                door_is_open=False,
157                timer_is_set=False,
158                start_is_pushed=False,
159                too_hot=True,
160            ),
161            OFF
162        )
163
164        self.assertEqual(
165            src.microwave.microwave(
166                door_is_open=False,
167                timer_is_set=False,
168                start_is_pushed=False,
169                too_hot=False,
170            ),
171            OFF
172        )
173
174
175# Exceptions seen
176# AssertionError
177# NameError
178# AttributeError
179# TypeError
180# SyntaxError

Microwave: solution

the code in microwave/src/microwave.py from Microwave

 1def microwave(
 2        door_is_open, start_is_pushed,
 3        timer_is_set=False, too_hot=False,
 4    ):
 5    off = 'OFF'
 6
 7    if too_hot:
 8        return off
 9    if not timer_is_set:
10        return off
11    if not start_is_pushed:
12        return off
13    if door_is_open:
14        return off
15
16    return 'HEATING'
17
18    if (
19        not door_is_open
20        and start_is_pushed
21        and timer_is_set
22        and not too_hot
23    ):
24        return 'HEATING'
25
26    return 'OFF'
27
28    if too_hot or not timer_is_set:
29        return 'OFF'
30
31    if door_is_open or not start_is_pushed:
32        return 'OFF'
33    else:
34        return 'HEATING'