From 619d9e52537f4734ed025639e349d9b8b38f5c29 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Wed, 9 Mar 2022 07:32:30 +0100 Subject: [PATCH] Acey Ducey (Python): Add unit tests The unit tests can be executed via: $ pip install pytest $ pytest . Also a few more type annotations --- 01_Acey_Ducey/python/acey_ducey_oo.py | 24 ++++++--------- 01_Acey_Ducey/python/test_acey_ducey.py | 19 ++++++++++++ 01_Acey_Ducey/python/test_acey_ducey_oo.py | 36 ++++++++++++++++++++++ 3 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 01_Acey_Ducey/python/test_acey_ducey.py create mode 100644 01_Acey_Ducey/python/test_acey_ducey_oo.py diff --git a/01_Acey_Ducey/python/acey_ducey_oo.py b/01_Acey_Ducey/python/acey_ducey_oo.py index d983c8b2..aa456fe9 100644 --- a/01_Acey_Ducey/python/acey_ducey_oo.py +++ b/01_Acey_Ducey/python/acey_ducey_oo.py @@ -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() diff --git a/01_Acey_Ducey/python/test_acey_ducey.py b/01_Acey_Ducey/python/test_acey_ducey.py new file mode 100644 index 00000000..d26ce790 --- /dev/null +++ b/01_Acey_Ducey/python/test_acey_ducey.py @@ -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" + ) diff --git a/01_Acey_Ducey/python/test_acey_ducey_oo.py b/01_Acey_Ducey/python/test_acey_ducey_oo.py new file mode 100644 index 00000000..12b85212 --- /dev/null +++ b/01_Acey_Ducey/python/test_acey_ducey_oo.py @@ -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