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

113 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 

19import pytest 

20 

21from ...src.base import CardMeta, GenericCard 

22 

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

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

25 

26 

27class DummyCard( 

28 GenericCard[T_Ranks, T_Suits], 

29 metaclass=CardMeta, 

30 rank_type=T_Ranks, 

31 suit_type=T_Suits 

32): 

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

34 pass 

35 

36 

37def test_card_init(): 

38 card1 = DummyCard(rank="2", suit="Blue") 

39 assert card1.rank == 1 

40 assert card1.suit == 2 

41 assert card1.trump is False 

42 

43 card2 = DummyCard(1, 2, True) 

44 assert card2.rank == 1 

45 assert card2.suit == 2 

46 assert card2.trump is True 

47 

48 with pytest.raises(ValueError): 

49 DummyCard("InvalidRank", "Red") # type: ignore 

50 with pytest.raises(ValueError): 

51 DummyCard("1", "InvalidSuit") # type: ignore 

52 

53 

54def test_card_get_rank(): 

55 card1 = DummyCard(0, 0) 

56 assert card1.get_rank() == "1" 

57 assert card1.get_rank(as_index=True) == 0 

58 

59 card2 = DummyCard(None, None) 

60 assert card2.get_rank() is None 

61 assert card2.get_rank(as_index=True) is None 

62 

63 

64def test_card_set_rank(): 

65 card = DummyCard(0, 0) 

66 card.change_rank("2") 

67 assert card.rank == 1 

68 card.change_rank(2) 

69 assert card.rank == 2 

70 card.change_rank(None) 

71 assert card.rank is None 

72 

73 with pytest.raises(ValueError): 

74 card.change_rank("InvalidRank") # type: ignore 

75 

76 with pytest.raises(ValueError): 

77 card.change_rank(10) 

78 

79 

80def test_card_get_suit(): 

81 card1 = DummyCard(0, 0) 

82 assert card1.get_suit() == "Red" 

83 assert card1.get_suit(as_index=True) == 0 

84 

85 card2 = DummyCard(None, None) 

86 assert card2.get_suit() is None 

87 assert card2.get_suit(as_index=True) is None 

88 

89 

90def test_card_set_suit(): 

91 card = DummyCard(0, 0) 

92 card.change_suit("Green") 

93 assert card.suit == 1 

94 card.change_suit(2) 

95 assert card.suit == 2 

96 card.change_suit(None) 

97 assert card.suit is None 

98 

99 with pytest.raises(ValueError): 

100 card.change_suit("InvalidSuit") # type: ignore 

101 

102 with pytest.raises(ValueError): 

103 card.change_suit(10) 

104 

105 

106def test_card_get_trump(): 

107 card = DummyCard(0, 0, True) 

108 assert card.is_trump() is True 

109 

110 

111def test_card_set_trump(): 

112 card = DummyCard(0, 0) 

113 card.set_trump(True) 

114 assert card.trump is True 

115 

116 with pytest.raises(TypeError): 

117 card.set_trump(1) # type: ignore 

118 

119 

120def test_card_copy(): 

121 card1 = DummyCard(0, 0) 

122 card2 = card1.__copy__() 

123 assert card1 is not card2 

124 assert card1 == card2 

125 assert card1.rank == card2.rank 

126 assert card1.suit == card2.suit 

127 assert card1.trump == card2.trump 

128 

129 card3 = DummyCard(None, None) 

130 card4 = card3.__copy__() 

131 assert card3 is not card4 

132 assert card3 == card4 

133 assert card3.rank is None 

134 assert card3.suit is None 

135 assert card3.trump is False 

136 

137 

138def test_card_str(): 

139 card1 = DummyCard(0, 0) 

140 assert str(card1) == "Red 1" 

141 

142 card2 = DummyCard(1, 2, True) 

143 assert str(card2) == "Blue 2 (trump)" 

144 

145 card3 = DummyCard(None, None) 

146 assert str(card3) == "None None" 

147 

148 

149def test_card_repr(): 

150 card1 = DummyCard(0, 0) 

151 assert repr(card1) == "DummyCard(rank=0, suit=0)" 

152 

153 card2 = DummyCard(2, 1, True) 

154 assert repr(card2) == "DummyCard(rank=2, suit=1, trump=True)" 

155 

156 card3 = DummyCard(None, None) 

157 assert repr(card3) == "DummyCard(rank=None, suit=None)" 

158 

159 

160def test_card_comparison(): 

161 card1 = DummyCard(0, 0) 

162 card2 = DummyCard(0, 0) 

163 card3 = DummyCard(1, 0) 

164 card4 = DummyCard(0, 1) 

165 card5 = DummyCard(0, 0, True) 

166 

167 assert card1 == card2 

168 assert card1 != card3 

169 assert card1 != card4 

170 assert card1 != card5 

171 assert card1 < card3 

172 assert card1 < card4 

173 assert card1 <= card5 

174 assert card3 > card1 

175 assert card4 > card1 

176 assert card5 >= card1 

177 

178 assert not card1 == "InvalidType" # type: ignore 

179 assert card1 != "InvalidType" # type: ignore