This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_connect4.py
98 lines (71 loc) · 3.77 KB
/
test_connect4.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
import connect4, numpy, unittest
from unittest.mock import patch
from io import StringIO
def metadata_of(array):
return [ type(array), array.ndim, array.shape, array.size, array.dtype ]
class TestConnect(unittest.TestCase):
def test_setup_game_board(self):
testArray = connect4.create_board()
expectedArray = numpy.zeros( (6,7) )
actualArrayMetadata = metadata_of(testArray)
expectedArrayMetadata = metadata_of(expectedArray)
self.assertEqual(actualArrayMetadata, expectedArrayMetadata)
def test_prompt_player_for_move(self):
with patch('sys.stdout', new=StringIO()) as fakeOutput:
currentPlayer = 1
expectedOutput = f'Player {currentPlayer}, make your selection (1-7):'
connect4.prompt_player(currentPlayer)
actualOutput = fakeOutput.getvalue().strip()
self.assertEqual(actualOutput, expectedOutput)
def test_initialization_of_gameVariables(self):
gameVariables = connect4.gameVariables()
expectedArray = numpy.zeros( (6,7) )
actualArrayMetadata = metadata_of(gameVariables.board)
expectedArrayMetadata = metadata_of(expectedArray)
self.assertEqual(actualArrayMetadata , expectedArrayMetadata) # Footnote 1
self.assertEqual(gameVariables.gameOver , False)
self.assertEqual(gameVariables.turnNumber , 1)
self.assertEqual(gameVariables.currentPlayer, 1)
# FOOTNOTES:
# 1. This duplicates test_setup_game_board.
# Is it possible to reuse any code?
def test_game_loop_exits_program_if_game_over(self):
gameVariables = connect4.gameVariables()
gameVariables.gameOver = True
with self.assertRaises(SystemExit):
connect4.game_loop_step(gameVariables)
def test_game_loop_prompts_player_for_move(self):
gameVariables = connect4.gameVariables()
expectedOutput = 'Player 1, make your selection (1-7):'
with patch('sys.stdout', new=StringIO()) as fakeOutput:
connect4.game_loop_step(gameVariables)
actualOutput = fakeOutput.getvalue().strip()
self.assertEqual(actualOutput, expectedOutput)
# ENDNOTES:
# 1. Like test_initialization_of_gameVariables duplicates test_setup_game_board,
# this duplicates test_prompt_player_for_move.
# Is there a place for code reuse here?
def test_game_loop_iterates_gameVariables(self):
gameVariables = connect4.gameVariables()
with patch('sys.stdout', new=StringIO()) as fakeOutput: # Footnote 1
# == FIRST ITERATION == #
expectedTurnNumber = 2
expectedCurrentPlayer = 2
connect4.game_loop_step(gameVariables) # Footnote 2
actualTurnNumber = gameVariables.turnNumber
actualCurrentPlayer = gameVariables.currentPlayer
self.assertEqual(actualTurnNumber , expectedTurnNumber)
self.assertEqual(actualCurrentPlayer, expectedCurrentPlayer)
# == SECOND ITERATION == #
expectedTurnNumber = 3
expectedCurrentPlayer = 1
connect4.game_loop_step(gameVariables)
actualTurnNumber = gameVariables.turnNumber
actualCurrentPlayer = gameVariables.currentPlayer
self.assertEqual(actualTurnNumber , expectedTurnNumber)
self.assertEqual(actualCurrentPlayer, expectedCurrentPlayer)
# FOOTNOTES:
# 1. Without this, running the game loop will print to the console.
# 2. There is duplication here; an opportunity for refactoring?
if __name__ == '__main__':
unittest.main()