~divmod-dev/divmod.org/no-addperson-2190

« back to all changes in this revision

Viewing changes to Imaginary/pottery/test/test_commands.py

  • Committer: exarkun
  • Date: 2006-02-26 02:37:39 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:4991
Merge move-pottery-to-trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""Unit tests for Pottery commands.
 
3
"""
 
4
 
 
5
import re, pprint
 
6
 
 
7
from twisted.trial import unittest
 
8
from twisted.test.proto_helpers import StringTransport
 
9
 
 
10
from pottery import objects, wiring, commands
 
11
 
 
12
E = re.escape
 
13
 
 
14
class Commands(unittest.TestCase):
 
15
    def setUp(self):
 
16
        self.location = objects.Room("Test Location",
 
17
                                     "Location for testing.")
 
18
 
 
19
        self.player = objects.Player("Test Player")
 
20
        self.player.useColors = False
 
21
        self.location.add(self.player)
 
22
        self.transport = StringTransport()
 
23
        class Protocol:
 
24
            write = self.transport.write
 
25
        self.player.setProtocol(Protocol())
 
26
 
 
27
        self.observer = objects.Player("Observer Player")
 
28
        self.location.add(self.observer)
 
29
        self.otransport = StringTransport()
 
30
        class Protocol:
 
31
            write = self.otransport.write
 
32
        self.observer.setProtocol(Protocol())
 
33
 
 
34
 
 
35
    def tearDown(self):
 
36
        for p in self.player, self.observer:
 
37
            try:
 
38
                p.destroy()
 
39
            except AttributeError:
 
40
                pass
 
41
 
 
42
    def _test(self, command, output, observed=()):
 
43
        wiring.parse(self.transport, self.player, command)
 
44
 
 
45
        output.insert(0, "> " + command)
 
46
 
 
47
        results = []
 
48
        for xport, oput in ((self.transport, output),
 
49
                            (self.otransport, observed)):
 
50
            results.append([])
 
51
            gotLines = xport.value().splitlines()
 
52
            for i, (got, expected) in enumerate(map(None, gotLines, oput)):
 
53
                got = got or ''
 
54
                expected = expected or '$^'
 
55
                m = re.compile(expected.rstrip()).match(got.rstrip())
 
56
                if m is None:
 
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))
 
61
                results[-1].append(m)
 
62
            xport.clear()
 
63
        return results
 
64
 
 
65
    def testBadCommand(self):
 
66
        self._test(
 
67
            "jibber jabber",
 
68
            ["Bad command or filename"])
 
69
 
 
70
    def testInventory(self):
 
71
        # There ain't no stuff
 
72
        self._test(
 
73
            "inventory",
 
74
            ["Inventory:"])
 
75
 
 
76
        # Give 'em something and make sure
 
77
        # they show up
 
78
        self.player.add(objects.Object("foobar"))
 
79
        self._test(
 
80
            "inventory",
 
81
            ["Inventory:",
 
82
             "foobar"])
 
83
 
 
84
        # Give 'em a couple more things
 
85
        self.player.add(objects.Object("barbaz"))
 
86
        self.player.add(objects.Object("barbaz"))
 
87
        self._test(
 
88
            "inventory",
 
89
            ["Inventory:",
 
90
             "foobar",
 
91
             "barbaz",
 
92
             "barbaz"])
 
93
 
 
94
    def testCommands(self):
 
95
        cmds = dict.fromkeys([
 
96
                getattr(cls, 'commandName', cls.__name__.lower())
 
97
                for cls
 
98
                in commands.Command.commands.values()]).keys()
 
99
        cmds.sort()
 
100
        self._test(
 
101
            "commands",
 
102
            [' '.join(cmds)])
 
103
 
 
104
    def testGet(self):
 
105
        # Try to get something that does not exist
 
106
        self._test(
 
107
            "get foobar",
 
108
            ["Nothing named foobar here."])
 
109
 
 
110
        # Try to get yourself
 
111
        self._test(
 
112
            "get self",
 
113
            ["Yea, right."])
 
114
        self.assertEquals(self.player.location, self.location)
 
115
 
 
116
        # Try to get the location
 
117
        self._test(
 
118
            "get here",
 
119
            ["Yea, right."])
 
120
        self.assertEquals(self.location.location, None)
 
121
 
 
122
        # Make an object and try to get it
 
123
        o = objects.Object("foo")
 
124
        self.location.add(o)
 
125
        self._test(
 
126
            "get foo",
 
127
            ["You take foo.",
 
128
             ">"],
 
129
            ["Test Player takes foo.",
 
130
             ">"])
 
131
        self.assertEquals(o.location, self.player)
 
132
 
 
133
        # Try to get the observer
 
134
        self._test(
 
135
            "get 'Observer Player'",
 
136
            ["Observer Player is too heavy to pick up.",
 
137
             ">"],
 
138
            ["Test Player tries to pick you up, but fails.",
 
139
             ">"])
 
140
        self.assertEquals(self.observer.location, self.location)
 
141
 
 
142
        # Container stuff
 
143
        self._test(
 
144
            "get baz from bar",
 
145
            ["Nothing named bar around here.",
 
146
             ">"])
 
147
 
 
148
        c = objects.Container("bar")
 
149
        self.location.add(c)
 
150
        o = objects.Object("baz")
 
151
        c.add(o)
 
152
        self._test(
 
153
            "get baz from bar",
 
154
            ["You take baz from bar.",
 
155
             ">"],
 
156
            ["Test Player takes baz from bar.",
 
157
             ">"])
 
158
        self.assertEquals(o.location, self.player)
 
159
 
 
160
    def testDrop(self):
 
161
        self._test(
 
162
            "drop foo",
 
163
            ["Nothing like that around here."])
 
164
 
 
165
        o = objects.Object("bar")
 
166
        self.player.add(o)
 
167
        self._test(
 
168
            "drop bar",
 
169
            ["You drop bar."],
 
170
            ["Test Player drops bar."])
 
171
        self.assertEquals(o.location, self.location)
 
172
 
 
173
    def testLook(self):
 
174
        self._test(
 
175
            "look",
 
176
            [E("[ Test Location ]"),
 
177
             E("(  )"),
 
178
             "Location for testing.",
 
179
             "Observer Player"])
 
180
 
 
181
        self._test(
 
182
            "look here",
 
183
            [E("[ Test Location ]"),
 
184
             E("(  )"),
 
185
             "Location for testing.",
 
186
             "Observer Player"])
 
187
 
 
188
        self._test(
 
189
            "look me",
 
190
            ["Test Player",
 
191
             "Test Player is great"])
 
192
 
 
193
        self._test(
 
194
            "look at me",
 
195
            ["Test Player",
 
196
             "Test Player is great"])
 
197
 
 
198
        self._test(
 
199
            "look at Observer Player",
 
200
            ["Observer Player",
 
201
             "Observer Player is great"],
 
202
            ["Test Player looks at you."])
 
203
 
 
204
        o = objects.Object("foo")
 
205
        self.location.add(o)
 
206
        self._test(
 
207
            "look at foo",
 
208
            ["foo"])
 
209
 
 
210
        self._test(
 
211
            "look at bar",
 
212
            ["You don't see bar here."])
 
213
 
 
214
    def testSay(self):
 
215
        self._test(
 
216
            "say hello",
 
217
            ["You say, 'hello'"],
 
218
            ["Test Player says, 'hello'"])
 
219
 
 
220
        self._test(
 
221
            "'hello",
 
222
            ["You say, 'hello'"],
 
223
            ["Test Player says, 'hello'"])
 
224
 
 
225
        self._test(
 
226
            "'hello world! quote -> '",
 
227
            ["You say, 'hello world! quote -> ''"],
 
228
            ["Test Player says, 'hello world! quote -> ''"])
 
229
 
 
230
    def testEmote(self):
 
231
        self._test(
 
232
            "emote jumps up and down",
 
233
            ["Test Player jumps up and down"],
 
234
            ["Test Player jumps up and down"])
 
235
 
 
236
        self._test(
 
237
            ":jumps up and down",
 
238
            ["Test Player jumps up and down"],
 
239
            ["Test Player jumps up and down"])
 
240
 
 
241
    def testSearch(self):
 
242
        self._test(
 
243
            "search self",
 
244
            ["Test Player",
 
245
             "Test Player is great.",
 
246
             ""])
 
247
 
 
248
        self._test(
 
249
            "search me",
 
250
            ["Test Player",
 
251
             "Test Player is great.",
 
252
             ""])
 
253
 
 
254
        self._test(
 
255
            "search here",
 
256
            [E("[ Test Location ]"),
 
257
             E("(  )"),
 
258
             "Location for testing.",
 
259
             "Observer Player",
 
260
             ""])
 
261
 
 
262
        self._test(
 
263
            "search 'Test Player'",
 
264
            ["Test Player",
 
265
             "Test Player is great.",
 
266
             ""])
 
267
 
 
268
        self._test(
 
269
            'search "Observer Player"',
 
270
            ["Observer Player",
 
271
             "Observer Player is great.",
 
272
             ""])
 
273
 
 
274
        self._test(
 
275
            "search blub",
 
276
            [""])
 
277
 
 
278
    def testDescribe(self):
 
279
        self._test(
 
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,
 
284
                          "test description")
 
285
 
 
286
        self._test(
 
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,
 
291
                          "description test")
 
292
 
 
293
 
 
294
    def testName(self):
 
295
        self._test(
 
296
            "name me fooix",
 
297
            ["You change Test Player's name."],
 
298
            ["Test Player changes Test Player's name to fooix."])
 
299
        self.assertEquals(self.player.name,
 
300
                          "fooix")
 
301
 
 
302
        self._test(
 
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,
 
307
                          "CRAP of CRAPness")
 
308
 
 
309
 
 
310
    def testHit(self):
 
311
        self._test(
 
312
            "hit self",
 
313
            [E("Hit yourself?  Stupid.")])
 
314
 
 
315
        self._test(
 
316
            "hit foobar",
 
317
            ["Who's that?"])
 
318
 
 
319
        self.player.stamina.current = 0
 
320
        self._test(
 
321
            "hit Observer Player",
 
322
            ["You're too tired!"])
 
323
 
 
324
        self.player.stamina.current = self.player.stamina.max
 
325
 
 
326
        x, y = self._test(
 
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())
 
331
 
 
332
        self.player.stamina.current = self.player.stamina.max
 
333
 
 
334
        x, y = self._test(
 
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())
 
339
 
 
340
 
 
341
    def testRestore(self):
 
342
        self._test(
 
343
            "restore foobar",
 
344
            ["Who's that?"])
 
345
 
 
346
        self._test(
 
347
            "restore here",
 
348
            ["Test Location cannot be restored."])
 
349
 
 
350
        self.player.hitpoints.decrease(25)
 
351
        self.player.stamina.decrease(25)
 
352
        self._test(
 
353
            "restore self",
 
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)
 
359
 
 
360
        self.observer.hitpoints.decrease(25)
 
361
        self.observer.stamina.decrease(25)
 
362
        self._test(
 
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)
 
370
 
 
371
    def testScore(self):
 
372
        # XXX This is kind of weak.  How can this test be improved?
 
373
        x, y = self._test(
 
374
            "score",
 
375
            [".*",
 
376
             ".*Level: (\\d+) Experience: (\\d+)",
 
377
             ".*Hitpoints: (\\d+)/(\\d+)",
 
378
             ".*Stamina: (\\d+)/(\\d+)",
 
379
             ".*"])
 
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(), ())
 
386
 
 
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
 
393
        x, y = self._test(
 
394
            "score",
 
395
            [".*",
 
396
             ".*Level: (\\d+) Experience: (\\d+)",
 
397
             ".*Hitpoints: (\\d+)/(\\d+)",
 
398
             ".*Stamina: (\\d+)/(\\d+)",
 
399
             ".*"])
 
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(), ())
 
406
 
 
407
    def testCreate(self):
 
408
        self._test(
 
409
            "create foobar",
 
410
            ["foobar created."],
 
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)
 
416
 
 
417
        self._test(
 
418
            "create 'bar foo'",
 
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)
 
425
 
 
426
        self._test(
 
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)
 
434
 
 
435
    def testSpawn(self):
 
436
        self._test(
 
437
            "spawn foobar",
 
438
            ["foobar created."],
 
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)
 
444
 
 
445
        self._test(
 
446
            'spawn "bar foo"',
 
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)
 
453
 
 
454
        self._test(
 
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)
 
462
 
 
463
        # XXX Some automatic cleanup maybe?
 
464
        for m in foobar, barfoo, monster:
 
465
            m.destroy()
 
466
 
 
467
    def testDig(self):
 
468
        self._test(
 
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)
 
476
 
 
477
        self._test(
 
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)
 
485
 
 
486
        self._test(
 
487
            "dig west boring tunnel",
 
488
            ["There is already an exit in that direction."])
 
489
 
 
490
    def testBury(self):
 
491
        self._test(
 
492
            "bury south",
 
493
            ["There isn't an exit in that direction."])
 
494
        self.assertEquals(self.location.exits, {})
 
495
 
 
496
        room = objects.Room("destination")
 
497
        room.exits['north'] = self.location
 
498
        self.location.exits['south'] = room
 
499
 
 
500
        self._test(
 
501
            "bury south",
 
502
            ["It's gone."],
 
503
            ["Test Player destroyed the exit to the south."])
 
504
        self.assertEquals(self.location.exits, {})
 
505
        self.assertEquals(room.exits, {})
 
506
 
 
507
        self.location.exits['south'] = room
 
508
        room.exits['north'] = self.location
 
509
        self.observer.moveTo(room)
 
510
 
 
511
        self._test(
 
512
            "bury south",
 
513
            ["It's gone."],
 
514
            ["The exit to the north crumbles and disappears."])
 
515
 
 
516
 
 
517
    def testGo(self):
 
518
        self._test(
 
519
            "go west",
 
520
            ["You can't go that way."])
 
521
        self._test(
 
522
            "west",
 
523
            ["You can't go that way."])
 
524
 
 
525
        room = objects.Room("destination")
 
526
        self.location.exits["west"] = room
 
527
        room.exits["east"] = self.location
 
528
 
 
529
        self._test(
 
530
            "west",
 
531
            [E("[ destination ]"),
 
532
             E("( east )"),
 
533
             ""],
 
534
            ["Test Player leaves west."])
 
535
 
 
536
        self._test(
 
537
            "north",
 
538
            ["You can't go that way."])
 
539
        self._test(
 
540
            "go east",
 
541
            [E("[ Test Location ]"),
 
542
             E("( west )"),
 
543
             "Location for testing",
 
544
             "Observer Player"],
 
545
            ["Test Player arrives from the west."])
 
546
 
 
547
    def testScrutinize(self):
 
548
        ptr = "0x[0123456789abcdefABCDEF]{8}"
 
549
        self._test(
 
550
            "scrutinize me",
 
551
            [E("('Player',"),
 
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})")])
 
563
 
 
564
        self._test(
 
565
            "scrutinize here",
 
566
            [E("('Room',"),
 
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.',"),
 
570
             E("  'exits': {},"),
 
571
             E("  'name': 'Test Location'})")])
 
572