Coverage for app/pycardgame/tests/base/test_player.py: 100.0%

87 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 

17from typing import Literal 

18 

19from ...src.base import GenericCard, GenericPlayer, CardMeta 

20 

21T_Ranks = Literal["1", "2", "3"] 

22T_Suits = Literal["Red", "Green", "Blue"] 

23 

24 

25class DummyCard( 

26 GenericCard[T_Ranks, T_Suits], 

27 metaclass=CardMeta, 

28 rank_type=T_Ranks, 

29 suit_type=T_Suits 

30): 

31 def effect(self, game, player, *args): # pragma: no cover 

32 pass 

33 

34 

35class DummyPlayer(GenericPlayer[DummyCard]): 

36 pass 

37 

38 

39def test_player_init(): 

40 cards = [DummyCard("2", "Green"), DummyCard("3", "Blue")] 

41 player = DummyPlayer("Alice", cards, 10) 

42 assert player.name == "Alice" 

43 assert player.hand == cards 

44 assert player.score == 10 

45 

46 

47def test_player_add_card(): 

48 player = DummyPlayer("Alice") 

49 card = DummyCard("2", "Green") 

50 player.add_cards(card) 

51 assert player.hand == [card] 

52 player.add_cards(card, card) 

53 assert player.hand == [card, card, card] 

54 

55 

56def test_player_remove_card(): 

57 card = DummyCard("2", "Green") 

58 player = DummyPlayer("Alice", [card, card, card]) 

59 player.remove_cards(card) 

60 assert player.hand == [card, card] 

61 player.remove_cards(card, card) 

62 assert player.hand == [] 

63 

64 

65def test_player_play_card(): 

66 card1 = DummyCard("2", "Green") 

67 card2 = DummyCard("3", "Blue") 

68 hand = [card1, card2] 

69 player = DummyPlayer("Alice", hand) 

70 assert player.play_cards() == hand 

71 

72 

73def test_player_get_hand(): 

74 cards = [DummyCard("2", "Green"), DummyCard("3", "Blue")] 

75 player = DummyPlayer("Alice", cards) 

76 assert player.get_hand() == cards 

77 

78 

79def test_player_get_score(): 

80 player = DummyPlayer("Alice", [], 10) 

81 assert player.get_score() == 10 

82 

83 

84def test_player_set_score(): 

85 player = DummyPlayer("Alice") 

86 player.set_score(10) 

87 assert player.score == 10 

88 player.set_score(20) 

89 assert player.score == 20 

90 

91 

92def test_player_get_name(): 

93 player = DummyPlayer("Alice") 

94 assert player.get_name() == "Alice" 

95 

96 

97def test_player_set_name(): 

98 player = DummyPlayer("Alice") 

99 player.set_name("Bob") 

100 assert player.name == "Bob" 

101 

102 

103def test_player_getitem(): 

104 cards = [DummyCard("2", "Green"), DummyCard("3", "Blue")] 

105 player = DummyPlayer("Alice", cards) 

106 assert player[0] == cards[0] 

107 assert player[1:-1:-1] == cards[1:-1:-1] 

108 for card in player: 

109 assert card in cards 

110 assert len(player) == len(cards) 

111 

112 

113def test_player_str(): 

114 player = DummyPlayer("Alice", [DummyCard("3", "Blue")]) 

115 assert str(player) == "Player Alice (1 card(s)):\n - Blue 3" 

116 

117 

118def test_player_repr(): 

119 player = DummyPlayer("Alice", [DummyCard("3", "Blue")]) 

120 player_repr = repr(player) 

121 assert player_repr.startswith("DummyPlayer('Alice', hand=[") 

122 assert "DummyCard(rank=2, suit=2)" in player_repr 

123 assert player_repr.endswith(", score=0)") 

124 

125 

126def test_player_equalities(): 

127 player1 = DummyPlayer("Alice", [DummyCard("2", "Green")]) 

128 player2 = DummyPlayer("Alice", [DummyCard("2", "Green")]) 

129 assert player1 == player2 

130 assert player1 != DummyPlayer("Bob", [DummyCard("2", "Green")]) 

131 assert player1 != DummyPlayer("Alice", [DummyCard("3", "Blue")]) 

132 assert player1 != DummyPlayer("Alice", [DummyCard("2", "Green")], 10) 

133 

134 assert not player1 == "InvalidType" # type: ignore 

135 assert player1 != "InvalidType" # type: ignore 

136 

137 

138def test_player_comparisons(): 

139 player1 = DummyPlayer("Alice", [DummyCard("2", "Green")], 10) 

140 player2 = DummyPlayer("Bob", [DummyCard("2", "Green")], 20) 

141 assert player1 < player2 

142 assert player1 <= player2 

143 assert player2 > player1 

144 assert player2 >= player1 

145 assert not player1 < DummyPlayer("Alice")