Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge 1d2c09a into d4511aa
Browse files Browse the repository at this point in the history
  • Loading branch information
OlafSzmidt authored Sep 1, 2017
2 parents d4511aa + 1d2c09a commit 02e04c9
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 18 deletions.
1 change: 1 addition & 0 deletions aimmo-game-creator/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
tests_require=[
'httmock',
'mock',
],
test_suite='tests',
zip_safe=False,
Expand Down
44 changes: 44 additions & 0 deletions aimmo-game-creator/tests/test_worker_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import cPickle as pickle
import unittest

from json import dumps

from httmock import HTTMock
import mock

from worker_manager import WorkerManager
from worker_manager import LocalWorkerManager


class ConcreteWorkerManager(WorkerManager):
Expand Down Expand Up @@ -95,3 +98,44 @@ def test_added_workers_given_correct_url(self):
'http://test/{}/'.format(i)
)
self.assertEqual(self.worker_manager.added_workers[str(i)]['name'], 'Game %s' % i)

class TestLocalWorkerManager(unittest.TestCase):

def test_create_worker(self):
with mock.patch('subprocess.Popen') as mocked_popen:
localWorkerManager = LocalWorkerManager("")

game_id = 1
game_data = {
"test" : "test"
}

localWorkerManager.create_worker(game_id, game_data)
call_args = mocked_popen.call_args

argument_dictionary = call_args[1]
self.assertTrue("aimmo-game" in argument_dictionary["cwd"])
self.assertEqual(argument_dictionary["env"]["test"], "test")

def test_remove_worker(self):
self.killed = False
class KillableWorker():
def __init__(self, binder):
self.binder = binder
self.binder.killed = False

def kill(self):
self.binder.killed = True

localWorkerManger = LocalWorkerManager("")
localWorkerManger.workers = {
1 : KillableWorker(self)
}

self.assertFalse(self.killed)
self.assertTrue(1 in localWorkerManger.workers)

localWorkerManger.remove_worker(1)

self.assertTrue(self.killed)
self.assertTrue(1 not in localWorkerManger.workers)
30 changes: 30 additions & 0 deletions aimmo-game-worker/tests/simulation/test_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import absolute_import

from unittest import TestCase

from simulation.location import Location


class TestLocation(TestCase):

def test_add(self):
dummy_location = Location(1, 2)
direction = Location(1, 1)
expected = Location(2, 3)
self.assertEqual(dummy_location + direction, expected)

def test_sub(self):
dummy_location = Location(3, 2)
direction = Location(1, 1)
expected = Location(2, 1)
self.assertEqual(dummy_location - direction, expected)

def test_repr(self):
dummy_location = Location(1, 2)
self.assertTrue("Location(1, 2)" == str(dummy_location))


def test_not_equal(self):
dummy_location_1 = Location(1, 1)
dummy_location_2 = Location(2, 2)
self.assertTrue(dummy_location_1 != dummy_location_2)
15 changes: 15 additions & 0 deletions aimmo-game/tests/test_simulation/avatar/test_fog_of_war.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import absolute_import

from unittest import TestCase

from simulation.avatar.fog_of_war import should_partially_fog


class TestFogOfWar(TestCase):
def test_should_partially_fog(self):
self.assertFalse(should_partially_fog(no_fog_distance=20, partial_fog_distance=2, x_dist=1, y_dist=10))
self.assertTrue(should_partially_fog(no_fog_distance=1, partial_fog_distance=2, x_dist=20, y_dist=10))




96 changes: 78 additions & 18 deletions aimmo-game/tests/test_simulation/test_turn_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from simulation.game_state import GameState
from simulation.location import Location
from simulation.turn_manager import ConcurrentTurnManager
from simulation.turn_manager import SequentialTurnManager
from .dummy_avatar import DummyAvatarManager
from .dummy_avatar import MoveEastDummy
from .dummy_avatar import MoveNorthDummy
Expand All @@ -32,16 +33,22 @@ class TestTurnManager(unittest.TestCase):
def construct_default_avatar_appearance(self):
return AvatarAppearance("#000", "#ddd", "#777", "#fff")

def construct_turn_manager(self, avatars, locations):
def construct_turn_manager(self, avatars, locations, manager):
self.avatar_manager = DummyAvatarManager(avatars)
self.game_state = MockGameState(InfiniteMap(), self.avatar_manager)
self.turn_manager = ConcurrentTurnManager(game_state=self.game_state,
self.turn_manager = manager(game_state=self.game_state,
end_turn_callback=lambda: None,
completion_url='')
for index, location in enumerate(locations):
self.game_state.add_avatar(index, "", location)
return self.turn_manager

def construct_concurrent_turn_manager(self, avatars, locations):
return self.construct_turn_manager(avatars, locations, ConcurrentTurnManager)

def construct_sequential_turn_manager(self, avatars, locations):
return self.construct_turn_manager(avatars, locations, SequentialTurnManager)

def assert_at(self, avatar, location):
self.assertEqual(avatar.location, location)
cell = self.game_state.world_map.get_cell(location)
Expand All @@ -53,41 +60,41 @@ def get_avatar(self, player_id):
def run_turn(self):
self.turn_manager.run_turn()

def test_run_turn(self):
def run_by_manager_turn(self, construct_manager):
'''
Given: > _
(1)
Expect: _ o
'''
self.construct_turn_manager([MoveEastDummy], [ORIGIN])
construct_manager([MoveEastDummy], [ORIGIN])
avatar = self.get_avatar(0)

self.assert_at(avatar, ORIGIN)
self.run_turn()
self.assert_at(avatar, RIGHT_OF_ORIGIN)

def test_run_several_turns(self):
def run_by_manager_several_turns(self, construct_manager):
'''
Given: > _ _ _ _ _
(5)
Expect: _ _ _ _ _ o
'''
self.construct_turn_manager([MoveEastDummy], [ORIGIN])
construct_manager([MoveEastDummy], [ORIGIN])
avatar = self.get_avatar(0)

self.assertEqual(avatar.location, ORIGIN)
[self.run_turn() for _ in range(5)]
self.assertEqual(avatar.location, FIVE_RIGHT_OF_ORIGIN)

def test_run_several_turns_and_avatars(self):
def run_by_manager_several_turns_and_avatars(self, construct_manager):
'''
Given: > _ _ _ _ _
> _ _ _ _ _
(5)
Expect: _ _ _ _ _ o
_ _ _ _ _ o
'''
self.construct_turn_manager([MoveEastDummy, MoveEastDummy],
construct_manager([MoveEastDummy, MoveEastDummy],
[ORIGIN, ABOVE_ORIGIN])
avatar0 = self.get_avatar(0)
avatar1 = self.get_avatar(1)
Expand All @@ -98,42 +105,42 @@ def test_run_several_turns_and_avatars(self):
self.assert_at(avatar0, FIVE_RIGHT_OF_ORIGIN)
self.assert_at(avatar1, FIVE_RIGHT_OF_ORIGIN_AND_ONE_ABOVE)

def test_move_chain_succeeds(self):
def run_by_manager_move_chain_succeeds(self, construct_manager):
'''
Given: > > > > > _
Expect: _ o o o o o
'''
self.construct_turn_manager([MoveEastDummy for _ in range(5)],
construct_manager([MoveEastDummy for _ in range(5)],
[Location(x, 0) for x in range(5)])
avatars = [self.get_avatar(i) for i in range(5)]

[self.assert_at(avatars[x], Location(x, 0)) for x in range(5)]
self.run_turn()
[self.assert_at(avatars[x], Location(x + 1, 0)) for x in range(5)]

def test_move_chain_fails_occupied(self):
def run_by_manager_move_chain_fails_occupied(self, construct_manager):
'''
Given: > > x _
Expect: x x x _
'''
self.construct_turn_manager([MoveEastDummy, MoveEastDummy, WaitDummy],
construct_manager([MoveEastDummy, MoveEastDummy, WaitDummy],
[Location(x, 0) for x in range(3)])
avatars = [self.get_avatar(i) for i in range(3)]

[self.assert_at(avatars[x], Location(x, 0)) for x in range(3)]
self.run_turn()
[self.assert_at(avatars[x], Location(x, 0)) for x in range(3)]

def test_move_chain_fails_collision(self):
def run_by_manager_move_chain_fails_collision(self, construct_manager):
'''
Given: > > > _ <
(1)
Expect: x x x _ x
'''
locations = [Location(0, 0), Location(1, 0), Location(2, 0), Location(4, 0)]
self.construct_turn_manager(
construct_manager(
[MoveEastDummy, MoveEastDummy, MoveEastDummy, MoveWestDummy],
locations)
avatars = [self.get_avatar(i) for i in range(4)]
Expand All @@ -142,7 +149,7 @@ def test_move_chain_fails_collision(self):
self.run_turn()
[self.assert_at(avatars[i], locations[i]) for i in range(4)]

def test_move_chain_fails_cycle(self):
def run_by_manager_move_chain_fails_cycle(self, construct_manager):
'''
Given: > v
^ <
Expand All @@ -151,7 +158,7 @@ def test_move_chain_fails_cycle(self):
x x
'''
locations = [Location(0, 1), Location(1, 1), Location(1, 0), Location(0, 0)]
self.construct_turn_manager(
construct_manager(
[MoveEastDummy, MoveSouthDummy, MoveWestDummy, MoveNorthDummy],
locations)
avatars = [self.get_avatar(i) for i in range(4)]
Expand All @@ -160,7 +167,7 @@ def test_move_chain_fails_cycle(self):
self.run_turn()
[self.assert_at(avatars[i], locations[i]) for i in range(4)]

def test_move_chain_fails_spiral(self):
def run_by_manager_move_chain_fails_spiral(self, construct_manager):
'''
Given: > > v
^ <
Expand All @@ -173,7 +180,7 @@ def test_move_chain_fails_spiral(self):
Location(2, 1),
Location(2, 0),
Location(1, 0)]
self.construct_turn_manager(
construct_manager(
[MoveEastDummy, MoveEastDummy, MoveSouthDummy, MoveWestDummy, MoveNorthDummy],
locations)
avatars = [self.get_avatar(i) for i in range(5)]
Expand All @@ -182,5 +189,58 @@ def test_move_chain_fails_spiral(self):
self.run_turn()
[self.assert_at(avatars[i], locations[i]) for i in range(5)]

def build_test_by_constructor(self, constructor):
self.run_by_manager_turn(constructor)
self.run_by_manager_several_turns_and_avatars(constructor)
self.run_by_manager_several_turns(constructor)
self.run_by_manager_move_chain_fails_spiral(constructor)
self.run_by_manager_move_chain_fails_cycle(constructor)
self.run_by_manager_move_chain_fails_occupied(constructor)

def test_concurrent_turn_manager(self):
constructor = lambda x, y: self.construct_concurrent_turn_manager(x, y)
self.build_test_by_constructor(constructor)
self.run_by_manager_move_chain_fails_collision(constructor)
self.run_by_manager_move_chain_succeeds(constructor)

def sequential_move_chain_consecutive_avatars_fails(self):
'''
Given: > > > > > _
Expect: _ o o o o o
This should fail for the sequential manager as the first avatar will bump into the second one
'''
self.construct_sequential_turn_manager([MoveEastDummy for _ in range(5)],
[Location(x, 0) for x in range(5)])
avatars = [self.get_avatar(i) for i in range(5)]

[self.assert_at(avatars[x], Location(x, 0)) for x in range(5)]
self.run_turn()
[self.assert_at(avatars[x], Location(x, 0)) for x in range(4)]
self.assert_at(avatars[4], Location(5, 0))

def sequential_move_chain_fails_collission(self):
'''
Given: > > > _ <
(1)
Expect: x x x _ x
'''
locations = [Location(0, 0), Location(1, 0), Location(2, 0), Location(4, 0)]
self.construct_sequential_turn_manager(
[MoveEastDummy, MoveEastDummy, MoveEastDummy, MoveWestDummy],
locations)
avatars = [self.get_avatar(i) for i in range(4)]

[self.assert_at(avatars[i], locations[i]) for i in range(4)]
self.run_turn()
[self.assert_at(avatars[i], locations[i]) for i in [0, 1, 3]]
self.assert_at(avatars[2], Location(3, 0))

def test_sequential_turn_manager(self):
constructor = lambda x, y: self.construct_sequential_turn_manager(x, y)
self.build_test_by_constructor(constructor)
self.sequential_move_chain_consecutive_avatars_fails()
self.sequential_move_chain_fails_collission()

if __name__ == '__main__':
unittest.main()

0 comments on commit 02e04c9

Please sign in to comment.