diff --git a/tests/unit/test_lidar_parser.py b/tests/unit/test_lidar_parser.py index 457561b..baa50fd 100644 --- a/tests/unit/test_lidar_parser.py +++ b/tests/unit/test_lidar_parser.py @@ -3,22 +3,30 @@ """ import pytest + from modules import lidar_detection -from modules import lidar_oscillation from modules.lidar_parser import lidar_parser ANGLE_UP = 15.0 ANGLE_DOWN = -15.0 +# pylint: disable=redefined-outer-name, duplicate-code + @pytest.fixture() def lidar_parser_instance() -> lidar_parser.LidarParser: # type: ignore + """ + Fixture to initialize a LidarParser instance for testing. + """ parser = lidar_parser.LidarParser() yield parser @pytest.fixture() def lidar_detection_up() -> lidar_detection.LidarDetection: # type: ignore + """ + Fixture to create an upward LidarDetection object. + """ result, detection = lidar_detection.LidarDetection.create(5.0, ANGLE_UP) assert result assert detection is not None @@ -27,6 +35,9 @@ def lidar_detection_up() -> lidar_detection.LidarDetection: # type: ignore @pytest.fixture() def higher_angle_detection_up(lidar_detection_up: lidar_detection.LidarDetection) -> lidar_detection.LidarDetection: # type: ignore + """ + Fixture to create a LidarDetection object with a higher upward angle than the previous detection. + """ result, higher_detection = lidar_detection.LidarDetection.create( 5.0, lidar_detection_up.angle + 5.0 ) @@ -37,6 +48,9 @@ def higher_angle_detection_up(lidar_detection_up: lidar_detection.LidarDetection @pytest.fixture() def lidar_detection_down() -> lidar_detection.LidarDetection: # type: ignore + """ + Fixture to create a downward LidarDetection object. + """ result, detection = lidar_detection.LidarDetection.create(5.0, ANGLE_DOWN) assert result assert detection is not None @@ -45,6 +59,9 @@ def lidar_detection_down() -> lidar_detection.LidarDetection: # type: ignore @pytest.fixture() def lower_angle_detection_down(lidar_detection_up: lidar_detection.LidarDetection) -> lidar_detection.LidarDetection: # type: ignore + """ + Fixture to create a LidarDetection object with a lower downward angle than the previous detection. + """ result, lower_detection = lidar_detection.LidarDetection.create( 5.0, lidar_detection_up.angle - 5.0 ) @@ -63,6 +80,9 @@ def test_initial_run_no_oscillation( lidar_parser_instance: lidar_parser.LidarParser, lidar_detection_up: lidar_detection.LidarDetection, ) -> None: + """ + Test that a single upward detection does not complete an oscillation. + """ result, oscillation = lidar_parser_instance.run(lidar_detection_up) assert not result assert oscillation is None @@ -74,7 +94,9 @@ def test_oscillation_detected_up_to_down( higher_angle_detection_up: lidar_detection.LidarDetection, lidar_detection_down: lidar_detection.LidarDetection, ) -> None: - + """ + Test detection of an oscillation with a change in direction from up to down after multiple detections. + """ lidar_parser_instance.run(lidar_detection_up) lidar_parser_instance.run(higher_angle_detection_up) result, oscillation = lidar_parser_instance.run(lidar_detection_down) @@ -86,7 +108,9 @@ def test_no_oscillation_on_same_direction( lidar_parser_instance: lidar_parser.LidarParser, lidar_detection_up: lidar_detection.LidarDetection, ) -> None: - + """ + Test that no oscillation is detected when multiple detections are in the same upward direction. + """ for _ in range(5): result, oscillation = lidar_parser_instance.run(lidar_detection_up) assert not result @@ -98,7 +122,9 @@ def test_direction_initialization_on_first_run( lidar_detection_up: lidar_detection.LidarDetection, lower_angle_detection_down: lidar_detection.LidarDetection, ) -> None: - + """ + Test that the initial detection sets the direction to DOWN based on a second downward detection. + """ lidar_parser_instance.run(lidar_detection_up) lidar_parser_instance.run(lower_angle_detection_down) assert lidar_parser_instance.direction == lidar_parser.Direction.DOWN @@ -110,7 +136,9 @@ def test_oscillation_reset_after_detection( higher_angle_detection_up: lidar_detection.LidarDetection, lidar_detection_down: lidar_detection.LidarDetection, ) -> None: - + """ + Test that the parser resets its readings after detecting an oscillation. + """ lidar_parser_instance.run(lidar_detection_up) lidar_parser_instance.run(higher_angle_detection_up) result, oscillation = lidar_parser_instance.run(lidar_detection_down) @@ -129,8 +157,6 @@ def test_alternating_up_down_oscillations( Test the parser with alternating up and down angles to simulate multiple oscillations. """ oscillation_count = 0 - - # Detect the first oscillation with three readings: two up, one down lidar_parser_instance.run(lidar_detection_up) lidar_parser_instance.run(higher_angle_detection_up) result, oscillation = lidar_parser_instance.run(lidar_detection_down) @@ -139,18 +165,16 @@ def test_alternating_up_down_oscillations( oscillation_count += 1 assert oscillation is not None - # Subsequent oscillations should alternate and be detected on every change of angle for i in range(4): - if i % 2 == 0: # Every even index provides an upward reading + if i % 2 == 0: result, oscillation = lidar_parser_instance.run(higher_angle_detection_up) - else: # Every odd index provides a downward reading + else: result, oscillation = lidar_parser_instance.run(lidar_detection_down) if result: oscillation_count += 1 assert oscillation is not None - # Verify that five oscillations were detected in total assert oscillation_count == 5 def test_no_oscillation_with_single_reading( @@ -158,6 +182,9 @@ def test_no_oscillation_with_single_reading( lidar_parser_instance: lidar_parser.LidarParser, lidar_detection_up: lidar_detection.LidarDetection, ) -> None: + """ + Test that a single detection does not produce an oscillation. + """ result, oscillation = lidar_parser_instance.run(lidar_detection_up) assert not result assert oscillation is None @@ -169,6 +196,9 @@ def test_oscillation_on_direction_change_after_none( higher_angle_detection_up: lidar_detection.LidarDetection, lidar_detection_down: lidar_detection.LidarDetection, ) -> None: + """ + Test that direction changes after Direction.NONE trigger the first oscillation properly. + """ lidar_parser_instance.run(lidar_detection_up) lidar_parser_instance.run(higher_angle_detection_up) assert lidar_parser_instance.direction == lidar_parser.Direction.UP