From 5e25aca23b28ac4ce3ef8115352dc47e8a0950fa Mon Sep 17 00:00:00 2001 From: Joakim Persson Date: Fri, 12 Jul 2024 15:28:01 +0200 Subject: [PATCH] =?UTF-8?q?Fixat=20explicit=20import=20av=20'mock'=20s?= =?UTF-8?q?=C3=A5=20att=20den=20faktiskt=20hittas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hanoi/hanoi.py | 14 +++++++- hanoi/unittest_hanoi.py | 76 ++++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/hanoi/hanoi.py b/hanoi/hanoi.py index e9140bb..9d922c3 100644 --- a/hanoi/hanoi.py +++ b/hanoi/hanoi.py @@ -1,4 +1,9 @@ +import io + def hanoi(n, source, auxiliary, target): + if n < 1: + print(f"No disks, nothing to do") + return if n == 1: print(f"Move disk 1 from {source} to {target}") return @@ -13,5 +18,12 @@ def hanoi(n, source, auxiliary, target): # hanoi(3, 'A', 'B', 'C') if __name__== "__main__": - hanoi(3,'A','B','C') + user_input = input("Please enter an integer: ") + try: + user_integer = int(user_input) + print(f"You entered the integer: {user_integer}") + except ValueError: + print("That's not a valid integer. Please try again.") + + hanoi(user_integer,'A','B','C') diff --git a/hanoi/unittest_hanoi.py b/hanoi/unittest_hanoi.py index 7ac5685..03418e2 100644 --- a/hanoi/unittest_hanoi.py +++ b/hanoi/unittest_hanoi.py @@ -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() +