115 lines
3.3 KiB
Python
115 lines
3.3 KiB
Python
"""
|
|
Tests for Card, Rank, and Suit classes
|
|
"""
|
|
|
|
import pytest
|
|
from poker.card import Card, Rank, Suit
|
|
|
|
# card.py测试
|
|
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
|
|
|
|
def test_rank_symbols(self):
|
|
"""Test rank symbols"""
|
|
assert Rank.TWO.symbol == '2'
|
|
assert Rank.ACE.symbol == 'A'
|
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
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_from_string_valid(self):
|
|
"""Test creating card from valid string"""
|
|
card = Card.createCard("As")
|
|
assert card.rank == Rank.ACE
|
|
assert card.suit == Suit.SPADES
|
|
|
|
card2 = Card.createCard("Kh")
|
|
assert card2.rank == Rank.KING
|
|
assert card2.suit == Suit.HEARTS
|
|
|
|
card3 = Card.createCard("2c")
|
|
assert card3.rank == Rank.TWO
|
|
assert card3.suit == Suit.CLUBS
|
|
|
|
card4 = Card.createCard("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.createCard("A") # Too short
|
|
|
|
with pytest.raises(ValueError):
|
|
Card.createCard("Asx") # Too long
|
|
|
|
with pytest.raises(ValueError):
|
|
Card.createCard("Xs") # Invalid rank
|
|
|
|
with pytest.raises(ValueError):
|
|
Card.createCard("Ax") # Invalid suit
|
|
|
|
def test_parse_cards_valid(self):
|
|
"""Test parsing multiple cards from string"""
|
|
cards = Card.parseCards("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.parseCards("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.parseCards("")
|
|
assert len(cards) == 0
|
|
|
|
cards = Card.parseCards(" ")
|
|
assert len(cards) == 0
|
|
|
|
def test_parse_cards_invalid(self):
|
|
"""Test parsing invalid card strings"""
|
|
with pytest.raises(ValueError):
|
|
Card.parseCards("AsKs A") # Incomplete card
|
|
|
|
with pytest.raises(ValueError):
|
|
Card.parseCards("AsKs Ax") # Invalid suit |