-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
184 lines (127 loc) · 5.12 KB
/
test_main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""Tests the Main Class"""
import os
import pytest
from main import App
from board import Board
from player import Player
from ai import Ai
@pytest.fixture
def create_game():
"""Creates Game"""
return App()
@pytest.fixture
def create_board():
"""Crates Board"""
board = Board()
board.clear()
return board
def test_create_board(create_game, mocker):
"""Tests the function create_board"""
app = create_game
mocker.patch('controller.Controller.get_number_player', return_value=1)
app.create_board()
assert app.board is not None and app.number_players == 1
def test_create_ai(create_game, mocker):
"""Tests the function create_ai"""
app = create_game
mocker.patch('controller.Controller.get_level_ai', return_value=3)
app.symbols[0] = "X"
app.default_symbols[1] = "X"
app.create_ai()
assert len(app.player) == 1 and app.artificial_intelligence is not None
del app
app = App()
app.symbols[0] = "X"
app.default_symbols[1] = "O"
app.create_ai()
assert len(app.player) == 1 and app.artificial_intelligence is not None
def test_create_player(create_game, mocker):
"""Tests the function create_player"""
app = create_game
app.number_players = 1
app.default_symbols = ["X", "O"]
app.symbols = ["X", "O"]
mocker.patch('view.View.get_input', return_value="1")
app.create_player()
assert len(app.player) == 2 and app.player[1].is_ai
def test_start_game(create_game, create_board, mocker):
"""Tests the function start_game"""
app = create_game
app.board = create_board
app.first_player = 0
app.player = [Player(0, "X", True, "Player"), Player(0, "X", False, "Player")]
app.artificial_intelligence = Ai(app.board, app.player[1], 3, "X")
mocker.patch('view.View.print_to_ui', return_value="")
mocker.patch('view.View.clear', return_value="")
mocker.patch('board.Board.is_winning', return_value=1)
mocker.patch('board.Board.show_board', return_value="")
mocker.patch('controller.Controller.get_input', return_value="n")
app.start_game()
assert app.finish_game() == "n"
mocker.patch('board.Board.is_winning', return_value=-1)
app.start_game()
mocker.patch('board.Board.is_winning', return_value=False)
mocker.patch('controller.Controller.get_input', side_effect=['1', 'a1', 's'])
mocker.patch('ai.Ai.move', return_value="")
mocker.patch('main.App.save_score', return_value="")
app.error = "Test"
app.start_game()
mocker.patch('board.Board.is_winning', return_value=-1)
mocker.patch('main.App.finish_game', side_effect=['y', 'n'])
app.start_game()
def test_finish_game(create_game, create_board, mocker):
"""Tests the function finish_game"""
app = create_game
board = create_board
app.player = [Player(0, "X", True, "Player1"), Player(1, "O", True, "Player2")]
app.artificial_intelligence = app.player[0]
app.board = board
mocker.patch('board.Board.is_winning', return_value=-1)
mocker.patch('controller.Controller.get_input', return_value='n')
app.finish_game()
assert app.artificial_intelligence.path == 0 and app.artificial_intelligence.sub_path == 0
mocker.patch('board.Board.is_winning', return_value=1)
app.finish_game()
assert app.artificial_intelligence.path == 0 and app.artificial_intelligence.sub_path == 0
def test_select_player(mocker, create_game):
"""Tests the function select_player"""
app = create_game
mocker.patch('controller.Controller.get_first_player', return_value=0)
app.select_player()
assert app.first_player == 0
def test_save_score(mocker, create_game, create_board):
"""Tests the function save_score"""
if os.path.exists("saves.dat"):
os.remove("saves.dat")
app = create_game
board = create_board
app.player = [Player(0, "X", True, "Player1"), Player(1, "O", False, "Player2")]
app.board = board
app.current_player = 0
app.first_player = 0
app.symbols = ["X", "O"]
app.artificial_intelligence = Ai(board, app.player[0], 3, "O")
app.artificial_intelligence.path = 1
app.artificial_intelligence.sub_path = 1
mocker.patch('os.path.exists', return_value=True)
mocker.patch('os.remove', return_value=True)
app.save_score()
assert os.path.exists("saves.dat")
def test_load_score(mocker, create_game, create_board):
"""Tests the function load_score"""
test_save_score(mocker, create_game, create_board)
app = App()
app.load_score()
assert app.board is not None and len(app.player) == 2 and len(app.symbols) == 2 and \
app.current_player == 0 and app.artificial_intelligence is not None
def test_run(mocker, create_game):
"""Tests the function run"""
app = create_game
app.current_player = None
mocker.patch('controller.Controller.show_menu', return_value=True)
mocker.patch('main.App.load_score', return_value="")
mocker.patch('main.App.create_board', return_value="")
mocker.patch('main.App.create_player', return_value="")
mocker.patch('main.App.select_player', return_value="")
mocker.patch('main.App.start_game', return_value="")
app.run()