Skip to content

Commit

Permalink
Merge e59da95 into bedfe6f
Browse files Browse the repository at this point in the history
  • Loading branch information
NiallEgan committed Sep 5, 2018
2 parents bedfe6f + e59da95 commit 44d1171
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 2 additions & 3 deletions aimmo-game-worker/avatar_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __init__(self, avatar=None, auto_update=True):
def _avatar_src_changed(self, new_avatar_code):
return new_avatar_code != self.avatar_source_code

@staticmethod
def _get_new_avatar(src_code):
def _get_new_avatar(self, src_code):
self.avatar_source_code = src_code
module = imp.new_module('avatar') # Create a temporary module to execute the src_code in
exec src_code in module.__dict__
return module.Avatar()
Expand All @@ -31,7 +31,6 @@ def _update_avatar(self, src_code):

if self.avatar is None or self.auto_update and self._avatar_src_changed(src_code):
self.avatar = self._get_new_avatar(src_code)
self.avatar_source_code = src_code

def process_avatar_turn(self, world_map, avatar_state, src_code):
output_log = StringIO()
Expand Down
12 changes: 11 additions & 1 deletion aimmo-game-worker/tests/test_avatar_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def handle_turn(self, world_map, avatar_state):
response = runner.process_avatar_turn(world_map={}, avatar_state={}, src_code=avatar)
self.assertEqual(response['action'], {'action_type': 'move', 'options': {'direction': direction}})

def test_update_code_flag(self):
def test_update_code_flag_simple(self):
avatar1 = '''class Avatar(object):
def handle_turn(self, world_map, avatar_state):
from simulation.action import MoveAction
Expand All @@ -90,3 +90,13 @@ def handle_turn(self, world_map, avatar_state):
self.assertTrue(response['avatar_updated'])
response = runner.process_avatar_turn(world_map={}, avatar_state={}, src_code=avatar2)
self.assertFalse(response['avatar_updated'])

def test_update_code_flag_with_syntax_errors(self):
avatar = '''class Avatar(object:
pass
'''
runner = AvatarRunner()
response = runner.process_avatar_turn(world_map={}, avatar_state={}, src_code=avatar)
self.assertTrue(response['avatar_updated'])
response = runner.process_avatar_turn(world_map={}, avatar_state={}, src_code=avatar)
self.assertFalse(response['avatar_updated'])

0 comments on commit 44d1171

Please sign in to comment.