-
Notifications
You must be signed in to change notification settings - Fork 0
/
hero_test.py
304 lines (240 loc) · 9.15 KB
/
hero_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
import pytest
import io
import sys
from ability import Ability
from weapon import Weapon
from armor import Armor
from hero import Hero
from team import Team
import math
import random
# Helper Function
def capture_console_output(function_body):
# _io.StringIO object
string_io = io.StringIO()
sys.stdout = string_io
function_body()
sys.stdout = sys.__stdout__
return string_io.getvalue()
# Test Abilities Class
def test_ability_instance():
# Test instantiation without error
big_strength = Ability("Overwhelming Strength", 300)
assert big_strength
def test_ability_name():
# Test for Correct Name
big_strength = Ability("Overwhelming Strength", 300)
assert big_strength.name == "Overwhelming Strength"
def test_ability_attack():
# Test for correct attack value
test_runs = 400
big_strength = Ability("Overwhelming Strength", 400)
for _ in range(0, test_runs):
attack = big_strength.attack()
assert attack >= 0 and attack <= 400
# Test Weapons Class
def test_weapon_instance():
big_stick = Weapon("Overwhelming Stick", 200)
assert "Weapon" in str(big_stick)
def test_weapon_attack():
big_stick = Weapon("Overwhelming Stick", 200)
test_runs = 100
for _ in range(0, test_runs):
attack = big_stick.attack()
assert attack <= 200 and attack >= 100
# Test Heroes Class
def test_hero_instance():
Athena = Hero("Athena")
assert Athena
def test_hero_add_ability():
big_strength = Ability("Overwhelming Strength", 300)
Athena = Hero("Athena")
assert len(Athena.abilities) == 0
Athena.add_ability(big_strength)
assert len(Athena.abilities) == 1
# Check for correct type
assert "Ability" in str(Athena.abilities[0])
assert Athena.abilities[0].name == "Overwhelming Strength"
def test_hero_add_multi_ability():
big_strength = Ability("Overwhelming Strength", 300)
speed = Ability("Lightning Speed", 500)
Athena = Hero("Athena")
assert len(Athena.abilities) == 0
Athena.add_ability(big_strength)
assert len(Athena.abilities) == 1
Athena.add_ability(speed)
assert len(Athena.abilities) == 2
# Check for correct type
assert "Ability" in str(Athena.abilities[0])
assert Athena.abilities[0].name == "Overwhelming Strength"
def test_hero_attack_ability():
big_strength = Ability("Overwhelming Strength", 30000)
athena = Hero("Athena")
assert athena.attack() == 0
athena.add_ability(big_strength)
attack = athena.attack()
assert attack <= 30000 and attack >= 0
def test_hero_ability_attack_mean_value():
athena = Hero("Athena")
strength = random.randint(10, 30000)
big_strength = Ability("Overwhelming Strength", strength)
athena.add_ability(big_strength)
calculated_mean = strength // 2
iterations = 6000
accepted_window = 400
total_attack = 0
for _ in range(iterations):
attack_value = athena.attack()
assert attack_value >= 0 and attack_value <= strength
total_attack += attack_value
actual_mean = total_attack / iterations
print("Max Allowed Damage: {}".format(strength))
print("Attacks Tested: {}".format(iterations))
print("Mean -- calculated: {} | actual: {}".format(calculated_mean, actual_mean))
print("Acceptable Distance from Mean: {} | Average distance from mean: {}".format(accepted_window, abs(calculated_mean - actual_mean)))
print("Acceptable min attack: {} | Acceptable max attack: {}".format(actual_mean - accepted_window, actual_mean + accepted_window))
assert actual_mean <= calculated_mean + accepted_window and actual_mean >= calculated_mean - accepted_window
def test_hero_ability_attack_standard_deviation():
willow_waffle = Hero("Willow Waffle")
strength = random.randint(400, 30000)
willow = Ability("Willowness", strength)
willow_waffle.add_ability(willow)
attacks = list()
total_attack = 0
number_tests = 1000
for _ in range(number_tests):
cur_attack = willow_waffle.attack()
attacks.append(cur_attack)
total_attack += cur_attack
mean = total_attack / number_tests
# Get Square Deviations
for index, value in enumerate(attacks):
attacks[index] = math.pow(value - mean, 2)
standard_dev = math.sqrt(sum(attacks) / len(attacks))
print("Standard Deviation Cannot be 0.\nRandom Numbers not generated for attack.")
assert standard_dev != 0.0
def test_hero_weapon_equip():
sans = Hero("Comic Sans")
weapon = Weapon("Garlic Hot Sauce", 400)
sans.add_ability(weapon)
assert len(sans.abilities) == 1
assert sans.abilities[0].name == "Garlic Hot Sauce"
# This tests if the average of all attacks is correct.
# This test will faile if the random range of values is not correct.
def test_hero_weapon_attack_mean_value():
kkrunch = Hero("Kaptain Krunch")
strength = random.randint(10, 30000)
min_attack = strength // 2
big_strength = Weapon("Sword of Whimsy", strength)
kkrunch.add_ability(big_strength)
calculated_mean = (strength - min_attack) // 2 + min_attack
accepted_window = 400
iterations = 6000
sum_of_sqr = 0
total_attack = 0
for _ in range(iterations):
attack_value = kkrunch.attack()
assert attack_value >= min_attack and attack_value <= strength
total_attack += attack_value
deviation = attack_value - calculated_mean
sum_of_sqr += deviation * deviation
actual_mean = total_attack / iterations
print("Max Allowed Damage: {}".format(strength))
print("Attacks Tested: {}".format(iterations))
print("Mean -- calculated: {} | actual: {}".format(calculated_mean, actual_mean))
print("Acceptable Min: {} | Acceptable Max: {}".format(actual_mean - accepted_window, actual_mean + accepted_window))
print("Tested Result: {}".format(actual_mean))
assert actual_mean <= calculated_mean + accepted_window
assert actual_mean >= calculated_mean - accepted_window
# This method uses statistics to check that a random value is given.
# This test will only fail if the same value is returned over the course of 1000 runs.
def test_hero_attack_standard_deviation():
willow_waffle = Hero("Willow Waffle")
strength = random.randint(400, 30000)
travel_agent = Weapon("Travel Agents", strength)
willow_waffle.add_ability(travel_agent)
attacks = list()
total_attack = 0
number_tests = 1000
for _ in range(number_tests):
cur_attack = willow_waffle.attack()
attacks.append(cur_attack)
total_attack += cur_attack
mean = total_attack / number_tests
# Get Square Deviations
for index, value in enumerate(attacks):
attacks[index] = math.pow(value - mean, 2)
standard_dev = math.sqrt(sum(attacks) / len(attacks))
print("Random values not given. Please make sure you're not returning the same value every time.")
assert standard_dev != 0.0
def test_hero_attack_weapon():
big_strength = Ability("Overwhelming Strength", 200)
Athena = Hero("Athena")
Athena.add_ability(big_strength)
test_runs = 100
for _ in range(0, test_runs):
attack = big_strength.attack()
assert attack <= 200 and attack >= 0
def test_hero_multi_weapon_attack():
strength = Weapon("Overwhelming Strength", 200)
sword_of_truth = Weapon("Sword of Truth", 700)
Athena = Hero("Athena")
Athena.add_ability(strength)
Athena.add_ability(sword_of_truth)
assert len(Athena.abilities) == 2
test_runs = 100
for _ in range(0, test_runs):
attack = Athena.attack()
assert attack <= 900 and attack >= 0
def test_hero_weapon_ability_attack():
quickness = Ability("Quickness", 1300)
sword_of_truth = Weapon("Sword of Truth", 700)
Athena = Hero("Athena")
Athena.add_ability(quickness)
Athena.add_ability(sword_of_truth)
assert len(Athena.abilities) == 2
attack_avg(Athena, 0, 2000)
def attack_avg(object, low, high):
test_runs = 100
for _ in range(0, test_runs):
attack = object.attack()
assert attack <= high and attack >= low
# Test Teams
def test_team_instance():
team = Team("One")
assert team
def test_team_name():
team = Team("One")
assert team.name == "One"
def test_team_hero():
team = Team("One")
jodie = Hero("Jodie Foster")
team.add_hero(jodie)
assert len(team.heroes) == 1
assert team.heroes[0].name == "Jodie Foster"
def test_team_remove_hero():
team = Team("One")
jodie = Hero("Jodie Foster")
team.add_hero(jodie)
assert team.heroes[0].name == "Jodie Foster"
team.remove_hero("Jodie Foster")
assert len(team.heroes) == 0
def test_team_remove_unlisted():
# Test that if no results found return 0
team = Team("One")
jodie = Hero("Jodie Foster")
team.add_hero(jodie)
code = team.remove_hero("Athena")
assert code == 0
def test_team_remove_empty_list():
team = Team("One")
assert team.remove_hero("Athena") == 0
def test_print_heroes():
team = Team("One")
jodie = Hero("Jodie Foster")
team.add_hero(jodie)
athena = Hero("Athena")
team.add_hero(athena)
output_string = capture_console_output(team.view_all_heroes)
assert "Jodie Foster" in output_string
assert "Athena" in output_string