Skip to content

Instantly share code, notes, and snippets.

@bmihaylov
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmihaylov/9708072 to your computer and use it in GitHub Desktop.
Save bmihaylov/9708072 to your computer and use it in GitHub Desktop.
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())),
random.choice(list(SUITS.values())))
cards = set()
while len(cards) < count:
cards.add(Card(random.choice(list(RANKS.values())),
random.choice(list(SUITS.values()))))
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)
deck.add(card1)
self.assertEqual(deck[0], card1)
deck.add(card2)
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.assertIsNotNone(deck[card_count-1])
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)
deck.draw_from_top()
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__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment