mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 06:43:06 -08:00
Acey Ducey (Python): Add unit tests
The unit tests can be executed via:
$ pip install pytest
$ pytest .
Also a few more type annotations
This commit is contained in:
@@ -11,35 +11,31 @@
|
||||
#
|
||||
######################################################
|
||||
|
||||
from typing import List
|
||||
from typing import List, Literal, TypeAlias, get_args
|
||||
|
||||
Suit: TypeAlias = Literal["\u2665", "\u2666", "\u2663", "\u2660"]
|
||||
Rank: TypeAlias = Literal[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
|
||||
|
||||
|
||||
class Card:
|
||||
def __init__(self, suit: str, rank: int):
|
||||
def __init__(self, suit: Suit, rank: Rank) -> None:
|
||||
self.suit = suit
|
||||
self.rank = rank
|
||||
|
||||
def __str__(self) -> str:
|
||||
r = str(self.rank)
|
||||
if r == "11":
|
||||
r = "J"
|
||||
elif r == "12":
|
||||
r = "Q"
|
||||
elif r == "13":
|
||||
r = "K"
|
||||
elif r == "14":
|
||||
r = "A"
|
||||
r = {"11": "J", "12": "Q", "13": "K", "14": "A"}.get(r, r)
|
||||
return f"{r}{self.suit}"
|
||||
|
||||
|
||||
class Deck:
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
self.cards: List[Card] = []
|
||||
self.build()
|
||||
|
||||
def build(self) -> None:
|
||||
for suit in ["\u2665", "\u2666", "\u2663", "\u2660"]:
|
||||
for rank in range(2, 15):
|
||||
for suit in get_args(Suit):
|
||||
for rank in get_args(Rank):
|
||||
self.cards.append(Card(suit, rank))
|
||||
|
||||
def shuffle(self) -> None:
|
||||
@@ -52,7 +48,7 @@ class Deck:
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
self.deck = Deck()
|
||||
self.deck.shuffle()
|
||||
self.card_a = self.deck.deal()
|
||||
|
||||
19
01_Acey_Ducey/python/test_acey_ducey.py
Normal file
19
01_Acey_Ducey/python/test_acey_ducey.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import io
|
||||
from unittest import mock
|
||||
|
||||
from acey_ducey import play_game
|
||||
|
||||
|
||||
@mock.patch("random.shuffle")
|
||||
def test_play_game_lose(mock_random_shuffle, monkeypatch, capsys) -> None:
|
||||
monkeypatch.setattr("sys.stdin", io.StringIO("100\n100"))
|
||||
mock_random_shuffle = lambda n: n
|
||||
play_game()
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == (
|
||||
"You now have 100 dollars\n\n"
|
||||
"Here are you next two cards\n King\n Ace\n\n"
|
||||
"What is your bet? Queen\n"
|
||||
"Sorry, you lose\n"
|
||||
"Sorry, friend, but you blew your wad\n"
|
||||
)
|
||||
36
01_Acey_Ducey/python/test_acey_ducey_oo.py
Normal file
36
01_Acey_Ducey/python/test_acey_ducey_oo.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from acey_ducey_oo import Card, Deck, Game
|
||||
|
||||
|
||||
def test_card_init() -> None:
|
||||
card = Card("\u2665", 2)
|
||||
assert card.suit == "\u2665"
|
||||
assert card.rank == 2
|
||||
|
||||
|
||||
def test_card_str() -> None:
|
||||
card = Card("\u2665", 2)
|
||||
assert str(card) == "2\u2665"
|
||||
|
||||
|
||||
def test_deck_init() -> None:
|
||||
deck = Deck()
|
||||
assert len(deck.cards) == 52
|
||||
assert deck.cards[0].suit == "\u2665"
|
||||
assert deck.cards[0].rank == 2
|
||||
|
||||
|
||||
def test_deck_shuffle() -> None:
|
||||
deck = Deck()
|
||||
deck.shuffle()
|
||||
|
||||
|
||||
def test_deck_deal() -> None:
|
||||
deck = Deck()
|
||||
card = deck.deal()
|
||||
assert card.rank == 14
|
||||
assert card.suit == "\u2660"
|
||||
|
||||
|
||||
def test_game_init() -> None:
|
||||
game = Game()
|
||||
assert len(game.deck.cards) == 50 # two are already dealt
|
||||
Reference in New Issue
Block a user