Coverage for app/pycardgame/tests/presets/uno/test_cards.py: 100.0%
149 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 ReverseCard,
23 SkipCard,
24 UnoCard,
25 UnoDeck,
26 UnoGame,
27 UnoPlayer,
28 WildCard,
29 WildDrawFourCard,
30)
33def test_uno_card_init():
34 card1 = UnoCard("5", "Red")
35 card2 = UnoCard(5, "Red")
36 card3 = UnoCard("5", 0)
37 assert card1.rank == 5
38 assert card1.suit == 0
39 assert str(card1) == "Red 5"
40 assert repr(card1) == "UnoCard(rank=5, suit=0)"
41 assert card1 == card2 == card3
43 with pytest.raises(ValueError):
44 UnoCard(["InvalidRankType"], "Red") # type: ignore
46 with pytest.raises(ValueError):
47 UnoCard("5", ["InvalidSuitType"]) # type: ignore
50def test_uno_card_is_wild():
51 card = UnoCard("2", "Green")
52 assert not card.is_wild()
55def test_uno_card_str():
56 card1 = UnoCard("2", "Green")
57 assert str(card1) == "Green 2"
59 card2 = UnoCard("Wild Draw Four", "Wild")
60 card2.wild = True
61 assert str(card2) == "Wild Draw Four"
63 card3 = UnoCard("Skip", "Red")
64 assert str(card3) == "Red Skip"
67def test_number_card_init():
68 card = NumberCard("5", "Red")
69 assert card.rank == 5
70 assert card.suit == 0
71 assert card.wild is False
72 assert str(card) == "Red 5"
73 assert repr(card) == "NumberCard(rank=5, suit=0)"
76def test_draw_two_card_init():
77 card = DrawTwoCard("Red")
78 assert card.rank == 12
79 assert card.suit == 0
80 assert card.wild is False
81 assert str(card) == "Red Draw Two"
82 assert repr(card) == "DrawTwoCard(rank=12, suit=0)"
85def test_draw_two_card_effect():
86 player1 = UnoPlayer("Player 1", [DrawTwoCard("Red")])
87 player2 = UnoPlayer("Player 2")
88 deck = UnoDeck()
89 game = UnoGame(player1, player2, draw_pile=deck)
91 game.discard_cards(UnoCard("5", "Red"))
92 game.play_card(DrawTwoCard("Red"), player1)
94 assert len(player1) == 0
95 assert len(player2) == 0
96 assert game.draw_count == 2
98 game.next_player()
99 drawn = game.draw_instead_of_play()
101 assert len(drawn) == 2
102 assert len(player2) == 2
103 assert game.draw_count == 0
106def test_skip_card_init():
107 card = SkipCard("Red")
108 assert card.rank == 10
109 assert card.suit == 0
110 assert card.wild is False
111 assert str(card) == "Red Skip"
112 assert repr(card) == "SkipCard(rank=10, suit=0)"
115def test_skip_card_effect():
116 player1 = UnoPlayer("Player 1", [SkipCard("Red")])
117 player2 = UnoPlayer("Player 2")
118 deck = UnoDeck()
119 game = UnoGame(player1, player2, draw_pile=deck)
120 game.discard_cards(UnoCard("5", "Red"))
121 game.play_card(SkipCard("Red"))
122 assert game.current_player_index == 1
125def test_reverse_card_init():
126 card = ReverseCard("Red")
127 assert card.rank == 11
128 assert card.suit == 0
129 assert card.wild is False
130 assert str(card) == "Red Reverse"
131 assert repr(card) == "ReverseCard(rank=11, suit=0)"
134def test_reverse_card_effect():
135 player1 = UnoPlayer("Player 1", [ReverseCard("Red")])
136 player2 = UnoPlayer("Player 2")
137 deck = UnoDeck()
138 game = UnoGame(player1, player2, draw_pile=deck)
139 game.discard_cards(UnoCard("5", "Red"))
140 game.play_card(ReverseCard("Red"))
141 assert game.direction == -1
144def test_wild_card_init():
145 card = WildCard()
146 assert card.rank == 13
147 assert card.suit == 4
148 assert card.wild is True
149 assert str(card) == "Wild"
150 assert repr(card) == "WildCard(rank=13, suit=4)"
153def test_wild_card_effect():
154 player1 = UnoPlayer("Player 1", [WildCard()] * 2)
155 player2 = UnoPlayer("Player 2")
156 deck = UnoDeck()
157 game = UnoGame(player1, player2, draw_pile=deck)
158 game.discard_cards(UnoCard("5", "Red"))
159 game.play_card(WildCard(), player1, "Blue")
160 assert game.discard_pile.cards[0].suit == 2
162 with pytest.raises(ValueError):
163 game.play_card(WildCard(), player1)
166def test_wild_draw_four_card_init():
167 card = WildDrawFourCard()
168 assert card.rank == 14
169 assert card.suit == 4
170 assert card.wild is True
171 assert str(card) == "Wild Draw Four"
172 assert repr(card) == "WildDrawFourCard(rank=14, suit=4)"
175def test_wild_draw_four_card_effect():
176 player1 = UnoPlayer("Player 1", [WildDrawFourCard()] * 2)
177 player2 = UnoPlayer("Player 2")
178 deck = UnoDeck()
179 game = UnoGame(player1, player2, draw_pile=deck)
180 game.discard_cards(UnoCard("5", "Red"))
181 game.play_card(WildDrawFourCard(), player1, "Blue")
182 assert game.discard_pile.cards[0].suit == 2
183 assert len(player2) == 4
185 with pytest.raises(ValueError):
186 game.play_card(WildDrawFourCard(), player1)
189def test_draw_two_card_stacking():
190 hand1 = [DrawTwoCard("Red"), NumberCard("5", "Yellow")]
191 hand2 = [DrawTwoCard("Yellow"), NumberCard("6", "Blue")]
192 hand3 = [NumberCard("7", "Yellow")] # No Draw Two card
194 player1 = UnoPlayer("Player 1", hand1)
195 player2 = UnoPlayer("Player 2", hand2)
196 player3 = UnoPlayer("Player 3", hand3)
198 deck_cards = [
199 NumberCard("1", "Blue"), NumberCard("2", "Green"),
200 NumberCard("3", "Red"), NumberCard("4", "Yellow"),
201 NumberCard("5", "Blue"), NumberCard("6", "Green"),
202 NumberCard("7", "Red"), NumberCard("8", "Yellow"),
203 NumberCard("9", "Blue"), NumberCard("1", "Green")
204 ]
205 deck = UnoDeck(deck_cards)
206 game = UnoGame(player1, player2, player3, draw_pile=deck)
208 game.discard_cards(NumberCard("4", "Red"))
210 game.play_card(DrawTwoCard("Red"))
211 assert game.draw_count == 2
212 assert len(player1) == 1
214 game.next_player()
216 game.play_card(DrawTwoCard("Yellow"))
217 assert game.draw_count == 4
218 assert len(player2) == 1
220 game.next_player()
222 assert len(player3) == 1
224 drawn_cards = game.draw_instead_of_play()
226 assert len(drawn_cards) == 4
227 assert len(player3) == 5
228 assert game.draw_count == 0
230 current_player = game.get_current_player()
231 initial_hand_size = len(current_player)
233 drawn = game.draw_instead_of_play()
235 assert len(drawn) == 1
236 assert len(current_player) == initial_hand_size + 1