2
"""Unit tests for Pottery commands.
7
from twisted.trial import unittest
8
from twisted.test.proto_helpers import StringTransport
10
from pottery import objects, wiring, commands
14
class Commands(unittest.TestCase):
16
self.location = objects.Room("Test Location",
17
"Location for testing.")
19
self.player = objects.Player("Test Player")
20
self.player.useColors = False
21
self.location.add(self.player)
22
self.transport = StringTransport()
24
write = self.transport.write
25
self.player.setProtocol(Protocol())
27
self.observer = objects.Player("Observer Player")
28
self.location.add(self.observer)
29
self.otransport = StringTransport()
31
write = self.otransport.write
32
self.observer.setProtocol(Protocol())
36
for p in self.player, self.observer:
39
except AttributeError:
42
def _test(self, command, output, observed=()):
43
wiring.parse(self.transport, self.player, command)
45
output.insert(0, "> " + command)
48
for xport, oput in ((self.transport, output),
49
(self.otransport, observed)):
51
gotLines = xport.value().splitlines()
52
for i, (got, expected) in enumerate(map(None, gotLines, oput)):
54
expected = expected or '$^'
55
m = re.compile(expected.rstrip()).match(got.rstrip())
57
s1 = pprint.pformat(gotLines)
58
s2 = pprint.pformat(oput)
59
raise unittest.FailTest(
60
"\n%s\ndid not match expected\n%s\n(Line %d)" % (s1, s2, i))
65
def testBadCommand(self):
68
["Bad command or filename"])
70
def testInventory(self):
71
# There ain't no stuff
76
# Give 'em something and make sure
78
self.player.add(objects.Object("foobar"))
84
# Give 'em a couple more things
85
self.player.add(objects.Object("barbaz"))
86
self.player.add(objects.Object("barbaz"))
94
def testCommands(self):
95
cmds = dict.fromkeys([
96
getattr(cls, 'commandName', cls.__name__.lower())
98
in commands.Command.commands.values()]).keys()
105
# Try to get something that does not exist
108
["Nothing named foobar here."])
110
# Try to get yourself
114
self.assertEquals(self.player.location, self.location)
116
# Try to get the location
120
self.assertEquals(self.location.location, None)
122
# Make an object and try to get it
123
o = objects.Object("foo")
129
["Test Player takes foo.",
131
self.assertEquals(o.location, self.player)
133
# Try to get the observer
135
"get 'Observer Player'",
136
["Observer Player is too heavy to pick up.",
138
["Test Player tries to pick you up, but fails.",
140
self.assertEquals(self.observer.location, self.location)
145
["Nothing named bar around here.",
148
c = objects.Container("bar")
150
o = objects.Object("baz")
154
["You take baz from bar.",
156
["Test Player takes baz from bar.",
158
self.assertEquals(o.location, self.player)
163
["Nothing like that around here."])
165
o = objects.Object("bar")
170
["Test Player drops bar."])
171
self.assertEquals(o.location, self.location)
176
[E("[ Test Location ]"),
178
"Location for testing.",
183
[E("[ Test Location ]"),
185
"Location for testing.",
191
"Test Player is great"])
196
"Test Player is great"])
199
"look at Observer Player",
201
"Observer Player is great"],
202
["Test Player looks at you."])
204
o = objects.Object("foo")
212
["You don't see bar here."])
217
["You say, 'hello'"],
218
["Test Player says, 'hello'"])
222
["You say, 'hello'"],
223
["Test Player says, 'hello'"])
226
"'hello world! quote -> '",
227
["You say, 'hello world! quote -> ''"],
228
["Test Player says, 'hello world! quote -> ''"])
232
"emote jumps up and down",
233
["Test Player jumps up and down"],
234
["Test Player jumps up and down"])
237
":jumps up and down",
238
["Test Player jumps up and down"],
239
["Test Player jumps up and down"])
241
def testSearch(self):
245
"Test Player is great.",
251
"Test Player is great.",
256
[E("[ Test Location ]"),
258
"Location for testing.",
263
"search 'Test Player'",
265
"Test Player is great.",
269
'search "Observer Player"',
271
"Observer Player is great.",
278
def testDescribe(self):
280
"describe me test description",
281
["You change Test Player's description."],
282
["Test Player changes Test Player's description."])
283
self.assertEquals(self.player.description,
287
"describe here description test",
288
["You change Test Location's description."],
289
["Test Player changes Test Location's description."])
290
self.assertEquals(self.location.description,
297
["You change Test Player's name."],
298
["Test Player changes Test Player's name to fooix."])
299
self.assertEquals(self.player.name,
303
"name here CRAP of CRAPness",
304
["You change Test Location's name."],
305
["fooix changes Test Location's name to CRAP of CRAPness."])
306
self.assertEquals(self.location.name,
313
[E("Hit yourself? Stupid.")])
319
self.player.stamina.current = 0
321
"hit Observer Player",
322
["You're too tired!"])
324
self.player.stamina.current = self.player.stamina.max
327
"hit Observer Player",
328
["You hit Observer Player for (\\d+) hitpoints."],
329
["Test Player hits you for (\\d+) hitpoints."])
330
self.assertEquals(x[1].groups(), y[0].groups())
332
self.player.stamina.current = self.player.stamina.max
335
"attack Observer Player",
336
["You hit Observer Player for (\\d+) hitpoints."],
337
["Test Player hits you for (\\d+) hitpoints."])
338
self.assertEquals(x[1].groups(), y[0].groups())
341
def testRestore(self):
348
["Test Location cannot be restored."])
350
self.player.hitpoints.decrease(25)
351
self.player.stamina.decrease(25)
354
["You have fully restored yourself."])
355
self.assertEquals(self.player.hitpoints.current,
356
self.player.hitpoints.max)
357
self.assertEquals(self.player.stamina.current,
358
self.player.stamina.max)
360
self.observer.hitpoints.decrease(25)
361
self.observer.stamina.decrease(25)
363
"restore Observer Player",
364
["You have restored Observer Player to full health."],
365
["Test Player has restored you to full health."])
366
self.assertEquals(self.observer.hitpoints.current,
367
self.observer.hitpoints.max)
368
self.assertEquals(self.observer.stamina.current,
369
self.observer.stamina.max)
372
# XXX This is kind of weak. How can this test be improved?
376
".*Level: (\\d+) Experience: (\\d+)",
377
".*Hitpoints: (\\d+)/(\\d+)",
378
".*Stamina: (\\d+)/(\\d+)",
380
self.assertEquals(x[0].groups(), ()) # Extra line for the command
381
self.assertEquals(x[1].groups(), ())
382
self.assertEquals(x[2].groups(), ('0', '0'))
383
self.assertEquals(x[3].groups(), ('100', '100'))
384
self.assertEquals(x[4].groups(), ('100', '100'))
385
self.assertEquals(x[5].groups(), ())
387
self.player.level = 3
388
self.player.experience = 63
389
self.player.hitpoints.current = 32
390
self.player.hitpoints.max = 74
391
self.player.stamina.current = 12
392
self.player.stamina.max = 39
396
".*Level: (\\d+) Experience: (\\d+)",
397
".*Hitpoints: (\\d+)/(\\d+)",
398
".*Stamina: (\\d+)/(\\d+)",
400
self.assertEquals(x[0].groups(), ())
401
self.assertEquals(x[1].groups(), ())
402
self.assertEquals(x[2].groups(), ('3', '63'))
403
self.assertEquals(x[3].groups(), ('32', '74'))
404
self.assertEquals(x[4].groups(), ('12', '39'))
405
self.assertEquals(x[5].groups(), ())
407
def testCreate(self):
411
["Test Player creates foobar."])
412
foobar = self.player.find("foobar")
413
self.assertEquals(foobar.name, "foobar")
414
self.assertEquals(foobar.description, "an undescribed object")
415
self.assertEquals(foobar.location, self.player)
419
["bar foo created."],
420
["Test Player creates bar foo."])
421
barfoo = self.player.find("bar foo")
422
self.assertEquals(barfoo.name, "bar foo")
423
self.assertEquals(barfoo.description, 'an undescribed object')
424
self.assertEquals(barfoo.location, self.player)
427
"create 'described thing' This is the things description.",
428
["described thing created."],
429
["Test Player creates described thing."])
430
thing = self.player.find("described thing")
431
self.assertEquals(thing.name, "described thing")
432
self.assertEquals(thing.description, "This is the things description.")
433
self.assertEquals(thing.location, self.player)
439
["Test Player creates foobar."])
440
foobar = self.player.find("foobar")
441
self.assertEquals(foobar.name, "foobar")
442
self.assertEquals(foobar.description, "an undescribed monster")
443
self.assertEquals(foobar.location, self.location)
447
["bar foo created."],
448
["Test Player creates bar foo."])
449
barfoo = self.player.find("bar foo")
450
self.assertEquals(barfoo.name, "bar foo")
451
self.assertEquals(barfoo.description, "an undescribed monster")
452
self.assertEquals(barfoo.location, self.location)
455
'spawn "described monster" It looks like a monster with a description.',
456
["described monster created."],
457
["Test Player creates described monster."])
458
monster = self.player.find("described monster")
459
self.assertEquals(monster.name, "described monster")
460
self.assertEquals(monster.description, "It looks like a monster with a description.")
461
self.assertEquals(monster.location, self.location)
463
# XXX Some automatic cleanup maybe?
464
for m in foobar, barfoo, monster:
469
"dig west dark tunnel",
470
["You create an exit."],
471
["Test Player created an exit to the west."])
472
room = self.location.exits['west']
473
self.assertEquals(room.name, "dark tunnel")
474
self.assertEquals(room.description, '')
475
self.assertIdentical(room.exits['east'], self.location)
478
"dig east bright tunnel",
479
["You create an exit."],
480
["Test Player created an exit to the east."])
481
room = self.location.exits['east']
482
self.assertEquals(room.name, "bright tunnel")
483
self.assertEquals(room.description, '')
484
self.assertIdentical(room.exits['west'], self.location)
487
"dig west boring tunnel",
488
["There is already an exit in that direction."])
493
["There isn't an exit in that direction."])
494
self.assertEquals(self.location.exits, {})
496
room = objects.Room("destination")
497
room.exits['north'] = self.location
498
self.location.exits['south'] = room
503
["Test Player destroyed the exit to the south."])
504
self.assertEquals(self.location.exits, {})
505
self.assertEquals(room.exits, {})
507
self.location.exits['south'] = room
508
room.exits['north'] = self.location
509
self.observer.moveTo(room)
514
["The exit to the north crumbles and disappears."])
520
["You can't go that way."])
523
["You can't go that way."])
525
room = objects.Room("destination")
526
self.location.exits["west"] = room
527
room.exits["east"] = self.location
531
[E("[ destination ]"),
534
["Test Player leaves west."])
538
["You can't go that way."])
541
[E("[ Test Location ]"),
543
"Location for testing",
545
["Test Player arrives from the west."])
547
def testScrutinize(self):
548
ptr = "0x[0123456789abcdefABCDEF]{8}"
552
E(" {'_heartbeatCall': <twisted.internet.base.DelayedCall instance at ") + ptr + E(">,"),
553
E(" 'contents': [],"),
554
E(" 'description': '',"),
555
E(" 'hitpoints': pottery.objects.Points(100, 100),"),
556
E(" 'location': Room(name='Test Location', location=None),"),
557
E(" 'name': 'Test Player',"),
558
E(" 'proto': <pottery.test.test_commands.Protocol instance at ") + ptr + E(">,"),
559
E(" 'stamina': pottery.objects.Points(100, 100),"),
560
E(" 'strength': pottery.objects.Points(100, 100),"),
561
E(" 'termAttrs': AttributeSet(bold=False, underline=False, reverseVideo=False, blink=False, fg=9, bg=9),"),
562
E(" 'useColors': False})")])
567
E(" {'contents': [Player(name='Test Player', location=Room(name='Test Location', location=None)),"),
568
E(" Player(name='Observer Player', location=Room(name='Test Location', location=None))],"),
569
E(" 'description': 'Location for testing.',"),
571
E(" 'name': 'Test Location'})")])