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

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/>. 

16 

17import pytest 

18 

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) 

31 

32 

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 

42 

43 with pytest.raises(ValueError): 

44 UnoCard(["InvalidRankType"], "Red") # type: ignore 

45 

46 with pytest.raises(ValueError): 

47 UnoCard("5", ["InvalidSuitType"]) # type: ignore 

48 

49 

50def test_uno_card_is_wild(): 

51 card = UnoCard("2", "Green") 

52 assert not card.is_wild() 

53 

54 

55def test_uno_card_str(): 

56 card1 = UnoCard("2", "Green") 

57 assert str(card1) == "Green 2" 

58 

59 card2 = UnoCard("Wild Draw Four", "Wild") 

60 card2.wild = True 

61 assert str(card2) == "Wild Draw Four" 

62 

63 card3 = UnoCard("Skip", "Red") 

64 assert str(card3) == "Red Skip" 

65 

66 

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)" 

74 

75 

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)" 

83 

84 

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) 

90 

91 game.discard_cards(UnoCard("5", "Red")) 

92 game.play_card(DrawTwoCard("Red"), player1) 

93 

94 assert len(player1) == 0 

95 assert len(player2) == 0 

96 assert game.draw_count == 2 

97 

98 game.next_player() 

99 drawn = game.draw_instead_of_play() 

100 

101 assert len(drawn) == 2 

102 assert len(player2) == 2 

103 assert game.draw_count == 0 

104 

105 

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)" 

113 

114 

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 

123 

124 

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)" 

132 

133 

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 

142 

143 

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)" 

151 

152 

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 

161 

162 with pytest.raises(ValueError): 

163 game.play_card(WildCard(), player1) 

164 

165 

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)" 

173 

174 

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 

184 

185 with pytest.raises(ValueError): 

186 game.play_card(WildDrawFourCard(), player1) 

187 

188 

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 

193 

194 player1 = UnoPlayer("Player 1", hand1) 

195 player2 = UnoPlayer("Player 2", hand2) 

196 player3 = UnoPlayer("Player 3", hand3) 

197 

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) 

207 

208 game.discard_cards(NumberCard("4", "Red")) 

209 

210 game.play_card(DrawTwoCard("Red")) 

211 assert game.draw_count == 2 

212 assert len(player1) == 1 

213 

214 game.next_player() 

215 

216 game.play_card(DrawTwoCard("Yellow")) 

217 assert game.draw_count == 4 

218 assert len(player2) == 1 

219 

220 game.next_player() 

221 

222 assert len(player3) == 1 

223 

224 drawn_cards = game.draw_instead_of_play() 

225 

226 assert len(drawn_cards) == 4 

227 assert len(player3) == 5 

228 assert game.draw_count == 0 

229 

230 current_player = game.get_current_player() 

231 initial_hand_size = len(current_player) 

232 

233 drawn = game.draw_instead_of_play() 

234 

235 assert len(drawn) == 1 

236 assert len(current_player) == initial_hand_size + 1