Fixat explicit import av 'mock' så att den faktiskt hittas
This commit is contained in:
+40
-36
@@ -1,42 +1,46 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
from hanoi import hanoi # Assuming the code is in a file named hanoi.py
|
||||
import io
|
||||
import sys
|
||||
|
||||
class TestHanoi(unittest.TestCase):
|
||||
|
||||
def test_hanoi_with_one_disk(self):
|
||||
with self.subTest(n=1, source='A', auxiliary='B', target='C'):
|
||||
expected_output = "Move disk 1 from A to C\n"
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(1, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_two_disks(self):
|
||||
with self.subTest(n=2, source='A', auxiliary='B', target='C'):
|
||||
expected_output = "Move disk 1 from A to B\nMove disk 2 from A to C\nMove disk 1 from B to C\n"
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(2, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_three_disks(self):
|
||||
with self.subTest(n=3, source='A', auxiliary='B', target='C'):
|
||||
expected_output = "Move disk 1 from A to C\nMove disk 2 from A to B\nMove disk 1 from C to B\nMove disk 3 from A to C\nMove disk 1 from B to A\nMove disk 2 from B to C\nMove disk 1 from A to C\n"
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(3, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_zero_disks(self):
|
||||
with self.subTest(n=0, source='A', auxiliary='B', target='C'):
|
||||
expected_output = ""
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(0, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_negative_disks(self):
|
||||
with self.subTest(n=-1, source='A', auxiliary='B', target='C'):
|
||||
expected_output = ""
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(-1, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_one_disk(self):
|
||||
with self.subTest(n=1, source='A', auxiliary='B', target='C'):
|
||||
expected_output = "Move disk 1 from A to C\n"
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(1, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_two_disks(self):
|
||||
with self.subTest(n=2, source='A', auxiliary='B', target='C'):
|
||||
expected_output = "Move disk 1 from A to B\nMove disk 2 from A to C\nMove disk 1 from B to C\n"
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(2, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_three_disks(self):
|
||||
with self.subTest(n=3, source='A', auxiliary='B', target='C'):
|
||||
expected_output = "Move disk 1 from A to C\nMove disk 2 from A to B\nMove disk 1 from C to B\nMove disk 3 from A to C\nMove disk 1 from B to A\nMove disk 2 from B to C\nMove disk 1 from A to C\n"
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(3, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_zero_disks(self):
|
||||
with self.subTest(n=0, source='A', auxiliary='B', target='C'):
|
||||
expected_output = "No disks, nothing to do\n"
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(0, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_hanoi_with_negative_disks(self):
|
||||
with self.subTest(n=-1, source='A', auxiliary='B', target='C'):
|
||||
expected_output = "No disks, nothing to do\n"
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(-1, 'A', 'B', 'C')
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user