本文整理匯總了Python中xatro.world.World類的典型用法代碼示例。如果您正苦於以下問題:Python World類的具體用法?Python World怎麽用?Python World使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了World類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_execute_energyRequired_command_fails
def test_execute_energyRequired_command_fails(self):
"""
If there is energy required and the command fails to run, the energy
should not be consumed.
"""
e = self.friendlyEngine()
# require some energy
e.engine.energyRequirement.return_value = 2
# make some energy
world = World(MagicMock())
actor = world.create('thing')
yield Charge(actor['id']).execute(world)
yield Charge(actor['id']).execute(world)
self.assertEqual(len(actor['energy']), 2)
# do the action and consume the energy
action = MagicMock()
action.subject.return_value = actor['id']
# synchronous
action.execute.side_effect = NotAllowed('foo')
self.assertFailure(e.execute(world, action), NotAllowed)
self.assertEqual(len(actor['energy']), 2, "Should not have consumed "
"the energy")
# asynchronous
action.execute.side_effect = None
action.execute.return_value = defer.fail(NotAllowed('foo'))
self.assertFailure(e.execute(world, action), NotAllowed)
self.assertEqual(len(actor['energy']), 2, "Should not have consumed "
"the energy")
示例2: test_failIfNotOnBoard
def test_failIfNotOnBoard(self):
"""
The following actions require a bot to be on the board.
"""
world = World(MagicMock())
rules = StandardRules()
bot = world.create('bot')['id']
actions = [
action.Charge(bot),
action.ShareEnergy(bot, 'foo', 2),
action.ConsumeEnergy(bot, 2),
action.Shoot(bot, 'foo', 1),
action.Repair(bot, 'foo', 1),
action.MakeTool(bot, 'foo', 'foo'),
action.OpenPortal(bot, 'foo', 'foo'),
action.AddLock(bot, 'foo'),
action.BreakLock(bot, 'foo'),
action.LookAt(bot, 'foo'),
]
for a in actions:
try:
rules.isAllowed(world, a)
except NotAllowed:
pass
else:
self.fail("You must be on a square to do %r" % (a,))
示例3: test_execute_Deferred
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')
示例4: test_receiverFor_same
def test_receiverFor_same(self):
"""
You should get the same function each time you ask for a receiver for
the same object.
"""
world = World(MagicMock())
self.assertEqual(world.receiverFor('foo'), world.receiverFor('foo'))
示例5: test_destroy_disableSubscribers
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, [])
示例6: test_makeSecondTool
def test_makeSecondTool(self):
"""
If you make a tool from a different piece of ore, your existing tool
is unequipped and the lifesource it was made from is reverted to ore.
"""
world = World(MagicMock())
ore1 = world.create('ore')
ore2 = world.create('ore')
bot = world.create('bot')
MakeTool(bot['id'], ore1['id'], 'knife').execute(world)
MakeTool(bot['id'], ore2['id'], 'butterfly net').execute(world)
self.assertEqual(bot['tool'], 'butterfly net',
"Should equip the new tool")
self.assertEqual(ore1['kind'], 'ore', "Should revert to ore")
self.assertEqual(ore2['kind'], 'lifesource')
# kill original
world.setAttr(ore1['id'], 'hp', 0)
self.assertEqual(bot['tool'], 'butterfly net', "should not change tool"
" when the original ore dies")
self.assertEqual(ore2['kind'], 'lifesource')
示例7: test_execute
def test_execute(self):
"""
Listing squares should return a list of all the things in the world
which are squares. It should include their coordinates and number of
each kind of thing inside them.
"""
world = World(MagicMock())
s1 = world.create('square')['id']
s2 = world.create('square')['id']
world.setAttr(s2, 'coordinates', (0, 1))
thing1 = world.create('thing')['id']
Move(thing1, s2).execute(world)
output = ListSquares(thing1).execute(world)
self.assertIn({
'id': s1,
'kind': 'square',
'coordinates': None,
'contents': {},
}, output)
self.assertIn({
'id': s2,
'kind': 'square',
'coordinates': (0, 1),
'contents': {
'thing': 1,
}
}, output)
self.assertEqual(len(output), 2)
示例8: test_execute_energyRequired_succeed
def test_execute_energyRequired_succeed(self):
"""
If energy is required and the actor has enough energy, do the action.
"""
e = self.friendlyEngine()
# require some energy
e.engine.energyRequirement.return_value = 2
# make some energy
world = World(MagicMock())
actor = world.create('thing')
yield Charge(actor['id']).execute(world)
yield Charge(actor['id']).execute(world)
self.assertEqual(len(actor['energy']), 2)
# do the action and consume the energy
action = MagicMock()
action.subject.return_value = actor['id']
action.execute.return_value = 'foo'
ret = yield e.execute(world, action)
self.assertEqual(ret, 'foo', "Should have executed the action")
self.assertEqual(len(actor['energy']), 0, "Should have consumed "
"the energy")
示例9: test_emit
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')
示例10: test_badPassword
def test_badPassword(self):
auth = FileStoredPasswords(self.mktemp())
world = World(MagicMock(), auth=auth)
thing = world.create('thing')
yield CreateTeam(thing['id'], 'teamA', 'password').execute(world)
self.assertFailure(JoinTeam(thing['id'], 'teamA',
'not password').execute(world), BadPassword)
示例11: test_create_uniqueId
def test_create_uniqueId(self):
"""
The id of an object should be unique
"""
world = World(MagicMock())
o1 = world.create('foo')
o2 = world.create('bar')
self.assertNotEqual(o1['id'], o2['id'])
示例12: test_get
def test_get(self):
"""
You can get objects.
"""
world = World(MagicMock())
obj = world.create('foo')
obj2 = world.get(obj['id'])
self.assertEqual(obj, obj2)
示例13: test_invalidLocation
def test_invalidLocation(self):
"""
It is an error to move to a non-entity.
"""
world = World(MagicMock())
thing = world.create('thing')['id']
self.assertRaises(NotAllowed, Move(thing, 4).execute, world)
示例14: test_nowhere
def test_nowhere(self):
"""
If you are nowhere, return an empty list.
"""
world = World(MagicMock())
thing = world.create('thing')
self.assertEqual(Look(thing['id']).execute(world), [])
示例15: test_emit_toEngine
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')