~divmod-dev/divmod.org/dangling-1091

« back to all changes in this revision

Viewing changes to Imaginary/imaginary/test/test_npc.py

  • Committer: exarkun
  • Date: 2006-05-28 17:02:53 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:6857
Merge greeter-930

Authors: exarkun, radix
Reviewer: idnar
Fixes #930

This adds a mouse NPC which squeaks at people, a better event for
things arriving at places, and better documentation for various
things.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from zope.interface import implements
 
3
 
 
4
from twisted.trial import unittest
 
5
 
 
6
from axiom import store, item, attributes
 
7
 
 
8
from imaginary import iimaginary, events, objects, commands, npc
 
9
from imaginary.test import commandutils
 
10
 
 
11
 
 
12
class MockIntelligence(item.Item):
 
13
    implements(iimaginary.IEventObserver)
 
14
 
 
15
    anAttribute = attributes.integer()
 
16
    concepts = attributes.inmemory()
 
17
 
 
18
    def activate(self):
 
19
        self.concepts = []
 
20
 
 
21
 
 
22
    def prepare(self, concept):
 
23
        return lambda: self.concepts.append(concept)
 
24
 
 
25
 
 
26
class IntelligenceTestCase(unittest.TestCase):
 
27
    def setUp(self):
 
28
        self.store = store.Store()
 
29
 
 
30
        self.location = objects.Thing(store=self.store, name=u"Place")
 
31
        self.locationContainer = objects.Container(store=self.store, capacity=1000)
 
32
        self.locationContainer.installOn(self.location)
 
33
 
 
34
        self.alice = objects.Thing(store=self.store, name=u"Alice")
 
35
        self.actor = objects.Actor(store=self.store)
 
36
        self.actor.installOn(self.alice)
 
37
 
 
38
        self.alice.moveTo(self.location)
 
39
 
 
40
        self.intelligence = MockIntelligence(store=self.store)
 
41
        self.actor.setEnduringIntelligence(self.intelligence)
 
42
 
 
43
 
 
44
    def test_intelligenceReceivesEvent(self):
 
45
        """
 
46
        Enduring intelligences should receive events.
 
47
        """
 
48
        evt = events.Success(
 
49
            location=self.location,
 
50
            otherMessage=u"Hello, how are you?")
 
51
 
 
52
        self.actor.send(evt)
 
53
        self.assertEquals(self.intelligence.concepts, [evt])
 
54
 
 
55
 
 
56
    def test_persistentIntelligence(self):
 
57
        """
 
58
        Whitebox test that enduring intelligencii are actually persistent.
 
59
        """
 
60
        # TB <---- THAT MEANS IT'S TRANSLUCENT
 
61
        self.assertIdentical(
 
62
            self.store.findUnique(
 
63
                objects.Actor,
 
64
                objects.Actor._enduringIntelligence == self.intelligence),
 
65
            self.actor)
 
66
 
 
67
 
 
68
 
 
69
class MouseTestCase(unittest.TestCase):
 
70
    def setUp(self):
 
71
        self.store = store.Store()
 
72
 
 
73
        self.clock = objects.Thing(store=self.store, name=u"Clock")
 
74
        self.clockContainer = objects.Container(store=self.store, capacity=10)
 
75
        self.clockContainer.installOn(self.clock)
 
76
 
 
77
        self.mouse = npc.createMouse(store=self.store, name=u"Squeaker McSqueakenson")
 
78
        self.mouseActor = iimaginary.IActor(self.mouse)
 
79
        self.mousehood = self.mouseActor.getIntelligence()
 
80
        self.mouse.moveTo(self.clock)
 
81
 
 
82
        self.player = objects.Thing(store=self.store, name=u"Mean Old Man")
 
83
        self.playerActor = objects.Actor(store=self.store)
 
84
        self.playerActor.installOn(self.player)
 
85
        self.playerIntelligence = MockIntelligence(store=self.store)
 
86
        self.playerActor.setEnduringIntelligence(self.playerIntelligence)
 
87
 
 
88
        self.player.moveTo(self.clock)
 
89
 
 
90
 
 
91
    def test_mouseSqueaksAtIntruders(self):
 
92
        """
 
93
        When a mean old man walks into the mouse's clock, the mouse will squeak
 
94
        ruthlessly.
 
95
        """
 
96
        evt = events.ArrivalEvent(thing=self.player, origin=None)
 
97
        self.mouseActor.send(evt)
 
98
 
 
99
        self.assertEquals(len(self.playerIntelligence.concepts), 1)
 
100
        event = self.playerIntelligence.concepts[0]
 
101
        self.assertEquals(
 
102
            commandutils.flatten(event.otherMessage.plaintext(self.player)),
 
103
            u"SQUEAK!")
 
104
 
 
105
 
 
106
    def test_mouseCanSqueak(self):
 
107
        commands.runEventTransaction(self.store, self.mousehood.squeak)
 
108
        self.assertEquals(len(self.playerIntelligence.concepts), 1)
 
109
        event = self.playerIntelligence.concepts[0]
 
110
        self.assertEquals(
 
111
            commandutils.flatten(event.otherMessage.plaintext(self.player)),
 
112
            u"SQUEAK!")
 
113
 
 
114
 
 
115
 
 
116
class MouseReactionTestCase(commandutils.CommandTestCaseMixin, unittest.TestCase):
 
117
    def testCreation(self):
 
118
        """
 
119
        Test that a mouse can be created with the create command.
 
120
        """
 
121
        self._test(
 
122
            "create mouse squeaker",
 
123
            ['Squeaker created.'],
 
124
            ['Test Player creates squeaker.'])
 
125
 
 
126
        [mouse] = list(self.playerContainer.getContents())
 
127
        self.failUnless(isinstance(iimaginary.IActor(mouse).getIntelligence(), npc.Mouse))
 
128
 
 
129
 
 
130
    def testSqueak(self):
 
131
        """
 
132
        Test that when someone walks into a room with a mouse, the mouse
 
133
        squeaks and the person who walked in hears it.
 
134
        """
 
135
 
 
136
        mouse = npc.createMouse(store=self.store, name=u"squeaker")
 
137
 
 
138
        elsewhere = objects.Thing(store=self.store, name=u"Mouse Hole")
 
139
        objects.Container(store=self.store, capacity=1000).installOn(elsewhere)
 
140
 
 
141
        objects.Exit.link(self.location, elsewhere, u"south")
 
142
 
 
143
        mouse.moveTo(elsewhere)
 
144
 
 
145
        self._test(
 
146
            "south",
 
147
            [commandutils.E("[ Mouse Hole ]"),
 
148
             commandutils.E("( north )"),
 
149
             commandutils.E("a squeaker"),
 
150
             commandutils.E("SQUEAK!")],
 
151
            ['Test Player leaves south.'])