-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_game.py
340 lines (257 loc) · 10.4 KB
/
test_game.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import StringIO
import sys
import unittest
from game import (
Game, Player, OutsideRoom, Item, Guard, HallwayRoom, Room,
UnknownCommand,
)
class TestGame(unittest.TestCase):
def setUp(self):
super(TestGame, self).setUp()
self.player = Player(gender='male', age='10', hair_color='blue')
self.game = Game(self.player, rooms=[OutsideRoom()])
def tearDown(self):
super(TestGame, self).tearDown()
def test_instantiate_game(self):
game = Game(self.player, rooms=[])
self.assertEqual(game.player, self.player)
self.assertEqual(game.rooms, [])
def test_room_traversal(self):
start_room = OutsideRoom()
end_room = HallwayRoom()
start_room.add_exit("north", end_room)
end_room.add_item(Item('feather'))
self.assertIn(start_room.about, start_room.describe())
self.assertTrue(start_room.describe().startswith(start_room.name))
possible_exits = start_room.possible_exits()
new_room = start_room.move(possible_exits[0])
self.assertIsInstance(new_room, Room)
self.assertEqual(new_room.about, end_room.about)
self.assertIn(end_room.about, new_room.describe())
self.assertIn('feather', new_room.describe())
possible_exits = new_room.possible_exits()
self.assertEqual(possible_exits, [])
def test_parse_action_direction(self):
self.assertEqual(self.game.parse_action("ne"), "northeast")
def test_parse_action_command(self):
self.assertEqual(self.game.parse_action("q"), "quit")
def test_parse_action_unknown_command(self):
self.assertRaises(UnknownCommand, self.game.parse_action, "-invalid-")
class TestScript(unittest.TestCase):
def setUp(self):
super(TestScript, self).setUp()
self.player = Player(gender='male', age='10', hair_color='blue')
# capture stdout from game loop
self.stdout = StringIO.StringIO()
self._orig_stdout = sys.stdout
sys.stdout = self.stdout
# initialize rooms
outside = OutsideRoom()
hallway = HallwayRoom()
guard = Guard('Guard 1')
hallway.add_item(guard)
# connect rooms
outside.add_exit("in", hallway)
hallway.add_exit("out", outside)
# initialize game and play
player = Player(gender='female', age='74', hair_color='red')
self.game = Game(
player,
rooms=[outside, hallway],
script=['quit']
)
def tearDown(self):
sys.stdout = self._orig_stdout
super(TestScript, self).tearDown()
def test_script(self):
self.assertRaises(SystemExit, self.game.play)
output = self.stdout.getvalue()
self.assertIn("Outside", output)
self.assertIn("Goodbye!", output)
def test_script_invalid_command(self):
self.game.script = ['-invalid-']
self.assertRaises(SystemExit, self.game.play)
self.assertIn("I don't understand", self.stdout.getvalue())
def test_script_1(self):
self.game.script = ['in', 'q']
self.assertRaises(SystemExit, self.game.play)
output = self.stdout.getvalue()
self.assertTrue(output.startswith("Outside"))
self.assertIn("Hallway", output)
self.assertIn("Guard 1", output)
self.assertEqual(self.game.player.location, self.game.rooms[1])
class TestPlayer(unittest.TestCase):
def test_player(self):
player = Player(gender='male', age='10', hair_color='blue')
self.assertEqual(player.gender, 'male')
self.assertEqual(player.age, '10')
self.assertEqual(player.hair_color, 'blue')
def test_has_moved(self):
player = Player(gender='male', age='10', hair_color='blue')
# initially, the player starts off by having been "moved" to a
# new location
self.assertTrue(player.moved)
self.assertTrue(player.has_moved())
# after checking for a move, the flag is reset
self.assertFalse(player.moved)
def test_set_location(self):
"""
test that set_location() sets the location and the moved flag
"""
hallway = HallwayRoom()
player = Player(gender='male', age='10', hair_color='blue')
# the player is initiated without a location
self.assertIsNone(player.location)
player.set_location(hallway)
self.assertEqual(player.location, hallway)
self.assertTrue(player.moved)
def test_empty_inventory(self):
player = Player(gender='male', age='10', hair_color='blue')
self.assertEqual(player.show_inventory(), "You are empty handed.")
def test_inventory(self):
player = Player(
gender='male',
age='10',
hair_color='blue',
inventory=[Item('elephant')],
)
self.assertIn("an elephant", player.show_inventory())
def test_drop_unknown_inventory_item(self):
player = Player(
gender='male',
age='10',
hair_color='blue',
inventory=[Item('elephant')],
)
player.location = OutsideRoom()
loc_items = len(player.location.items)
self.assertEqual(len(player.inventory), 1)
player.drop('rock')
self.assertEqual(len(player.inventory), 1)
self.assertEqual(len(player.location.items), loc_items)
def test_drop_inventory_item(self):
"""
test that dropping an item leaves the item in the current
location
"""
player = Player(
gender='male',
age='10',
hair_color='blue',
inventory=[Item('elephant')],
)
self.assertIn("an elephant", player.show_inventory())
player.location = OutsideRoom()
self.assertNotIn("an elephant", player.location.show_items())
player.drop('elephant')
self.assertNotIn("an elephant", player.show_inventory())
self.assertIn("elephant", player.location.show_items())
class TestRooms(unittest.TestCase):
def test_outside(self):
outside = OutsideRoom()
self.assertGreater(outside.about, 0)
self.assertIsInstance(outside.items, list)
def test_add_item(self):
rock = Item('rock')
room = Room()
before = len(room.items)
self.assertNotIn(rock, room.items)
room.add_item(rock)
self.assertEqual(len(room.items), before+1)
self.assertIn(rock, room.items)
def test_show_items(self):
room = Room()
room.items = [Item('rock'), Item('egg'), Item('gun')]
self.assertIn('rock', room.show_items())
self.assertIn('egg', room.show_items())
self.assertIn('gun', room.show_items())
def test_show_no_items(self):
room = Room()
room.items = []
self.assertEqual(room.show_items(), '')
def test_hallway(self):
hallway = HallwayRoom()
self.assertGreater(hallway.about, 0)
self.assertIsInstance(hallway.items, list)
def test_hallway_guard(self):
hallway = HallwayRoom()
guard = Guard('Guard 1')
before = len(hallway.items)
self.assertNotIn(guard, hallway.items)
hallway.add_item(guard)
self.assertEqual(len(hallway.items), before+1)
self.assertIn(guard, hallway.items)
def test_describe(self):
hallway = HallwayRoom()
self.assertIn(hallway.about, hallway.describe())
# after the first visit, the description is omitted
self.assertNotIn(hallway.about, hallway.describe())
# full description can be shown again if requested
self.assertIn(hallway.about, hallway.describe(verbose=True))
def test_add_exit(self):
hallway = HallwayRoom()
outside = OutsideRoom()
before = len(hallway.exits)
self.assertNotIn("north", hallway.exits)
hallway.add_exit("north", outside)
self.assertEqual(len(hallway.exits), before+1)
self.assertIn("north", hallway.exits)
self.assertEqual(hallway.exits["north"], outside)
def test_add_exit_by_alias(self):
hallway = HallwayRoom()
outside = OutsideRoom()
before = len(hallway.exits)
self.assertNotIn("northwest", hallway.exits)
hallway.add_exit("nw", outside)
self.assertEqual(len(hallway.exits), before+1)
self.assertIn("northwest", hallway.exits)
self.assertEqual(hallway.exits["northwest"], outside)
def test_add_exit_invalid_direction(self):
hallway = HallwayRoom()
self.assertRaises(ValueError, hallway.add_exit, "-invalid-", hallway)
def test_possible_exits(self):
"""test that possible_exits returns a list of directions"""
room = HallwayRoom()
# initially, there are no exits
self.assertEqual(room.possible_exits(), [])
room.add_exit("north", room)
self.assertEqual(room.possible_exits(), ["north"])
room.add_exit("south", room)
self.assertEqual(room.possible_exits(), ["north", "south"])
def test_move(self):
hallway = HallwayRoom()
outside = OutsideRoom()
hallway.add_exit("north", outside)
self.assertEqual(hallway.move("north"), outside)
def test_move_non_exit(self):
hallway = HallwayRoom()
outside = OutsideRoom()
hallway.add_exit("north", outside)
self.assertIsNone(hallway.move("south"))
def test_move_by_alias(self):
hallway = HallwayRoom()
outside = OutsideRoom()
hallway.add_exit("north", outside)
self.assertEqual(hallway.move("n"), outside)
class TestItems(unittest.TestCase):
def test_item_default_desc(self):
item = Item('rock')
self.assertEqual(item.name, 'rock')
self.assertEqual(item.desc, "It's just a rock...")
def test_item_custom_desc(self):
item = Item('', 'xyzzy')
self.assertEqual(item.desc, 'xyzzy')
def test_item_default_desc_leading_vowel(self):
item = Item('egg')
self.assertEqual(item.name, 'egg')
self.assertEqual(item.desc, "It's just an egg...")
def test_npc_item(self):
guard = Guard('Guard')
self.assertGreater(len(guard.commands), 0)
self.assertEqual(guard.say(), 'Stop')
self.assertEqual(guard.name, 'Guard')
def test_item_say(self):
item = Item('rock')
self.assertEqual(item.say(), '...')