task1
This commit is contained in:
@@ -5,37 +5,19 @@ 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
|
||||
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:
|
||||
@@ -47,11 +29,7 @@ class TestSuit:
|
||||
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:
|
||||
@@ -71,60 +49,42 @@ class TestCard:
|
||||
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")
|
||||
card = Card.createCard("As")
|
||||
assert card.rank == Rank.ACE
|
||||
assert card.suit == Suit.SPADES
|
||||
|
||||
card2 = Card.from_string("Kh")
|
||||
|
||||
card2 = Card.createCard("Kh")
|
||||
assert card2.rank == Rank.KING
|
||||
assert card2.suit == Suit.HEARTS
|
||||
|
||||
card3 = Card.from_string("2c")
|
||||
|
||||
card3 = Card.createCard("2c")
|
||||
assert card3.rank == Rank.TWO
|
||||
assert card3.suit == Suit.CLUBS
|
||||
|
||||
card4 = Card.from_string("Td")
|
||||
|
||||
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.from_string("A") # Too short
|
||||
Card.createCard("A") # Too short
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Card.from_string("Asx") # Too long
|
||||
Card.createCard("Asx") # Too long
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Card.from_string("Xs") # Invalid rank
|
||||
Card.createCard("Xs") # Invalid rank
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Card.from_string("Ax") # Invalid suit
|
||||
Card.createCard("Ax") # Invalid suit
|
||||
|
||||
def test_parse_cards_valid(self):
|
||||
"""Test parsing multiple cards from string"""
|
||||
cards = Card.parse_cards("AsKs AhAdAc6s7s")
|
||||
cards = Card.parseCards("AsKs AhAdAc6s7s")
|
||||
assert len(cards) == 7
|
||||
assert str(cards[0]) == "As"
|
||||
assert str(cards[1]) == "Ks"
|
||||
@@ -133,23 +93,23 @@ class TestCard:
|
||||
|
||||
def test_parse_cards_with_spaces(self):
|
||||
"""Test parsing cards with various spacing"""
|
||||
cards = Card.parse_cards("As Ks Ah Ad Ac 6s 7s")
|
||||
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.parse_cards("")
|
||||
cards = Card.parseCards("")
|
||||
assert len(cards) == 0
|
||||
|
||||
cards = Card.parse_cards(" ")
|
||||
|
||||
cards = Card.parseCards(" ")
|
||||
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
|
||||
|
||||
Card.parseCards("AsKs A") # Incomplete card
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Card.parse_cards("AsKs Ax") # Invalid suit
|
||||
Card.parseCards("AsKs Ax") # Invalid suit
|
||||
Reference in New Issue
Block a user