-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LNX-177 Added tests and some adjustments
- Loading branch information
Showing
3 changed files
with
118 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,51 @@ | ||
from dataclasses import dataclass | ||
import random | ||
from typing import List | ||
|
||
|
||
from lynx.common.actions.action import Action | ||
from lynx.common.vector import Vector | ||
from lynx.common.object import Object | ||
from lynx.common.actions.common_requirements import CommonRequirements as Req | ||
|
||
|
||
def objects_around(object_id: int, scene: 'Scene', distance: int) -> List[int]: | ||
selected_objects: List[int] = [] | ||
agent_position: Vector = scene.get_object_by_id(object_id).position | ||
for obj in scene.entities: | ||
if obj.position.dist_to(agent_position) < distance: | ||
if obj.position.manhattan_distance(agent_position) <= distance: | ||
selected_objects.append(obj.id) | ||
selected_objects.remove(object_id) | ||
return selected_objects | ||
|
||
|
||
def get_position(scene: 'Scene',object_id: int) -> Vector: | ||
def get_position(scene: 'Scene', object_id: int) -> Vector: | ||
return scene.get_object_by_id(object_id).position | ||
|
||
|
||
def get_type(scene: 'Scene',object_id: int) -> str: | ||
def can_i_stand(scene: 'Scene', positon: Vector) -> bool: | ||
return Req.is_walkable(scene, positon) | ||
|
||
|
||
def get_type(scene: 'Scene', object_id: int) -> str: | ||
return scene.get_object_by_id(object_id).name | ||
|
||
|
||
def distance_to(agent_id: int, scene: 'Scene', object_id: int) -> int: | ||
return scene.get_object_by_id(agent_id).position.dist_to(scene.get_object_by_id(object_id).position) | ||
def distance_to(scene: 'Scene', object_id: int, agent_id: int) -> int: | ||
return scene.get_object_by_id(agent_id).position.manhattan_distance(scene.get_object_by_id(object_id).position) | ||
|
||
|
||
def fillter_list(scene: 'Scene', object_ids: List[int], object_type: str) -> List[int]: | ||
def fillter_objects(scene: 'Scene', object_ids: List[int], object_type: str) -> List[int]: | ||
filltered_objects: List[int] = [] | ||
for object_id in object_ids: | ||
if scene.get_object_by_id(object_id).name == object_type: | ||
filltered_objects.append(object_id) | ||
return filltered_objects | ||
return filltered_objects | ||
|
||
|
||
def random_direction(scene: 'Scene', object_id: int) -> Vector: | ||
positions: List[Vector] = [Vector(0, 1), Vector(1, 0), Vector(-1, 0), Vector(0, -1)] | ||
available_positions: List[Vector] = [] | ||
object_position: Vector = scene.get_object_by_id(object_id).position | ||
for position in positions: | ||
if Req.is_walkable(scene, object_position + position): | ||
available_positions.append(position) | ||
random_choice = random.choice(available_positions) | ||
return random_choice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import random | ||
from typing import List | ||
|
||
from lynx.common.object import Object | ||
from lynx.common.scene import Scene | ||
from lynx.common.vector import Vector | ||
from lynx.common.actions.user_helper_functions import objects_around, get_position, can_i_stand, get_type, distance_to, fillter_objects, random_direction | ||
|
||
|
||
class TestUserHelperFunctions: | ||
scene = Scene() | ||
dummy_agent = Object(id=1, name="agent", position=Vector(0, 0)) | ||
scene.add_entity(dummy_agent) | ||
dummy_object1 = Object(id=2, name="dummy", position=Vector(5, 5), tags=['walkable']) | ||
scene.add_entity(dummy_object1) | ||
dummy_object2 = Object(id=3, name="dummy", position=Vector(3, 5)) | ||
scene.add_entity(dummy_object2) | ||
dummy_object3 = Object(id=4, name="diff_dummy", position=Vector(1, 1)) | ||
scene.add_entity(dummy_object3) | ||
|
||
def test_objects_around_success(self): | ||
expected_result = [3, 4] | ||
|
||
assert expected_result == objects_around(1, self.scene, 9) | ||
|
||
def test_objects_around_failure(self): | ||
expected_result = [3, 4] | ||
|
||
assert expected_result != objects_around(1, self.scene, 7) | ||
|
||
def test_get_position_success(self): | ||
position: Vector = Vector(3, 5) | ||
|
||
assert position == get_position(self.scene, 3) | ||
|
||
def test_can_i_stand_success(self): | ||
position: bool = True | ||
|
||
assert position == can_i_stand(self.scene, Vector(5, 5)) | ||
|
||
def test_can_i_stand_failure(self): | ||
position: bool = False | ||
|
||
assert position == can_i_stand(self.scene, Vector(3, 5)) | ||
|
||
|
||
def test_get_type_success(self): | ||
name_type: str = 'agent' | ||
|
||
assert name_type == get_type(self.scene, 1) | ||
|
||
def test_get_type_failure(self): | ||
name_type: str = 'agent' | ||
|
||
assert name_type != get_type(self.scene, 2) | ||
|
||
def test_distance_to_success(self): | ||
distance: float = 2 | ||
|
||
assert distance == distance_to(self.scene, 4, 1) | ||
|
||
def test_fillter_objects_success(self): | ||
selected_ids: List[int] = [2, 3] | ||
|
||
assert selected_ids == fillter_objects(self.scene, [1,2,3,4], 'dummy') | ||
|
||
def test_fillter_objects_failure(self): | ||
selected_ids: List[int] = [1] | ||
|
||
assert selected_ids != fillter_objects(self.scene, [1, 2, 3, 4], 'empty') | ||
|
||
|
||
def test_random_direction_success(self): | ||
random.seed(12) | ||
expected_direction: Vector = Vector(-1, 0) | ||
|
||
self.scene.add_entity(Object(id=10, name="Grass", | ||
position=Vector(-1, 0), tags=['walkable'])) | ||
|
||
self.scene.add_entity(Object(id=11, name="Grass", | ||
position=Vector(0, 1), tags=['walkable'])) | ||
assert expected_direction == random_direction(self.scene, 1) | ||
|
||
def test_random_direction_failure(self): | ||
random.seed(12) | ||
expected_direction: Vector = Vector(1, 0) | ||
|
||
assert expected_direction != random_direction(self.scene, 1) |
431070f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report