-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2_vampire_unittest.py
More file actions
353 lines (299 loc) · 14 KB
/
a2_vampire_unittest.py
File metadata and controls
353 lines (299 loc) · 14 KB
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
"""
Basic Unittests for your implementation of a vampire for A2.
Passing these tests ensures that our test scripts can run on your code, and will
determine a portion of your mark (see Grading Scheme).
Passing these tests does NOT mean your code is flawless. These tests just
check for all of the basic functionality, without searching too deeply for logic
errors.
Try playing your game through multiple times and trying various combinations of
actions.
"""
import unittest
# Import the student solution
from a2_game import CHARACTER_CLASSES
from a2_playstyle import ManualPlaystyle
from a2_battle_queue import BattleQueue
VampireConstructor = CHARACTER_CLASSES['v']
class VampireUnitTests(unittest.TestCase):
def setUp(self):
"""
Sets up a Battle Queue containing 2 Vampires for all of the
unittests.
"""
self.battle_queue = BattleQueue()
playstyle = ManualPlaystyle(self.battle_queue)
self.p1 = VampireConstructor("P1", self.battle_queue, playstyle)
self.p2 = VampireConstructor("P2", self.battle_queue, playstyle)
self.p1.enemy = self.p2
self.p2.enemy = self.p1
self.battle_queue.add(self.p1)
self.battle_queue.add(self.p2)
def tearDown(self):
"""
Delete the attributes that were created in setUp.
"""
del self.battle_queue
del self.p1
del self.p2
def test_attack_sp(self):
"""
Test to make sure a single attack reduces SP correctly.
"""
self.p1.attack()
remaining_sp = self.p1.get_sp()
expected_sp = 100 - 15
self.assertEqual(remaining_sp, expected_sp,
("After using an 'A' attack, a vampire should " +
"have {} " +
"SP left over but got {} instead.").format(
expected_sp, remaining_sp))
def test_special_attack_sp(self):
"""
Test to make sure a single special attack reduces SP correctly.
"""
self.p1.special_attack()
remaining_sp = self.p1.get_sp()
expected_sp = 100 - 20
self.assertEqual(remaining_sp, expected_sp,
("After using an 'S' attack, a vampire should " +
"have {} " +
"SP left over but got {} instead.").format(
expected_sp, remaining_sp))
def test_attack_hp(self):
"""
Test to make sure a single attack reduces HP correctly when the enemy
is a vampire.
Make sure the vampire's HP heals by the amount changed
"""
self.p1.attack()
remaining_hp = self.p2.get_hp()
expected_hp = 100 - (20 - 3)
self.assertEqual(remaining_hp, expected_hp,
("After using an 'A' attack, a vampire's target " +
"(which " +
"is also a vampire) should have {} " +
"HP left over but got {} instead.").format(
expected_hp, remaining_hp))
remaining_hp = self.p1.get_hp()
expected_hp = 100 + (20 - 3)
self.assertEqual(remaining_hp, expected_hp,
("After using an 'A' attack on a vampire target, " +
"the attacking vampire should have {} " +
"HP but got {} instead.").format(
expected_hp, remaining_hp))
def test_special_attack_hp(self):
"""
Test to make sure a single special attack reduces HP correctly when the
enemy is a vampire.
"""
self.p1.special_attack()
remaining_hp = self.p2.get_hp()
expected_hp = 100 - (30 - 3)
self.assertEqual(remaining_hp, expected_hp,
("After using an 'S' attack, a vampire's target " +
"(which " +
"is also a vampire) should have {} " +
"HP left over but got {} instead.").format(
expected_hp, remaining_hp))
remaining_hp = self.p1.get_hp()
expected_hp = 100 + (30 - 3)
self.assertEqual(remaining_hp, expected_hp,
("After using an 'A' attack on a vampire target, " +
"the attacking vampire should have {} " +
"HP but got {} instead.").format(
expected_hp, remaining_hp))
def test_is_valid_action(self):
"""
Test to make sure is_valid_action returns True for both 'A' and 'S'
for a newly created character.
"""
self.assertTrue(self.p1.is_valid_action('A'),
("Calling is_valid_action('A') on a newly created " +
"vampire should return True but got False."))
self.assertTrue(self.p1.is_valid_action('S'),
("Calling is_valid_action('S') on a newly created " +
"vampire should return True but got False."))
def test_is_valid_action_false(self):
"""
Test to make sure is_valid_action returns False when passed a skill
and when there's not enough sp to use that skill.
"""
self.p1.set_sp(19)
self.assertFalse(self.p1.is_valid_action('S'),
("Calling is_valid_action('S') on a vampire " +
"which has " +
"19 SP should return False but got True."))
def test_get_available_actions(self):
"""
Test to make sure get_available_actions returns both 'A' and 'S'
for a newly created character.
"""
actions = self.p1.get_available_actions()
actions.sort()
self.assertEqual(actions, ['A', 'S'],
("Calling get_available_actions() on a newly created" +
" vampire should return both 'A' and 'S' but got " +
"{} instead.").format(actions))
def test_repr(self):
"""
Test to make sure the repr is in the correct format.
"""
actual = repr(self.p1)
self.assertEqual(actual, "P1 (Vampire): 100/100",
("Calling repr on a vampire should give a repr in the " +
"form Name (Character): HP/SP but got " +
"{} instead.").format(actual))
def test_special_attack_battle_queue(self):
"""
Test to make sure the special attack correctly adds the enemy into
the Queue before an attacking vampire's special attack.
"""
self.p1.special_attack()
self.battle_queue.remove()
# The queue should be P2 -> P1 -> P1 -> P2
queue_order = []
for i in range(4):
queue_order.append(self.battle_queue.remove().get_name())
self.assertEqual(queue_order, ['P2', 'P1', 'P1', 'P2'],
("After using a special attack, expected the battle " +
"queue to be in the order P2 -> P1 -> P1 -> P2 but " +
"got {} instead.").format(" -> ".join(queue_order)))
def test_get_next_sprite_idle(self):
"""
Test to make sure get_next_sprite gives the correct sprites when in
idle pose.
"""
expected_sprites = ["vampire_idle_0",
"vampire_idle_1",
"vampire_idle_2",
"vampire_idle_3",
"vampire_idle_4",
"vampire_idle_5",
"vampire_idle_6",
"vampire_idle_7",
"vampire_idle_8",
"vampire_idle_9",
"vampire_idle_0",
]
obtained_sprites = []
for i in range(11):
obtained_sprites.append(self.p1.get_next_sprite())
self.assertEqual(obtained_sprites, expected_sprites,
("Calling get_next_sprite when the vampire was " +
"in idle pose should have given us sprites in the " +
"order:\n{}\bBut got:\n{}\ninstead.").format(
", ".join(expected_sprites),
", ".join(obtained_sprites)))
def test_get_next_sprite_attack(self):
"""
Test to make sure get_next_sprite gives the correct sprites when in
attack pose.
"""
self.p1.attack()
expected_sprites = ["vampire_attack_0",
"vampire_attack_1",
"vampire_attack_2",
"vampire_attack_3",
"vampire_attack_4",
"vampire_attack_5",
"vampire_attack_6",
"vampire_attack_7",
"vampire_attack_8",
"vampire_attack_9",
"vampire_idle_0",
]
obtained_sprites = []
for i in range(11):
obtained_sprites.append(self.p1.get_next_sprite())
self.assertEqual(obtained_sprites, expected_sprites,
("Calling get_next_sprite when the vampire was " +
"in attack pose should have given us sprites in the" +
" order:\n{}\bBut got:\n{}\ninstead.").format(
", ".join(expected_sprites),
", ".join(obtained_sprites)))
def test_get_next_sprite_special_attack(self):
"""
Test to make sure get_next_sprite gives the correct sprites when in
special attack pose.
"""
self.p1.special_attack()
expected_sprites = ["vampire_special_0",
"vampire_special_1",
"vampire_special_2",
"vampire_special_3",
"vampire_special_4",
"vampire_special_5",
"vampire_special_6",
"vampire_special_7",
"vampire_special_8",
"vampire_special_9",
"vampire_idle_0",
]
obtained_sprites = []
for i in range(11):
obtained_sprites.append(self.p1.get_next_sprite())
self.assertEqual(obtained_sprites, expected_sprites,
("Calling get_next_sprite when the vampire was " +
"in special pose should have given us sprites " +
"in the order:\n{}\nBut got:\n{}\ninstead.").format(
", ".join(expected_sprites),
", ".join(obtained_sprites)))
def test_get_next_sprite_attack_reset(self):
"""
Test to make sure get_next_sprite() returns the correct sprite when
calling attack twice (without finishing the animation)
"""
self.p1.attack()
expected_sprites = ["vampire_attack_0",
"vampire_attack_1",
"vampire_attack_2",
"vampire_attack_0",
"vampire_attack_1",
"vampire_attack_2",
"vampire_attack_3",
]
obtained_sprites = []
for i in range(3):
obtained_sprites.append(self.p1.get_next_sprite())
self.p1.attack()
for i in range(4):
obtained_sprites.append(self.p1.get_next_sprite())
self.assertEqual(obtained_sprites, expected_sprites,
("Calling get_next_sprite when the vampire was " +
"in attack pose and attacking again after calling " +
"get_next_sprite 3 times " +
"should have given us sprites " +
"in the order:\n{}\nBut got:\n{}\ninstead.").format(
", ".join(expected_sprites),
", ".join(obtained_sprites)))
def test_get_next_sprite_special_reset(self):
"""
Test to make sure get_next_sprite() returns the correct sprite when
calling special twice (without finishing the animation)
"""
self.p1.special_attack()
expected_sprites = ["vampire_special_0",
"vampire_special_1",
"vampire_special_2",
"vampire_special_0",
"vampire_special_1",
"vampire_special_2",
"vampire_special_3",
]
obtained_sprites = []
for i in range(3):
obtained_sprites.append(self.p1.get_next_sprite())
self.p1.special_attack()
for i in range(4):
obtained_sprites.append(self.p1.get_next_sprite())
self.assertEqual(obtained_sprites, expected_sprites,
("Calling get_next_sprite when the vampire was " +
"in special attack pose and special attacking again" +
" after calling " +
"get_next_sprite 3 times " +
"should have given us sprites " +
"in the order:\n{}\bBut got:\n{}\ninstead.").format(
", ".join(expected_sprites),
", ".join(obtained_sprites)))
if __name__ == "__main__":
unittest.main(exit = False)