def test_nearest_human(self, zombie): environment = [ (Vector(3, -3), default_human()), (Vector(2, 2), default_human()), (Vector(-3, 3), default_human()), ] limits = BoundingBox(Vector(-100, -100), Vector(100, 100)) assert zombie.move(FakeViewpoint(environment), limits) == Vector(1, 1)
def test_attack_action(self): character = Character(state=Undead()) target = default_human() next_action = character.next_action( FakeViewpoint([(Vector(1, 1), target)]), BoundingBox.range(5), FakeActions()) assert next_action == Attack(Vector(1, 1))
def test_move_action(self): character = Character(state=Undead()) environment = FakeViewpoint([(Vector(3, 3), default_human())]) next_action: Action = character.next_action(environment, BoundingBox.range(5), FakeActions()) assert next_action == Move(Vector(1, 1))
def test_alternate_path(self, zombie): environment = [ (Vector(2, 2), default_human()), (Vector(1, 1), default_zombie()), (Vector(1, 0), default_zombie()), ] limits = BoundingBox(Vector(-100, -100), Vector(100, 100)) assert zombie.move(FakeViewpoint(environment), limits) == Vector(0, 1)
def test_zombie_approaches_human(self): zombie = default_zombie() human = default_human() characters = {Point(0, 0): zombie, Point(2, 2): human} area = Area(Point(0, 0), Point(3, 3)) roster = Roster.partitioned(characters, area=area, partition_func=LifeState.for_character) roster = Tick(roster).next() assert sorted(roster.positions) == [(Point(1, 1), zombie), (Point(2, 2), human)]
def test_all_paths_blocked(self, zombie): """Test that zombies stay still when surrounded by other zombies. This effectively functions as a last check that zombies always have their own position as a fall-back, and don't register as blocking their own non-movement. """ def env_contents(vector): return default_zombie() if vector else zombie vectors = [Vector(dx, dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]] distant_human = [(Vector(2, 2), default_human())] zombies_all_around = [(v, env_contents(v)) for v in vectors] viewpoint = FakeViewpoint(distant_human + zombies_all_around) limits = BoundingBox(Vector(-100, -100), Vector(100, 100)) assert zombie.move(viewpoint, limits) == Vector.ZERO
def human(self): return default_human()
class TestZombie: @pytest.fixture(scope="session") def zombie(self): return default_zombie() @given(environments_and_limits()) def test_move_returns_a_vector(self, zombie, environment_and_limits): environment, limits = environment_and_limits assert isinstance(zombie.move(FakeViewpoint(environment), limits), Vector) @given(environments(characters=humans, min_size=1, max_size=1), containing_boxes) def test_never_moves_away_from_human(self, zombie, environment, limits): viewpoint = FakeViewpoint(environment) move = zombie.move(viewpoint, limits) assert (environment[0][0] - move).distance <= environment[0][0].distance @given(environments_and_limits(characters=humans, min_chars=1, max_chars=1)) def test_move_approaches_single_human(self, zombie, environment_and_limits): environment, limits = environment_and_limits assume(environment[0][0].distance > 1) move = zombie.move(FakeViewpoint(environment), limits) assert (environment[0][0] - move).distance < environment[0][0].distance @given(environments_and_limits()) def test_does_not_move_onto_occupied_space(self, zombie, environment_and_limits): environment, limits = environment_and_limits move = zombie.move(FakeViewpoint(environment), limits) assert move not in [e[0] for e in environment] @given(environments_and_limits()) def test_moves_up_to_one_space(self, zombie, environment_and_limits): environment, limits = environment_and_limits move = zombie.move(FakeViewpoint(environment), limits) assert abs(move.dx) <= 1 assert abs(move.dy) <= 1 @given(environments_and_limits(characters=zombies)) def test_ignores_zombies(self, zombie, environment_and_limits): environment, limits = environment_and_limits assert zombie.move(FakeViewpoint(environment), limits) == Vector.ZERO @given(environments_and_limits()) def test_respects_limits(self, zombie, environment_and_limits): environment, limits = environment_and_limits move = zombie.move(FakeViewpoint(environment), limits) assert move in limits @given(containing_boxes) def test_nothing_nearby(self, zombie, limits): assert zombie.move(FakeViewpoint([]), limits) == Vector.ZERO def test_nearest_human(self, zombie): environment = [ (Vector(3, -3), default_human()), (Vector(2, 2), default_human()), (Vector(-3, 3), default_human()), ] limits = BoundingBox(Vector(-100, -100), Vector(100, 100)) assert zombie.move(FakeViewpoint(environment), limits) == Vector(1, 1) def test_blocked_path(self, zombie): environment = [ (Vector(2, 2), default_human()), (Vector(1, 1), default_zombie()), (Vector(1, 0), default_zombie()), (Vector(0, 1), default_zombie()), ] limits = BoundingBox(Vector(-100, -100), Vector(100, 100)) assert zombie.move(FakeViewpoint(environment), limits) == Vector.ZERO def test_all_paths_blocked(self, zombie): """Test that zombies stay still when surrounded by other zombies. This effectively functions as a last check that zombies always have their own position as a fall-back, and don't register as blocking their own non-movement. """ def env_contents(vector): return default_zombie() if vector else zombie vectors = [Vector(dx, dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]] distant_human = [(Vector(2, 2), default_human())] zombies_all_around = [(v, env_contents(v)) for v in vectors] viewpoint = FakeViewpoint(distant_human + zombies_all_around) limits = BoundingBox(Vector(-100, -100), Vector(100, 100)) assert zombie.move(viewpoint, limits) == Vector.ZERO def test_alternate_path(self, zombie): environment = [ (Vector(2, 2), default_human()), (Vector(1, 1), default_zombie()), (Vector(1, 0), default_zombie()), ] limits = BoundingBox(Vector(-100, -100), Vector(100, 100)) assert zombie.move(FakeViewpoint(environment), limits) == Vector(0, 1) @given( st.lists(st.tuples(vectors(max_offset=1), humans), min_size=1, max_size=1)) def test_attack(self, zombie, environment): vector = environment[0][0] assert zombie.attack(FakeViewpoint(environment)) == vector @given(environments(characters=humans)) @example([(Vector(2, 0), default_human())]) def test_targets_out_of_range(self, zombie, environment): biting_range = BoundingBox(Vector(-1, -1), Vector(2, 2)) assume(all(e[0] not in biting_range for e in environment)) assert zombie.attack(FakeViewpoint(environment)) is None