修改
This commit is contained in:
155
tests/test_card.py
Normal file
155
tests/test_card.py
Normal file
@@ -0,0 +1,155 @@
|
||||
"""
|
||||
Tests for Card, Rank, and Suit classes
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from poker.card import Card, Rank, Suit
|
||||
|
||||
|
||||
class TestRank:
|
||||
"""Test cases for Rank enum"""
|
||||
|
||||
def test_rank_values(self):
|
||||
"""Test rank numeric values"""
|
||||
assert Rank.TWO.numeric_value == 2
|
||||
assert Rank.ACE.numeric_value == 14
|
||||
assert Rank.KING.numeric_value == 13
|
||||
assert Rank.JACK.numeric_value == 11
|
||||
|
||||
def test_rank_symbols(self):
|
||||
"""Test rank symbols"""
|
||||
assert Rank.TWO.symbol == '2'
|
||||
assert Rank.ACE.symbol == 'A'
|
||||
assert Rank.KING.symbol == 'K'
|
||||
assert Rank.TEN.symbol == 'T'
|
||||
|
||||
def test_rank_comparison(self):
|
||||
"""Test rank comparison operations"""
|
||||
assert Rank.TWO < Rank.THREE
|
||||
assert Rank.KING < Rank.ACE
|
||||
assert Rank.JACK > Rank.TEN
|
||||
assert Rank.ACE >= Rank.KING
|
||||
assert Rank.TWO <= Rank.TWO
|
||||
|
||||
def test_rank_str(self):
|
||||
"""Test string representation"""
|
||||
assert str(Rank.ACE) == 'A'
|
||||
assert str(Rank.KING) == 'K'
|
||||
assert str(Rank.TWO) == '2'
|
||||
|
||||
|
||||
class TestSuit:
|
||||
"""Test cases for Suit enum"""
|
||||
|
||||
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_str(self):
|
||||
"""Test string representation"""
|
||||
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(Rank.ACE, Suit.SPADES)
|
||||
assert card.rank == Rank.ACE
|
||||
assert card.suit == Suit.SPADES
|
||||
|
||||
def test_card_str(self):
|
||||
"""Test string representation"""
|
||||
card = Card(Rank.ACE, Suit.SPADES)
|
||||
assert str(card) == 'As'
|
||||
|
||||
card2 = Card(Rank.KING, Suit.HEARTS)
|
||||
assert str(card2) == 'Kh'
|
||||
|
||||
def test_card_equality(self):
|
||||
"""Test card equality"""
|
||||
card1 = Card(Rank.ACE, Suit.SPADES)
|
||||
card2 = Card(Rank.ACE, Suit.SPADES)
|
||||
card3 = Card(Rank.ACE, Suit.HEARTS)
|
||||
|
||||
assert card1 == card2
|
||||
assert card1 != card3
|
||||
|
||||
def test_card_comparison(self):
|
||||
"""Test card comparison (by rank)"""
|
||||
ace_spades = Card(Rank.ACE, Suit.SPADES)
|
||||
king_hearts = Card(Rank.KING, Suit.HEARTS)
|
||||
two_clubs = Card(Rank.TWO, Suit.CLUBS)
|
||||
|
||||
assert two_clubs < king_hearts
|
||||
assert king_hearts < ace_spades
|
||||
assert not (ace_spades < king_hearts)
|
||||
|
||||
def test_from_string_valid(self):
|
||||
"""Test creating card from valid string"""
|
||||
card = Card.from_string("As")
|
||||
assert card.rank == Rank.ACE
|
||||
assert card.suit == Suit.SPADES
|
||||
|
||||
card2 = Card.from_string("Kh")
|
||||
assert card2.rank == Rank.KING
|
||||
assert card2.suit == Suit.HEARTS
|
||||
|
||||
card3 = Card.from_string("2c")
|
||||
assert card3.rank == Rank.TWO
|
||||
assert card3.suit == Suit.CLUBS
|
||||
|
||||
card4 = Card.from_string("Td")
|
||||
assert card4.rank == Rank.TEN
|
||||
assert card4.suit == Suit.DIAMONDS
|
||||
|
||||
def test_from_string_invalid(self):
|
||||
"""Test creating card from invalid string"""
|
||||
with pytest.raises(ValueError):
|
||||
Card.from_string("A") # Too short
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Card.from_string("Asx") # Too long
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Card.from_string("Xs") # Invalid rank
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Card.from_string("Ax") # Invalid suit
|
||||
|
||||
def test_parse_cards_valid(self):
|
||||
"""Test parsing multiple cards from string"""
|
||||
cards = Card.parse_cards("AsKs AhAdAc6s7s")
|
||||
assert len(cards) == 7
|
||||
assert str(cards[0]) == "As"
|
||||
assert str(cards[1]) == "Ks"
|
||||
assert str(cards[2]) == "Ah"
|
||||
assert str(cards[6]) == "7s"
|
||||
|
||||
def test_parse_cards_with_spaces(self):
|
||||
"""Test parsing cards with various spacing"""
|
||||
cards = Card.parse_cards("As Ks Ah Ad Ac 6s 7s")
|
||||
assert len(cards) == 7
|
||||
assert str(cards[0]) == "As"
|
||||
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(self):
|
||||
"""Test parsing invalid card strings"""
|
||||
with pytest.raises(ValueError):
|
||||
Card.parse_cards("AsKs A") # Incomplete card
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Card.parse_cards("AsKs Ax") # Invalid suit
|
||||
Reference in New Issue
Block a user