Coverage for app/pycardgame/tests/presets/uno/test_game.py: 100.0%
174 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-04-07 20:57 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-04-07 20:57 +0000
1# PyCardGame - A base library for creating card games in Python
2# Copyright (C) 2025 Popa-42
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <https://www.gnu.org/licenses/>.
17import pytest
19from ....src.presets import (
20 DrawTwoCard,
21 NumberCard,
22 UnoCard,
23 UnoDeck,
24 UnoGame,
25 UnoPlayer,
26 WildCard,
27)
30def test_uno_player_init():
31 player = UnoPlayer("Player 1", [NumberCard("5", "Red")])
32 assert player.name == "Player 1"
33 assert len(player) == 1
34 assert isinstance(player.hand[0], NumberCard)
37def test_uno_player_call_uno():
38 player = UnoPlayer("Player 1", [NumberCard("5", "Red")])
39 assert player.uno is False
40 player.call_uno()
41 assert player.uno is True
44def test_uno_player_reset_uno():
45 player = UnoPlayer("Player 1")
46 player.uno = True
47 assert player.uno is True
48 player.reset_uno()
49 assert player.uno is False
52def test_uno_player_str():
53 player = UnoPlayer("Player 1", [NumberCard("5", "Red")])
54 assert str(player) == "Player Player 1 (1 card(s)):\n - Red 5"
57def test_uno_player_repr():
58 player = UnoPlayer("Player 1", [NumberCard("5", "Red")])
59 print(player.score)
60 assert repr(player) == ("UnoPlayer('Player 1', "
61 "hand=[NumberCard(rank=5, suit=0)], uno=False)")
64def test_uno_game_init():
65 player1 = UnoPlayer("Player 1")
66 player2 = UnoPlayer("Player 2")
67 game = UnoGame(player1, player2)
68 assert len(game.players) == 2
69 assert isinstance(game.draw_pile, UnoDeck)
70 assert isinstance(game.discard_pile, UnoDeck)
71 assert game.hand_size == 7
72 assert game.direction == 1
75def test_uno_game_check_valid_play():
76 card1 = NumberCard("5", "Red")
77 card2 = NumberCard("5", "Blue")
78 card3 = NumberCard("7", "Blue")
79 game = UnoGame()
81 assert game.check_valid_play(card1) is False
82 assert game.check_valid_play(None, card1) is False # type: ignore
84 assert game.check_valid_play(card1, card2) is True
85 assert game.check_valid_play(card1, card3) is False
86 assert game.check_valid_play(card2, card3) is True
88 wild = WildCard()
89 cards = [card1, card2, card3, wild]
90 assert (game.check_valid_play(card, wild) is True for card in cards)
92 game.draw_count = 2
93 assert game.check_valid_play(card1, card2) is False
96def test_uno_game_discard_cards():
97 player = UnoPlayer("Player 1", [NumberCard("5", "Red")])
98 game = UnoGame(player)
99 game.discard_cards(NumberCard("5", "Red"))
100 assert len(game.discard_pile) == 1
101 assert game.discard_pile.get_top_card() == NumberCard("5", "Red")
104def test_uno_game_get_top_card():
105 player = UnoPlayer("Player 1", [NumberCard("5", "Red")])
106 game = UnoGame(player)
107 game.discard_cards(NumberCard("5", "Red"))
108 assert game.get_top_card() == NumberCard("5", "Red")
111def test_uno_game_get_next_player():
112 player1 = UnoPlayer("Player 1")
113 player2 = UnoPlayer("Player 2")
114 player3 = UnoPlayer("Player 3")
115 game = UnoGame(player1, player2, player3)
117 assert game.get_next_player() == player2
118 game.direction = -1
119 assert game.get_next_player() == player3
122def test_uno_game_play_card():
123 player1 = UnoPlayer("Player 1", [NumberCard("5", "Red")])
124 player2 = UnoPlayer("Player 2", [NumberCard("7", "Blue")])
125 game = UnoGame(player1, player2)
127 assert game.play_card(NumberCard("5", "Red")) is False
129 game.discard_cards(NumberCard("1", "Red"))
131 assert game.play_card(NumberCard("5", "Red")) is True
132 assert len(player1) == 0
133 assert len(game.discard_pile) == 2
134 assert game.get_top_card() == NumberCard("5", "Red")
135 assert game.play_card(NumberCard("7", "Blue"), player2) is False
138def test_uno_game_draw_instead_of_play():
139 player1 = UnoPlayer("Player 1", [DrawTwoCard("Red")])
140 player2 = UnoPlayer("Player 2", [NumberCard("7", "Blue")])
141 game = UnoGame(player1, player2)
143 game.draw_pile = UnoDeck()
145 game.discard_cards(NumberCard("5", "Red"))
146 game.play_card(DrawTwoCard("Red"), player1)
147 assert len(player1) == 0
148 assert len(player2) == 1
149 assert game.draw_count == 2
151 game.next_player()
152 drawn = game.draw_instead_of_play()
153 assert len(drawn) == 2
154 assert len(player2) == 3
155 assert game.draw_count == 0
158def test_uno_game_draw_cards():
159 player = UnoPlayer("Player 1")
160 game = UnoGame(player)
162 drawn_cards = game.draw_cards(player, 2)
163 assert drawn_cards is not None
164 assert len(drawn_cards) == 2
165 assert len(player) == 2
166 assert len(game.draw_pile) == 106
168 drawn_card = game.draw_cards(player)
169 assert drawn_card is not None
170 assert len(drawn_card) == 1
171 assert len(player) == 3
172 assert len(game.draw_pile) == 105
174 with pytest.raises(ValueError):
175 game.draw_cards(player, 999)
178def test_uno_game_reverse_direction():
179 player1 = UnoPlayer("Player 1")
180 player2 = UnoPlayer("Player 2")
181 game = UnoGame(player1, player2)
183 assert game.direction == 1
184 game.reverse_direction()
185 assert game.direction == -1
186 game.reverse_direction()
187 assert game.direction == 1
190def test_uno_game_start_game():
191 player1 = UnoPlayer("Player 1")
192 player2 = UnoPlayer("Player 2")
193 game = UnoGame(player1, player2)
195 game.start_game()
196 assert len(player1) == 7
197 assert len(player2) == 7
198 assert len(game.draw_pile) == 108 - 14 - 1
199 assert len(game.discard_pile) == 1
200 assert isinstance(game.discard_pile.get_top_card(), UnoCard)
203def test_uno_game_next_player():
204 player1 = UnoPlayer("Player 1")
205 player2 = UnoPlayer("Player 2")
206 player3 = UnoPlayer("Player 3")
207 game = UnoGame(player1, player2, player3)
209 assert game.current_player_index == 0
210 game.next_player()
211 assert game.current_player_index == 1
212 game.next_player()
213 assert game.current_player_index == 2
214 game.next_player()
215 assert game.current_player_index == 0
218def test_uno_game_determine_winner():
219 player1 = UnoPlayer("Player 1", [NumberCard("5", "Red")])
220 player2 = UnoPlayer("Player 2", [NumberCard("7", "Blue")])
221 game = UnoGame(player1, player2)
223 assert game.determine_winner() is None
225 player1.play_cards(NumberCard("5", "Red"))
226 assert game.determine_winner() == player1
229def test_uno_game_end_game():
230 player1 = UnoPlayer("Player 1", [NumberCard("5", "Red")])
231 player2 = UnoPlayer("Player 2", [NumberCard("7", "Blue")])
232 game1 = UnoGame(player1, player2)
234 assert game1.game_ended is False
235 assert game1.end_game() is None
236 assert game1.game_ended is True
237 assert len(game1.draw_pile) == 0
238 assert len(game1.discard_pile) == 0
239 assert len(game1.players) == 0
240 assert len(player1) == 0
241 assert len(player2) == 0
243 player2.add_cards(NumberCard("5", "Red"))
244 game2 = UnoGame(player1, player2)
245 assert game2.end_game() is not None
248def test_uno_game_str():
249 player1 = UnoPlayer("Player 1", [NumberCard("5", "Red")])
250 player2 = UnoPlayer("Player 2", [NumberCard("7", "Blue")])
251 game = UnoGame(player1, player2)
252 game.discard_cards(NumberCard("1", "Red"))
254 assert str(game) == ("UNO Game\n"
255 "Current Player: Player 1\n"
256 "Draw Pile: 108 card(s)\n"
257 "Discard Pile: 1 card(s)\n"
258 "Direction: Clockwise\n"
259 "Top Card: Red 1\n"
260 "Players:\n"
261 " - Player 1: 1 card(s)\n"
262 " - Player 2: 1 card(s)")
265def test_uno_game_repr():
266 player1 = UnoPlayer("Player 1", [NumberCard("5", "Red")])
267 player2 = UnoPlayer("Player 2", [NumberCard("7", "Blue")])
268 deck = UnoDeck()
269 game = UnoGame(player1, player2, draw_pile=deck)
271 assert repr(game) == (f"UnoGame(players=[{player1!r}, {player2!r}], "
272 f"draw_pile={UnoDeck()!r}, "
273 f"discard_pile={UnoDeck([])!r}, "
274 f"hand_size=7, current_player_index=0, direction=1)")