Skip to content

Commit

Permalink
Fix #4 and add corresponding tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tin Lai <[email protected]>
  • Loading branch information
soraxas committed Oct 2, 2021
1 parent 2478bb5 commit 1cc0756
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions env.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def __getattr__(self, attr):
"""
return object.__getattribute__(self.visualiser, attr)

@property
def sampler(self):
"""Pass through attribute access to sampler."""
return self.args.sampler

@staticmethod
def radian_dist(p1: np.ndarray, p2: np.ndarray):
"""Return the (possibly wrapped) distance between two vector of angles in
Expand Down
83 changes: 83 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from unittest import TestCase

import numpy as np

import env
import visualiser
from planners.basePlanner import Planner
from samplers.baseSampler import Sampler
from main import generate_args
from tests.common_vars import DummyPlannerClass
from utils import planner_registry


class TestGenerateArgs(TestCase):
def test_missing_argunment(self):
with self.assertRaises(TypeError):
generate_args()
with self.assertRaises(TypeError):
generate_args(planner_id="rrt")
with self.assertRaises(TypeError):
generate_args(map_fname="maps/4d.png")

# should not raise error
generate_args(planner_id="rrt", map_fname="maps/4d.png")

# test error if the planner id has not been registered yet
with self.assertRaises(ValueError):
generate_args(planner_id="my_planner_id", map_fname="maps/4d.png")

# test that after the planner id is registered, it will work.
planner_registry.register_planner(
planner_id="my_planner_id",
planner_class=DummyPlannerClass,
sampler_id="random",
)
generate_args(planner_id="my_planner_id", map_fname="maps/4d.png")

def test_actual_planning(self):
visualiser.VisualiserSwitcher.choose_visualiser("base")
args = generate_args(
planner_id="rrt",
map_fname="maps/test.png",
start_pt=np.array([25, 123]),
goal_pt=np.array([225, 42]),
)
args.no_display = True

e = env.Env(args, fixed_seed=0)
ori_method = e.planner.run_once

# prepare an exception to escape from the planning loop
class PlanningSuccess(Exception):
pass

def planner_run_once_with_side_effect(*args, **kwargs):
# pass through to planner
ori_method(*args, **kwargs)
if e.planner.c_max < float("inf"):
raise PlanningSuccess()

# patch the planner run_once such that it will terminates as soon as the
# planning problem is finished.
e.planner.run_once = planner_run_once_with_side_effect
with self.assertRaises(PlanningSuccess):
e.run()

def test_get_attribute(self):
visualiser.VisualiserSwitcher.choose_visualiser("pygame")
args = generate_args(
planner_id="rrt",
map_fname="maps/test.png",
start_pt=np.array([25, 123]),
goal_pt=np.array([225, 42]),
)
args.no_display = True

e = env.Env(args, fixed_seed=0)

# test get planner
assert isinstance(e.planner, Planner)

# test get sampler
assert isinstance(e.sampler, Sampler)

0 comments on commit 1cc0756

Please sign in to comment.