This commit is contained in:
2025-09-24 16:54:52 +08:00
parent 45f86ba95e
commit 57a7e9216e
22 changed files with 1647 additions and 102 deletions

View File

@@ -0,0 +1,203 @@
"""
Tests for Short Deck Card implementation
"""
import pytest
from poker.card import Card, ShortDeckRank, Suit # 使用统一的Card类
class TestShortDeckRank:
"""Test cases for ShortDeckRank enum"""
def test_rank_values(self):
"""Test that short deck ranks have correct values"""
assert ShortDeckRank.SIX.numeric_value == 6
assert ShortDeckRank.SEVEN.numeric_value == 7
assert ShortDeckRank.EIGHT.numeric_value == 8
assert ShortDeckRank.NINE.numeric_value == 9
assert ShortDeckRank.TEN.numeric_value == 10
assert ShortDeckRank.JACK.numeric_value == 11
assert ShortDeckRank.QUEEN.numeric_value == 12
assert ShortDeckRank.KING.numeric_value == 13
assert ShortDeckRank.ACE.numeric_value == 14
def test_rank_symbols(self):
"""Test that short deck ranks have correct symbols"""
assert ShortDeckRank.SIX.symbol == '6'
assert ShortDeckRank.SEVEN.symbol == '7'
assert ShortDeckRank.EIGHT.symbol == '8'
assert ShortDeckRank.NINE.symbol == '9'
assert ShortDeckRank.TEN.symbol == 'T'
assert ShortDeckRank.JACK.symbol == 'J'
assert ShortDeckRank.QUEEN.symbol == 'Q'
assert ShortDeckRank.KING.symbol == 'K'
assert ShortDeckRank.ACE.symbol == 'A'
def test_rank_string_representation(self):
"""Test string representation of ranks"""
assert str(ShortDeckRank.SIX) == '6'
assert str(ShortDeckRank.ACE) == 'A'
def test_rank_comparison(self):
"""Test rank comparison"""
assert ShortDeckRank.SIX < ShortDeckRank.SEVEN
assert ShortDeckRank.KING < ShortDeckRank.ACE
assert ShortDeckRank.ACE > ShortDeckRank.KING
assert ShortDeckRank.SIX <= ShortDeckRank.SIX
assert ShortDeckRank.ACE >= ShortDeckRank.ACE
def test_rank_equality(self):
"""Test rank equality"""
assert ShortDeckRank.SIX == ShortDeckRank.SIX
assert ShortDeckRank.ACE != ShortDeckRank.KING
def test_rank_hash(self):
"""Test rank hash functionality"""
rank_set = {ShortDeckRank.SIX, ShortDeckRank.SIX, ShortDeckRank.SEVEN}
assert len(rank_set) == 2 # No duplicate SIX
def test_no_low_ranks(self):
"""Test that ranks 2,3,4,5 are not present in short deck"""
rank_values = [rank.numeric_value for rank in ShortDeckRank]
assert 2 not in rank_values
assert 3 not in rank_values
assert 4 not in rank_values
assert 5 not in rank_values
assert len(list(ShortDeckRank)) == 9 # Only 9 ranks
class TestSuit:
"""Test cases for Suit enum (same as regular poker)"""
def test_suit_values(self):
"""Test suit values"""
assert Suit.SPADES.value == 's'
assert Suit.HEARTS.value == 'h'
assert Suit.DIAMONDS.value == 'd'
assert Suit.CLUBS.value == 'c'
def test_suit_string_representation(self):
"""Test string representation of suits"""
assert str(Suit.SPADES) == 's'
assert str(Suit.HEARTS) == 'h'
class TestCard:
"""Test cases for Card class"""
def test_card_creation(self):
"""Test card creation"""
card = Card(ShortDeckRank.ACE, Suit.SPADES)
assert card.rank == ShortDeckRank.ACE
assert card.suit == Suit.SPADES
def test_card_string_representation(self):
"""Test card string representation"""
card = Card(ShortDeckRank.ACE, Suit.SPADES)
assert str(card) == "As"
card2 = Card(ShortDeckRank.KING, Suit.HEARTS)
assert str(card2) == "Kh"
def test_card_comparison(self):
"""Test card comparison"""
ace_spades = Card(ShortDeckRank.ACE, Suit.SPADES)
king_spades = Card(ShortDeckRank.KING, Suit.SPADES)
ace_hearts = Card(ShortDeckRank.ACE, Suit.HEARTS)
assert king_spades < ace_spades
assert ace_hearts < ace_spades # Same rank, suit comparison
assert ace_spades > king_spades
def test_card_equality(self):
"""Test card equality"""
card1 = Card(ShortDeckRank.ACE, Suit.SPADES)
card2 = Card(ShortDeckRank.ACE, Suit.SPADES)
card3 = Card(ShortDeckRank.KING, Suit.SPADES)
assert card1 == card2
assert card1 != card3
def test_create_card_valid(self):
"""Test creating cards from valid strings"""
# 使用短牌模式创建卡片
card = Card.create_card("As", is_short_deck=True)
assert card.rank == ShortDeckRank.ACE
assert card.suit == Suit.SPADES
card2 = Card.create_card("6h", is_short_deck=True)
assert card2.rank == ShortDeckRank.SIX
assert card2.suit == Suit.HEARTS
def test_create_card_invalid_length(self):
"""Test creating card with invalid string length"""
with pytest.raises(ValueError, match="Invalid card string"):
Card.create_card("A")
with pytest.raises(ValueError, match="Invalid card string"):
Card.create_card("Ash")
def test_create_card_invalid_rank(self):
"""Test creating card with invalid rank (should reject 2,3,4,5)"""
with pytest.raises(ValueError, match="Invalid rank"):
Card.create_card("2s", is_short_deck=True)
with pytest.raises(ValueError, match="Invalid rank"):
Card.create_card("5h", is_short_deck=True)
with pytest.raises(ValueError, match="Invalid rank"):
Card.create_card("Xs", is_short_deck=True)
def test_create_card_invalid_suit(self):
"""Test creating card with invalid suit"""
with pytest.raises(ValueError, match="Invalid suit"):
Card.create_card("Ax")
def test_parse_cards_valid(self):
"""Test parsing multiple valid cards"""
cards = Card.parse_cards("AsKs AhAdAc6s7s")
assert len(cards) == 7
assert str(cards[0]) == "As"
assert str(cards[1]) == "Ks"
assert str(cards[6]) == "7s"
def test_parse_cards_empty(self):
"""Test parsing empty string"""
cards = Card.parse_cards("")
assert len(cards) == 0
cards = Card.parse_cards(" ")
assert len(cards) == 0
def test_parse_cards_invalid_format(self):
"""Test parsing cards with invalid format"""
with pytest.raises(ValueError, match="Invalid card format"):
Card.parse_cards("AsK") # Odd length
def test_parse_cards_with_invalid_cards(self):
"""Test parsing string containing invalid cards"""
with pytest.raises(ValueError, match="Invalid rank"):
Card.parse_cards("As 2s Kh", is_short_deck=True) # Contains '2s'
def test_get_all_cards(self):
"""Test getting all 36 cards in short deck"""
all_cards = []
for rank in ShortDeckRank:
for suit in Suit:
all_cards.append(Card(rank, suit))
assert len(all_cards) == 36 # 9 ranks * 4 suits
# Check that all combinations are present
ranks_found = set()
suits_found = set()
for card in all_cards:
ranks_found.add(card.rank)
suits_found.add(card.suit)
assert len(ranks_found) == 9 # All 9 short deck ranks
assert len(suits_found) == 4 # All 4 suits
# Verify no low cards (2,3,4,5) are present
for card in all_cards:
assert card.rank.numeric_value >= 6