forked from Chennaipy/hangman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
98 lines (69 loc) · 2.91 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Unit test cases for hangman game."""
import unittest
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import hangman
class HangmanTestCase(unittest.TestCase):
"""Test case for hangman game."""
def setUp(self):
randint_patcher = patch("hangman.random.randint")
self.randint = randint_patcher.start()
self.addCleanup(randint_patcher.stop)
print_patcher = patch("builtins.print")
self.print = print_patcher.start()
self.addCleanup(print_patcher.stop)
input_patcher = patch("builtins.input")
self.input = input_patcher.start()
self.addCleanup(input_patcher.stop)
def test_win(self):
"""Test user win scenario."""
self.randint.return_value = 0
self.input.side_effect = list("ant" "n")
hangman.main()
self.print.assert_any_call('Yes! The secret word is "ant"! '
'You have won!')
def test_lose(self):
"""Test user lose scenario."""
self.randint.return_value = 0
self.input.side_effect = list("bcdefg" "n")
hangman.main()
self.print.assert_any_call('You have run out of guesses!')
def test_two_game(self):
"""Test two winning game plays."""
self.randint.side_effect = [0, 1]
self.input.side_effect = list("ant" "y" "babon" "n")
hangman.main()
self.print.assert_any_call('Yes! The secret word is "ant"! '
'You have won!')
self.print.assert_any_call('Yes! The secret word is "baboon"! '
'You have won!')
def test_out_of_order(self):
"""Test win scenario with out of order input of letters."""
self.randint.return_value = 0
self.input.side_effect = list("tan" "n")
hangman.main()
self.print.assert_any_call('Yes! The secret word is "ant"! '
'You have won!')
def test_numeric_input(self):
"""Test error message when user inputs numbers."""
self.randint.return_value = 0
self.input.side_effect = list("a2nt" "n")
hangman.main()
self.print.assert_any_call('Please enter a LETTER.')
def test_multiple_char_input(self):
"""Test error message when user inputs multiple characters."""
self.randint.return_value = 0
self.input.side_effect = ["a", "nt", "n", "t", ] + ["n"]
hangman.main()
self.print.assert_any_call('Please enter a single letter.')
def test_same_letter_twice(self):
"""Test error message when user enters same letter twice."""
self.randint.return_value = 0
self.input.side_effect = list("anntn")
hangman.main()
self.print.assert_any_call("You have already guessed that letter. "
"Choose again.")
if __name__ == "__main__":
unittest.main()