-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtests.py
More file actions
278 lines (209 loc) · 9.7 KB
/
tests.py
File metadata and controls
278 lines (209 loc) · 9.7 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
import unittest
import os
import re
import pag
from pag.parser import parse_command
from pag.parser import Parser
from pag.parser import Token
from pag.interfaces import SilentUI
class TestPlayer(unittest.TestCase):
@classmethod
def setUpClass(self):
self.l = pag.classes.Location('Test')
self.l.description = 'Test description'
self.l2 = pag.classes.Location('Test 2', description='t2 description')
self.l.exits = {'north': self.l2}
self.l2.exits = {'south': self.l}
self.player = pag.classes.Player(pag.classes.location_list, self.l, ui=SilentUI())
def test_fist_in_inventory(self):
self.assertTrue(self.player.inventory[0].name == 'fist')
def test_change_score(self):
score = self.player.score + 1
self.player._change_score(1)
self.assertEqual(score, self.player.score)
score = self.player.score - 4
self.player._change_score(-4)
self.assertEqual(score, self.player.score)
def test_typing_error(self):
score = self.player.score - 1
self.player._typing_error()
self.assertEqual(score, self.player.score)
def test_can_carry(self):
i = pag.classes.Item('test', '', '', 99)
self.assertTrue(self.player._can_carry(i))
i = pag.classes.Item('test', '', '', 104.347)
self.assertFalse(self.player._can_carry(i))
def test_drop_item(self):
i = pag.classes.Item('test', '', '', 0)
self.player.inventory.append(i)
self.assertTrue(i in self.player.inventory)
self.player._drop_item(i)
self.assertTrue(i not in self.player.inventory)
self.player.location.items.remove(i)
def test_undroppable_fist(self):
fists = [obj for obj in self.player.inventory if isinstance(obj, pag.classes.Fist)]
self.assertEqual(1, len(fists))
self.player.drop('drop', 'fist')
fists = [obj for obj in self.player.inventory if isinstance(obj, pag.classes.Fist)]
self.assertEqual(1, len(fists))
def test_go(self):
self.player.go('go', 'north')
self.assertEqual(self.player.location, self.l2)
self.player.go('', '', self.l)
self.assertEqual(self.player.location, self.l)
class MockOrc(pag.classes.Baddie):
def __init__(self):
super().__init__(name='MockOrc',
hp=5,
description='Mock baddie.',
power=1)
class TestGameworld(unittest.TestCase):
def setUp(self):
self.l_start = pag.classes.Location('TestGameworld start')
self.l_start.description = 'Test description'
self.l_north = pag.classes.Location('Test north', [], [], description='North description')
self.l_east = pag.classes.Location('Test east', [], [MockOrc(), MockOrc(), MockOrc(), MockOrc()], description='East description')
self.l_start.exits = {'north': self.l_north, 'east' : self.l_east}
self.l_north.exits = {'south': self.l_start}
self.l_east.exits = {'west': self.l_start}
self._world = pag.GameWorld(locations=pag.classes.location_list)
self._ui = SilentUI()
self.player = pag.classes.Player(pag.classes.location_list, self.l_start, self._ui)
self._world._player = self.player
def test_go(self):
self.assertEqual(self.player.location, self.l_start)
command = pag.parser.parse_command('go north')
self._world.game_turn(command)
self.assertEqual(self.player.location, self.l_north)
def test_combat(self):
"""
Using a simple prompt/response mock UI, simulate a fight with 4 orcs.
"""
self.assertEqual(self.player.location, self.l_start)
# Enter a room with orcs, with a UI set to retreat.
self._ui.set_reply(re.compile('.*Attack.*', re.MULTILINE), '2')
self._ui.set_reply(re.compile('#'), '2')
command = pag.parser.parse_command('go east')
self._world.game_turn(command)
self.assertEqual(self.player.location, self.l_start)
# Try again, but this time UI is set to fight.
self._ui.set_reply(re.compile('.*Attack.*', re.MULTILINE), '1')
self._ui.set_reply(re.compile('#'), '1')
self._ui.set_reply(re.compile('.*Weapo.*'), 'fist')
command = pag.parser.parse_command('go east')
self._world.game_turn(command)
self.assertEqual(self.player.location, self.l_east)
class TestWords(unittest.TestCase):
def test_get_word_list(self):
test_dict_path = '.testdict'
f = open(test_dict_path, 'w')
f.write('t1\nt2:test2,testtwo\nt3:test3\n#test comment')
f.close()
wl = pag.words.get_word_list(test_dict_path)
self.assertFalse('#test comment' in wl)
self.assertEqual(wl['t2'][1], 'testtwo')
os.remove(test_dict_path)
class TestParser(unittest.TestCase):
def test_go_direction(self):
str1 = 's'
str2 = 'south'
str3 = 'go s'
str4 = 'go south'
expected = ['go', 'south']
self.assertEqual(pag.parser.parse_command(str1), expected)
self.assertEqual(pag.parser.parse_command(str2), expected)
self.assertEqual(pag.parser.parse_command(str3), expected)
self.assertEqual(pag.parser.parse_command(str4), expected)
self.assertEqual(pag.parser.parse_command("n"), ['go', 'north'])
def test_take(self):
string = 'take sword'
self.assertEqual(pag.parser.parse_command(string), ['take', 'sword'])
string = 'take toilet paper'
self.assertEqual(pag.parser.parse_command(string), ['take', 'toilet paper'])
def test_remove_extras(self):
"""
Verify that extras are stripped from commands.
"""
str1 = "look at toilet paper"
str2 = "look toilet paper"
expected = ['look', 'toilet paper']
self.assertEqual(pag.parser.parse_command(str1), expected)
self.assertEqual(pag.parser.parse_command(str2), expected)
def test_handle_whitespace(self):
# Empty command inputs.
self.assertEqual(pag.parser.parse_command(""), None)
self.assertEqual(pag.parser.parse_command(" "), None)
self.assertEqual(pag.parser.parse_command(" "), None)
# Command inputs with extra whitespace. Can't handle whitespace in the middle of words though.
expected = ['look', 'toilet paper']
strings = [ "look toilet paper ",
" look toilet paper ",
" look toilet paper ",
" look toilet\tpaper ",]
for string in strings:
self.assertEqual(pag.parser.parse_command(string), expected)
def test_eat_verb(self):
p = Parser()
word_seq = ['sword', 'sword']
token = p.eat_verb(word_seq)
self.assertEqual(None, token)
self.assertEqual(2, len(word_seq))
word_seq = ['take', 'sword']
token = p.eat_verb(word_seq)
self.assertEqual(Token('take'), token)
self.assertEqual(1, len(word_seq))
def test_eat_noun(self):
p = Parser()
# Eat two swords.
word_seq = ['sword', 'sword']
token = p.eat_noun(word_seq)
self.assertEqual(Token('sword', Token.T_NOUN), token)
self.assertEqual(1, len(word_seq))
token = p.eat_noun(word_seq)
self.assertEqual(Token('sword', Token.T_NOUN), token)
self.assertEqual(0, len(word_seq))
# Eat a noun, but leave extra nouns
word_seq = ['toilet', 'paper', 'sword']
token = p.eat_noun(word_seq)
self.assertEqual(Token('toilet paper', Token.T_NOUN), token)
self.assertEqual(1, len(word_seq))
# Eat a noun, all of it.
word_seq = ['toilet', 'paper', 'roll']
token = p.eat_noun(word_seq)
self.assertEqual(Token('toilet paper', Token.T_NOUN), token)
self.assertEqual(0, len(word_seq))
def test_noun_management(self):
"""
Noun parsing.
"""
str1 = "look" ; exp1 = ['look']
str2 = "look fist" ; exp2 = ['look', 'fist']
str3 = "look look" ; exp3 = None # & printed error 'I don't understand the noun "look."'
str4 = "look xixt" ; exp4 = None # & printed error 'I don't understand the noun "xixt."'
str5 = "look fi st" ; exp5 = None # & printed error 'I don't understand the noun "fi st."'
str6 = "look toilet paper" ; exp6 = ['look', 'toilet paper']
str7 = "look fist fist " ; exp7 = None # & printed error 'I don't understand the extra word "fist."
self.assertEqual(pag.parser.parse_command(str1), exp1)
self.assertEqual(pag.parser.parse_command(str2), exp2)
self.assertEqual(pag.parser.parse_command(str3), exp3)
self.assertEqual(pag.parser.parse_command(str4), exp4)
self.assertEqual(pag.parser.parse_command(str5), exp5)
self.assertEqual(pag.parser.parse_command(str6), exp6)
self.assertEqual(pag.parser.parse_command(str7), exp7)
def test_substitute_synonyms(self):
"""
Verify that synonyms are replaced with their canonical representation.
"""
str1 = "take toilet paper"
str2 = "grab toilet paper"
str3 = "pick up toilet paper"
str4 = "pick up toilet paper roll"
expected = ['take', 'toilet paper']
self.assertEqual(pag.parser.parse_command(str1), expected)
self.assertEqual(pag.parser.parse_command(str2), expected)
self.assertEqual(pag.parser.parse_command(str3), expected)
self.assertEqual(pag.parser.parse_command(str4), expected)
self.assertEqual(pag.parser.parse_command('travel nw'), ['go', 'northwest'])
self.assertEqual(pag.parser.parse_command('see s'), ['look', 'south'])
if __name__ == '__main__':
unittest.main()