-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck of Cards Testing.py
82 lines (67 loc) · 2.89 KB
/
Deck of Cards Testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from deck import Card
from deck import Deck
import unittest
class CardTests(unittest.TestCase):
def setUp(self):
self.card = Card("Hearts", "A")
def test_init(self):
"""cards should have a suit and a value"""
self.assertEqual(self.card.suit, "Hearts")
self.assertEqual(self.card.value, "A")
def test_repr(self):
"""repr should return string: 'VALUE of SUIT'"""
self.assertEqual(repr(self.card), "A of Hearts")
class DeckTests(unittest.TestCase):
def setUp(self):
self.deck = Deck()
def test_init(self):
"""decks should have a cards attribute, which is a list with 52 cards"""
self.assertTrue(self.deck.cards, list)
self.assertEqual(len(self.deck.cards), 52)
def test_repr(self):
"""repr should return string: 'Deck of 52 Cards'"""
self.assertEqual(repr(self.deck), "Deck of 52 cards.")
def test_count(self):
"""count should return a count of the number of cards in the Deck"""
self.assertEqual(self.deck.count(), 52)
self.deck.cards.pop()
self.assertEqual(self.deck.count(), 51)
def test_deal_sufficient_cards(self):
"""_deal should deal the number of cards specified"""
cards = self.deck._deal(10)
self.assertEqual(len(cards), 10)
self.assertEqual(self.deck.count(), 42)
def test_deal_insufficient_cards(self):
"""_deal should deal the number of cards left in the deck, no more"""
cards = self.deck._deal(100)
self.assertEqual(len(cards), 52)
self.assertEqual(self.deck.count(), 0)
def test_deal_no_cards(self):
"""_deal should throw ValueError if the deck is empty'"""
self.deck._deal(self.deck.count())
with self.assertRaises(ValueError):
self.deck._deal(1)
def test_deal_card(self):
"""deal_card should deal a single card from the deck"""
card = self.deck.cards[-1]
dealt_card = self.deck.deal_card()
self.assertEqual(card, dealt_card)
self.assertEqual(self.deck.count(), 51)
def test_deal_hand(self):
"""deal_hand should deal the number of cards passed into it"""
cards = self.deck.deal_hand(20)
self.assertEqual(len(cards), 20)
self.assertEqual(self.deck.count(), 32)
def test_shuffle_full_deck(self):
"""shuffle should shuffle the deck if deck is full"""
cards = self.deck.cards[:]
self.deck.shuffle()
self.assertNotEqual(cards, self.deck.cards)
self.assertEqual(self.deck.count(), 52)
def test_shuffle_not_full_deck(self):
"""shuffle should throw ValueError if the deck isn't full"""
self.deck._deal(1)
with self.assertRaises(ValueError):
self.deck.shuffle()
if __name__ == "__main__":
unittest.main()