/
test.py
359 lines (269 loc) · 12.5 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
import copy
import unittest
from dominion import Board, Card, Dominion, Phase, Player, Slot, Turn
from dominion import KingdomCard, TreasureCard, VictoryCard
class DominionTest(unittest.TestCase):
def setUp(self):
self.game = Dominion()
self.game.run()
def tearDown(self):
pass
def test_game_sets_starting_player(self):
self.assertIsInstance(self.game.starting_player, Player)
def test_game_has_two_players_if_none_specified(self):
self.assertEqual(2, len(self.game.players))
def test_game_has_specified_number_of_players(self):
game = Dominion(4)
self.assertEqual(4, len(game.players))
def test_game_cannot_have_more_than_four_players(self):
game = Dominion(20)
self.assertEqual(4, len(game.players))
def test_game_players_type_is_player_object(self):
for player in self.game.players:
self.assertIsInstance(player, Player)
def test_game_has_board(self):
self.assertIsInstance(self.game.board, Board)
def test_game_generates_turn_when_run(self):
turn = self.game.run()
self.assertIsInstance(turn, Turn)
class TurnTest(unittest.TestCase):
def setUp(self):
self.board = Board()
self.player = Player(self.board)
self.turn = Turn(self.player, self.board)
def tearDown(self):
pass
def test_turn_starts_with_one_action(self):
self.assertEqual(1, self.turn.actions)
def test_turn_starts_with_one_buy(self):
self.assertEqual(1, self.turn.buys)
def test_turn_has_three_phases(self):
self.assertEqual(3, len(self.turn.phases))
def test_turn_first_phase_is_action(self):
self.assertEqual('action', self.turn.phases[0].type)
def test_turn_second_phase_is_buy(self):
self.assertEqual('buy', self.turn.phases[1].type)
def test_turn_third_phase_is_cleanup(self):
self.assertEqual('cleanup', self.turn.phases[-1].type)
def test_action_phase_plays_action_card_if_one_available(self):
board = Board()
player = Player(board)
self.player.generate_hand()
player.current_hand.append(KingdomCard('Village'))
turn = Turn(player, board)
phase = Phase('action', player)
self.assertTrue(turn.take_phase(phase))
def test_action_phase_passes_if_no_action_cards_available(self):
phase = Phase('action', self.player)
self.assertFalse(self.turn.take_phase(phase))
def test_buy_phase_adds_card_to_player_discard(self):
phase = Phase('buy', self.player)
self.turn.take_phase(phase)
self.assertEqual(1, len(self.player.discard))
def test_buy_phase_removes_card_from_board(self):
self.board.slots = [Slot(TreasureCard('Copper'))]
phase = Phase('buy', self.player)
self.turn.take_phase(phase)
self.assertEqual(45, self.board.slots[0].num_cards)
class PlayerTest(unittest.TestCase):
def setUp(self):
self.board = Board()
self.player = Player(self.board)
def test_player_starts_with_three_victory_points(self):
self.assertEqual(3, self.player.victory_points)
def test_player_starts_with_ten_total_cards_in_deck(self):
self.assertEqual(10, len(self.player.deck))
def test_player_starts_with_seven_treasure_copper_cards(self):
self.assertEqual(7, len(self.player.get_cards_of_type('treasure')))
def test_player_starts_with_three_victory_estate_cards(self):
self.assertEqual(3, len(self.player.get_cards_of_type('victory')))
def test_player_starts_with_no_action_cards(self):
action_cards = self.player.get_cards_of_type('kingdom')
self.assertEqual([], action_cards)
def test_player_starts_not_as_starting_player(self):
self.assertFalse(self.player.is_starting)
def test_player_returns_action_card_if_one_available(self):
self.player.deck.append(KingdomCard('Village'))
self.assertIsInstance(
self.player.get_cards_of_type('kingdom')[0], KingdomCard)
def test_player_can_play_card_in_hand(self):
turn = Turn(self.player, self.board)
self.player.current_hand.append(KingdomCard('Village'))
self.player.play_card(self.player.current_hand[0], turn)
def test_player_cannot_play_card_not_in_hand(self):
turn = Turn(self.player, self.board)
self.player.generate_hand()
deck_card = KingdomCard('Village')
self.player.deck.append(deck_card)
self.assertRaises(Exception, self.player.play_card, deck_card, turn)
def test_player_generates_hand_of_five_cards(self):
self.player.generate_hand()
self.assertEqual(5, len(self.player.current_hand))
def test_player_cards_in_hand_not_in_deck(self):
self.player.generate_hand()
for card in self.player.current_hand:
self.assertNotIn(card, self.player.deck)
def test_player_discard_starts_empty(self):
self.assertEqual([], self.player.discard)
def test_player_discard_hand_empties_hand(self):
self.player.generate_hand()
self.player.discard_hand()
self.assertEqual([], self.player.current_hand)
def test_player_discard_hand_goes_to_discard(self):
self.player.generate_hand()
hand = copy.copy(self.player.current_hand)
self.player.discard_hand()
self.assertEqual(hand, self.player.discard)
def test_player_discards_card_after_playing_it(self):
turn = Turn(self.player, self.board)
self.player.generate_hand()
card_to_play = self.player.current_hand[0]
self.player.play_card(self.player.current_hand[0], turn)
self.assertIn(card_to_play, self.player.discard)
def test_player_can_buy_copper_treasure_card(self):
card = TreasureCard('Copper')
self.player.buy_card(card)
self.assertIn(card, self.player.discard)
def test_player_can_discard_copper_treasure_card(self):
self.player.generate_hand()
copper = [card for card in self.player.current_hand
if card.name == 'Copper'][0]
self.player.trash(copper)
self.assertIn(copper, self.player.board.trash)
def test_player_can_count_coins_in_current_hand(self):
self.player.current_hand = [TreasureCard('Copper'),
TreasureCard('Copper'),
TreasureCard('Copper'),
TreasureCard('Copper')]
self.assertEqual(len(self.player.current_hand),
self.player.count_coins_in_hand())
def test_player_chooses_purchase_if_one_option_available(self):
self.board.slots = [Slot(KingdomCard('Village'))]
self.player.current_hand = [TreasureCard('Copper'),
TreasureCard('Copper'),
TreasureCard('Copper'),
TreasureCard('Copper')]
self.assertIsInstance(self.player.determine_purchase(
self.board.display_cards()), KingdomCard)
class BoardTest(unittest.TestCase):
def setUp(self):
self.board = Board()
def test_board_defaults_to_two_players(self):
self.assertEqual(2, self.board.num_players)
def test_board_has_ten_slots_for_kingdom_cards(self):
self.assertEqual(10, len(self.board.kingdom_slots))
def test_board_has_three_treasure_card_slots(self):
self.assertEqual(3, len(self.board.treasure_slots))
def test_board_has_three_victory_card_slots(self):
self.assertEqual(3, len(self.board.victory_slots))
def test_kingdom_cards_in_board_slots_are_unique_to_board(self):
card_names = [x.card.name for x in self.board.kingdom_slots]
card_names_set = set(card_names)
self.assertEqual(len(card_names_set), len(card_names))
def test_board_has_trash_pile(self):
self.assertEqual([], self.board.trash)
def test_board_displays_available_cards(self):
all_cards = self.board.display_cards()
for card in all_cards:
self.assertIsInstance(card, Card)
class SlotTest(unittest.TestCase):
def setUp(self):
self.slot = Slot()
def test_board_slot_contains_card(self):
self.assertIsInstance(self.slot.card, Card)
def test_board_slot_default_card_type_is_kingdom(self):
self.assertEqual('kingdom', self.slot.card.type)
def test_board_treasure_slot_copper_has_card_type_treasure(self):
slot = Slot(TreasureCard('Copper'))
self.assertEqual('treasure', slot.card.type)
def test_board_victory_slot_estate_has_card_type_treasure(self):
slot = Slot(VictoryCard('Estate'))
self.assertEqual('victory', slot.card.type)
def test_board_slot_has_ten_cards_of_card_type_kingdom(self):
self.assertEqual(10, self.slot.num_cards)
def test_board_slot_has_forty_six_copper_cards_by_default(self):
slot = Slot(TreasureCard('Copper'))
self.assertEqual(46, slot.num_cards)
def test_board_slot_has_thirty_nine_copper_cards_if_three_players(self):
slot = Slot(TreasureCard('Copper'), 3)
self.assertEqual(39, slot.num_cards)
def test_board_slot_has_forty_silver_cards(self):
slot = Slot(TreasureCard('Silver'))
self.assertEqual(40, slot.num_cards)
def test_board_slot_has_thirty_gold_cards(self):
slot = Slot(TreasureCard('Gold'))
self.assertEqual(30, slot.num_cards)
def test_board_slot_has_eight_victory_cards_by_default(self):
slot = Slot(VictoryCard('Estate'))
self.assertEqual(8, slot.num_cards)
def test_board_slot_has_twelve_victory_cards_if_three_players(self):
slot = Slot(VictoryCard('Estate'), 3)
self.assertEqual(12, slot.num_cards)
class KingdomCardTest(unittest.TestCase):
def test_kingdom_card_type_is_kingdom(self):
card = KingdomCard()
self.assertEqual('kingdom', card.type)
def test_kingdom_card_village_has_name_village(self):
card = KingdomCard('Village')
self.assertEqual('Village', card.name)
def test_kingdom_card_village_has_cost(self):
card = KingdomCard('Village')
self.assertEqual(3, card.cost)
def test_kingdom_card_village_has_no_value(self):
card = KingdomCard('Village')
self.assertIsNone(card.value)
def test_kingdom_card_woodcutter_has_value(self):
card = KingdomCard('Woodcutter')
self.assertEqual(2, card.value)
def test_kingdom_card_village_has_two_actions(self):
card = KingdomCard('Village')
self.assertEqual(2, card.actions)
def test_kingom_card_village_has_actions_attribute(self):
card = KingdomCard('Village')
self.assertEqual(2, card.actions)
def test_kingom_card_woodcutter_has_no_actions_attribute(self):
card = KingdomCard('Woodcutter')
self.assertIsNone(card.actions)
def test_kingdom_card_woodcutter_has_buys_attribute(self):
card = KingdomCard('Woodcutter')
self.assertEqual(1, card.buys)
def test_kingom_card_village_has_no_buys_attribute(self):
card = KingdomCard('Village')
self.assertIsNone(card.buys)
def test_playing_kingdom_card_village_adds_two_actions_to_turn(self):
board = Board()
player = Player(board)
turn = Turn(player, board)
card = KingdomCard('Village')
card.play(turn)
self.assertEqual(3, turn.actions)
def test_playing_kingdom_card_woodcutter_adds_no_actions_to_turn(self):
board = Board()
player = Player(board)
turn = Turn(player, board)
card = KingdomCard('Woodcutter')
card.play(turn)
self.assertEqual(1, turn.actions)
def test_playing_kingdom_card_village_adds_card_to_hand(self):
board = Board()
player = Player(board)
turn = Turn(player, board)
card = KingdomCard('Village')
card.play(turn)
self.assertEqual(1, len(turn.player.current_hand))
def test_playing_kingdom_card_smithy_adds_three_cards_to_hand(self):
board = Board()
player = Player(board)
turn = Turn(player, board)
card = KingdomCard('Smithy')
card.play(turn)
self.assertEqual(3, len(turn.player.current_hand))
def test_playing_kingdom_card_woodcutter_adds_no_cards_to_hand(self):
board = Board()
player = Player(board)
turn = Turn(player, board)
card = KingdomCard('Woodcutter')
card.play(turn)
self.assertEqual(0, len(turn.player.current_hand))
if __name__ == '__main__':
unittest.main()