-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_risk_2.py
473 lines (409 loc) · 17.3 KB
/
test_risk_2.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
from unittest import TestCase
from unittest.mock import patch, call
def get_p_b():
for p in range(2, 7):
for b in range(p + 1):
yield p, b
def get_p_b_pp():
for p, b in get_p_b():
for pp in range(1, p + 1):
yield p, b, pp
class TestCreateDict(TestCase):
def setUp(self):
from risk_2 import CreateDict
self.cd = CreateDict
def test_territories(self):
t_dict = self.cd.territories()
self.assertIsInstance(t_dict, dict)
self.assertTrue(len(t_dict) == 42)
for v in t_dict.values():
self.assertTrue(len(v) == 5)
def test_players(self):
for p, b in get_p_b():
p_dict = self.cd.players(p, b)
self.assertEqual(len(p_dict), p)
bots = 0
for player in p_dict.values():
if player["bot"]:
bots += 1
self.assertEqual(bots, b)
def test_continents(self):
continents = self.cd.continents()
self.assertIsInstance(continents, dict)
self.assertEqual(len(continents), 6)
def test_star_exchange_rate(self):
ser = self.cd.star_exchange_rate()
self.assertIsInstance(ser, dict)
def random_td_pd(self, p, b):
from random import randint
td = self.cd.territories()
pd = self.cd.players(p, b)
for t in td:
td[t]["troops"] = randint(1, 2)
td[t]["occupier"] = pd[randint(1, p)]["name"]
return td, pd
def test_attacker(self):
for p, b, pp in get_p_b_pp():
td, pd = self.random_td_pd(p, b)
attack_dict = self.cd.attacker(t_dict=td, p_dict=pd, pnp=pp)
self.assertIsInstance(attack_dict, dict)
for c in attack_dict:
name = pd[pp]["name"]
self.assertNotIn(name, [x[1] for x in attack_dict[c]])
self.assertEqual(td[c]["occupier"], name)
self.assertGreater(td[c]["troops"], 1)
def test_transfer(self):
for p, b, pp in get_p_b_pp():
td, pd = self.random_td_pd(p, b)
transfer_dict = self.cd.transfer(td, pd, pp)
self.assertIsInstance(transfer_dict, dict)
for c in transfer_dict:
name = pd[pp]["name"]
self.assertIn(name, [x[1] for x in transfer_dict[c]])
self.assertEqual(td[c]["occupier"], name)
self.assertGreater(td[c]["troops"], 1)
class TestDeck(TestCase):
def setUp(self):
from risk_2 import CreateDict, Deck
self.t = CreateDict.territories()
self.deck = Deck(self.t)
def test_pop(self):
l1 = len(self.deck)
first = self.deck[0]
popped = self.deck.pop(0)
l2 = len(self.deck)
self.assertEqual(first, popped)
self.assertIs(first, popped)
self.assertEqual(l1, l2 + 1)
def test_draw_card(self):
l = len(self.deck)
for i in range(1, l + 2):
deck, card = self.deck.draw_card()
self.assertEqual(deck, self.deck.deck)
self.assertIsInstance(deck, type(self.deck.deck))
self.assertEqual(len(deck), len(self.deck))
self.assertEqual(len(deck), l - i if i <= l else 0)
self.assertIsInstance(card[0], str)
self.assertIsInstance(card[1], int)
if i == l + 1:
self.assertEqual(card[1], 0)
else:
self.assertGreater(card[1], 0)
self.assertNotIn(card, deck)
ps = iter([str(p) for p, _ in get_p_b()])
bs = iter([str(b) for _, b in get_p_b()])
def mock_valid_input_p_count(prompt):
if "number of players:" in prompt.lower():
return next(ps)
if "number of bots:" in prompt.lower():
return next(bs)
def get_ips_iter():
return iter(["", "-1", "0", "1", "7", "nonnumerical_str", "\n", " ", "6"])
def get_ibs_iter():
return iter(["", "7", "nonnumerical_str", "\n", " ", "6"])
ips = get_ips_iter()
ibs = get_ibs_iter()
def mock_invalid_input_p_count(prompt):
if "number of players:" in prompt.lower():
return next(ips)
if "number of bots:" in prompt.lower():
return next(ibs)
def get_wl_iter():
ns = [f"Player {n}" for n in range(1, 7)]
return iter([[n for n in ns[:x]] for x in range(1, 7)])
def random_td_pd(p, b):
from risk_2 import CreateDict
from random import randint
td = CreateDict.territories()
pd = CreateDict.players(p, b)
for t in td:
td[t]["troops"] = randint(1, 2)
td[t]["occupier"] = pd[randint(1, p)]["name"]
for p in pd:
pd[p]["stars"] = randint(1, 10)
return td, pd
def get_c_iter():
from risk_2 import CreateDict
return iter([c for c in CreateDict.territories()])
class TestPrompts(TestCase):
def setUp(self):
from risk_2 import Prompts
self.p = Prompts
def test_player_count(self):
with patch('builtins.input', mock_valid_input_p_count):
while not StopIteration:
p, b = self.p.player_count()
self.assertIsInstance(p, int)
self.assertIsInstance(b, int)
self.assertTrue(2 <= p <= 6)
self.assertTrue(0 <= b <= p)
with patch('builtins.input', mock_invalid_input_p_count), patch('sys.stdout') as out:
p, b = self.p.player_count()
self.assertEqual(p, 6)
self.assertEqual(b, 6)
errs = []
for e in get_ips_iter():
if e != "6":
errs.append(call.write(f"\nError: {e} is an invalid entry for player count.\n"))
errs.append(call.write('\n'))
for e in get_ibs_iter():
if e != "6":
errs.append(call.write(f"\nError: {e} is an invalid entry for bot count.\n"))
errs.append(call.write('\n'))
out.assert_has_calls(errs)
def test_goes_first(self):
with patch('sys.stdout') as out:
self.p.goes_first(1, 6)
out.assert_has_calls([call.write("\n1 had the highest roll of [6] and goes first.\n")])
def test_dice_roll(self):
with patch('sys.stdout') as out:
self.p.dice_roll("Player 1", 6)
out.assert_has_calls([call.write("Player 1 rolls: [6]")])
def test_tied_rollers(self):
for wl in get_wl_iter():
with patch('sys.stdout') as out:
self.p.tied_rollers(wl, 6)
out.assert_has_calls([call.write(f"\n{' & '.join(wl).strip(' & ')} tied the highest roll of [6]\n")])
# print(out.mock_calls)
def test_display_terrs(self):
from risk_2 import CreateDict
import re
for p, b, pp in get_p_b_pp():
t_dict = CreateDict.territories()
p_dict = CreateDict.players(p, b)
p_name = p_dict[pp]["name"]
with patch('sys.stdout') as out:
self.p.display_terrs(t_dict, player=p_name, troop_min=2, attack=True)
self.assertEqual(len(out.mock_calls), 6 * 4)
with patch('sys.stdout') as out:
self.p.display_terrs(t_dict, hide_occ=True)
self.assertEqual(len(out.mock_calls), (6 * 4) + (42 * 2))
with patch('sys.stdout') as out:
self.p.display_terrs(t_dict, player=p_name)
self.assertEqual(len(out.mock_calls), (6 * 4))
with patch('sys.stdout') as out:
self.p.display_terrs(t_dict, player=p_name, troop_min=2, transfer=True)
self.assertEqual(len(out.mock_calls), (6 * 4))
t_dict, p_dict = random_td_pd(p, b)
with patch('sys.stdout') as out:
self.p.display_terrs(t_dict, player=p_name, troop_min=2, attack=True)
for c in [
re.split(
r'\s{2,}', str(s).lstrip("call.write('").rstrip("')")
) for s in out.mock_calls if "Occupier:" in str(s)
]:
self.assertEqual(t_dict[c[0]]["occupier"], c[1].lstrip("Occupier: "))
self.assertGreater(t_dict[c[0]]["troops"], 1)
self.assertEqual(t_dict[c[0]]["troops"], int(c[2].lstrip("Troops: ")))
with patch('sys.stdout') as out:
self.p.display_terrs(t_dict, hide_occ=True)
self.assertEqual(len(out.mock_calls), 6 * 4)
with patch('sys.stdout') as out:
self.p.display_terrs(t_dict, player=p_name)
for c in [
re.split(
r'\s{2,}', str(s).lstrip("call.write('").rstrip("')")
) for s in out.mock_calls if "Occupier:" in str(s)
]:
self.assertEqual(t_dict[c[0]]["occupier"], c[1].lstrip("Occupier: "))
with patch('sys.stdout') as out:
self.p.display_terrs(t_dict, player=p_name, troop_min=2, transfer=True)
for c in [
re.split(
r'\s{2,}', str(s).lstrip("call.write('").rstrip("')")
) for s in out.mock_calls if "Occupier:" in str(s)
]:
self.assertEqual(t_dict[c[0]]["occupier"], c[1].lstrip("Occupier: "))
self.assertGreater(t_dict[c[0]]["troops"], 1)
def test_claim_terr(self):
from risk_2 import CreateDict
for p, b, pp in get_p_b_pp():
player_dict = CreateDict.players(p, b)
terr_dict = CreateDict.territories()
c_iter = get_c_iter()
def mock_valid_input_claim_terr(prompt):
if "enter a territory name to claim it" in prompt.lower():
n = next(c_iter)
return n
for c in terr_dict:
with patch('builtins.input', mock_valid_input_claim_terr), patch('sys.stdout') as out:
claim = self.p.claim_terr(player_dict, terr_dict, pp)
self.assertIn(f"Player {pp} claims {claim}", str(out.mock_calls))
if not player_dict[pp]["bot"]:
self.assertEqual(claim, c)
def test_select_terr(self):
for p, b, pp in get_p_b_pp():
t_dict, p_dict = random_td_pd(p, b)
valid_c_ls = [c for c in t_dict if t_dict[c]["occupier"] == p_dict[pp]["name"]]
c_iter = iter(valid_c_ls)
def mock_valid_input_transfer_terrs(prompt):
return next(c_iter)
while not StopIteration:
with patch("builtins.input", mock_valid_input_transfer_terrs):
deploy_target = self.p.select_terr(p_dict, t_dict, p_dict[pp]["name"], pp)
self.assertIsInstance(deploy_target, str)
self.assertIn(deploy_target, t_dict)
invalid_iter = iter(["INVALID INPUT"] + [valid_c_ls[0]])
with patch("builtins.input", lambda _: next(invalid_iter)), patch("sys.stdout") as out:
self.p.select_terr(p_dict, t_dict, p_dict[pp]["name"], pp)
if not p_dict[pp]["bot"]:
self.assertIn("Error: INVALID INPUT is not a territory under your control.", str(out.mock_calls))
def test_troop_level(self):
for p, b, pp in get_p_b_pp():
t_dict, p_dict = random_td_pd(p, b)
pn = p_dict[pp]["name"]
mt = p_dict[pp]["troops"]
for t in t_dict:
for i in range(mt + 1):
def mock_valid_troop_level_input(prompt):
if f"{pn} Enter the amount of troops you would like to transfer to {t} [Min: 0 Max: {mt}]" in prompt:
return i
self.fail()
with patch("builtins.input", mock_valid_troop_level_input), patch("sys.stdout") as out:
troop_target = self.p.troop_level(p_dict, pp, pn, t, minimum=0)
if not p_dict[pp]["bot"]:
self.assertEqual(troop_target, i)
else:
self.assertTrue(0 <= troop_target <= mt)
self.assertIn(f"{troop_target} troops transferred to {t}", str(out.mock_calls))
invalid_ls = ["-1", "Nonumerical", "420.69", str(mt + 1), " ", "/n", "1"]
invalid_iter = iter(invalid_ls)
def mock_invalid_troop_input(prompt):
return next(invalid_iter)
if not p_dict[pp]["bot"]:
with patch("builtins.input", mock_invalid_troop_input), patch("sys.stdout") as out:
troop_target = self.p.troop_level(p_dict, pp, pn, t, minimum=0)
self.assertEqual(troop_target, 1)
for bad_input in iter(invalid_ls[:-1]):
self.assertIn(f"Error: {bad_input} is not a valid amount of troops", str(out.mock_calls))
quick_check_ls = ["0", "1"]
quick_check_iter = iter(quick_check_ls)
with patch("builtins.input", lambda _: next(quick_check_iter)), patch("sys.stdout") as out:
troop_target = self.p.troop_level(p_dict, pp, pn, t)
if not p_dict[pp]["bot"]:
self.assertEqual(troop_target, 1)
self.assertIn(f"Error: 0 is not a valid amount of troops", str(out.mock_calls))
self.assertIsInstance(troop_target, int)
def test_star_trade(self):
y_ls = ["Y", "Yes", "yes", "y", "YES"]
ex_ls = ["N", "BLAHH", "0"]
for p, b, pp in get_p_b_pp():
t_dict, p_dict = random_td_pd(p, b)
valid_stars_ls = [str(x) for x in range(p_dict[pp]["stars"] + 1)]
valid_stars_iter = iter(valid_stars_ls)
y_iter = iter(y_ls * 2)
star_check = 0
def mock_valid_star_input(prompt):
if "Would you like to exchange them [Y/N]" in prompt:
return next(y_iter)
if "Enter the amount of stars you would like to trade in:" in prompt:
return next(valid_stars_iter)
while not StopIteration:
with patch("builtins.input", mock_valid_star_input), patch("sys.stdout"):
stars_out, troops_in = self.p.star_trade(p_dict[pp]["stars"], p_dict[pp]["name"], p_dict, pp)
self.assertIsInstance(stars_out, int)
self.assertIsInstance(troops_in, int)
if not p_dict[pp]["bot"]:
self.assertEqual(stars_out, star_check)
ex_input_iter = iter(ex_ls)
def mock_exit_input(prompt):
return next(ex_input_iter)
while not StopIteration:
with patch("builtins.input", mock_exit_input), patch("sys.stdout"):
stars_out, troops_in = self.p.star_trade(p_dict[pp]["stars"], p_dict[pp]["name"], p_dict, pp)
if not p_dict[pp]["bot"]:
self.assertEqual(stars_out, 0)
self.assertEqual(troops_in, 0)
invalid_in_ls = ["-1", str(p_dict[pp]["stars"] + 1), "NONUMERICAL", "\n", "0"]
invalid_in_iter = iter(invalid_in_ls)
def mock_invalid_star_input(prompt):
if "Would you like to exchange them [Y/N]" in prompt:
return "Y"
if "Enter the amount of stars you would like to trade in:" in prompt:
return next(invalid_in_iter)
self.fail()
with patch("builtins.input", mock_invalid_star_input), patch("sys.stdout") as out:
stars_out, troops_in = self.p.star_trade(p_dict[pp]["stars"], p_dict[pp]["name"], p_dict, pp)
if not p_dict[pp]["bot"]:
self.assertEqual(stars_out, 0)
self.assertEqual(troops_in, 0)
self.assertIn("is not a valid entry.", str(out.mock_calls))
def test_load_autosave(self):
for y in ("Y", "y", "YES", "Yes", "yes"):
with patch("builtins.input", lambda _: y):
confirm = self.p.load_autosave()
self.assertTrue(confirm)
for n in ("N", "No", "NO", "no", "n"):
with patch("builtins.input", lambda _: n):
confirm = self.p.load_autosave()
self.assertFalse(confirm)
# def test_transfer(self):
# self.fail()
#
# def test_transfer_count(self):
# self.fail()
#
# def test_attacker(self):
# self.fail()
#
# def test_attacker_force(self):
# self.fail()
#
#
# class TestGameFunctions(TestCase):
# def test_pnp_turner(self):
# self.fail()
#
# def test_banked_troops(self):
# self.fail()
#
# def test_unclaimed_terrs(self):
# self.fail()
#
# def test_roll_to_go_first(self):
# self.fail()
#
# def test_claim_territories(self):
# self.fail()
#
# def test_deploy_additional_troops(self):
# self.fail()
#
# def test_no_winner(self):
# self.fail()
#
# def test_cont_bonus(self):
# self.fail()
#
# def test_star_trade_in(self):
# self.fail()
#
# def test_add_banked_troops(self):
# self.fail()
#
# def test_battle(self):
# self.fail()
#
# def test_get_loser_id(self):
# self.fail()
#
# def test_battle_report(self):
# self.fail()
#
# def test_attack_stage(self):
# self.fail()
#
# def test_fortify_stage(self):
# self.fail()
#
# def test_play(self):
# self.fail()
#
# def test_auto_save_save(self):
# self.fail()
#
# def test_auto_save_load(self):
# self.fail()
#
# def test_main(self):
# self.fail()