Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bad_move_function(team_to_test):
""" Test that having a move function that returns a bad type
appends a FatalException. """
def stopping(b, state):
return b.position
def move0(b, state):
return None
def move1(b, state):
return 0
def move3(b, state):
return 0, 0, 0
def move4(b): # TypeError: move4() takes 1 positional argument but 2 were given
return (0, 0), 0
layout_name, layout_string = layout.get_random_layout()
l = layout.parse_layout(layout_string)
def test_run_game(move):
# Flips the order of the teams depending on team_to_test
if team_to_test == 0:
teams = [move, stopping]
elif team_to_test == 1:
teams = [stopping, move]
return run_game(teams, layout_dict=l, max_rounds=10)
other = 1 - team_to_test
res = test_run_game(move0)
assert res['gameover']
assert res['whowins'] == other
assert res['fatal_errors'][team_to_test][0]['type'] == 'FatalException'
def test_non_existing_file():
# TODO: Change error message to be more meaningful
layout_name, layout_string = layout.get_random_layout()
l = layout.parse_layout(layout_string)
res = run_game(["blah", "nothing"], max_rounds=1, layout_dict=l)
assert res['fatal_errors'][0][0] == {
'description': '("Could not load blah: No module named \'blah\'", \'ModuleNotFoundError\')',
'round': None,
'turn': 0,
'type': 'PlayerDisconnected'
}
def test_minimal_remote_game():
def move(b, s):
return b.position
layout_name, layout_string = layout.get_random_layout()
l = layout.parse_layout(layout_string)
final_state = run_game(["test/demo01_stopping.py", move], max_rounds=20, layout_dict=l)
final_state = run_game(["test/demo01_stopping.py", 'test/demo02_random.py'], max_rounds=20, layout_dict=l)
assert final_state['gameover'] is True
assert final_state['score'] == [0, 0]
assert final_state['round'] == 20
def test_minimal_game():
def move(b, s):
return b.position
layout_name, layout_string = layout.get_random_layout()
l = layout.parse_layout(layout_string)
final_state = run_game([move, move], max_rounds=20, layout_dict=l)
assert final_state['gameover'] is True
assert final_state['score'] == [0, 0]
assert final_state['round'] == 20
def test_remote_errors(tmp_path):
# TODO: Change error messages to be more meaningful
# we change to the tmp dir, to make our paths simpler
syntax_error = dedent("""
def move(b, state)
return b.position
""")
import_error = dedent("""
import does_not_exist
def move(b, state):
return b.position
""")
layout_name, layout_string = layout.get_random_layout()
l = layout.parse_layout(layout_string)
with temp_wd(tmp_path):
s_py = Path("s.py")
s_py.write_text(syntax_error)
i_py = Path("i.py")
i_py.write_text(import_error)
res = run_game([str(s_py), str(i_py)], layout_dict=l, max_rounds=20)
assert res['fatal_errors'][0][0] == {
'description': "('Could not load s.py: invalid syntax (s.py, line 2)', 'SyntaxError')",
'round': None,
'turn': 0,
'type': 'PlayerDisconnected'
}
# Both teams fail during setup: DRAW
def _parse_layout_arg(*, layout=None, food=None, bots=None, enemy=None,
is_noisy=None, is_blue=True, agnostic=True, seed=None):
from .layout import (get_random_layout, get_layout_by_name, get_available_layouts,
parse_layout, layout_for_team, layout_agnostic)
# prepare layout argument to be passed to pelita.game.run_game
if layout is None:
layout_name, layout_str = get_random_layout(size='normal', seed=seed)
layout_dict = parse_layout(layout_str, allow_enemy_chars=False)
if not agnostic:
layout_dict = layout_for_team(layout_dict, is_blue=is_blue)
elif layout in get_available_layouts(size='all'):
# check if this is a built-in layout
layout_name = layout
layout_str = get_layout_by_name(layout)
layout_dict = parse_layout(layout_str, allow_enemy_chars=False)
if not agnostic:
layout_dict = layout_for_team(layout_dict, is_blue=is_blue)
else:
# OK, then it is a (user-provided, i.e. with 'E's) layout string
layout_str = layout
layout_name = ''
# be strict and complain if the layout does not contain two bots and two enemies
layout_dict = parse_layout(layout_str, food=food, bots=bots, enemy=enemy, strict=True,
if (layout_string and layout_name or
layout_string and layout_file or
layout_name and layout_file or
layout_string and layout_name and layout_file):
raise ValueError("Can only supply one of: 'layout_string'"+\
"'layout_name' or 'layout_file'")
elif layout_string:
self.layout = layout_string
elif layout_name:
self.layout = get_layout_by_name(layout_name)
elif layout_file:
with open(layout_file) as file:
self.layout = file.read()
else:
self.layout = get_random_layout(filter=layout_filter)
self.players = players
self.rounds = rounds
self.silent = silent
if local:
self.host = None
self.port = None
signal.signal(signal.SIGINT, keyboard_interrupt_handler)
else:
self.host = host
self.port = port
self.server = None
self.remote = None