Skip to content

Commit

Permalink
LNX-177 Added tests and some adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
EasyJB committed Jul 30, 2023
1 parent bf3c5c4 commit 431070f
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 10 deletions.
35 changes: 25 additions & 10 deletions lynx/common/actions/user_helper_functions.py
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
5 changes: 5 additions & 0 deletions lynx/common/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ def dist_to(self, point) -> float:
# Euclidean distance from `self` to `point`
dist: float = math.sqrt((self.x - point.x)**2 + (self.y - point.y)**2)
return dist

def manhattan_distance(self, point) -> float:
dist: float = abs(self.x - point.x) + abs(self.y - point.y)
return dist

88 changes: 88 additions & 0 deletions tests/user_helper_functions_test.py
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)

1 comment on commit 431070f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
lynx/common
   enums.py16850%17–24
   scene.py50198%50
lynx/common/actions
   common_requirements.py43393%18–19, 32
   create_object.py13285%20–22
   error_log.py9278%14, 17
   message_log.py10280%15, 18
   remove_object.py12192%21
TOTAL4031995% 

Tests Skipped Failures Errors Time
64 0 💤 0 ❌ 0 🔥 0.296s ⏱️

Please sign in to comment.