Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 13:57
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Tests for the third homework of the Programming with Python course @ FMI
import unittest
import random
from solution import Card, RANKS, SUITS, CardCollection
from solution import StandardDeck, BeloteDeck, SixtySixDeck
Card.__hash__ = lambda self: 1
def random_cards(count=1):
if count == 1:
return Card(random.choice(list(RANKS.values())),
cards = set()
while len(cards) < count:
return cards
class CardTest(unittest.TestCase):
def test_card_instance(self):
aos = Card(RANKS["Ace"], SUITS["Spades"])
self.assertIsInstance(aos.rank, RANKS["Ace"])
self.assertIsInstance(aos.suit, SUITS["Spades"])
def test_card_equals(self):
aos1 = Card(RANKS["Ace"], SUITS["Spades"])
aos2 = Card(RANKS["Ace"], SUITS["Spades"])
self.assertEqual(aos1, aos2)
def test_suit_rank_equals(self):
aos = Card(RANKS["Ace"], SUITS["Spades"])
self.assertEqual(aos.rank, RANKS["Ace"]())
self.assertEqual(aos.suit, SUITS["Spades"]())
def test_to_string(self):
aos = Card(RANKS["Ace"], SUITS["Spades"])
self.assertEqual(str(aos), "Ace of Spades")
#### new tests ####
def test_immutability(self):
card = random_cards()
self.assertRaises(TypeError, card.__setattr__,
'rank', RANKS['Ace'])
self.assertRaises(TypeError, card.__delattr__,
'rank', RANKS['Ace'])
class CardCollectionTest(unittest.TestCase):
def setUp(self):
self.deck = [Card(rank, suit) for rank in RANKS.values()
for suit in SUITS.values()]
def test_standard_deck(self):
deck = CardCollection(self.deck)
self.assertEqual(len(deck), 52)
def test_deck_add(self):
deck = CardCollection()
card1, card2 = random_cards(2)
self.assertEqual(deck[0], card1)
self.assertEqual(deck[1], card2)
self.assertEqual(len(deck), 2)
def test_deck_draw(self):
deck = CardCollection(self.deck)
card = deck.top_card()
self.assertEqual(card, deck.draw_from_top())
self.assertEqual(len(deck), 51)
#### new tests ####
deck = CardCollection(random_cards(2))
card = deck.bottom_card()
self.assertEqual(card, deck.draw_from_bottom())
self.assertEqual(len(deck), 1)
def test_deck_iteration(self):
deck = CardCollection(self.deck)
for card in deck:
self.assertIsInstance(card, Card)
#### new tests ####
def test_indexing_overload(self):
deck = CardCollection(self.deck)
card_count = len(deck)
self.assertRaises(IndexError, deck.__getitem__, len(deck))
def test_index_function(self):
card = Card(RANKS['Queen'], SUITS['Clubs'])
deck = CardCollection((card,))
self.assertEqual(deck[0], card)
self.assertRaises(ValueError, deck.index, card)
#### new tests ####
class StandardDecksTest(unittest.TestCase):
def test_full_deck_len(self):
self.assertEqual(len(StandardDeck()), 52)
def test_belote_deck_len(self):
self.assertEqual(len(BeloteDeck()), 32)
def test_sixty_six_deck_len(self):
self.assertEqual(len(SixtySixDeck()), 24)
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment