-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_actions.py
960 lines (798 loc) · 30.8 KB
/
test_actions.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
"""
Unit tests for Imaginary actions.
"""
from zope.interface import Interface, implementer
from twisted.trial import unittest
from twisted.python import filepath
from axiom.item import Item
from axiom.attributes import reference
from imaginary import iimaginary, objects, events, pyparsing
from imaginary.enhancement import Enhancement
from imaginary.action import Action, TargetAction, Help
from imaginary.test import commandutils
from imaginary.test.commandutils import E
# Regular expression for matching variable parts of the output of certain
# commands.
PTR = "[a-fA-F0-9]{1,16}"
STOREID = "\\d+"
class TransactionalEventBroadcasterTestCase(unittest.TestCase):
def testAddEvent(self):
L = []
teb = events.TransactionalEventBroadcaster()
teb.addEvent(lambda: L.append('win'))
teb.addRevertEvent(lambda: L.append('lose'))
teb.broadcastEvents()
self.assertEquals(L, ['win'])
def testAddRevertEvent(self):
L = []
teb = events.TransactionalEventBroadcaster()
teb.addEvent(lambda: L.append('lose'))
teb.addRevertEvent(lambda: L.append('win'))
teb.broadcastRevertEvents()
self.assertEquals(L, ['win'])
def testBadEvents(self):
teb = events.TransactionalEventBroadcaster()
self.assertRaises(ValueError, teb.addEvent, None)
self.assertRaises(ValueError, teb.addRevertEvent, None)
class TargetActionTests(commandutils.CommandTestCaseMixin, unittest.TestCase):
"""
Tests for general functionality provided by L{TargetAction} which is not
specific to any particular action.
"""
def test_resolveTarget(self):
"""
L{TargetAction.resolve} finds things by name when passed a C{"target"}
string.
"""
self.assertEqual(
TargetAction().resolve(self.player, "target", u"Observer Player"),
[self.observer])
def test_resolveTargetCaseInsensitively(self):
"""
L{TargetAction.resolve} considers names case-insensitively when
searching for things.
"""
self.assertEqual(
TargetAction().resolve(self.player, "target", u"observer player"),
[self.observer])
class IUnimplemented(Interface):
"""
This is an interface that nothing implements.
"""
class Unperformable(Action):
"""
This is an action that no one can perform.
"""
actorInterface = IUnimplemented
expr = pyparsing.Literal("unperformable")
class AnotherUnperformable(Action):
"""
This is another action that no one can perform.
"""
actorInterface = IUnimplemented
expr = pyparsing.Literal("action-parse-collision")
class IImplemented(Interface):
"""
This is an interface that something implements.
"""
class Performable(Action):
"""
This is an action with the same parse as L{AnotherUnperformable} but which
can actually be performed.
"""
actorInterface = IImplemented
# Share the expression since we're supposed to have the same parse.
expr = AnotherUnperformable.expr
def do(self, player, line):
events.Success(
actor=player.thing,
actorMessage=["You perform the action you're capable of."],
).broadcast()
@implementer(IImplemented)
class Performance(Item, Enhancement):
"""
An enhancement for a L{Thing} confering ability to perform L{Performable}.
"""
powerupInterfaces = [IImplemented]
thing = reference()
class Actions(commandutils.CommandTestCaseMixin, unittest.TestCase):
def testBadCommand(self):
"""
If the line of text corresponds to no existing action then the player is
shown a snarky response indicating no action has been taken.
"""
self._test(
"jibber jabber",
["Bad command or filename."])
def test_missingActorInterface(self):
"""
If the player attempts to invoke an action for which the player does not
provide the required C{actorInterface} then the action is not invoked
and a reasonable explanation is given to the player.
"""
self._test(
"unperformable",
["Bad command or filename."],
[],
)
def test_missingActorInterfaceFallback(self):
"""
If there are two possible action parses for an input and the player only
provides the required C{actorInterface} for the second then the second
action is invoked.
"""
# This test assumes that actions are processed in some order and that
# the order could have some impact on the resulting behavior. There
# is no explicit action order but the order in which Action subclass
# definitions are encountered does control the order in which they are
# later processed. This is not a great property of the system and we
# should do something about it. For now, though, validate our
# assumption that the performable action in this collision comes
# "after" the unperformable action.
index_a = Action.actions.index(AnotherUnperformable)
index_b = Action.actions.index(Performable)
self.assertTrue(
index_a < index_b,
"AnotherUnperformable is at {}; Performable is at {}".format(
index_a,
index_b,
),
)
Performance.createFor(self.player)
self._test(
"action-parse-collision",
["You perform the action you're capable of."],
[],
)
def testInventory(self):
# There ain't no stuff
self._test(
"inventory",
["Inventory:"])
playerContainer = iimaginary.IContainer(self.player)
# Give 'em something and make sure
# they show up
playerContainer.add(objects.Thing(store=self.store, name=u"foobar"))
self._test(
"inventory",
["Inventory:",
"foobar"])
# Give 'em a couple more things
playerContainer.add(objects.Thing(store=self.store, name=u"barbaz"))
playerContainer.add(objects.Thing(store=self.store, name=u"barbaz"))
self._test(
"inventory",
["Inventory:",
"foobar",
"barbaz",
"barbaz"])
def test_actions(self):
"""
The I{actions} action lists all of the actions a character can take.
"""
cmds = dict.fromkeys([
getattr(cls, 'actionName', cls.__name__.lower())
for cls
in Action.actions]).keys()
cmds.sort()
self._test(
"actions",
[' '.join(cmds)])
def test_commands(self):
"""
The I{commands} action is a stub to help those unfamiliar with
Imaginary's action system find their way to the I{actions} command
instead.
"""
self._test(
"commands",
["Try 'actions' instead."])
def testGet(self):
# Try to get something that does not exist
self._test(
"get foobar",
["Nothing like that around here."])
# Try to get yourself
self._test(
"get self",
["You cannot take Test Player."])
self.assertEquals(self.player.location, self.location)
# Try to get the location
self._test(
"get here",
["You cannot take Test Location."])
self.assertEquals(self.location.location, None)
# Make an object and try to get it
o = objects.Thing(store=self.store, name=u"foo")
iimaginary.IContainer(self.location).add(o)
self._test(
"get foo",
["You take a foo."],
["Test Player takes a foo."])
self.assertEquals(o.location, self.player)
# Try to get the observer
self._test(
"get 'Observer Player'",
["Observer Player is too heavy to pick up."],
["Test Player tries to pick you up, but fails."])
self.assertEquals(self.observer.location, self.location)
# Container stuff
self._test(
"get baz from bar",
["Nothing like that around here."])
c = objects.Thing(store=self.store, name=u"bar")
cContainer = objects.Container.createFor(c, capacity=1)
iimaginary.IContainer(self.location).add(c)
o = objects.Thing(store=self.store, name=u"baz")
cContainer.add(o)
self._test(
"get baz from bar",
["You take a baz from the bar."],
["Test Player takes a baz from the bar."])
self.assertEquals(o.location, self.player)
# Can't get things from a closed container
o.moveTo(c)
cContainer.closed = True
self._test(
"get baz from bar",
["Nothing like that around here."],
[])
self.assertEquals(o.location, c)
self.assertEquals(list(cContainer.getContents()), [o])
def testTakeImmoveable(self):
"""
A L{Thing} with C{portable} set to C{False} cannot be taken.
"""
immoveable = objects.Thing(
store=self.store,
name=u"immoveable",
portable=False,
)
iimaginary.IContainer(self.location).add(immoveable)
self._test(
"take immoveable",
["An immoveable cannot be taken."],
["Test Player regards an immoveable thoughtfully."],
)
def testTakeNonContainingImmoveable(self):
"""
A L{Thing} with C{portable} set to C{False} cannot be taken even if the
taker is not contained by it. it.
"""
immoveable = objects.Thing(
store=self.store,
name=u"immoveable",
portable=False,
)
objects.Container.createFor(immoveable)
objects.Exit.link(self.location, immoveable, u'north')
self._test(
"take immoveable",
["An immoveable cannot be taken."],
["Test Player regards an immoveable thoughtfully."],
)
def testDrop(self):
self._test(
"drop foo",
["Nothing like that around here."])
o = objects.Thing(store=self.store, name=u"bar")
iimaginary.IContainer(self.player).add(o)
self._test(
"drop bar",
["You drop the bar."],
["Test Player drops a bar."])
self.assertEquals(o.location, self.location)
def testSay(self):
self._test(
"say hello",
["You say, 'hello'"],
["Test Player says, 'hello'"])
self._test(
"'hello",
["You say, 'hello'"],
["Test Player says, 'hello'"])
self._test(
"'hello world! quote -> '",
["You say, 'hello world! quote -> ''"],
["Test Player says, 'hello world! quote -> ''"])
def testEmote(self):
self._test(
"emote jumps up and down",
["Test Player jumps up and down"],
["Test Player jumps up and down"])
self._test(
":jumps up and down",
["Test Player jumps up and down"],
["Test Player jumps up and down"])
def testDescribe(self):
self._test(
"describe me test description",
["You change Test Player's description."],
["Test Player changes Test Player's description."])
self.assertEquals(self.player.description,
"test description")
self._test(
"describe here description test",
["You change Test Location's description."],
["Test Player changes Test Location's description."])
self.assertEquals(self.location.description,
"description test")
def testName(self):
self._test(
"name me fooix",
["You change Test Player's name."],
["Test Player changes Test Player's name to fooix."])
self.assertEquals(self.player.name,
"fooix")
self._test(
"name here CRAP of CRAPness",
["You change Test Location's name."],
["Fooix changes Test Location's name to CRAP of CRAPness."])
self.assertEquals(self.location.name,
"CRAP of CRAPness")
def testRestore(self):
self._test(
"restore foobar",
[E("Who's that?")])
self._test(
"restore here",
["Test Location cannot be restored."])
actor = iimaginary.IActor(self.player)
actor.hitpoints.decrease(25)
actor.stamina.decrease(25)
self._test(
"restore self",
["You have fully restored yourself."])
self.assertEquals(actor.hitpoints.current,
actor.hitpoints.max)
self.assertEquals(actor.stamina.current,
actor.stamina.max)
oactor = iimaginary.IActor(self.observer)
oactor.hitpoints.decrease(25)
oactor.stamina.decrease(25)
self._test(
"restore Observer Player",
["You have restored Observer Player to full health."],
["Test Player has restored you to full health."])
self.assertEquals(oactor.hitpoints.current,
oactor.hitpoints.max)
self.assertEquals(oactor.stamina.current,
oactor.stamina.max)
def testScore(self):
# XXX This is kind of weak. How can this test be improved?
x, y = self._test(
"score",
[".*",
".*Level: +(\\d+) Experience: +(\\d+)",
".*Hitpoints: +(\\d+)/(\\d+)",
".*Stamina: +(\\d+)/(\\d+)",
".*"])
self.assertEquals(x[0].groups(), ()) # Extra line for the command
self.assertEquals(x[1].groups(), ())
self.assertEquals(x[2].groups(), ('0', '0'))
self.assertEquals(x[3].groups(), ('100', '100'))
self.assertEquals(x[4].groups(), ('100', '100'))
self.assertEquals(x[5].groups(), ())
actor = iimaginary.IActor(self.player)
actor.level = 3
actor.experience = 63
actor.hitpoints.current = 32
actor.hitpoints.max = 74
actor.stamina.current = 12
actor.stamina.max = 39
x, y = self._test(
"score",
[".*",
".*Level: +(\\d+) Experience: +(\\d+)",
".*Hitpoints: +(\\d+)/(\\d+)",
".*Stamina: +(\\d+)/(\\d+)",
".*"])
self.assertEquals(x[0].groups(), ())
self.assertEquals(x[1].groups(), ())
self.assertEquals(x[2].groups(), ('3', '63'))
self.assertEquals(x[3].groups(), ('32', '74'))
self.assertEquals(x[4].groups(), ('12', '39'))
self.assertEquals(x[5].groups(), ())
def test_dig(self):
"""
The I{dig} action creates a new location connected to the current
location through an exit in the specified direction.
"""
self._test(
"dig west dark tunnel",
["You create an exit."],
["Test Player created an exit to the west."])
room = iimaginary.IContainer(self.location).getExitNamed(u'west').toLocation
self.assertEquals(room.name, u"dark tunnel")
self.assertEquals(room.description, u'')
self.assertIdentical(iimaginary.IContainer(room).getExitNamed(u'east').toLocation,
self.location)
self._test(
"dig east bright tunnel",
["You create an exit."],
["Test Player created an exit to the east."])
room = iimaginary.IContainer(self.location).getExitNamed(u'east').toLocation
self.assertEquals(room.name, u"bright tunnel")
self.assertEquals(room.description, u'')
self.assertIdentical(iimaginary.IContainer(room).getExitNamed(u'west').toLocation, self.location)
self._test(
"dig west boring tunnel",
["There is already an exit in that direction."])
def test_digWithDirectionAliases(self):
"""
The I{dig} action creates a new location connected to the current
location through an exit in the specified direction even when that
direction is an alias.
"""
self._test(
"dig w dark tunnel",
["You create an exit."],
["Test Player created an exit to the west."])
room = iimaginary.IContainer(self.location).getExitNamed(u'west').toLocation
self.assertEquals(room.name, u"dark tunnel")
self.assertEquals(room.description, u'')
self.assertIdentical(iimaginary.IContainer(room).getExitNamed(u'east').toLocation,
self.location)
self._test(
"dig e bright tunnel",
["You create an exit."],
["Test Player created an exit to the east."])
room = iimaginary.IContainer(self.location).getExitNamed(u'east').toLocation
self.assertEquals(room.name, u"bright tunnel")
self.assertEquals(room.description, u'')
self.assertIdentical(iimaginary.IContainer(room).getExitNamed(u'west').toLocation, self.location)
self._test(
"dig w boring tunnel",
["There is already an exit in that direction."])
def test_bury(self):
"""
The I{bury} action destroys an exit in the specified direction.
"""
self._test(
"bury south",
["There isn't an exit in that direction."])
self.assertEquals(list(iimaginary.IContainer(self.location).getExits()), [])
room = objects.Thing(store=self.store, name=u"destination", proper=True)
objects.Container.createFor(room, capacity=1000)
objects.Exit.link(room, self.location, u'north')
self._test(
"bury south",
["It's gone."],
["Test Player destroyed the exit to destination."])
self.assertEquals(
list(iimaginary.IContainer(self.location).getExits()),
[])
self.assertEquals(
list(iimaginary.IContainer(room).getExits()),
[])
objects.Exit.link(self.location, room, u'south')
self.observer.moveTo(room)
self._test(
"bury south",
["It's gone."],
["The exit to Test Location crumbles and disappears."])
self.assertEquals(
list(iimaginary.IContainer(self.location).getExits()),
[])
self.assertEquals(
list(iimaginary.IContainer(room).getExits()),
[])
def test_buryWithDirectionAliases(self):
"""
The I{bury} action destroys an exit in the specified direction even
when that direction is an alias.
"""
self._test(
"bury s",
["There isn't an exit in that direction."])
self.assertEquals(list(iimaginary.IContainer(self.location).getExits()), [])
room = objects.Thing(store=self.store, name=u"destination", proper=True)
objects.Container.createFor(room, capacity=1000)
objects.Exit.link(room, self.location, u'north')
self._test(
"bury s",
["It's gone."],
["Test Player destroyed the exit to destination."])
self.assertEquals(
list(iimaginary.IContainer(self.location).getExits()),
[])
self.assertEquals(
list(iimaginary.IContainer(room).getExits()),
[])
objects.Exit.link(self.location, room, u'south')
self.observer.moveTo(room)
self._test(
"bury s",
["It's gone."],
["The exit to Test Location crumbles and disappears."])
self.assertEquals(
list(iimaginary.IContainer(self.location).getExits()),
[])
self.assertEquals(
list(iimaginary.IContainer(room).getExits()),
[])
def test_go(self):
"""
The I{go} action moves the player through an exit in the specified
direction.
"""
self._test(
"go west",
["You can't go that way."])
self._test(
"west",
["You can't go that way."])
room = objects.Thing(store=self.store, name=u"destination")
objects.Container.createFor(room, capacity=1000)
objects.Exit.link(self.location, room, u"west")
self._test(
"west",
[E("[ destination ]"),
E("( east )"),
""],
["Test Player leaves west."])
self._test(
"north",
["You can't go that way."])
self._test(
"go east",
[E("[ Test Location ]"),
E("( west )"),
"Location for testing.",
"Here, you see Observer Player."],
["Test Player arrives from the west."])
def test_goThroughDirectionAliases(self):
"""
The I{go} action moves the player through an exit in the specified
direction even when that direction is an alias.
"""
self._test(
"go w",
["You can't go that way."])
self._test(
"w",
["You can't go that way."])
room = objects.Thing(store=self.store, name=u"destination")
objects.Container.createFor(room, capacity=1000)
objects.Exit.link(self.location, room, u"west")
self._test(
"w",
[E("[ destination ]"),
E("( east )"),
""],
["Test Player leaves west."])
self._test(
"n",
["You can't go that way."])
self._test(
"go e",
[E("[ Test Location ]"),
E("( west )"),
"Location for testing.",
"Here, you see Observer Player."],
["Test Player arrives from the west."])
def test_goThroughOneWayExit(self):
"""
Going through a one-way exit with a known direction will announce that
the player arrived from that direction; with an unknown direction it
will simply announce that they have arrived.
"""
secretRoom = objects.Thing(store=self.store, name=u'Secret Room!')
objects.Container.createFor(secretRoom, capacity=1000)
myExit = objects.Exit(store=self.store, fromLocation=secretRoom,
toLocation=self.location, name=u'north')
self.player.moveTo(secretRoom)
self._test(
"north",
[E("[ Test Location ]"),
"Location for testing.",
"Here, you see Observer Player."],
["Test Player arrives from the south."])
self.player.moveTo(secretRoom)
myExit.name = u'elsewhere'
self.assertCommandOutput(
"go elsewhere",
[E("[ Test Location ]"),
"Location for testing.",
"Here, you see Observer Player."],
["Test Player arrives."])
def test_goDoesntJumpOverExits(self):
"""
You can't go through an exit without passing through exits which lead
to it. Going through an exit named 'east' will only work if it is east
of your I{present} location, even if it is easily reachable from where
you stand.
"""
northRoom = objects.Thing(store=self.store, name=u'Northerly')
eastRoom = objects.Thing(store=self.store, name=u'Easterly')
for room in northRoom, eastRoom:
objects.Container.createFor(room, capacity=1000)
objects.Exit.link(self.location, northRoom, u'north', distance=0.1)
objects.Exit.link(northRoom, eastRoom, u'east', distance=0.1)
self.assertCommandOutput("go east", [E("You can't go that way.")], [])
def test_directionalMovement(self):
"""
You can move through exits in standard directions by just specifying
the direction.
"""
# A couple tweaks to state to make the test simpler
self.observer.location = None
self.location.description = None
oldRoom = self.location
allDirections = ["northwest", "north", "northeast", "east",
"west", "southwest", "south", "southeast"]
for d in allDirections[:len(allDirections) / 2]:
room = objects.Thing(store=self.store, name=u"destination")
objects.Container.createFor(room, capacity=1000)
objects.Exit.link(oldRoom, room, unicode(d, 'ascii'))
oldRoom = room
for d, rd in zip(allDirections, reversed(allDirections)):
self._test(
d,
[E("[ ") + ".*" + E(" ]"), # Not testing room description
E("( ") + ".*" + E(" )"), # Just do enough to make sure it was not an error.
""])
shortDirections = ["nw", "n", "ne", "e", "w", "sw", "s", "se"]
for d, rd in zip(shortDirections, reversed(shortDirections)):
self._test(
d,
[E("[ ") + ".*" + E(" ]"), # Not testing room description
E("( ") + ".*" + E(" )"), # Just do enough to make sure it was not an error.
""])
def test_goClosed(self):
"""
You cannot move through an exit to a "closed" location.
"""
unreachable = objects.Thing(store=self.store, name=u"unreachable")
objects.Container.createFor(unreachable, capacity=1000, closed=True)
objects.Exit.link(self.location, unreachable, u"west")
self._test(
"west",
["The way is shut."],
[],
)
def test_scrutinize(self):
"""
The scrutinize action takes a thing as a target and displays a lot of
details about its internal state and construction.
"""
self._test(
"scrutinize me",
[E("('Thing',"),
E(" {'contents': [],"),
E(" 'description': u'',"),
E(" 'gender': 2,"),
E(" 'location': Thing(description=u'Location for testing.', "
"gender=3, location=None, name=u'Test Location', portable=True, "
"proper=True, weight=1, storeID=") + STOREID + E(")@0x") + PTR
+ E(","),
E(" 'name': u'Test Player',"),
E(" 'portable': True,"),
E(" 'proper': True,"),
E(" 'weight': 100})"),
])
self._test(
"scrutinize here",
[E("('Thing',"),
E(" {'contents': [Thing(description=u'', gender=2, location="
"reference(") +
STOREID + E("), name=u'Test Player', portable=True, proper="
"True, weight=100, storeID=") +
STOREID + E(")@0x") + PTR + E(","),
E(" Thing(description=u'', gender=2, location="
"reference(") +
STOREID + E("), name=u'Observer Player', portable=True, "
"proper=True, weight=100, storeID=") +
STOREID + E(")@0x") + PTR + E("],"),
E(" 'description': u'Location for testing.',"),
E(" 'gender': 3,"),
E(" 'location': None,"),
E(" 'name': u'Test Location',"),
E(" 'portable': True,"),
E(" 'proper': True,"),
E(" 'weight': 1})")])
def test_scrutinizeNonContainer(self):
"""
The scrutinize action produces results for a thing which is not a
container.
"""
o = objects.Thing(store=self.store, name=u"foo")
iimaginary.IContainer(self.location).add(o)
self._test(
"scrutinize foo",
[E(u"('Thing',"),
E(u" {'description': u'',"),
E(u" 'gender': 3,"),
E(u" 'location': Thing(description=u'Location for testing.', "
"gender=3, location=None, name=u'Test Location', portable="
"True, proper=True, weight=1, storeID=") +
STOREID + E(")@0x") + PTR + E(","),
E(u" 'name': u'foo',"),
E(u" 'portable': True,"),
E(u" 'proper': False,"),
E(u" 'weight': 1})")])
def testOpenClose(self):
container = objects.Thing(store=self.store, name=u"container")
objects.Container.createFor(container, capacity=1)
iimaginary.IContainer(self.location).add(container)
self._test(
"close container",
["You close the container."],
["Test Player closes a container."])
self._test(
"close container",
["The container is already closed."],
[])
self._test(
"open container",
["You open the container."],
["Test Player opens a container."])
self._test(
"open container",
["The container is already open."],
[])
def test_invalidHelp(self):
"""
The help action tells the player that there is no help for topics for
which there is no help.
"""
self._test(
# Hope you don't make a command with this name.
"help abcxyz123",
["No help available on abcxyz123."],
[])
self._test(
"help /etc/passwd",
["No help available on /etc/passwd."],
[])
def test_help(self):
"""
The help action displays information from files in the resource
directory.
"""
resources = filepath.FilePath(__file__).parent().sibling("resources")
self._test(
"help help",
resources.child("help").child("help").getContent().splitlines(),
[])
def test_helpMultiword(self):
"""
The help action supports arguments with spaces in them.
"""
fakeHelp = filepath.FilePath(self.mktemp())
fakeHelp.makedirs()
fakeHelp.child("foo bar").setContent("baz")
original = Help.helpContentPath
try:
Help.helpContentPath = fakeHelp
self._test("help foo bar", ["baz"], [])
finally:
Help.helpContentPath = original
def test_hit(self):
"""
The hit action removes some hitpoints from the target.
"""
self._violenceTest("hit")
def test_kill(self):
"""
The hit action has I{kill} as an alias.
"""
self._violenceTest("hit")
def test_attack(self):
"""
The hit action has I{attack} as an alias.
"""
self._violenceTest("hit")
def _violenceTest(self, verb):
fuzzy = self.world.create(u"fuzzy", proper=True)
# Flush the buffer to simplify the real assertion.
self.assertCommandOutput(
None,
["Fuzzy arrives."],
["Fuzzy arrives."],
)
self.assertCommandOutput(
verb + " fuzzy",
["You hit fuzzy for [12345] hitpoints."],
["Test Player hits fuzzy."],
)