Fixat explicit import av 'mock' så att den faktiskt hittas
This commit is contained in:
+13
-1
@@ -1,4 +1,9 @@
|
|||||||
|
import io
|
||||||
|
|
||||||
def hanoi(n, source, auxiliary, target):
|
def hanoi(n, source, auxiliary, target):
|
||||||
|
if n < 1:
|
||||||
|
print(f"No disks, nothing to do")
|
||||||
|
return
|
||||||
if n == 1:
|
if n == 1:
|
||||||
print(f"Move disk 1 from {source} to {target}")
|
print(f"Move disk 1 from {source} to {target}")
|
||||||
return
|
return
|
||||||
@@ -13,5 +18,12 @@ def hanoi(n, source, auxiliary, target):
|
|||||||
# hanoi(3, 'A', 'B', 'C')
|
# hanoi(3, 'A', 'B', 'C')
|
||||||
|
|
||||||
if __name__== "__main__":
|
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')
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
from hanoi import hanoi # Assuming the code is in a file named hanoi.py
|
from hanoi import hanoi # Assuming the code is in a file named hanoi.py
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
|
||||||
class TestHanoi(unittest.TestCase):
|
class TestHanoi(unittest.TestCase):
|
||||||
|
|
||||||
@@ -26,17 +29,18 @@ class TestHanoi(unittest.TestCase):
|
|||||||
|
|
||||||
def test_hanoi_with_zero_disks(self):
|
def test_hanoi_with_zero_disks(self):
|
||||||
with self.subTest(n=0, source='A', auxiliary='B', target='C'):
|
with self.subTest(n=0, source='A', auxiliary='B', target='C'):
|
||||||
expected_output = ""
|
expected_output = "No disks, nothing to do\n"
|
||||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||||
hanoi(0, 'A', 'B', 'C')
|
hanoi(0, 'A', 'B', 'C')
|
||||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||||
|
|
||||||
def test_hanoi_with_negative_disks(self):
|
def test_hanoi_with_negative_disks(self):
|
||||||
with self.subTest(n=-1, source='A', auxiliary='B', target='C'):
|
with self.subTest(n=-1, source='A', auxiliary='B', target='C'):
|
||||||
expected_output = ""
|
expected_output = "No disks, nothing to do\n"
|
||||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||||
hanoi(-1, 'A', 'B', 'C')
|
hanoi(-1, 'A', 'B', 'C')
|
||||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user