-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
executable file
·185 lines (172 loc) · 7.01 KB
/
tests.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
#
# Poker Cards
#
# Python module for working with poker cards and managing games.
#
# Copyright 2013 Michal Belica <[email protected]>
#
# This file is part of Poker Cards.
#
# Poker Cards is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Poker Cards is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Poker Cards. If not, see <http://www.gnu.org/licenses/>.
import random
import unittest
from collections import Counter
from pokercards import cards
from pokercards.logsetup import setup_console_logging, INFO
class TestCard(unittest.TestCase):
def setUp(self):
"""Create a list of a few cards for testing. Ordered by
descending rank."""
self.cards = [
cards.Card('KH'),
cards.Card('QC'),
cards.Card('JD'),
cards.Card('TS'),
cards.Card('9S'),
cards.Card('8S'),
]
def test_sort(self):
"""Test sorting of cards by rank"""
self.cards2 = self.cards[:] # copy list
random.shuffle(self.cards) # shuffle
self.cards.sort(reverse=True) # and sort back
self.assertEqual(self.cards, self.cards2)
def test_exceptions(self):
"""Test exceptions when creating cards of invalid rank/suit"""
self.assertRaises(ValueError, cards.Card, 'xH')
self.assertRaises(ValueError, cards.Card, 'Kx')
class TestDeck(unittest.TestCase):
def setUp(self):
"""Create a new deck for testing."""
self.deck = cards.Deck()
def test_pop(self):
"""Test popping and returning a card"""
stack = []
self.deck.shuffle()
# pop three cards
stack.append(self.deck.pop())
stack.append(self.deck.pop())
stack.append(self.deck.pop())
# check internal counts
self.assertEqual(len(self.deck.popped), 3)
self.assertEqual(len(self.deck.active), 52-3)
# see if the cards are in popped list
self.assertEqual(self.deck.popped, stack)
# return them to top of the deck
self.deck.return_popped(pos=cards.POS_TOP)
# check counts again
self.assertEqual(len(self.deck.popped), 0)
self.assertEqual(len(self.deck.active), 52)
# see if the cards really are on top of the deck
self.assertEqual(self.deck.active[-3:], stack)
def test_discard(self):
"""Test discarding and returning a card"""
stack = []
self.deck.shuffle()
# discard three cards
self.deck.discard()
stack.append(self.deck.discarded[-1])
self.deck.discard()
stack.append(self.deck.discarded[-1])
self.deck.discard()
stack.append(self.deck.discarded[-1])
# check internal counts
self.assertEqual(len(self.deck.discarded), 3)
self.assertEqual(len(self.deck.active), 52-3)
# see if the cards are in discarded list
self.assertEqual(self.deck.discarded, stack)
# return them to top of the deck
self.deck.return_discarded(pos=cards.POS_TOP)
# check counts again
self.assertEqual(len(self.deck.discarded), 0)
self.assertEqual(len(self.deck.active), 52)
# see if the cards really are on top of the deck
self.assertEqual(self.deck.active[-3:], stack)
class TestHand(unittest.TestCase):
def setUp(self):
"""Create some hands for testing, along with information on how
they should be evaluated. Ordered in descending strength."""
self.testhands = [
{
'hand': cards.PokerHand(
cards.Card.card_list('KC', 'QH', 'JH', 'TH', '9H', '8H', '7H')
),
'hand_rank': 8,
'hand_cards': cards.Card.card_list('QH', 'JH', 'TH', '9H', '8H'),
'kickers': [],
},{
'hand': cards.PokerHand(
cards.Card.card_list('7H', 'JS', 'QH', 'JD', 'JC', 'AH', 'JH')
),
'hand_rank': 7,
'hand_cards': cards.Card.card_list('JS', 'JH', 'JD', 'JC'),
'kickers': [cards.Card('AH')],
},{
'hand': cards.PokerHand(
cards.Card.card_list('5C', 'AS', '5H', '2H', '5D', 'AC', '7H')
),
'hand_rank': 6,
'hand_cards': cards.Card.card_list('5C', '5H', '5D', 'AS', 'AC'),
'kickers': [],
},{
'hand': cards.PokerHand(
cards.Card.card_list('4C', 'AS', '4H', '2H', '4D', '2C', '2H')
),
'hand_rank': 6,
'hand_cards': cards.Card.card_list('4C', '4H', '4D', '2H', '2C'),
'kickers': [],
},{
'hand': cards.PokerHand(
cards.Card.card_list('5C', 'AS', '5H', 'KS', '2D', 'KD', '7H')
),
'hand_rank': 2,
'hand_cards': cards.Card.card_list('5C', '5H', 'KS', 'KD'),
'kickers': cards.Card.card_list('AS'),
},{
'hand': cards.PokerHand(
cards.Card.card_list('5D', 'QS', '5S', 'KH', '2C', 'KC', '7H')
),
'hand_rank': 2,
'hand_cards': cards.Card.card_list('5D', '5S', 'KH', 'KC'),
'kickers': cards.Card.card_list('QS'),
},{
'hand': cards.PokerHand(
cards.Card.card_list('5S', '4S', 'QD', 'AH', '8S')
),
'hand_rank': 0,
'hand_cards': cards.Card.card_list('AH'),
'kickers': cards.Card.card_list('QD','8S','5S','4S'),
}]
def test_evaluation(self):
"""Evaluate the testing hands
and compare results to those entered manually"""
for testhand in self.testhands:
hand = testhand['hand']
self.assertEqual(hand.hand_rank, testhand['hand_rank'])
self.assertEqual(Counter(hand.hand_cards), Counter(testhand['hand_cards']))
self.assertEqual(Counter(hand.kickers), Counter(testhand['kickers']))
def test_comparison(self):
"""Test hand comparison and sorting"""
hands = [x['hand'] for x in self.testhands]
hands2 = hands[:]
random.shuffle(hands)
hands.sort(reverse=True)
self.assertEqual(hands2, hands)
if __name__ == '__main__':
setup_console_logging(level=INFO)
suite = unittest.TestSuite()
tl = unittest.TestLoader()
suite.addTests(map(tl.loadTestsFromTestCase, (TestCard, TestDeck, TestHand)))
unittest.TextTestRunner(verbosity=2).run(suite)