本文整理匯總了Python中xatro.world.World.receiveFor方法的典型用法代碼示例。如果您正苦於以下問題:Python World.receiveFor方法的具體用法?Python World.receiveFor怎麽用?Python World.receiveFor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類xatro.world.World
的用法示例。
在下文中一共展示了World.receiveFor方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_destroy_disableSubscribers
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [as 別名]
def test_destroy_disableSubscribers(self):
"""
When an object is destroyed, things subscribed to its events will
no longer receive events.
"""
world = World(MagicMock())
thing = world.create('foo')
received = []
world.receiveFor(thing['id'], received.append)
emitted = []
world.subscribeTo(thing['id'], emitted.append)
receiver = world.receiverFor(thing['id'])
emitter = world.emitterFor(thing['id'])
world.destroy(thing['id'])
received.pop()
emitted.pop()
receiver('foo')
self.assertEqual(received, [])
self.assertEqual(emitted, [])
emitter('foo')
self.assertEqual(received, [])
self.assertEqual(emitted, [])
示例2: test_disable_selfReceiving
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [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.")
示例3: test_selfReceiving
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [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")
示例4: test_receiverFor
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [as 別名]
def test_receiverFor(self):
"""
You can get a function that will call eventReceived for a given object.
"""
world = World(MagicMock())
obj = world.create('foo')
called = []
world.receiveFor(obj['id'], called.append)
receiver = world.receiverFor(obj['id'])
receiver('hey')
self.assertEqual(called, ['hey'])
示例5: test_emit_receiveOnce
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [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")
示例6: test_destroy
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [as 別名]
def test_destroy(self):
"""
You can destroy an object, and be notified about it.
"""
ev = MagicMock()
world = World(ev)
obj = world.create('foo')
ev.reset_mock()
called = []
world.receiveFor(obj['id'], called.append)
world.destroy(obj['id'])
ev.assert_any_call(Destroyed(obj['id']))
self.assertEqual(called, [Destroyed(obj['id'])], "Should notify things"
" receiving events for the object")
self.assertNotIn(obj['id'], world.objects)
示例7: test_thingsInTheSameRoom
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [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'])
示例8: test_receiveFor
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [as 別名]
def test_receiveFor(self):
"""
You can subscribe to the events that are received by a particular
object.
"""
ev = MagicMock()
world = World(ev)
obj = world.create('foo')
called = []
world.receiveFor(obj['id'], called.append)
world.eventReceived('event', obj['id'])
self.assertEqual(called, ['event'])
called.pop()
world.stopReceivingFor(obj['id'], called.append)
world.eventReceived('foo', obj['id'])
self.assertEqual(called, [], "Should not receive")
示例9: test_emit_oneEventAtATime
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [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")
示例10: test_moveToNone
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [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")
示例11: test_execute
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [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")
示例12: test_execute_inRoom
# 需要導入模塊: from xatro.world import World [as 別名]
# 或者: from xatro.world.World import receiveFor [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.")