-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
716 lines (583 loc) · 35.3 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
__author__ = 'wwagner'
import unittest
import unittest.mock
import Thud
import json
class BoardTest(unittest.TestCase):
def setUp(self):
self.test_board = Thud.Board()
def test_initial_piece_setup(self):
# Arrayed identically to board
dwarf_positions = [(5, 14), (6, 14), (8, 14), (9, 14),
(4, 13), (10, 13),
(3, 12), (11, 12),
(2, 11), (12, 11),
(1, 10), (13, 10),
(0, 9), (14, 9),
(0, 8), (14, 8),
(0, 6), (14, 6),
(0, 5), (14, 5),
(1, 4), (13, 4),
(2, 3), (12, 3),
(3, 2), (11, 2),
(4, 1), (10, 1),
(5, 0), (6, 0), (8, 0), (9, 0)
]
for coordinates in dwarf_positions:
row = coordinates[0]
column = coordinates[1]
self.assertEqual(self.test_board.squares[row][column].type, 'Dwarf')
# Arrayed identically to board
troll_positions = [(6, 8), (7, 8), (8, 8),
(6, 7), (8, 7),
(6, 6), (7, 6), (8, 6)]
for coordinates in troll_positions:
row = coordinates[0]
column = coordinates[1]
self.assertEqual(self.test_board.squares[row][column].type, 'Troll')
def test_getitem(self):
self.assertEqual(self.test_board[7][7], 0)
def test_setitem(self):
self.assertEqual(self.test_board[7][7], 0)
self.test_board[7][7] = 12345
self.assertEqual(self.test_board[7][7], 12345)
def test_get_piece_success(self):
x = 6
y = 7
test_piece = self.test_board[x][y]
self.assertEqual(self.test_board.get_piece(x, y), test_piece)
def test_get_piece_failure(self):
x = 4
y = 7
self.assertFalse(self.test_board.get_piece(x, y))
def test_set_square_success(self):
self.assertEqual(self.test_board.squares[4][7], '4,7')
self.test_board.set_square(4, 7, '1,1')
self.assertEqual(self.test_board.squares[4][7], '1,1')
def test_move_piece(self):
start_x = 6
start_y = 7
destination_x = 4
destination_y = 7
test_piece = self.test_board.squares[start_x][start_y]
self.test_board.move_piece_on_board(test_piece, 4, 7)
self.assertEqual(self.test_board.squares[destination_x][destination_y], test_piece)
self.assertEqual(self.test_board.squares[start_x][start_y], (str(start_x) + ',' + str(start_y)))
def test_initialize_unit_list(self):
self.assertEqual(len(self.test_board.units), 40)
def test_capture_piece(self):
test_troll = self.test_board.get_piece(6, 6)
self.test_board.capture_piece(test_troll)
self.assertFalse(self.test_board.get_piece(6, 6))
self.assertEqual(self.test_board.squares[6][6], '6,6')
def test_move_piece_on_board(self):
test_dwarf = self.test_board.get_piece(5, 0)
self.test_board.move_piece_on_board(test_dwarf, 5, 1)
self.assertEqual(self.test_board.get_piece(5, 1), test_dwarf)
def test_generate_piece_id(self):
self.assertEqual(self.test_board.generate_piece_id(), 41)
self.assertEqual(self.test_board.generate_piece_id(), 42)
self.assertEqual(self.test_board.generate_piece_id(), 43)
class GameManagerTest(unittest.TestCase):
class MockSocket(object):
def send_message(self, message):
self.message = message
return True
def setUp(self):
self.test_game_manager = Thud.GameManager()
self.game_token, self.player_one_token, self.player_two_token = self.test_game_manager.start_game(
'test_one', 'test_two'
)
self.socket_one = self.MockSocket()
self.socket_two = self.MockSocket()
def array_data_helper(self, live_data, test_data, test_object=""):
if test_object:
test_data = json.loads(test_data)[test_object]
for row in live_data:
self.assertEqual(live_data[row], test_data[row])
def dwarf_move_helper(self, start, destination):
return {"game": self.game_token,
"player": self.player_one_token,
"start": start,
"destination": destination}
def troll_move_helper(self, start, destination):
return {"game": self.game_token,
"player": self.player_two_token,
"start": start,
"destination": destination}
def get_unit_helper(self, x, y):
return self.test_game_manager.active_games[self.game_token].board.get_piece(x, y)
def test_create_identical_game_names(self):
game_token_2, player_one_token_2, player_two_token_2 = self.test_game_manager.start_game(
'test_one', 'test_two'
)
self.assertFalse(self.game_token == game_token_2)
def test_report_game_state(self):
with open("api_test.json") as file:
test_data = file.read()
live_data = self.test_game_manager.report_game_state(self.game_token)
self.array_data_helper(live_data, test_data, "base state")
def test_process_input_start_game(self):
test_start = {"game": "begin",
"player": "null",
"start": "null",
"destination": "null",
"player_one": "Will",
"player_two": "Tom"}
data = self.test_game_manager.process_start(test_start)
player_one = data["player_one"]
player_two = data["player_two"]
self.assertTrue(data)
self.assertTrue(player_one)
self.assertTrue(player_two)
def test_process_move_failure_dwarf_player_moves_troll(self):
test_move = {"game": self.game_token, "player": self.player_one_token,
"start": [6, 6], "destination": [5, 6]}
self.assertFalse(self.test_game_manager.process_move(test_move))
def test_process_move_success_dwarf_first_move(self):
test_move = {"game": self.game_token, "player": self.player_one_token,
"start": [5, 0], "destination": [5, 13]}
self.assertTrue(self.test_game_manager.process_move(test_move))
def test_process_move_failure_bad_game_token(self):
test_malformed = {"game": "badtoken", "player": "badtoken", "start": [1, 1], "destination": [1, 2]}
self.assertFalse(self.test_game_manager.process_move(test_malformed))
def test_process_move_failure_missing_attributes(self):
test_malformed = {"player": "badtoken", "start": [1, 1], "destination": [1, 2]}
# the second half of the error is random because it's pulling a dict, so only check the non-random part
self.assertEqual(self.test_game_manager.process_move(test_malformed)[:31], "Bad JSON data 'game' as part of")
def test_process_input_success_troll_group_attack(self):
# move a troll near a group of dwarves
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 0), (9, 1))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((6, 6), (6, 5))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 1), (9, 0))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((6, 5), (6, 4))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 0), (9, 1))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((6, 4), (6, 3))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 1), (9, 0))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((6, 3), (6, 2))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 0), (9, 1))))
# capture the dwarves with the troll
expected_captures = [(4, 1), (5, 0), (6, 0)]
self.assertEqual(self.test_game_manager.process_move(self.troll_move_helper((6, 2), (5, 1))),
expected_captures)
def test_process_input_success_dwarf_attack(self):
# move some dwarves and trolls to setup toss
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((10, 13), (8, 13))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((7, 8), (8, 9))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((11, 12), (8, 12))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((7, 6), (8, 5))))
# capture a troll with a dwarf
self.assertEqual(self.test_game_manager.process_move(self.dwarf_move_helper((8, 12), (8, 9))), [(8, 9)])
def test_modified_game_state(self):
test_move = {"game": self.game_token, "player": self.player_one_token,
"start": [5, 0], "destination": [5, 13]}
with open("api_test.json") as file:
test_data = file.read()
# guard test to make the move and ensure it was successful
self.assertTrue(self.test_game_manager.process_move(test_move))
live_data = self.test_game_manager.report_game_state(self.game_token)
self.array_data_helper(live_data, test_data, "first move")
def test_process_move_failure_troll_moves_first(self):
test_move = {"game": self.game_token, "player": self.player_one_token,
"start": [6, 6], "destination": [5, 6]}
with open("api_test.json") as file:
test_data = file.read()
self.assertFalse(self.test_game_manager.process_move(test_move))
def test_troll_move_failure(self):
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((10, 13), (8, 13))))
self.assertFalse(self.test_game_manager.process_move(self.troll_move_helper((8, 8), (7, 10))))
def test_troll_attack_failure_invalid_toss(self):
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((10, 13), (8, 13))))
self.assertFalse(self.test_game_manager.process_move(self.troll_move_helper((6, 6), (6, 1))))
def test_process_move_test_move(self):
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((6, 0), (6, 5)), test=True))
self.assertEqual(self.test_game_manager.active_games[self.game_token].move_history, [])
def test_process_move_test_attack(self):
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((6, 0), (6, 4))))
moves = self.test_game_manager.active_games[self.game_token].move_history
self.assertEqual(self.test_game_manager.process_move(self.troll_move_helper((6, 6), (6, 5)), test=True),
[(6, 4)])
self.assertEqual(self.test_game_manager.active_games[self.game_token].move_history, moves)
def test_process_input_test_troll_attack(self):
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 0), (9, 1))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((6, 6), (6, 5))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 1), (9, 0))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((6, 5), (6, 4))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 0), (9, 1))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((6, 4), (6, 3))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 1), (9, 0))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((6, 3), (6, 2))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((9, 0), (9, 1))))
board_state = self.test_game_manager.report_game_state(self.game_token)
expected_captures = [(4, 1), (5, 0), (6, 0)]
self.assertEqual(self.test_game_manager.process_move(self.troll_move_helper((6, 2), (5, 1)), test=True),
expected_captures)
self.assertEqual(board_state, self.test_game_manager.report_game_state(self.game_token))
def test_process_input_test_dwarf_toss(self):
# move some dwarves and trolls to setup toss
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((10, 13), (8, 13))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((7, 8), (8, 9))))
self.assertTrue(self.test_game_manager.process_move(self.dwarf_move_helper((11, 12), (8, 12))))
self.assertTrue(self.test_game_manager.process_move(self.troll_move_helper((7, 6), (8, 5))))
# capture a troll with a dwarf
board_state = self.test_game_manager.report_game_state(self.game_token)
self.assertEqual(self.test_game_manager.process_move(self.dwarf_move_helper((8, 12), (8, 9)), test=True),
[(8, 9)])
self.array_data_helper(self.test_game_manager.report_game_state(self.game_token), board_state)
def test_save_load(self):
self.test_game = self.test_game_manager
initial = self.log_game()
del self.test_game.active_games['test_onetest_two1']
self.test_game.load_game('test_onetest_two1')
ini_load = self.log_game()
self.player_one = self.test_game.active_games['test_onetest_two1'].player_one
self.player_two = self.test_game.active_games['test_onetest_two1'].player_two
self.test_game.process_move({"game": 'test_onetest_two1', "player": self.player_one.token,
"start": [3, 2], "destination": [3, 3]})
self.test_game.process_move({"game": 'test_onetest_two1', "player": self.player_two.token,
"start": [6, 6], "destination": [5, 5]})
# Save this data, should be the same as move_load
load_check = self.log_game()
self.test_game.process_move({"game": 'test_onetest_two1', "player": self.player_one.token,
"start": [0, 5], "destination": [2, 5]})
test_troll = self.test_game.active_games['test_onetest_two1'].board.get_piece(5, 5)
self.test_game.active_games['test_onetest_two1'].board.capture_piece(test_troll)
first_move = self.log_game()
del self.test_game.active_games['test_onetest_two1']
self.test_game.load_game('test_onetest_two1')
first_move_load = self.log_game()
self.test_game.process_move({"game": 'test_onetest_two1', "player": self.player_one.token,
"start": [3, 2], "destination": [3, 3]})
self.test_game.process_move({"game": 'test_onetest_two1', "player": self.player_two.token,
"start": [6, 6], "destination": [5, 5]})
self.test_game.process_move({"game": 'test_onetest_two1', "player": self.player_one.token,
"start": [0, 5], "destination": [2, 5]})
test_troll = self.test_game.active_games['test_onetest_two1'].board.get_piece(5, 5)
self.test_game.active_games['test_onetest_two1'].board.capture_piece(test_troll)
second_move = self.log_game()
for x in range(0, len(initial)):
self.assertTrue(initial[x] == ini_load[x])
for x in range(0, len(initial)):
self.assertTrue(load_check[x] == first_move_load[x])
for x in range(0, len(initial)):
self.assertTrue(first_move[x] == second_move[x])
def log_game(self):
# Saves game state
game = self.test_game.active_games['test_onetest_two1']
game_array = [json.dumps(game.move_history),str(game.player_one.name), str(game.player_one.token), str(game.player_one.race), str(game.player_two.name), str(game.player_two.token),
str(game.player_two.race),str(game.board.squares),{}]
for unit in game.board.units:
game_array[8][str(unit.id)] = [str(unit.type), str(unit.x), str(unit.y), json.dumps(unit.moves), str(unit.status)]
return game_array
def test_add_match_player(self):
self.assertEqual(self.test_game_manager.free_players, {})
self.test_game_manager.add_match_player("Will", self.socket_one)
self.assertEqual(self.test_game_manager.free_players, {"Will": self.socket_one})
self.test_game_manager.add_match_player("Tom", self.socket_two)
self.assertEqual(self.test_game_manager.free_players, {"Will": self.socket_one, "Tom": self.socket_two})
def test_report_free_players(self):
self.assertEqual(self.test_game_manager.report_free_players(), [])
self.test_game_manager.add_match_player("Will", self.socket_one)
self.assertEqual(self.test_game_manager.report_free_players(), ["Will"])
self.test_game_manager.add_match_player("Tom", self.socket_two)
for player in self.test_game_manager.report_free_players():
self.assertTrue(player in ["Will", "Tom"])
def test_add_player_to_server(self):
self.assertEqual(self.test_game_manager.add_player_to_server("Will", self.socket_one), [])
self.assertEqual(self.test_game_manager.add_player_to_server("Tom", self.socket_two), ["Will"])
def test_assign_sockets_both_players(self):
# mock including the sockets in the free players list
self.test_game_manager.free_players["Will"] = self.socket_one
self.test_game_manager.free_players["Tom"] = self.socket_two
self.test_game_manager.assign_sockets(self.game_token, "Will", "Tom")
will_socket = self.test_game_manager.active_games[self.game_token].player_one.websocket
tom_socket = self.test_game_manager.active_games[self.game_token].player_two.websocket
self.assertEqual(will_socket, self.socket_one)
self.assertEqual(tom_socket, self.socket_two)
def test_assign_team(self):
test_value = self.test_game_manager.assign_team()
self.assertTrue(("Troll", "Dwarf") == test_value or ("Dwarf", "Troll") == test_value)
def test_process_match_start(self):
self.test_game_manager.add_match_player("Will", self.socket_one)
self.test_game_manager.add_match_player("Tom", self.socket_two)
player_one_response = self.test_game_manager.process_match_start("Tom", "Will")
player_one_game_token = player_one_response["game"]
player_one_game = self.test_game_manager.active_games[player_one_game_token]
self.assertEqual(player_one_response["game"], player_one_game_token)
self.assertEqual(player_one_response["board"], self.test_game_manager.report_game_state(player_one_game_token))
self.assertEqual(player_one_response["player"], player_one_game.player_two.token)
self.assertTrue(player_one_response["race"] in ("Troll", "Dwarf"))
player_two_response = self.socket_two.message["start"]
player_two_game_token = player_two_response["game"]
self.assertEqual(player_one_game_token, player_two_game_token)
player_two_game = self.test_game_manager.active_games[player_two_game_token]
self.assertEqual(player_one_game, player_two_game)
self.assertEqual(player_two_response["board"], self.test_game_manager.report_game_state(player_two_game_token))
self.assertTrue(player_two_response["race"] in ("Troll", "Dwarf"))
self.assertFalse(player_one_response["race"] == player_two_response["race"])
class GameTest(unittest.TestCase):
def setUp(self):
self.test_game = Thud.Game('test_one', 'test_two')
self.player_one = self.test_game.player_one.token
self.player_two = self.test_game.player_two.token
def test_validate_destination_success(self):
self.assertTrue(self.test_game.validate_destination((6, 4)))
self.assertTrue(self.test_game.validate_destination((11, 9)))
self.assertTrue(self.test_game.validate_destination((4, 2)))
def test_validate_destination_failure_blocked(self):
self.assertFalse(self.test_game.validate_destination((0, 0)))
self.assertFalse(self.test_game.validate_destination((13, 11)))
self.assertFalse(self.test_game.validate_destination((7, 27)))
def test_validate_destination_failure_over_max_x(self):
self.assertFalse(self.test_game.validate_destination((15, 4)))
def test_validate_destination_failure_under_min_x(self):
self.assertFalse(self.test_game.validate_destination((-1, 4)))
def test_validate_destination_failure_over_max_y(self):
self.assertFalse(self.test_game.validate_destination((6, 15)))
def test_validate_destination_failure_under_min_y(self):
self.assertFalse(self.test_game.validate_destination((6, -1)))
def test_validate_clear_path_success(self):
test_piece = self.test_game.board.get_piece(1, 4)
self.assertTrue(self.test_game.validate_clear_path(test_piece, (1, 9)))
def test_validate_clear_path_through_piece_failure(self):
test_piece = self.test_game.board.get_piece(7, 8)
self.assertFalse(self.test_game.validate_clear_path(test_piece, (9, 8)))
def test_validate_clear_path_through_multiple_pieces_failure(self):
test_piece = self.test_game.board.get_piece(12, 3)
self.assertFalse(self.test_game.validate_clear_path(test_piece, (2, 13)))
def test_validate_clear_path_to_troll_success(self):
test_dwarf = self.test_game.board.get_piece(6, 0)
test_troll = self.test_game.board.get_piece(6, 6)
def test_validate_dwarf_move_east_success(self):
test_dwarf = self.test_game.board.get_piece(0, 5)
self.assertTrue(self.test_game.validate_dwarf_move(test_dwarf, (13, 5)))
def test_validate_dwarf_move_west_success(self):
test_dwarf = self.test_game.board.get_piece(12, 3)
self.assertTrue(self.test_game.validate_dwarf_move(test_dwarf, (3, 3)))
def test_validate_dwarf_move_north_success(self):
test_dwarf = self.test_game.board.get_piece(10, 13)
self.assertTrue(self.test_game.validate_dwarf_move(test_dwarf, (10, 2)))
def test_validate_dwarf_move_south_success(self):
test_dwarf = self.test_game.board.get_piece(3, 2)
self.assertTrue(self.test_game.validate_dwarf_move(test_dwarf, (3, 11)))
def test_validate_dwarf_move_northeast_success(self):
test_dwarf = self.test_game.board.get_piece(2, 11)
self.assertTrue(self.test_game.validate_dwarf_move(test_dwarf, (5, 8)))
def test_validate_dwarf_move_southeast_success(self):
test_dwarf = self.test_game.board.get_piece(2, 3)
self.assertTrue(self.test_game.validate_dwarf_move(test_dwarf, (5, 6)))
def test_validate_dwarf_move_northwest_success(self):
test_dwarf = self.test_game.board.get_piece(10, 13)
self.assertTrue(self.test_game.validate_dwarf_move(test_dwarf, (6, 9)))
def test_validate_dwarf_move_southwest_success(self):
test_dwarf = self.test_game.board.get_piece(12, 3)
self.assertTrue(self.test_game.validate_dwarf_move(test_dwarf, (9, 6)))
def test_validate_dwarf_move_non_diagonal_failure(self):
test_dwarf = self.test_game.board.get_piece(12, 3)
self.assertFalse(self.test_game.validate_dwarf_move(test_dwarf, (10, 6)))
def test_validate_dwarf_move_non_horizontal_failure(self):
test_dwarf = self.test_game.board.get_piece(12, 3)
self.assertFalse(self.test_game.validate_dwarf_move(test_dwarf, (2, 4)))
def test_validate_dwarf_move_impassable_failure(self):
test_dwarf = self.test_game.board.get_piece(14, 5)
self.assertFalse(self.test_game.validate_dwarf_move(test_dwarf, (14, 3)))
def test_validate_dwarf_move_creature_failure(self):
test_dwarf = self.test_game.board.get_piece(12, 3)
self.assertFalse(self.test_game.validate_dwarf_move(test_dwarf, (5, 10)))
def test_validate_troll_move_east_success(self):
test_troll = self.test_game.board[8][6]
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (9, 6)))
def test_validate_troll_move_west_success(self):
test_troll = self.test_game.board[6][8]
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (5, 7)))
def test_validate_troll_move_south_success(self):
test_troll = self.test_game.board[6][8]
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (6, 9)))
def test_validate_troll_move_north_success(self):
test_troll = self.test_game.board[8][6]
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (8, 5)))
def test_validate_troll_move_northeast_success(self):
test_troll = self.test_game.board[8][6]
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (9, 6)))
def test_validate_troll_move_southeast_success(self):
test_troll = self.test_game.board[8][6]
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (9, 7)))
def test_validate_troll_move_northwest_success(self):
test_troll = self.test_game.board[6][8]
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (5, 7)))
def test_validate_troll_move_southwest_success(self):
test_troll = self.test_game.board[6][8]
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (5, 8)))
def test_find_adjacent_dwarves_find_three_success(self):
target_square = (1, 5)
target_dwarves = [self.test_game.board.get_piece(0, 5), self.test_game.board.get_piece(1, 4),
self.test_game.board.get_piece(0, 6)]
self.assertEqual(self.test_game.find_adjacent_dwarves(target_square), target_dwarves)
def test_find_adjacent_dwarves_find_two_success(self):
target_square = (14, 7)
target_dwarves = [self.test_game.board.get_piece(14, 6), self.test_game.board.get_piece(14, 8)]
self.assertEqual(self.test_game.find_adjacent_dwarves(target_square), target_dwarves)
def test_validate_troll_move_or_attack_success_attack(self):
# move troll and dwarves together to allow attack
self.test_game.execute_move(self.player_one, (3, 2), (3, 3))
self.test_game.execute_move(self.player_two, (6, 6), (5, 5))
self.test_game.execute_move(self.player_one, (1, 4), (2, 4))
self.test_game.execute_move(self.player_one, (3, 2), (3, 3))
self.test_game.execute_move(self.player_two, (5, 5), (4, 5))
self.test_game.execute_move(self.player_one, (0, 5), (2, 5))
target_dwarves = [(3, 3), (2, 3), (2, 4), (2, 5)]
response = self.test_game.execute_move(self.player_two, (4, 5), (3, 4))
for square in target_dwarves:
self.assertTrue(square in response)
def test_validate_troll_move_or_attack_is_move(self):
test_troll = self.test_game.board.get_piece(6, 6)
destination = (5, 6)
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, destination))
def test_validate_troll_move_or_attack_is_neither(self):
test_troll = self.test_game.board.get_piece(6, 6)
destination = (4, 6)
self.assertFalse(self.test_game.validate_troll_move_or_attack(test_troll, destination))
def test_validate__move_troll_on_top_of_troll(self):
self.assertFalse(self.test_game.validate_move((8, 8), (8, 7)))
def test_validate_move_troll_toss_west_success(self):
# move a dwarf so that the attack check passes
target_dwarf = self.test_game.board.get_piece(0, 6)
self.assertTrue(self.test_game.board.move_piece_on_board(target_dwarf, 2, 6))
# toss troll next to dwarf
test_troll = self.test_game.board.get_piece(6, 6)
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (3, 6)))
def test_validate_move_troll_toss_north_success(self):
# move a dwarf so that the attack check passes
target_dwarf = self.test_game.board.get_piece(6, 0)
self.assertTrue(self.test_game.board.move_piece_on_board(target_dwarf, 6, 2))
# toss troll next to dwarf
test_troll = self.test_game.board.get_piece(6, 6)
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (6, 3)))
def test_validate_move_troll_toss_east_success(self):
# move a dwarf so that the attack check passes
target_dwarf = self.test_game.board.get_piece(14, 8)
self.assertTrue(self.test_game.board.move_piece_on_board(target_dwarf, 12, 8))
# toss troll next to dwarf
test_troll = self.test_game.board.get_piece(8, 8)
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (11, 8)))
def test_validate_move_troll_toss_south_success(self):
# move a dwarf so that the attack check passes
target_dwarf = self.test_game.board.get_piece(8, 14)
self.assertTrue(self.test_game.board.move_piece_on_board(target_dwarf, 8, 12))
# toss troll next to dwarf
test_troll = self.test_game.board.get_piece(8, 8)
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_troll, (8, 11)))
def test_validate_toss_west_success(self):
# toss troll to empty space
test_troll = self.test_game.board.get_piece(6, 6)
self.assertTrue(self.test_game.validate_throw(test_troll, (3, 6)))
def test_validate_toss_north_success(self):
# toss troll to empty space
test_troll = self.test_game.board.get_piece(6, 6)
self.assertTrue(self.test_game.validate_throw(test_troll, (6, 3)))
def test_validate_toss_east_success(self):
# toss troll to empty space
test_troll = self.test_game.board.get_piece(8, 8)
self.assertTrue(self.test_game.validate_throw(test_troll, (11, 8)))
def test_validate_toss_south_success(self):
# toss troll to empty space
test_troll = self.test_game.board.get_piece(8, 8)
self.assertTrue(self.test_game.validate_throw(test_troll, (8, 11)))
def test_validate_throw_troll_multiple_squares_failure(self):
test_piece = self.test_game.board.get_piece(6, 6)
self.assertFalse(self.test_game.validate_throw(test_piece, (1, 5)))
def test_validate_throw_dwarf_multiple_squares_failure(self):
test_piece = self.test_game.board.get_piece(6, 6)
self.assertTrue(self.test_game.validate_throw(test_piece, (3, 6)))
def test_validate_throw_single_square_success(self):
test_dwarf = self.test_game.board.get_piece(0, 6)
# guard assertion to ensure this really was an empty square before
self.assertFalse(self.test_game.find_adjacent_dwarves((6, 5)))
# move a troll target 2 squares south
target_troll = self.test_game.board.get_piece(6, 6)
self.test_game.board.move_piece_on_board(target_troll, 1, 6)
target_troll.move((1, 6))
self.assertEqual(self.test_game.validate_dwarf_attack(test_dwarf, target_troll), [target_troll])
def test_validate_troll_move_or_attack_failure_through_piece(self):
test_troll = self.test_game.board.get_piece(8, 7)
# setup a dwarf to allow a shove
test_dwarf = self.test_game.board.get_piece(5, 0)
self.test_game.board.move_piece_on_board(test_dwarf, 8, 4)
self.assertEqual(test_dwarf, self.test_game.board.get_piece(8, 4))
self.assertFalse(self.test_game.validate_troll_move_or_attack(test_troll, (7, 5)))
def test_validate_troll_move_or_attack_success_move(self):
test_piece = self.test_game.board.get_piece(6, 6)
self.assertTrue(self.test_game.validate_troll_move_or_attack(test_piece, (6, 5)))
def test_move_troll_success(self):
self.test_game.execute_move(self.player_one, (6, 6), (5, 5))
def test_move_dwarf_success(self):
self.test_game.execute_move(self.player_one, (5,0), (5, 14))
def test_execute_move(self):
self.assertTrue(self.test_game.execute_move(self.player_one, (5, 0), (5, 1)))
def test_execute_move_failure_troll_moves_first(self):
self.assertFalse(self.test_game.execute_move(self.player_two, (6, 6), (5, 5)))
def test_execute_move_success_first_three_turns(self):
self.assertTrue(self.test_game.execute_move(self.player_one, (5, 0), (5, 1)))
self.assertTrue(self.test_game.execute_move(self.player_two, (6, 6), (5, 5)))
self.assertTrue(self.test_game.execute_move(self.player_one, (5, 1), (5, 2)))
def test_store_move(self):
start = (1, 1)
destination = (2, 2)
self.test_game.store_move(start, destination)
self.assertEqual(self.test_game.move_history, [(start, destination)])
def test_store_multiple_moves(self):
start = (8, 8)
move_one = (9, 9)
move_two = (9, 10)
destination = (10, 10)
self.test_game.store_move(start, move_one)
self.test_game.store_move(move_one, move_two)
self.test_game.store_move(move_two, destination)
self.assertEqual(self.test_game.move_history, [(start, move_one),
(move_one, move_two), (move_two, destination)])
def test_generate_player_token(self):
self.test_game.generate_player_token()
self.assertEqual(len(self.test_game.player_one.token), 20)
self.assertEqual(len(self.test_game.player_two.token), 20)
def test_init_creates_different_player_tokens(self):
self.assertTrue(self.player_one != self.player_two)
def test_validate_player(self):
piece_one = self.test_game.board.get_piece(5, 0)
self.assertTrue(self.test_game.validate_player(self.player_one, piece_one))
# make a dummy move to advance to the bottom of the turn
self.test_game.store_move(1, 1)
piece_two = self.test_game.board.get_piece(6, 6)
self.assertTrue(self.test_game.validate_player(self.player_two, piece_two))
def test_validate_player_failure_troll_moves_first(self):
piece = self.test_game.board.get_piece(6, 6)
self.assertFalse(self.test_game.validate_player(self.player_two, piece))
def test_move_success(self):
test_dwarf = self.test_game.board.get_piece(5, 0)
self.test_game.move(test_dwarf, (5, 1))
self.assertEqual(self.test_game.board.get_piece(5, 1), test_dwarf)
def test_move_failure_to_center(self):
self.test_game.execute_move(self.player_one, (0, 6), (4, 6))
self.assertFalse(self.test_game.execute_move(self.player_two, (6, 6), (7, 7)))
def test_execute_move_test_move(self):
self.test_game.execute_move(self.player_one, (6, 0), (6, 1), test=True)
self.assertEqual(self.test_game.move_history, [])
def test_execute_move_test_attack(self):
self.test_game.execute_move(self.player_one, (5, 0), (5, 4))
moves = self.test_game.move_history
self.assertEqual(self.test_game.execute_move(self.player_two, (6, 6), (5, 5)), [(5, 4)])
self.assertEqual(self.test_game.move_history, moves)
class PieceTest(unittest.TestCase):
def test_piece_init_location(self):
test_piece = Thud.Dwarf(1, 2, 1)
self.assertEqual(test_piece.x, 1)
self.assertEqual(test_piece.y, 2)
def test_piece_move(self):
test_piece = Thud.Dwarf(1, 2, 1)
test_piece.move((6, 6))
self.assertEqual(test_piece.x, 6)
self.assertEqual(test_piece.y, 6)
def test_piece_capture(self):
test_piece = Thud.Dwarf(1, 1, 1)
self.assertEqual(test_piece.status, 'Alive')
test_piece.capture()
self.assertEqual(test_piece.status, 'Captured')