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,177 @@
"""
Tests for Short Deck HandRanking and HandType classes
"""
import pytest
from poker.card import Card, ShortDeckRank, Suit # 使用统一的Card类
from shortdeck.hand_ranking import ShortDeckHandRanking, ShortDeckHandType
class TestShortDeckHandType:
"""Test cases for ShortDeckHandType enum"""
def test_hand_type_values(self):
"""Test hand type strength values - Flush beats Full House in Short Deck"""
assert ShortDeckHandType.HIGH_CARD.strength == 1
assert ShortDeckHandType.ONE_PAIR.strength == 2
assert ShortDeckHandType.TWO_PAIR.strength == 3
assert ShortDeckHandType.THREE_OF_A_KIND.strength == 4
assert ShortDeckHandType.STRAIGHT.strength == 5
assert ShortDeckHandType.FULL_HOUSE.strength == 6
assert ShortDeckHandType.FLUSH.strength == 7 # Higher than Full House!
assert ShortDeckHandType.FOUR_OF_A_KIND.strength == 8
assert ShortDeckHandType.STRAIGHT_FLUSH.strength == 9
assert ShortDeckHandType.ROYAL_FLUSH.strength == 10
def test_hand_type_names(self):
"""Test hand type names"""
assert ShortDeckHandType.HIGH_CARD.type_name == "High Card"
assert ShortDeckHandType.FOUR_OF_A_KIND.type_name == "Four of a Kind"
assert ShortDeckHandType.ROYAL_FLUSH.type_name == "Royal Flush"
assert ShortDeckHandType.FLUSH.type_name == "Flush"
assert ShortDeckHandType.FULL_HOUSE.type_name == "Full House"
def test_hand_type_string_representation(self):
"""Test string representation of hand types"""
assert str(ShortDeckHandType.HIGH_CARD) == "High Card"
assert str(ShortDeckHandType.ROYAL_FLUSH) == "Royal Flush"
def test_hand_type_comparison(self):
"""Test hand type comparison - KEY: Flush > Full House in Short Deck"""
assert ShortDeckHandType.HIGH_CARD < ShortDeckHandType.ONE_PAIR
assert ShortDeckHandType.FLUSH > ShortDeckHandType.FULL_HOUSE # Short Deck rule!
assert ShortDeckHandType.ROYAL_FLUSH >= ShortDeckHandType.STRAIGHT_FLUSH
assert ShortDeckHandType.TWO_PAIR <= ShortDeckHandType.THREE_OF_A_KIND
assert ShortDeckHandType.STRAIGHT < ShortDeckHandType.FULL_HOUSE
assert ShortDeckHandType.FULL_HOUSE < ShortDeckHandType.FLUSH
def test_hand_type_equality(self):
"""Test hand type equality"""
assert ShortDeckHandType.FLUSH == ShortDeckHandType.FLUSH
assert ShortDeckHandType.FLUSH != ShortDeckHandType.FULL_HOUSE
class TestShortDeckHandRanking:
"""Test cases for ShortDeckHandRanking class"""
def test_hand_ranking_creation(self):
"""Test hand ranking creation"""
cards = [
Card(ShortDeckRank.ACE, Suit.SPADES),
Card(ShortDeckRank.ACE, Suit.HEARTS),
Card(ShortDeckRank.ACE, Suit.DIAMONDS),
Card(ShortDeckRank.ACE, Suit.CLUBS),
Card(ShortDeckRank.KING, Suit.SPADES)
]
ranking = ShortDeckHandRanking(ShortDeckHandType.FOUR_OF_A_KIND, [ShortDeckRank.ACE, ShortDeckRank.KING], cards)
assert ranking.hand_type == ShortDeckHandType.FOUR_OF_A_KIND
assert ranking.key_ranks == [ShortDeckRank.ACE, ShortDeckRank.KING]
assert ranking.cards == cards
def test_quad_string_representation(self):
"""Test string representation for four of a kind"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.FOUR_OF_A_KIND, [ShortDeckRank.ACE, ShortDeckRank.KING], cards)
assert str(ranking) == "Quad(A)"
def test_full_house_string_representation(self):
"""Test string representation for full house"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.FULL_HOUSE, [ShortDeckRank.ACE, ShortDeckRank.KING], cards)
assert str(ranking) == "Full House(A over K)"
def test_flush_string_representation(self):
"""Test string representation for flush"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.FLUSH, [ShortDeckRank.ACE], cards)
assert str(ranking) == "Flush(A high)"
def test_straight_string_representation(self):
"""Test string representation for straight"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.STRAIGHT, [ShortDeckRank.ACE], cards)
assert str(ranking) == "Straight(A high)"
def test_straight_flush_string_representation(self):
"""Test string representation for straight flush"""
cards = [Card(ShortDeckRank.KING, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.STRAIGHT_FLUSH, [ShortDeckRank.KING], cards)
assert str(ranking) == "Straight Flush(K high)"
def test_royal_flush_string_representation(self):
"""Test string representation for royal flush"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
# Direct royal flush
ranking1 = ShortDeckHandRanking(ShortDeckHandType.ROYAL_FLUSH, [ShortDeckRank.ACE], cards)
assert str(ranking1) == "Royal Flush"
# Straight flush with Ace high should also show as Royal Flush
ranking2 = ShortDeckHandRanking(ShortDeckHandType.STRAIGHT_FLUSH, [ShortDeckRank.ACE], cards)
assert str(ranking2) == "Royal Flush"
def test_three_of_a_kind_string_representation(self):
"""Test string representation for three of a kind"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.THREE_OF_A_KIND, [ShortDeckRank.ACE], cards)
assert str(ranking) == "Three of a Kind(A)"
def test_two_pair_string_representation(self):
"""Test string representation for two pair"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.TWO_PAIR, [ShortDeckRank.ACE, ShortDeckRank.KING], cards)
assert str(ranking) == "Two Pair(A and K)"
def test_one_pair_string_representation(self):
"""Test string representation for one pair"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.ONE_PAIR, [ShortDeckRank.ACE], cards)
assert str(ranking) == "Pair(A)"
def test_high_card_string_representation(self):
"""Test string representation for high card"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ranking = ShortDeckHandRanking(ShortDeckHandType.HIGH_CARD, [ShortDeckRank.ACE], cards)
assert str(ranking) == "High Card(A)"
def test_hand_ranking_comparison(self):
"""Test hand ranking comparison"""
# Create some test cards
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
# Create different rankings
quad_aces = ShortDeckHandRanking(ShortDeckHandType.FOUR_OF_A_KIND, [ShortDeckRank.ACE], cards)
flush_ace = ShortDeckHandRanking(ShortDeckHandType.FLUSH, [ShortDeckRank.ACE], cards)
full_house = ShortDeckHandRanking(ShortDeckHandType.FULL_HOUSE, [ShortDeckRank.ACE, ShortDeckRank.KING], cards)
straight_ace = ShortDeckHandRanking(ShortDeckHandType.STRAIGHT, [ShortDeckRank.ACE], cards)
# Test Short Deck specific comparison: Flush > Full House
assert flush_ace > full_house
assert full_house < flush_ace
# Test other comparisons
assert quad_aces > flush_ace
assert straight_ace < full_house
def test_hand_ranking_equality(self):
"""Test hand ranking equality"""
cards1 = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
cards2 = [Card(ShortDeckRank.KING, Suit.HEARTS)] * 5
ranking1 = ShortDeckHandRanking(ShortDeckHandType.FLUSH, [ShortDeckRank.ACE], cards1)
ranking2 = ShortDeckHandRanking(ShortDeckHandType.FLUSH, [ShortDeckRank.ACE], cards2)
ranking3 = ShortDeckHandRanking(ShortDeckHandType.FLUSH, [ShortDeckRank.KING], cards2)
assert ranking1 == ranking2 # Same hand type and key ranks
assert ranking1 != ranking3 # Different key ranks
def test_same_type_different_ranks(self):
"""Test comparison of same hand type with different key ranks"""
cards = [Card(ShortDeckRank.ACE, Suit.SPADES)] * 5
ace_high = ShortDeckHandRanking(ShortDeckHandType.HIGH_CARD, [ShortDeckRank.ACE], cards)
king_high = ShortDeckHandRanking(ShortDeckHandType.HIGH_CARD, [ShortDeckRank.KING], cards)
assert ace_high > king_high
assert king_high < ace_high