-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
848 lines (659 loc) · 33.7 KB
/
test.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
import unittest
import core.action as action
from core.player import Player
from core.game import GameState
class Actions(unittest.TestCase):
def setUp(self):
GameState.reset()
GameState.PlayerList = []
self.player = Player()
def test_Income(self):
player = self.player
status, response = player.play(action.Income)
self.assertEqual(player.coins, 3)
def test_ForeignAid(self):
player = self.player
status, response = player.play(action.ForeignAid)
self.assertEqual(player.coins, 4)
def test_Coup(self):
player = self.player
player2 = Player()
# test for no target
player.coins = 7
with self.assertRaises(action.TargetRequired):
status, response = player.play(action.Coup)
# test for no player having insufficient money
player.coins = 6
with self.assertRaises(action.NotEnoughCoins) as exc:
status, response = player.play(action.Coup, player2)
self.assertEqual(exc.exception.coinsNeeded, 7)
# test for targetting self
player.coins = 7
with self.assertRaises(action.TargetRequired):
status, response = player.play(action.Coup, player)
# test for succesful coup with opponent has 2 influence
player2.alive = True
player.coins = 7
status, response = player.play(action.Coup, player2)
self.assertTrue(status, response)
self.assertLess(player.coins, 7)
self.assertEqual(len(player2.influence), 1)
self.assertTrue(player2.alive)
# test for succesful coup with opponent has 1 influence
player2.alive = True
player.coins = 7
self.assertEqual(len(player2.influence), 1)
status, response = player.play(action.Coup, player2)
self.assertTrue(status, response)
self.assertLess(player.coins, 7)
self.assertEqual(len(player2.influence), 0)
self.assertFalse(player2.alive)
# test for coup against dead opponent
player2.alive = False
player.coins = 7
with self.assertRaises(action.DeadPlayer):
status, response = player.play(action.Coup, player2)
def test_Duke(self):
player = self.player
status, response = player.play(action.Duke)
self.assertEqual(player.coins, 5)
def test_Captain(self):
player = self.player
player2 = Player()
# test for no target
with self.assertRaises(action.TargetRequired):
status, response = player.play(action.Captain)
self.assertEqual(player.coins, 2)
self.assertEqual(player2.coins, 2)
# test for targeting self
with self.assertRaises(action.TargetRequired):
status, response = player.play(action.Captain, player)
# test for steal from player 2
player.play(action.Captain, player2)
self.assertEqual(player.coins, 4)
self.assertEqual(player2.coins, 0)
# test for steal with only one coin
player2.coins = 1
player.play(action.Captain, player2)
self.assertEqual(player.coins, 5)
self.assertEqual(player2.coins, 0)
def test_Contessa(self):
player = self.player
# using Contessa as an action
with self.assertRaises(action.BlockOnly):
status, response = player.play(action.Contessa)
# using Contessa as a block
class BlockWithContessa(Player):
def confirmBlock(self, activePlayer, opponentAction):
return action.Contessa
player2 = BlockWithContessa()
player.coins = 3
status, response = player.play(action.Assassin, player2)
self.assertEqual(len(player2.influence), 2)
def test_Assassin(self):
player = self.player
player2 = Player()
# test for no player having insufficient money
player.coins = 2
self.assertEqual(len(player2.influence), 2)
with self.assertRaises(action.NotEnoughCoins) as exc:
status, response = player.play(action.Assassin, player2)
self.assertEqual(exc.exception.coinsNeeded, 3)
self.assertEqual(len(player2.influence), 2)
self.assertEqual(player.coins, 2)
# test with sufficient money, no target
player.coins = 3
with self.assertRaises(action.TargetRequired):
status, response = player.play(action.Assassin)
self.assertEqual(len(player2.influence), 2)
# test with sufficient money against player with 2 influences
self.assertEqual(player.coins, 3)
status, response = player.play(action.Assassin, player2)
self.assertEqual(player.coins, 0)
self.assertEqual(len(player2.influence), 1)
# test with sufficient money against 1 influence and killing them
player.coins = 3
status, response = player.play(action.Assassin, player2)
self.assertEqual(player.coins, 0)
self.assertEqual(len(player2.influence), 0)
self.assertFalse(player2.alive)
def test_Ambassador(self):
class AmbassadorTester(Player):
def __init__(self, CardToPick):
self.CardToPick = CardToPick
Player.__init__(self)
def selectAmbassadorInfluence(self, choices, influenceRemaining):
return self.CardToPick
# test with player having two influence
player = AmbassadorTester([action.Duke, action.ForeignAid])
player.influence = [action.Income, action.ForeignAid]
GameState.Deck = [action.Duke, action.Ambassador]
status, response = player.play(action.Ambassador)
self.assertTrue(status, response)
self.assertIn(action.Duke, player.influence)
self.assertIn(action.ForeignAid, player.influence)
self.assertIn(action.Income, GameState.Deck)
self.assertIn(action.Ambassador, GameState.Deck)
# test with player having one influence
player = AmbassadorTester(action.Duke)
player.influence = [action.Income]
GameState.Deck = [action.Duke, action.Ambassador]
status, response = player.play(action.Ambassador)
self.assertIn(action.Duke, player.influence)
self.assertIn(action.Income, GameState.Deck)
self.assertIn(action.Ambassador, GameState.Deck)
# test duplicates
player = AmbassadorTester([action.Ambassador])
player.influence = [action.Ambassador]
GameState.Deck = [action.Ambassador, action.Ambassador]
status, response = player.play(action.Ambassador)
self.assertEqual(len(player.influence), 1)
self.assertEqual(len(GameState.Deck), 2)
self.assertEqual(player.influence[0], action.Ambassador)
self.assertEqual(GameState.Deck[0], action.Ambassador)
self.assertEqual(GameState.Deck[1], action.Ambassador)
class AmbassadorCheaterTester(Player):
def __init__(self, CardToPick):
self.CardToPick = CardToPick
Player.__init__(self)
def selectAmbassadorInfluence(self, choices, influenceRemaining):
return self.CardToPick
# test with player cheating by selecting a card that is not in the choices
player = AmbassadorCheaterTester([action.Contessa, action.Contessa])
player.influence = [action.Income, action.ForeignAid]
GameState.Deck = [action.Duke, action.Ambassador]
with self.assertRaises(action.InvalidTarget):
status, response = player.play(action.Ambassador)
# test with player cheating by having just one influence but selecting two
player = AmbassadorCheaterTester([action.Duke, action.Ambassador])
player.influence = [action.Income]
GameState.Deck = [action.Duke, action.Ambassador]
with self.assertRaises(action.InvalidTarget):
status, response = player.play(action.Ambassador)
def test_Ambassador_ComplexScenario(self):
# test where active player uses Ambassador, called by opponent, shows Ambassador,
# removes one influence by the opponent, active player's Ambassador card is shuffled
# into the deck, and the Ambassador action still passes
class AmbassadorComplexTester(Player):
def selectAmbassadorInfluence(self, choices, influenceRemaining):
return [action.Duke, action.Duke]
class AlwaysCallingPlayer(Player):
def confirmCall(self, activePlayer, action): return True
player = AmbassadorComplexTester()
player.influence = [action.Ambassador, action.Duke]
player2 = AlwaysCallingPlayer()
player2.influence = [action.Ambassador, action.Duke]
GameState.Deck = [action.Duke, action.Assassin]
def randomShuffle(deck): pass # does not shuffle
def randomSelector(deck): return deck[0] # select the first card in the deck
# change the random functions used by the Game State so we can test
GameState.randomShuffle = randomShuffle
GameState.randomSelector = randomSelector
status, response = player.play(action.Ambassador)
self.assertTrue(status, response)
self.assertEqual(len(player.influence), 2)
self.assertEqual(len(player2.influence), 1)
self.assertEqual(player.influence[0], action.Duke)
self.assertEqual(player.influence[1], action.Duke)
self.assertEqual(GameState.Deck[0], action.Assassin)
self.assertEqual(GameState.Deck[1], action.Ambassador)
class Players(unittest.TestCase):
def setUp(self):
GameState.reset()
GameState.PlayerList = []
self.player = Player()
def test_PlayerList(self):
self.assertEqual(len(GameState.PlayerList), 1)
self.assertIn(self.player, GameState.PlayerList)
def test_PlayerInitialState(self):
player = self.player
self.assertEqual(player.coins, 2)
self.assertTrue(player.alive)
def test_DeadPlayerPlaying(self):
""" test to make sure a dead player can't play an action """
player = self.player
player.alive = False
with self.assertRaises(action.DeadPlayer):
status, response = player.play(action.Income)
def test_DeadPlayerTarget(self):
""" test to make sure a dead player can't be targetted """
player = self.player
player2 = Player()
player2.alive = False
with self.assertRaises(action.DeadPlayer):
status, response = player.play(action.Captain, player2)
def test_CourtDeck(self):
"""
Tests related to the court deck found in GameState class.
Card Drawing
Drawing card from an empty deck (should raise MajorError)
Returning card to deck
"""
GameState.Deck = [action.Income]
card = GameState.DrawCard()
self.assertEqual(len(GameState.Deck), 0)
self.assertEqual(action.Income, card)
with self.assertRaises(action.MajorError):
card = GameState.DrawCard()
GameState.AddToDeck(action.ForeignAid)
self.assertEqual(len(GameState.Deck), 1)
self.assertIn(action.ForeignAid, GameState.Deck)
def test_PlayerChangeCard(self):
"""
Tests releated to the ChangeCard in Player class:
- player has two influences
- player has one influence
- player has no influence
"""
class FirstInfluenceDies(Player):
def selectInfluenceToDie(self):
return self.influence[0]
player = FirstInfluenceDies()
player.influence = [action.Income, action.ForeignAid]
# changing card that is not in the player's influence.
card = action.Duke
with self.assertRaises(BaseException):
player.changeCard(card)
# drawing from an empty deck. In normal play, this will never happen, but is tested nonetheless
GameState.Deck = []
player.changeCard(action.Income)
self.assertEqual(len(GameState.Deck), 0)
# one card in the deck.
GameState.Deck = [action.Duke]
player.changeCard(action.Income)
# because there's only one card in the deck, either the duke or income will be taken.
# todo: add code to take over the rng for testing purposes
self.assertTrue ((action.Duke in GameState.Deck) or (action.Income in GameState.Deck))
self.assertEqual(len(GameState.Deck), 1)
GameState.Deck = [action.Duke]
player.influence = [action.Income, action.ForeignAid]
player.loseInfluence()
self.assertEqual(len(player.influence), 1)
self.assertTrue(player.influence[0] == action.ForeignAid)
player.changeCard(player.influence[0])
# because there's only one card in the deck, either the duke or income will be taken.
# todo: add code to take over the rng for testing purposes
self.assertTrue ((action.Duke in GameState.Deck) or (action.ForeignAid in GameState.Deck))
self.assertEqual(len(GameState.Deck), 1)
# player should be dead
player.loseInfluence()
self.assertFalse(player.alive)
self.assertEqual(len(player.influence), 0)
with self.assertRaises(IndexError):
player.changeCard(player.influence[0])
def test_ForceCoup(self):
player = self.player
# test that player can't play any actions other than coup when holding more than action.ForceCoupCoins coins
player.coins = action.ForceCoupCoins
with self.assertRaises(action.ActionNotAllowed):
status, response = player.play(action.Income)
self.assertEqual(player.coins, action.ForceCoupCoins)
# test that player can play any actions other than coup when holding less than action.ForceCoupCoins coins
player.coins = action.ForceCoupCoins - 1
status, response = player.play(action.Income)
self.assertTrue(status, response)
self.assertEqual(player.coins, action.ForceCoupCoins)
# test that dead players can't play any actions other than coup when holding more than action.ForceCoupCoins coins
player.coins = action.ForceCoupCoins
player.alive = False
with self.assertRaises(action.DeadPlayer):
status, response = player.play(action.Income)
self.assertEqual(player.coins, action.ForceCoupCoins)
def test_RequestBlocksRotation(self):
"""
This tests that requests are performed in a clockwise rotation (http://boardgamegeek.com/article/18425206#18425206).
However, for the sake of game flow, the targetted player (if any) will be requested first.
"""
GameState.reset()
GameState.PlayerList = []
Order = []
class PlayerNumber(Player):
def __init__(self, position, Order):
self.position = position
self.Order = Order
Player.__init__(self)
def confirmBlock(self, activePlayer, opponentAction):
self.Order.append(self.position)
return None
player1 = PlayerNumber(1, Order)
player2 = PlayerNumber(2, Order)
player3 = PlayerNumber(3, Order)
player4 = PlayerNumber(4, Order)
player5 = PlayerNumber(5, Order)
player6 = PlayerNumber(6, Order)
status, response = player4.play(action.Captain, player3)
self.assertTrue(status, response)
self.assertEqual(Order, [3, 5, 6, 1, 2])
def test_RequestCallsRotation(self):
"""
This tests that requests are performed in a clockwise rotation (http://boardgamegeek.com/article/18425206#18425206).
However, for the sake of game flow, the targetted player (if any) will be requested first.
"""
GameState.reset()
GameState.PlayerList = []
Order = []
class PlayerNumber(Player):
def __init__(self, position, Order):
self.position = position
self.Order = Order
Player.__init__(self)
def confirmCall(self, activePlayer, action):
self.Order.append(self.position)
return False
player1 = PlayerNumber(1, Order)
player2 = PlayerNumber(2, Order)
player3 = PlayerNumber(3, Order)
player4 = PlayerNumber(4, Order)
player5 = PlayerNumber(5, Order)
player6 = PlayerNumber(6, Order)
status, response = player4.play(action.Captain, player3)
self.assertTrue(status, response)
self.assertEqual(Order, [3, 5, 6, 1, 2])
class BlockingSystem(unittest.TestCase):
def setUp(self):
GameState.reset()
GameState.PlayerList = []
self.player = Player()
class AlwaysBlockingPlayer(Player):
def __init__(self, CardUsedToBlock):
self.CardUsedToBlock = CardUsedToBlock
Player.__init__(self)
def confirmBlock(self, activePlayer, opponentAction):
return self.CardUsedToBlock
class NeverBlockingPlayer(Player):
def confirmBlock(self, activePlayer, opponentAction): return None
class AlwaysCallingPlayer(Player):
def confirmCall(self, activePlayer, action): return True
def test_getBlockingActions(self):
# Foreign Aid
blockers = GameState.getBlockingActions(action.ForeignAid)
self.assertIn(action.Duke, blockers)
# Captain
blockers = GameState.getBlockingActions(action.Captain)
self.assertIn(action.Captain, blockers)
self.assertIn(action.Ambassador, blockers)
# Assassin
blockers = GameState.getBlockingActions(action.Assassin)
self.assertIn(action.Contessa, blockers)
def test_SelfBlocking(self):
""" Make sure that player can't block themselves """
player = BlockingSystem.AlwaysBlockingPlayer(action.ForeignAid)
self.assertEqual(player.coins, 2)
status, response = player.play(action.ForeignAid)
self.assertNotEqual(player.coins, 2)
def test_BlockingAction(self):
""" Test if players can block """
#todo: use a mock object to create a mock action that is blockable
player = self.player
player_blocker = BlockingSystem.AlwaysBlockingPlayer(action.Duke)
self.assertIn(player_blocker, GameState.PlayerList)
self.assertEqual(player.coins, 2)
status, response = player.play(action.ForeignAid)
self.assertEqual(player.coins, 2)
self.assertFalse(status, response)
expectedMessage = "Blocked by %s" % player_blocker.name
self.assertEqual(response, expectedMessage)
def test_BlockingAction_NoResponse(self):
""" Test if players can block """
#todo: use a mock object to create a mock action that is blockable
player = self.player
player_nonblocker = BlockingSystem.NeverBlockingPlayer()
self.assertIn(player_nonblocker, GameState.PlayerList)
self.assertEqual(player.coins, 2)
status, response = player.play(action.ForeignAid)
self.assertEqual(player.coins, 4)
self.assertTrue(status)
def test_ValidBlockAction_Fail(self):
""" Only actions that can block active player's action can be used. This test checks that if an invalid block is used, nothing happens. """
player = self.player
player_blocker = BlockingSystem.AlwaysBlockingPlayer(action.ForeignAid)
self.assertEqual(player.coins, 2)
status, response = player.play(action.ForeignAid)
self.assertEqual(player.coins, 4)
self.assertTrue(status)
def test_CallBlockingActionAsBluff_Success(self):
"""
Opoosing player blocks action.
Active player calls bluff.
Opposing player is bluffing and should lose influence.
The active player's action should succeed.
"""
#todo: use a mock object to create a mock action that is blockable
player = BlockingSystem.AlwaysCallingPlayer()
player_blocker = BlockingSystem.AlwaysBlockingPlayer(action.Duke)
player_blocker.influence = [action.Income, action.Income]
self.assertEqual(len(player.influence), 2)
self.assertEqual(len(player_blocker.influence), 2)
self.assertEqual(player.coins, 2)
status, response = player.play(action.ForeignAid)
self.assertTrue(status)
self.assertEqual(player.coins, 4)
self.assertEqual(len(player.influence), 2)
self.assertEqual(len(player_blocker.influence), 1)
def test_CallBlockingActionAsBluff_Fail(self):
"""
Opoosing player blocks action.
Active player calls bluff.
Opposing player is telling the truth. Their card should be shuffled back and active player should lose influence.
The action should fail.
"""
#todo: use a mock object to create a mock action that is blockable
player = BlockingSystem.AlwaysCallingPlayer()
player_blocker = BlockingSystem.AlwaysBlockingPlayer(action.Duke)
player_blocker.influence = [action.Duke, action.Duke]
self.assertEqual(len(player.influence), 2)
self.assertEqual(len(player_blocker.influence), 2)
self.assertEqual(player.coins, 2)
status, response = player.play(action.ForeignAid)
self.assertFalse(status)
self.assertEqual(player.coins, 2)
self.assertEqual(len(player.influence), 1)
self.assertEqual(len(player_blocker.influence), 2)
def test_CallBlockingActionAsBluffWithOneInfluence_Fail(self):
"""
Opoosing player blocks action.
Active player, with one influence, calls bluff.
Opposing player is telling the truth. Their card should be shuffled back and active player should loses the game.
The action should fail.
"""
#todo: use a mock object to create a mock action that is blockable
player = BlockingSystem.AlwaysCallingPlayer()
player.influence = [action.Duke]
player_blocker = BlockingSystem.AlwaysBlockingPlayer(action.Duke)
player_blocker.influence = [action.Duke, action.Duke]
self.assertEqual(len(player.influence), 1)
self.assertEqual(len(player_blocker.influence), 2)
self.assertEqual(player.coins, 2)
status, response = player.play(action.ForeignAid)
self.assertFalse(status)
self.assertEqual(player.coins, 2)
self.assertFalse(player.alive)
self.assertEqual(len(player.influence), 0)
self.assertEqual(len(player_blocker.influence), 2)
def test_ChallengeFailAndPlayerShouldShuffleShownCard(self):
"""
Create a scenario where active player plays action, opposing player calls bluff, active player is telling the truth.
This will test that the active player's card is revealed, returned to the player deck and a new card is drawn.
This will also test if after returning the card, the action still plays
"""
#todo: use a mock object to create a mock action that is blockable
player = self.player
player.influence = [action.Income, action.Duke]
player_caller = BlockingSystem.AlwaysCallingPlayer()
def randomShuffle(deck): pass # does not shuffle
def randomSelector(deck): return deck[0] # select the first card in the deck
# change the random functions used by the Game State so we can test
GameState.randomShuffle = randomShuffle
GameState.randomSelector = randomSelector
GameState.Deck = [action.Income]
self.assertEqual(player.coins, 2)
status, response = player.play(action.Duke)
self.assertTrue(status, response)
self.assertEqual(player.coins, 5)
self.assertEqual(len(player.influence), 2)
self.assertEqual(len(player_caller.influence), 1)
# check that the winner of the challenge draw from the deck.
self.assertEqual(player.influence[0], action.Income)
self.assertEqual(player.influence[1], action.Income)
self.assertEqual(GameState.Deck[0], action.Duke)
def test_PlayerIsBlockedAndChallengeFailAndBlockerShouldShuffleShownCard(self):
"""
Create a scenario where active player plays action, opposing player blocks, active player calls and
opposing player is telling the truth.
This will test that the blocking player's card is revealed, returned to the player deck and a new card is drawn.
This will also test if after returning the card, the action is still blocked
"""
#todo: use a mock object to create a mock action that is blockable
player = BlockingSystem.AlwaysCallingPlayer()
player.influence = [action.Captain, action.Captain]
player_blocker = BlockingSystem.AlwaysBlockingPlayer(action.Captain)
player_blocker.influence = [action.Captain, action.Duke]
def randomShuffle(deck): pass # does not shuffle
def randomSelector(deck): return deck[0] # select the first card in the deck
# change the random functions used by the Game State so we can test
GameState.randomShuffle = randomShuffle
GameState.randomSelector = randomSelector
GameState.Deck = [action.Duke]
self.assertEqual(player.coins, 2)
status, response = player.play(action.Captain, player_blocker)
self.assertFalse(status, response)
self.assertEqual(player.coins, 2)
self.assertEqual(len(player.influence), 1)
self.assertEqual(len(player_blocker.influence), 2)
# check that the winner of the challenge draw from the deck.
self.assertEqual(player.influence[0], action.Captain)
self.assertEqual(player_blocker.influence[0], action.Duke)
self.assertEqual(player_blocker.influence[1], action.Duke)
self.assertEqual(GameState.Deck[0], action.Captain)
class PlayerCallToLoseAndCannotBlock(Player):
"""For testing of complex scenario in test_BlockingPlayerBluffIsCalledAndTheyLoseGame()"""
def confirmCall(self, activePlayer, action):
return True
def confirmBlock(self, activePlayer, opponentAction):
raise action.DeadPlayer
def test_PlayerCallToLoseAndCannotBlock(self):
"""
This test the following scenario:
1. Active player plays an action
2. Another player calls bluff but active player is telling the truth
3. Blocking player loses their last influence
4. Blocking player SHOULD NOT get the ability to block active player's action
"""
player = BlockingSystem.AlwaysCallingPlayer()
player_blocker = BlockingSystem.PlayerCallToLoseAndCannotBlock()
player.influence = [action.Captain, action.Captain]
player_blocker.influence = [action.Duke]
self.assertEqual(len(player_blocker.influence), 1)
status, response = player.play(action.Captain, player_blocker)
self.assertEqual(len(player_blocker.influence), 0)
self.assertFalse(player_blocker.alive)
# action.DeadPlayer should never be called. If it does, then step #4 of this scenario happened
class ActionBlocking(unittest.TestCase):
def setUp(self):
GameState.reset()
GameState.PlayerList = []
self.player = Player()
class AlwaysBlockingPlayer(Player):
def __init__(self, CardUsedToBlock):
self.CardUsedToBlock = CardUsedToBlock
Player.__init__(self)
def confirmBlock(self, activePlayer, opponentAction):
return self.CardUsedToBlock
class NeverBlockingPlayer(Player):
def confirmBlock(self, activePlayer, opponentAction): return None
class AlwaysCallingPlayer(Player):
def confirmCall(self, activePlayer, action): return True
def test_ForeignAid(self):
""" Test for players blocking foriegn aid """
#todo: use a mock object to create a mock action that is blockable
player = self.player
player_blocker = ActionBlocking.AlwaysBlockingPlayer(action.Duke)
self.assertEqual(player.coins, 2)
status, response = player.play(action.ForeignAid)
self.assertFalse(status, response)
self.assertEqual(player.coins, 2)
def test_Captain(self):
""" Test for players blocking stealing """
#todo: use a mock object to create a mock action that is blockable
player = self.player
player_blocker = ActionBlocking.AlwaysBlockingPlayer(action.Captain)
self.assertEqual(player.coins, 2)
status, response = player.play(action.Captain, player_blocker)
self.assertFalse(status, response)
self.assertEqual(player.coins, 2)
# todo: add tests for all cards
class CallBluff(unittest.TestCase):
def setUp(self):
GameState.reset()
GameState.PlayerList = []
self.player = Player()
class AlwaysCallingPlayer(Player):
def confirmCall(self, activePlayer, action): return True
class AlwaysBlockingPlayer(Player):
def __init__(self, CardUsedToBlock):
self.CardUsedToBlock = CardUsedToBlock
Player.__init__(self)
def confirmBlock(self, activePlayer, opponentAction):
return self.CardUsedToBlock
def test_SelfCalling(self):
""" Make sure that player can't call themselves as bluffers"""
class GenericCardThatCanBlockItself(action.Action):
name = "Generic Card That Can Block Itelf"
description = "For testing purposes"
def play(self, player, target = None):
player.coins += 1
return True, "Success"
player = CallBluff.AlwaysBlockingPlayer(GenericCardThatCanBlockItself)
self.assertEqual(player.coins, 2)
status, response = player.play(GenericCardThatCanBlockItself)
self.assertEqual(player.coins, 3)
def test_CallCommonAction(self):
""" Test to make sure that players shouldn't be able to call common actions as bluffs"""
player = self.player
player.giveCards(action.ForeignAid) #todo: add mock object for action
player_CallBluff = CallBluff.AlwaysCallingPlayer()
playedAction = action.Income
self.assertEqual(player.coins, 2)
status, response = player.play(playedAction)
self.assertEqual(player.coins, 3)
def test_CallActivePlayerBluff_Success(self):
""" Test if other players can call active player's bluff """
player = self.player
player.giveCards(action.Income) #todo: add mock object for action
self.assertEqual(len(player.influence), 2)
player_CallBluff = CallBluff.AlwaysCallingPlayer()
playedAction = action.Captain
self.assertEqual(player.coins, 2)
status, response = player.play(playedAction, player_CallBluff)
self.assertEqual(player.coins, 2)
self.assertEqual(len(player.influence), 1)
self.assertFalse(status, response)
expectedMessage = "Bluffing %s failed for %s" % (playedAction.name, player.name)
self.assertEqual(response, expectedMessage)
def test_CallActivePlayerBluff_Failed(self):
""" Test if other players can call active player's bluff """
player = self.player
player.giveCards(action.ForeignAid)
self.assertEqual(len(player.influence), 2)
player_CallBluff = CallBluff.AlwaysCallingPlayer()
playedAction = action.ForeignAid
self.assertEqual(player.coins, 2)
status, response = player.play(playedAction)
self.assertEqual(player.coins, 4)
self.assertEqual(len(player.influence), 2)
self.assertTrue(status)
def test_Assasinate_FailedContessaBluff(self):
""" Important rule test: An assasination attempt is done to opponent. Opponent bluff with Contessa. Active player calls bluff. The opposing player should lose. This will test for situations where the opposing player has two or one influence """
class ContessaBluffer(Player):
def confirmBlock(self, activePlayer, opponentAction): return action.Contessa
player = CallBluff.AlwaysCallingPlayer()
player2 = ContessaBluffer()
player2.influence = [action.Income, action.Income]
self.assertEqual(len(player2.influence), 2)
self.assertTrue(player2.alive)
player.coins = 3
status, response = player.play(action.Assassin, player2)
self.assertEqual(player.coins, 0)
self.assertEqual(len(player2.influence), 0)
if __name__ == "__main__":
unittest.main()