本文整理匯總了Python中xatro.world.World.emit方法的典型用法代碼示例。如果您正苦於以下問題:Python World.emit方法的具體用法?Python World.emit怎麽用?Python World.emit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類xatro.world.World
的用法示例。
在下文中一共展示了World.emit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_emit_receiveOnce
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_emit_receiveOnce(self):
"""
Events should only be received by each object once.
"""
world = World(MagicMock())
obj1 = world.create('foo')['id']
obj2 = world.create('foo')['id']
obj1_received = []
world.receiveFor(obj1, obj1_received.append)
obj2_received = []
world.receiveFor(obj2, obj2_received.append)
# obj1 emits everything it receives
world.receiveFor(obj1, world.emitterFor(obj1))
# obj2 emits everything it receives
world.receiveFor(obj2, world.emitterFor(obj2))
# obj2 receives emissions from obj1
world.subscribeTo(obj1, world.receiverFor(obj2))
# obj1 receives emissions from obj2
world.subscribeTo(obj2, world.receiverFor(obj1))
# we have a nice loop set up. When this test fails, it is likely to
# continue spinning forever.
world.emit('echo', obj1)
self.assertEqual(obj1_received, ['echo'], "Should have received the "
"message once")
self.assertEqual(obj2_received, ['echo'], "Should have received the "
"message once")
示例2: test_emit
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_emit(self):
"""
All emissions are sent to my event_receiver
"""
ev = MagicMock()
world = World(ev)
world.emit('something', 'foo')
ev.assert_called_once_with('something')
示例3: test_emit_toEngine
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_emit_toEngine(self):
"""
All emissions are sent to the engine.
"""
ev = MagicMock()
engine = MagicMock()
world = World(ev, engine)
world.emit('foo', 'object_id')
engine.worldEventReceived.assert_called_once_with(world, 'foo')
示例4: test_disable_selfReceiving
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_disable_selfReceiving(self):
"""
You can disable self-receipt by creating things with a special arg.
"""
world = World(MagicMock())
thing = world.create('foo', receive_emissions=False)
called = []
world.receiveFor(thing['id'], called.append)
world.emit('foo', thing['id'])
self.assertEqual(called, [], "Should not receive because it was "
"disabled on creation.")
示例5: test_selfReceiving
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_selfReceiving(self):
"""
All things should receive their own events by default.
"""
world = World(MagicMock())
thing = world.create('foo')
called = []
world.receiveFor(thing['id'], called.append)
world.emit('foo', thing['id'])
self.assertEqual(called, ['foo'], "Things should receive their own "
"emissions")
示例6: test_emit_oneEventAtATime
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_emit_oneEventAtATime(self):
"""
Only one event should be emitted at a time. If an event emission
causes another event to be emitted, it should be appended to a queue
and be emitted after the current event is finished.
"""
world = World(MagicMock())
obj1 = world.create('foo')['id']
obj2 = world.create('foo')['id']
obj3 = world.create('foo')['id']
obj1_received = []
world.receiveFor(obj1, obj1_received.append)
obj2_received = []
world.receiveFor(obj2, obj2_received.append)
obj3_received = []
world.receiveFor(obj3, obj3_received.append)
# obj1 will emit to
# obj2 and obj3
world.subscribeTo(obj1, world.receiverFor(obj2))
world.subscribeTo(obj1, world.receiverFor(obj3))
# obj2 will emit to
# obj1 and obj3
world.subscribeTo(obj2, world.receiverFor(obj1))
world.subscribeTo(obj2, world.receiverFor(obj3))
# 1_
# /|\
# / \
# |/_ _\|
# 3 <---- 2
# obj2 will emit "obj2" every time he receives an event
def noisy(_):
world.emit('obj2', obj2)
world.receiveFor(obj2, noisy)
# If things are working properly, then obj3 will receive
# the 'obj2' emissions AFTER it receives the event from obj1
world.emit('obj1', obj1)
self.assertEqual(obj1_received, ['obj1', 'obj2'],
"obj1 should receive obj1 from self, then obj2")
self.assertEqual(obj2_received, ['obj1', 'obj2'],
"obj2 should receive obj1, then obj2 from self")
self.assertEqual(obj3_received, ['obj1', 'obj2'],
"obj3 should receiver obj1 first, then obj2")
示例7: test_execute_inRoom
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_execute_inRoom(self):
"""
If an object is already located in another container, then moving it
should:
- Unsubscribe from the location's events
- Unsubscribe the location from the thing's events
- Cause C{contents} to be updated.
"""
world = World(MagicMock())
thing = world.create('thing')['id']
room1 = world.create('room1')['id']
room2 = world.create('room2')['id']
move1 = Move(thing, room1)
move1.execute(world)
move2 = Move(thing, room2)
move2.execute(world)
room1_called = []
world.receiveFor(room1, room1_called.append)
room2_called = []
world.receiveFor(room2, room2_called.append)
thing_called = []
world.receiveFor(thing, thing_called.append)
# emit from room1
world.emit('foo', room1)
self.assertEqual(thing_called, [], "Thing should detach from previous "
"room events")
room1_called.pop()
# emit from thing
world.emit('bar', thing)
self.assertEqual(thing_called, ['bar'], "Thing should see own events")
self.assertEqual(room1_called, [], "Room1 should detach from thing"
" events")
# room1 contents
room1_obj = world.get(room1)
self.assertEqual(room1_obj['contents'], [], "Should remove from "
"contents of room1")
location = world.get(thing)['location']
self.assertEqual(location, room2, "Should update the "
"location of the thing. This is mostly to confirm "
"that the default moving behavior tested in the other "
"test happened.")
示例8: test_onEvent_cancel
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_onEvent_cancel(self):
"""
You can cancel the deferred returned by onEvent
"""
world = World(MagicMock())
obj = world.create('foo')
d = world.onEvent(obj['id'], 'hey')
d.cancel()
world.emit('hey', obj['id'])
self.assertFailure(d, defer.CancelledError)
示例9: test_execute_Deferred
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_execute_Deferred(self):
"""
If a command returns a successful deferred, wait to emit.
"""
engine = MagicMock()
d = defer.Deferred()
engine.execute.return_value = d
world = World(MagicMock(), engine)
world.emit = MagicMock()
action = MagicMock()
action.emitters.return_value = ['I did it']
r = world.execute(action)
self.assertEqual(r, d, "Should return the result of the execution")
engine.execute.assert_called_once_with(world, action)
self.assertEqual(world.emit.call_count, 0, "Should not have emitted "
"the ActionPerformed event yet, because it hasn't "
"finished")
d.callback('foo')
self.assertEqual(self.successResultOf(r), 'foo',
"Should return the result of execution")
world.emit.assert_called_once_with(ActionPerformed(action), 'I did it')
示例10: test_onEvent
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_onEvent(self):
"""
You can be notified when a certain event happens.
"""
world = World(MagicMock())
obj = world.create('foo')['id']
d = world.onEvent(obj, 'event')
self.assertEqual(d.called, False)
world.emit('event', obj)
self.assertEqual(self.successResultOf(d), 'event')
# shouldn't die calling again
world.emit('event', obj)
示例11: test_thingsInTheSameRoom
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_thingsInTheSameRoom(self):
"""
Things in the same location should see events emitted by each other.
"""
world = World(MagicMock())
room = world.create('a')['id']
thing1 = world.create('thing')['id']
thing2 = world.create('thing')['id']
Move(thing1, room).execute(world)
Move(thing2, room).execute(world)
c1 = []
world.receiveFor(thing1, c1.append)
c2 = []
world.receiveFor(thing2, c2.append)
world.emit('foo', thing1)
self.assertEqual(c1, ['foo'])
self.assertEqual(c2, ['foo'])
示例12: test_emit_Exception
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_emit_Exception(self):
"""
Exceptions caused by event receivers should not prevent other event
receivers from receiving the events.
"""
ev1 = MagicMock()
ev1.side_effect = Exception()
world = World(ev1)
ev2 = MagicMock()
ev2.side_effect = Exception()
world.subscribeTo('1234', ev2)
ev3 = MagicMock()
world.subscribeTo('1234', ev3)
world.emit('hey', '1234')
ev1.assert_called_once_with('hey')
ev2.assert_called_once_with('hey')
ev3.assert_called_once_with('hey')
示例13: test_subscribeTo
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_subscribeTo(self):
"""
You can subscribe to the events that are emitted by a particular object.
"""
ev = MagicMock()
world = World(ev)
obj = world.create('foo')
called = []
world.subscribeTo(obj['id'], called.append)
ev.reset_mock()
world.emit('event', obj['id'])
self.assertEqual(called, ['event'])
ev.assert_called_once_with('event')
ev.reset_mock()
world.unsubscribeFrom(obj['id'], called.append)
world.emit('event', obj['id'])
self.assertEqual(called, ['event'], "Should not have changed")
ev.assert_called_once_with('event')
示例14: test_moveToNone
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_moveToNone(self):
"""
Moving something to nowhere should result in the location being
None.
"""
world = World(MagicMock())
thing = world.create('thing')['id']
room1 = world.create('room1')['id']
move1 = Move(thing, room1)
move1.execute(world)
move2 = Move(thing, None)
move2.execute(world)
room1_called = []
world.receiveFor(room1, room1_called.append)
thing_called = []
world.receiveFor(thing, thing_called.append)
# emit from room1
world.emit('foo', room1)
self.assertEqual(thing_called, [], "Thing should detach from previous "
"room events")
room1_called.pop()
# emit from thing
world.emit('bar', thing)
self.assertEqual(thing_called, ['bar'], "Thing should see own events")
self.assertEqual(room1_called, [], "Room1 should detach from thing"
" events")
# room1 contents
room1_obj = world.get(room1)
self.assertEqual(room1_obj['contents'], [], "Should remove from "
"contents of room1")
location = world.get(thing)['location']
self.assertEqual(location, None, "Should update the location")
示例15: test_execute
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import emit [as 別名]
def test_execute(self):
"""
Moving to a new location should:
- Cause C{location} to be updated
- Cause C{contents} to be updated
- Subscribe the thing to the location's events
- Subscribe the location to the thing's events
"""
world = World(MagicMock())
thing = world.create('thing')
room = world.create('room')
move = Move(thing['id'], room['id'])
move.execute(world)
self.assertEqual(thing['location'], room['id'], "Should set location")
self.assertEqual(room['contents'], [thing['id']], "Should set contents")
room_called = []
world.receiveFor(room['id'], room_called.append)
thing_called = []
world.receiveFor(thing['id'], thing_called.append)
world.emit('foo', room['id'])
self.assertEqual(room_called, ['foo'],
"Room should receive room events")
self.assertEqual(thing_called, ['foo'],
"Thing should receive room events")
room_called.pop()
thing_called.pop()
world.emit('foo', thing['id'])
self.assertEqual(room_called, ['foo'],
"Room should receive thing events")
self.assertEqual(thing_called, ['foo'],
"Thing should receive thing events")