-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_game.py
More file actions
74 lines (56 loc) · 2.03 KB
/
test_game.py
File metadata and controls
74 lines (56 loc) · 2.03 KB
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
import unittest
from unittest.mock import patch
from game import Game
class TestGame(unittest.TestCase):
def setUp(self):
self.game = Game()
class OutputCollector(object):
def __init__(self, *args, **kwargs):
self.output_collector = []
def __call__(self, output):
self.output_collector.append(output)
self.output_collector = OutputCollector()
def tearDown(self):
pass
@patch('game.Game.get_input', return_value='9')
def test_quit_game(self, mock_input):
with patch('game.Game.output', side_effect=self.output_collector):
self.game.play()
self.assertEqual(
self.output_collector.output_collector,
[],
)
def test_game_selection(self):
self.assertEqual(
self.game.game_inputs(),
'Select Game\n'
'0: Guess Number Game\n'
'1: Generala\n'
'2: Blackjack\n'
'3: Truco Game\n'
'9: to quit\n'
)
def test_play_guess_number_game(self):
class ControlInputValues(object):
def __init__(self, *args, **kwargs):
self.played = False
self.play_count = 0
def __call__(self, console_output):
if 'Select Game' in console_output:
if self.played:
return '9'
self.played = True
return '0'
if 'Give me a number from 0 to 100' in console_output:
return '50'
with \
patch('game.Game.get_input', side_effect=ControlInputValues()), \
patch('game.Game.output', side_effect=self.output_collector), \
patch('guess_number_game.guess_number_game.randint', return_value=50):
self.game.play()
self.assertEqual(
self.output_collector.output_collector,
['[]', 'you win'],
)
if __name__ == "__main__":
unittest.main()