Omarbetade tester och tydligare funktions- och variabelnamn
This commit is contained in:
+61
-43
@@ -1,47 +1,65 @@
|
||||
import unittest
|
||||
import io
|
||||
from unittest.mock import patch
|
||||
from hanoi import hanoi, get_hanoi_input
|
||||
import sys
|
||||
from hanoi import tower_of_hanoi, prompt_for_integer
|
||||
|
||||
class TestHanoi(unittest.TestCase):
|
||||
|
||||
def test_hanoi_zero_disks(self):
|
||||
expected_output = "No disks, nothing to do\n"
|
||||
# with self.assertRaises(SystemExit):
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(0, 'A', 'B', 'C')
|
||||
|
||||
def test_hanoi_one_disk(self):
|
||||
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_two_disks(self):
|
||||
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_three_disks(self):
|
||||
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_negative_input(self):
|
||||
expected_output = "No disks, nothing to do\n"
|
||||
# with self.assertRaises(SystemExit):
|
||||
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as mock_stdout:
|
||||
hanoi(-1, 'A', 'B', 'C')
|
||||
|
||||
def test_get_hanoi_input_valid_integer(self):
|
||||
with unittest.mock.patch('builtins.input', return_value='4'):
|
||||
self.assertEqual(get_hanoi_input(), 4)
|
||||
|
||||
def test_get_hanoi_input_invalid_input(self):
|
||||
with unittest.mock.patch('builtins.input', side_effect=['abc', '5']):
|
||||
self.assertEqual(get_hanoi_input(), 5)
|
||||
class TestTowerOfHanoi(unittest.TestCase):
|
||||
"""
|
||||
Explanation of Tests:
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
test_valid_input():
|
||||
This test checks the core functionality by moving 3 disks using tower_of_hanoi.
|
||||
It asserts that the function completes without raising errors.
|
||||
|
||||
test_edge_case_single_disk():
|
||||
This handles the base case of a single disk, ensuring the correct move
|
||||
is printed ("Move disk 1 from A to C").
|
||||
|
||||
test_invalid_input_zero_disks():
|
||||
This checks that the function raises a ValueError when given zero disks.
|
||||
|
||||
test_prompt_for_integer_valid():
|
||||
Tests the input function with valid integer input ("3") and asserts it
|
||||
returns the correct value (3).
|
||||
|
||||
test_prompt_for_integer_invalid_negative():
|
||||
Checks for handling negative integers, expecting a ValueError.
|
||||
|
||||
test_prompt_for_integer_invalid_string():
|
||||
Tests that invalid string input is handled gracefully and eventually
|
||||
leads to a valid integer being returned.
|
||||
"""
|
||||
|
||||
def test_valid_input(self):
|
||||
self.assertEqual(tower_of_hanoi(3, 'A', 'B', 'C'), None) # Example with 3 disks
|
||||
|
||||
def test_edge_case_single_disk(self):
|
||||
expected_output = "Move disk 1 from A to C\n"
|
||||
captured_output = io.StringIO()
|
||||
sys.stdout = captured_output
|
||||
tower_of_hanoi(1, 'A', 'B', 'C')
|
||||
sys.stdout = sys.__stdout__ # Reset stdout
|
||||
self.assertEqual(captured_output.getvalue(), expected_output)
|
||||
|
||||
def test_invalid_input_zero_disks(self):
|
||||
with self.assertRaises(ValueError):
|
||||
tower_of_hanoi(0, 'A', 'B', 'C')
|
||||
|
||||
def test_prompt_for_integer_valid(self):
|
||||
with io.StringIO("3\n") as mock_stdin:
|
||||
sys.stdin = mock_stdin
|
||||
self.assertEqual(prompt_for_integer(), 3)
|
||||
|
||||
def test_prompt_for_integer_invalid_negative(self):
|
||||
with io.StringIO("-2\n1\n") as mock_stdin:
|
||||
sys.stdin = mock_stdin
|
||||
self.assertEqual(prompt_for_integer(), 1) # Should eventually get a valid integer
|
||||
|
||||
|
||||
def test_prompt_for_integer_invalid_string(self):
|
||||
with io.StringIO("abc\n3\n") as mock_stdin:
|
||||
sys.stdin = mock_stdin
|
||||
self.assertEqual(prompt_for_integer(), 3) # Should eventually get a valid integer
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user