From ed494ec8b2c5511731632fa1c8d5e5abdf5f5289 Mon Sep 17 00:00:00 2001 From: cerberus Date: Tue, 28 Oct 2025 21:04:39 +0100 Subject: [PATCH] added tests --- pyproject.toml | 4 ++++ tests/__init__.py | 0 tests/test_card.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_deck.py | 26 +++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_card.py create mode 100644 tests/test_deck.py diff --git a/pyproject.toml b/pyproject.toml index e69de29..1991d97 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -0,0 +1,4 @@ +[tool.pytest.ini_options] + +pythonpath="src" +testpaths="tests" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_card.py b/tests/test_card.py new file mode 100644 index 0000000..ba384d0 --- /dev/null +++ b/tests/test_card.py @@ -0,0 +1,52 @@ +import pytest + +from pyjack.card import Card + + +@pytest.fixture +def cards(): + cards = [ + Card("diamonds", "2"), + Card("hearts", "A"), + Card("clubs", "9"), + Card("spades", "Q"), + ] + return cards + + +@pytest.fixture +def expected_values(): + values = [2, 11, 9, 10] + return values + + +@pytest.fixture +def expected_strings(): + strings = [ + "2 of diamonds", + "A of hearts", + "9 of clubs", + "Q of spades", + ] + return strings + + +def test_get_value(cards, expected_values): + """tests if the correct value is returned by the function""" + results = [] + for card in cards: + results.append(card.get_value()) + + # The zip() function takes multiple iterable objects (like lists + # or tuples) and combines them element-wise, creating an iterator + # of tuples. Each tuple contains one element from each input + # iterable at the corresponding index. + # It effectively allows for parallel iteration. + for expected_value, result in zip(expected_values, results): + assert expected_value == result + + +def test_default_return_value(cards, expected_strings): + """Tests if the __str__ method returns the correct value""" + + assert expected_strings == [card.__str__() for card in cards] diff --git a/tests/test_deck.py b/tests/test_deck.py new file mode 100644 index 0000000..56fbbdd --- /dev/null +++ b/tests/test_deck.py @@ -0,0 +1,26 @@ +import pytest +from pyjack.deck import Deck + + +@pytest.fixture +def default_deck() -> Deck: + deck = Deck() + return deck + + +def test_deck_init(default_deck): + """Tests if the right amount of cards is created""" + assert len(default_deck.cards) == 52 + + +def test_shuffle_deck(default_deck): + """Tests if the deck is randomized after method call""" + assert default_deck != default_deck.shuffle() + + +def test_card_dealing(default_deck): + """Tests if the a card is correctly removed from the deck.""" + default_deck.shuffle() + removed_card = default_deck.deal_card() + assert len(default_deck.cards) == 51 + assert removed_card is not None