-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_ginplayer.py
294 lines (222 loc) · 10.2 KB
/
test_ginplayer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from ginmatch import *
import unittest
from test_ginstrategy import MockGinStrategy
from test_helpers import *
class MockListener:
def __init__(self):
self.did_it_knock = False
self.did_it_knock_gin = False
self.knocker = None
def notify_of_knock(self, knocker):
self.did_it_knock = True
self.knocker = knocker
def notify_of_knock_gin(self, knocker):
self.did_it_knock_gin = True
self.knocker = knocker
class MockGinMatch(object):
def __init__(self):
pass
# noinspection PyProtectedMember
class TestGinPlayer(Helper):
def setUp(self):
self.p1 = GinPlayer()
self.p2 = GinPlayer()
self.l = MockListener()
self.t = GinTable()
self.p1.sit_at_table(self.t)
self.gm = MockGinMatch()
def test_new_ginplayer(self):
# the guid shouldn't be a predictable number (this will fail 1 in 2**128 times)
self.assertNotEqual(123456, self.p1.id)
# has a GinStrategy
# self.assertIsInstance(self.p.strategy, GinStrategy, "Does not contain a GinStrategy")
# has a GinHand
self.assertIsInstance(self.p1.hand, GinHand, "Does not contain a GinHand")
# allows other classes to observe potential knocks
self.assertIsInstance(self.p1._knock_listeners, list, "Does not implement observer on knocks")
# allows other classes to observe potential gin calls
self.assertIsInstance(self.p1._knock_gin_listeners, list, "Does not implement observer on gins")
def test__register_knock_listener(self):
self.p1 = GinPlayer()
self.p1.register_knock_listener(self.l)
# verify the register happened
self.assertEqual(1, len(self.p1._knock_listeners))
# verify we cant register a second time
self.p1.register_knock_listener(self.l)
self.assertEqual(1, len(self.p1._knock_listeners))
# note that this also sufficiently tests knock()
def test__notify_knock_listeners(self):
self.p1.register_knock_listener(self.l)
dummy_card = GinCard(2, 'd')
self.p1._add_card(dummy_card)
# verify the knock callback occurs
self.p1.knock(dummy_card)
self.assertEqual(True, self.l.did_it_knock)
# verify we have the correct knocker
self.assertEqual(self.p1, self.l.knocker)
def test__register_knock_gin_listener(self):
self.p1.register_knock_gin_listener(self.l)
# verify the register happened
self.assertEqual(1, len(self.p1._knock_gin_listeners))
# verity we cannot register a second time
self.p1.register_knock_gin_listener(self.l)
self.assertEqual(1, len(self.p1._knock_gin_listeners))
# note that this also sufficiently tests knock_gin()
def test__notify_knock_gin_listener(self):
self.p1.register_knock_gin_listener(self.l)
dummy_card = GinCard(2, 'd')
self.p1._add_card(dummy_card)
# verify the knock callback occurs
self.p1.knock_gin(dummy_card)
self.assertEqual(True, self.l.did_it_knock_gin)
# verify we have the correct knocker
self.assertEqual(self.p1, self.l.knocker)
def test_sit_at_table(self):
# empty the table as set up in setUp:
self.p1.table = False
self.t.player1 = False
self.t.player2 = False
# sit down and ensure table is made of wood
self.p1.sit_at_table(self.t)
self.assertIsInstance(self.p1.table, GinTable, "Player not sitting at GinTable")
def test__add_card(self):
# verify empty hand
self.assertEqual(self.p1.hand.size(), 0)
# draw a card and verify length of hand is 1
self.p1._add_card(GinCard(9, 'c'))
self.assertEqual(self.p1.hand.size(), 1)
def test_organize_data(self):
c1 = GinCard(2, 'c')
c2 = GinCard(3, 'd')
self.p1._add_card(c1)
self.p1._add_card(c2)
data = self.p1.organize_data()
self.assertIn(c1.ranking(), data.values())
self.assertIn(c2.ranking(), data.values())
def test_draw(self):
self.assertEqual(self.p1.hand.size(), 0)
self.p1.draw()
self.assertEqual(self.p1.hand.size(), 1)
self.p1.draw()
self.assertEqual(self.p1.hand.size(), 2)
for i in range(9):
self.p1.draw()
self.assertEqual(self.p1.hand.size(), 11)
# ensure we cannot hold more than 11 cards
self.assertRaises(DrawException, self.p1.draw)
def test_consult_strategy(self):
strat = MockGinStrategy({'end': ['DISCARD', 0]})
self.p1.strategy = strat
# ensure the mock strategy gives us exactly one action to perform
self.assertEqual(False, self.p1.action)
self.p1.consult_strategy(phase='end')
self.assertEqual('DISCARD', self.p1.action[0])
self.assertEqual(0, self.p1.action[1])
def test_execute_strategy_discard(self):
strat = MockGinStrategy({'end': ['DISCARD', 0]})
self.p1.strategy = strat
# monkey-load a card into the player's hand
self.p1.hand.add_card(GinCard(4, 'c'))
# run the strategy, which should cause us to discard our first card
self.p1.consult_strategy(phase='end')
self.p1.execute_strategy()
self.assertEqual(0, self.p1.hand.size())
def test_execute_strategy_draw(self):
strat = MockGinStrategy({'start': ['DRAW']})
self.p1.strategy = strat
# monkey-patching in a table
# self.p.table = GinTable()
# ensure the player picks up the card from the top of the deck.
topcard = self.p1.table.deck.cards[-1]
self.assertEqual(0, self.p1.hand.size())
self.assertEqual(52, len(self.p1.table.deck.cards))
self.p1.consult_strategy(phase='start')
self.p1.execute_strategy()
self.assertEqual(1, self.p1.hand.size())
self.assertTrue(self.p1.hand.contains_card(topcard))
self.assertEqual(51, len(self.p1.table.deck.cards))
def test_execute_strategy_pickup_discard(self):
strat = MockGinStrategy({'start': ['PICKUP-FROM-DISCARD']})
self.p1.strategy = strat
# monkey-patching in a table and a discard pile of depth 3
# p.table = GinTable()
self.p1.table.discard_pile.append(self.p1.table.deck.cards.pop())
self.p1.table.discard_pile.append(self.p1.table.deck.cards.pop())
self.p1.table.discard_pile.append(self.p1.table.deck.cards.pop())
# ensure the player picks up the card from the top of the discard pile
topcard = self.p1.table.discard_pile[-1]
self.assertEqual(0, self.p1.hand.size())
self.assertEqual(49, len(self.p1.table.deck.cards))
self.p1.consult_strategy(phase='start')
self.p1.execute_strategy()
self.assertEqual(1, self.p1.hand.size())
self.assertTrue(self.p1.hand.contains_card(topcard))
self.assertEqual(49, len(self.p1.table.deck.cards))
def test_execute_strategy_knock(self):
self.p1.register_knock_listener(self.l)
strat = MockGinStrategy({'end': ['KNOCK', 0]})
self.p1.strategy = strat
# load up a fake hand. note these cards are chosen to be sorted into slots 0 and 1
card_0 = Card(4, 'c')
card_1 = Card(5, 'd')
self.p1.hand.add_card(card_0)
self.p1.hand.add_card(card_1)
# run strategy
self.p1.consult_strategy(phase='end')
self.p1.execute_strategy()
# verify we discard card 0 into the discard pile AND the listener receives a knock
self.assertEqual(1, self.p1.hand.size())
self.assertEqual(card_1, self.p1.hand.cards[0])
self.assertEqual(card_0, self.p1.table.discard_pile[0])
self.assertEqual(True, self.l.did_it_knock)
def test_execute_strategy_knock_gin(self):
self.p1.register_knock_gin_listener(self.l)
strat = MockGinStrategy({'end': ['KNOCK-GIN', 0]})
self.p1.strategy = strat
# load up a fake hand. note these cards are chosen to be sorted into slots 0 and 1
card_0 = Card(4, 'c')
card_1 = Card(5, 'd')
self.p1.hand.add_card(card_0)
self.p1.hand.add_card(card_1)
# run strategy
self.p1.consult_strategy(phase='end')
self.p1.execute_strategy()
# verify we discard card 0 into the discard pile AND the listener receives a knock
self.assertEqual(1, self.p1.hand.size())
self.assertEqual(card_1, self.p1.hand.cards[0])
self.assertEqual(1, len(self.p1.table.discard_pile))
self.assertEqual(card_0, self.p1.table.discard_pile[0])
self.assertEqual(True, self.l.did_it_knock_gin)
def test_take_turn(self):
strat = MockGinStrategy({'end': ['DRAW']})
self.p1.strategy = strat
self.p1.hand = GinHand()
# verify we reach an error if we do not have enough cards in hand
self.assertRaises(AssertionError, self.p1.take_turn)
def test_pickup_discard(self):
# monkey-patch a card into the discard pile
self.t.discard_pile.append(GinCard(4, 'c'))
# a player's hand should change after picking up a discard
size_before = self.p1.hand.size()
self.p1.pickup_discard()
size_after = self.p1.hand.size()
self.assertNotEqual(size_before, size_after)
self.assertTrue(self.p1.hand.contains(4, 'c'))
def test_discard_card(self):
# ensure our hand is empty
self.assertEqual(0, self.p1.hand.size())
# draw a card and ensure we're holding it
card = self.p1.draw()
self.assertEqual(1, self.p1.hand.size())
# discard it and ensure we have 0 cards in hand, and that it is on top of the discard pile
self.p1.discard_card(card)
self.assertEqual(0, self.p1.hand.size())
self.assertEqual(card, self.t.discard_pile.pop())
def test_accept_improper_knock(self):
# verify that we return whatever the mock strategy says to return
self.p1.strategy = MockGinStrategy({'start': ['WHATEVER']})
self.assertTrue(self.p1.accept_improper_knock())
def test_empty_hand(self):
self.p1.hand = self.generate_ginhand_from_card_data(self.card_data1)
self.p1.empty_hand()
self.assertEqual(0, len(self.p1.hand.cards))