-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_dfrandom.py
executable file
·266 lines (221 loc) · 6.94 KB
/
test_dfrandom.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
# /usr/bin/env pytest-3
import xml.etree.ElementTree as et
import dfrandom
def test_parse_spell_prereq_name_is():
xml = """
<spell_prereq has="yes">
<name compare="is">light</name>
</spell_prereq>"""
el = et.fromstring(xml)
assert (
dfrandom._parse_spell_prereq(el, "f1")
== """
def f1(traits, trait_names):
return '''Light''' in trait_names
"""
)
def test_parse_spell_prereq_name_college_count():
xml = """<spell_prereq has="yes">
<college_count compare="at_least">10</college_count>
</spell_prereq>
"""
el = et.fromstring(xml)
assert (
dfrandom._parse_spell_prereq(el, "f1")
== """
def f1(traits, trait_names):
count = count_spell_colleges(traits)
return count >= 10
"""
)
def test_parse_advantage_prereq():
xml = """<advantage_prereq has="yes">
<name compare="is">Magery</name>
<notes compare="is anything"></notes>
<level compare="at_least">3</level>
</advantage_prereq>
"""
el = et.fromstring(xml)
assert (
dfrandom._parse_advantage_prereq(el, "f1")
== """
def f1(traits, trait_names):
for trait in trait_names:
if trait.startswith('''Magery'''):
regexp = "(\d+).*$"
match = re.search(regexp, trait)
if match:
level = int(match.group(1))
return level >= int('''3''')
return False
"""
)
def test_parse_attribute_prereq():
xml = '<attribute_prereq has="yes" which="iq" compare="at_least">13</attribute_prereq>'
el = et.fromstring(xml)
assert (
dfrandom._parse_attribute_prereq(el, "f1")
== """
def f1(traits, trait_names):
for trait in trait_names:
if trait.startswith('''IQ '''):
regexp = "(\d+).*$"
match = re.search(regexp, trait)
if match:
level = int(match.group(1))
return level >= 13
return False
"""
)
def test_parse_prereq_list():
xml = """
<prereq_list all="no">
<advantage_prereq has="yes">
<name compare="is">Magery</name>
<notes compare="contains">one college (gate)</notes>
<level compare="at_least">2</level>
</advantage_prereq>
<advantage_prereq has="yes">
<name compare="is">Magery</name>
<notes compare="does not contain">one college</notes>
<level compare="at_least">2</level>
</advantage_prereq>
</prereq_list>"""
el = et.fromstring(xml)
top_name = "top_0"
dfrandom.function_name_incrementor = 1
assert (
dfrandom._parse_prereq_list(el, top_name)
== """
def and_(*args):
for arg in args:
if not arg:
return False
return True
def or_(*args):
for arg in args:
if arg:
return True
return False
def ppl_1(traits, trait_names):
for trait in trait_names:
if trait.startswith('''Magery''') and '''one college (gate)''' in trait:
regexp = "(\d+).*$"
match = re.search(regexp, trait)
if match:
level = int(match.group(1))
return level >= int('''2''')
return False
def ppl_2(traits, trait_names):
for trait in trait_names:
if trait.startswith('''Magery''') and '''one college''' not in trait:
regexp = "(\d+).*$"
match = re.search(regexp, trait)
if match:
level = int(match.group(1))
return level >= int('''2''')
return False
top_0 = or_(ppl_1(traits, trait_names), ppl_2(traits, trait_names))
"""
)
def test_parse_prereq_list2():
xml = """
<prereq_list all="yes">
<spell_prereq has="yes">
<college_count compare="at_least">10</college_count>
</spell_prereq>
<spell_prereq has="yes">
<college_count compare="at_least">10</college_count>
</spell_prereq>
<attribute_prereq has="yes" which="iq" compare="at_least">13</attribute_prereq>
<prereq_list all="no">
<advantage_prereq has="yes">
<name compare="is">Magery</name>
<notes compare="contains">one college (gate)</notes>
<level compare="at_least">2</level>
</advantage_prereq>
<advantage_prereq has="yes">
<name compare="is">Magery</name>
<notes compare="does not contain">one college</notes>
<level compare="at_least">2</level>
</advantage_prereq>
</prereq_list>
</prereq_list>
"""
el = et.fromstring(xml)
top_name = "top_0"
dfrandom.function_name_incrementor = 1
assert (
dfrandom._parse_prereq_list(el, top_name)
== """
def and_(*args):
for arg in args:
if not arg:
return False
return True
def or_(*args):
for arg in args:
if arg:
return True
return False
def ppl_1(traits, trait_names):
count = count_spell_colleges(traits)
return count >= 10
def ppl_2(traits, trait_names):
count = count_spell_colleges(traits)
return count >= 10
def ppl_3(traits, trait_names):
for trait in trait_names:
if trait.startswith('''IQ '''):
regexp = "(\d+).*$"
match = re.search(regexp, trait)
if match:
level = int(match.group(1))
return level >= 13
return False
def and_(*args):
for arg in args:
if not arg:
return False
return True
def or_(*args):
for arg in args:
if arg:
return True
return False
def ppl_5(traits, trait_names):
for trait in trait_names:
if trait.startswith('''Magery''') and '''one college (gate)''' in trait:
regexp = "(\d+).*$"
match = re.search(regexp, trait)
if match:
level = int(match.group(1))
return level >= int('''2''')
return False
def ppl_6(traits, trait_names):
for trait in trait_names:
if trait.startswith('''Magery''') and '''one college''' not in trait:
regexp = "(\d+).*$"
match = re.search(regexp, trait)
if match:
level = int(match.group(1))
return level >= int('''2''')
return False
top_4 = or_(ppl_5(traits, trait_names), ppl_6(traits, trait_names))
top_0 = and_(ppl_1(traits, trait_names), ppl_2(traits, trait_names), ppl_3(traits, trait_names), top_4)
"""
)
def test_add_spell():
traits = [("IQ 15", 100), ("Magery 3", 35)]
trait_names = set([trait[0] for trait in traits])
dfrandom.build_spell_prereqs()
NUM_SPELLS = 425
for unused in range(NUM_SPELLS):
dfrandom.add_spell(traits, trait_names)
assert len(traits) == len(trait_names) == NUM_SPELLS + 2
def test_merge_traits_attr():
traits = [("ST 14", 40, dfrandom.PA), ("ST +2", 20, dfrandom.PA)]
assert dfrandom.merge_traits(traits) == [("ST 16", 60, dfrandom.PA)]
def test_merge_traits_advantage():
traits = [("Magery 3", 35, dfrandom.AD), ("Magery 4", 10, dfrandom.AD)]
assert dfrandom.merge_traits(traits) == [("Magery 4", 45, dfrandom.AD)]