From b48b5b67891f119cfbfa5e21b94dd9d59abadde7 Mon Sep 17 00:00:00 2001 From: Han Date: Fri, 29 Nov 2024 23:34:46 +0800 Subject: [PATCH] Improve test coverage --- irsim/lib/behavior/behavior.py | 6 +++--- tests/test_all_objects.py | 30 ++++++++++++++++++++++++++++-- tests/test_all_objects.yaml | 16 ++++++++++++++++ tests/test_keyboard_handle.py | 25 ------------------------- 4 files changed, 47 insertions(+), 30 deletions(-) delete mode 100644 tests/test_keyboard_handle.py diff --git a/irsim/lib/behavior/behavior.py b/irsim/lib/behavior/behavior.py index 77c3316..d61892b 100644 --- a/irsim/lib/behavior/behavior.py +++ b/irsim/lib/behavior/behavior.py @@ -39,9 +39,9 @@ def gen_vel(self, objects): np.array (2, 1): Generated velocity for the agent. """ - if self.behavior_dict is None: - env_param.logger.error( - "Behavior not defined for object {}.".format(self.object_info.id) + if self.behavior_dict is None or not self.behavior_dict: + env_param.logger.warning( + "Behavior not defined for object {}. Robot will be static".format(self.object_info.id) ) return np.zeros((2, 1)) diff --git a/tests/test_all_objects.py b/tests/test_all_objects.py index 47f734a..49f7fbf 100644 --- a/tests/test_all_objects.py +++ b/tests/test_all_objects.py @@ -1,5 +1,7 @@ from irsim.util.util import time_it +from unittest.mock import Mock import irsim +from pynput import keyboard @time_it("test_all_objects") def test_all_objects(): @@ -19,11 +21,14 @@ def test_all_objects(): env2 = irsim.make('test_all_objects.yaml', display=False) - env2.robot.get_lidar_points() + env2.random_polygon_shape() + temp_points = env2.robot.get_lidar_points() + env2.draw_points(temp_points) + env2.robot.get_lidar_scan() env2.robot.get_lidar_offset() - for i in range(2): + for i in range(10): env2.step() env2.render(0.01) env2.end() @@ -58,5 +63,26 @@ def test_all_objects(): env6.render(0.01) env6.end() + env7 = irsim.make('test_keyboard_control.yaml', save_ani=False, display=False) + key_list = ['w', 'a', 's', 'd', 'q', 'e', 'z', 'c', 'r'] + + mock_key_list = [] + for c in key_list: + mock_key = Mock(spec=keyboard.Key) + mock_key.char = c + mock_key_list.append(mock_key) + + for i in range(30): + + for mock_key in mock_key_list: + env7._on_press(mock_key) + env7._on_release(mock_key) + + env7.step() + env7.render(0.01) + + env7.end() + + if __name__ == "__main__": test_all_objects() diff --git a/tests/test_all_objects.yaml b/tests/test_all_objects.yaml index 96f2c5d..b8acf66 100644 --- a/tests/test_all_objects.yaml +++ b/tests/test_all_objects.yaml @@ -22,6 +22,22 @@ robot: angle_range: 3.1415926 number: 100 noise: False + has_velocity: True + + - kinematics: {name: 'diff', noise: True} # omni, diff, ackers + shape: {name: 'circle', radius: 0.2} # radius + # shape: {name: 'rectangle', length: 0.5, width: 0.2} # radius + state: [1, 1, 0] + goal: [9, 9, 0] + # acce: [3, .inf] # acce of [linear, angular] or [v_x, v_y] or [linear, steer] + sensors: + - type: 'lidar2d' + range_min: 0 + range_max: 10 + angle_range: 3.1415926 + number: 100 + noise: False + has_velocity: True - kinematics: {name: 'acker'} # omni, diff, acker shape: {name: 'circle', radius: 0.2, wheel_base: 0.2} # radius diff --git a/tests/test_keyboard_handle.py b/tests/test_keyboard_handle.py deleted file mode 100644 index 15b1c1a..0000000 --- a/tests/test_keyboard_handle.py +++ /dev/null @@ -1,25 +0,0 @@ -import irsim -from pynput import keyboard -from unittest.mock import Mock - -env = irsim.make('test_keyboard_control.yaml', save_ani=False, display=False) - -key_list = ['w', 'a', 's', 'd', 'q', 'e', 'z', 'c', 'r'] - -mock_key_list = [] -for c in key_list: - mock_key = Mock(spec=keyboard.Key) - mock_key.char = c - mock_key_list.append(mock_key) - - -for i in range(30): - - for mock_key in mock_key_list: - env._on_press(mock_key) - env._on_release(mock_key) - - env.step() - env.render(0.01) - -env.end() \ No newline at end of file