-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_game.py
129 lines (112 loc) · 5.39 KB
/
test_game.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
import pytest
import itertools
import random as rd
import game
class TestClass:
"""
This class is used for pytest testing
"""
INITIAL_TILES = 4
MAX_TILE_NUMBER = 11
@pytest.fixture(params=[2, 3, 4])
def setup_init(self, request):
game_host = game.GameHost(request.param, self.INITIAL_TILES, self.MAX_TILE_NUMBER)
game_host.init_game()
return game_host
def test_init(self, setup_init, request):
game_host = setup_init
number_players = request.node.callspec.params["setup_init"]
assert (
len(game_host.all_players) == number_players
) # test if the number of generated players is correct
for player in game_host.all_players:
assert (
len(player.tile_set) == self.INITIAL_TILES
) # test if the number of tiles drawn by each player is correct
def test_making_guess(self, setup_init, request):
game_host = setup_init
number_players = request.node.callspec.params["setup_init"]
player_combs = list(
itertools.permutations([player_index for player_index in range(0, number_players)], 2)
)
for player_index in range(0, number_players):
with pytest.raises(
ValueError
): # test the exception with target index that is the player it self
game_host.all_players[player_index].make_guess(
game_host.all_players,
player_index,
rd.randint(0, self.INITIAL_TILES),
rd.randint(0, self.MAX_TILE_NUMBER),
)
for player_comb in player_combs:
with pytest.raises(ValueError): # test the exception with target index lower than bound
game_host.all_players[player_comb[0]].make_guess(
game_host.all_players,
-1,
rd.randint(0, self.INITIAL_TILES),
rd.randint(0, self.MAX_TILE_NUMBER),
)
with pytest.raises(
ValueError
): # test the exception with target index higher than bound
game_host.all_players[player_comb[0]].make_guess(
game_host.all_players,
len(game_host.all_players) + 1,
rd.randint(0, self.INITIAL_TILES),
rd.randint(0, self.MAX_TILE_NUMBER),
)
with pytest.raises(ValueError): # test the exception with tile index lower than bound
game_host.all_players[player_comb[0]].make_guess(
game_host.all_players,
player_comb[1],
-1,
rd.randint(0, self.MAX_TILE_NUMBER),
)
with pytest.raises(ValueError): # test the exception with tile index higher than bound
game_host.all_players[player_comb[0]].make_guess(
game_host.all_players,
player_comb[1],
len(game_host.all_players[player_comb[1]].tile_set) + 1,
rd.randint(0, self.MAX_TILE_NUMBER),
)
# game_host.all_players[0].make_guess(all_players: list, target_index: int, tile_index: int, tile_number: int)
def test_guess_validation(self, setup_init, request):
game_host = setup_init
number_players = request.node.callspec.params["setup_init"]
# TODO: add tests for successful guessing
def test_empty_table_draw(self, setup_init, request):
game_host = setup_init
number_players = request.node.callspec.params["setup_init"]
while len(game_host.table_tile_set.tile_set) > 0:
try:
game_host.all_players[rd.randint(0, number_players - 1)].draw_tile(
game_host.table_tile_set
)
except ValueError: # test the exception with an non-empty table
pytest.fail("Unexpected EmptyTableError exception raised")
with pytest.raises(ValueError): # test the exception with an empty table
game_host.all_players[rd.randint(0, number_players - 1)].draw_tile(
game_host.table_tile_set
)
def test_winner_appears(self, setup_init, request):
game_host = setup_init
number_players = request.node.callspec.params["setup_init"]
for draw in range(0, len(game_host.table_tile_set.tile_set)):
try:
game_host.all_players[rd.randint(0, number_players - 1)].draw_tile(
game_host.table_tile_set
)
except game.EmptyTableError: # test the exception with an non-empty table
pytest.fail("Unexpected EmptyTableError exception raised")
player_index = rd.randint(0, number_players - 1)
other_player_indexes = list(index for index in range(0, number_players))
other_player_indexes.remove(player_index)
for other_player_index in other_player_indexes:
for tile in game_host.all_players[other_player_index].tile_set:
# for tileTemp in game_host.all_players[other_player_index].tile_set:
# print(tileTemp)
# print(game_host.is_game_over())
assert game_host.is_game_over() == False
tile.direction = game.Tile.Directions.PUBLIC
assert game_host.is_game_over() == True