2
from zope.interface import implements
4
from twisted.trial import unittest
6
from axiom import store, item, attributes
8
from imaginary import iimaginary, events, objects, commands, npc
9
from imaginary.test import commandutils
12
class MockIntelligence(item.Item):
13
implements(iimaginary.IEventObserver)
15
anAttribute = attributes.integer()
16
concepts = attributes.inmemory()
22
def prepare(self, concept):
23
return lambda: self.concepts.append(concept)
26
class IntelligenceTestCase(unittest.TestCase):
28
self.store = store.Store()
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)
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)
38
self.alice.moveTo(self.location)
40
self.intelligence = MockIntelligence(store=self.store)
41
self.actor.setEnduringIntelligence(self.intelligence)
44
def test_intelligenceReceivesEvent(self):
46
Enduring intelligences should receive events.
49
location=self.location,
50
otherMessage=u"Hello, how are you?")
53
self.assertEquals(self.intelligence.concepts, [evt])
56
def test_persistentIntelligence(self):
58
Whitebox test that enduring intelligencii are actually persistent.
60
# TB <---- THAT MEANS IT'S TRANSLUCENT
62
self.store.findUnique(
64
objects.Actor._enduringIntelligence == self.intelligence),
69
class MouseTestCase(unittest.TestCase):
71
self.store = store.Store()
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)
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)
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)
88
self.player.moveTo(self.clock)
91
def test_mouseSqueaksAtIntruders(self):
93
When a mean old man walks into the mouse's clock, the mouse will squeak
96
evt = events.ArrivalEvent(thing=self.player, origin=None)
97
self.mouseActor.send(evt)
99
self.assertEquals(len(self.playerIntelligence.concepts), 1)
100
event = self.playerIntelligence.concepts[0]
102
commandutils.flatten(event.otherMessage.plaintext(self.player)),
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]
111
commandutils.flatten(event.otherMessage.plaintext(self.player)),
116
class MouseReactionTestCase(commandutils.CommandTestCaseMixin, unittest.TestCase):
117
def testCreation(self):
119
Test that a mouse can be created with the create command.
122
"create mouse squeaker",
123
['Squeaker created.'],
124
['Test Player creates squeaker.'])
126
[mouse] = list(self.playerContainer.getContents())
127
self.failUnless(isinstance(iimaginary.IActor(mouse).getIntelligence(), npc.Mouse))
130
def testSqueak(self):
132
Test that when someone walks into a room with a mouse, the mouse
133
squeaks and the person who walked in hears it.
136
mouse = npc.createMouse(store=self.store, name=u"squeaker")
138
elsewhere = objects.Thing(store=self.store, name=u"Mouse Hole")
139
objects.Container(store=self.store, capacity=1000).installOn(elsewhere)
141
objects.Exit.link(self.location, elsewhere, u"south")
143
mouse.moveTo(elsewhere)
147
[commandutils.E("[ Mouse Hole ]"),
148
commandutils.E("( north )"),
149
commandutils.E("a squeaker"),
150
commandutils.E("SQUEAK!")],
151
['Test Player leaves south.'])