Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into catch_invalid_action
Browse files Browse the repository at this point in the history
  • Loading branch information
Paris Goldman-Smith committed Sep 5, 2018
2 parents fffe652 + 0956515 commit b9d5f96
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 @@ -20,8 +20,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 @@ -32,7 +32,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 @@ -69,7 +69,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 @@ -95,6 +95,16 @@ def handle_turn(self, world_map, avatar_state):
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'])

def test_invalid_action_exception(self):
avatar = '''class Avatar(object):
def handle_turn(self, world_map, avatar_state):
Expand Down

0 comments on commit b9d5f96

Please sign in to comment.