3
3
"""
4
4
5
5
import pytest
6
+
6
7
from modules import lidar_detection
7
- from modules import lidar_oscillation
8
8
from modules .lidar_parser import lidar_parser
9
9
10
10
ANGLE_UP = 15.0
11
11
ANGLE_DOWN = - 15.0
12
12
13
+ # pylint: disable=redefined-outer-name, duplicate-code
14
+
13
15
14
16
@pytest .fixture ()
15
17
def lidar_parser_instance () -> lidar_parser .LidarParser : # type: ignore
18
+ """
19
+ Fixture to initialize a LidarParser instance for testing.
20
+ """
16
21
parser = lidar_parser .LidarParser ()
17
22
yield parser
18
23
19
24
20
25
@pytest .fixture ()
21
26
def lidar_detection_up () -> lidar_detection .LidarDetection : # type: ignore
27
+ """
28
+ Fixture to create an upward LidarDetection object.
29
+ """
22
30
result , detection = lidar_detection .LidarDetection .create (5.0 , ANGLE_UP )
23
31
assert result
24
32
assert detection is not None
@@ -27,6 +35,9 @@ def lidar_detection_up() -> lidar_detection.LidarDetection: # type: ignore
27
35
28
36
@pytest .fixture ()
29
37
def higher_angle_detection_up (lidar_detection_up : lidar_detection .LidarDetection ) -> lidar_detection .LidarDetection : # type: ignore
38
+ """
39
+ Fixture to create a LidarDetection object with a higher upward angle than the previous detection.
40
+ """
30
41
result , higher_detection = lidar_detection .LidarDetection .create (
31
42
5.0 , lidar_detection_up .angle + 5.0
32
43
)
@@ -37,6 +48,9 @@ def higher_angle_detection_up(lidar_detection_up: lidar_detection.LidarDetection
37
48
38
49
@pytest .fixture ()
39
50
def lidar_detection_down () -> lidar_detection .LidarDetection : # type: ignore
51
+ """
52
+ Fixture to create a downward LidarDetection object.
53
+ """
40
54
result , detection = lidar_detection .LidarDetection .create (5.0 , ANGLE_DOWN )
41
55
assert result
42
56
assert detection is not None
@@ -45,6 +59,9 @@ def lidar_detection_down() -> lidar_detection.LidarDetection: # type: ignore
45
59
46
60
@pytest .fixture ()
47
61
def lower_angle_detection_down (lidar_detection_up : lidar_detection .LidarDetection ) -> lidar_detection .LidarDetection : # type: ignore
62
+ """
63
+ Fixture to create a LidarDetection object with a lower downward angle than the previous detection.
64
+ """
48
65
result , lower_detection = lidar_detection .LidarDetection .create (
49
66
5.0 , lidar_detection_up .angle - 5.0
50
67
)
@@ -63,6 +80,9 @@ def test_initial_run_no_oscillation(
63
80
lidar_parser_instance : lidar_parser .LidarParser ,
64
81
lidar_detection_up : lidar_detection .LidarDetection ,
65
82
) -> None :
83
+ """
84
+ Test that a single upward detection does not complete an oscillation.
85
+ """
66
86
result , oscillation = lidar_parser_instance .run (lidar_detection_up )
67
87
assert not result
68
88
assert oscillation is None
@@ -74,7 +94,9 @@ def test_oscillation_detected_up_to_down(
74
94
higher_angle_detection_up : lidar_detection .LidarDetection ,
75
95
lidar_detection_down : lidar_detection .LidarDetection ,
76
96
) -> None :
77
-
97
+ """
98
+ Test detection of an oscillation with a change in direction from up to down after multiple detections.
99
+ """
78
100
lidar_parser_instance .run (lidar_detection_up )
79
101
lidar_parser_instance .run (higher_angle_detection_up )
80
102
result , oscillation = lidar_parser_instance .run (lidar_detection_down )
@@ -86,7 +108,9 @@ def test_no_oscillation_on_same_direction(
86
108
lidar_parser_instance : lidar_parser .LidarParser ,
87
109
lidar_detection_up : lidar_detection .LidarDetection ,
88
110
) -> None :
89
-
111
+ """
112
+ Test that no oscillation is detected when multiple detections are in the same upward direction.
113
+ """
90
114
for _ in range (5 ):
91
115
result , oscillation = lidar_parser_instance .run (lidar_detection_up )
92
116
assert not result
@@ -98,7 +122,9 @@ def test_direction_initialization_on_first_run(
98
122
lidar_detection_up : lidar_detection .LidarDetection ,
99
123
lower_angle_detection_down : lidar_detection .LidarDetection ,
100
124
) -> None :
101
-
125
+ """
126
+ Test that the initial detection sets the direction to DOWN based on a second downward detection.
127
+ """
102
128
lidar_parser_instance .run (lidar_detection_up )
103
129
lidar_parser_instance .run (lower_angle_detection_down )
104
130
assert lidar_parser_instance .direction == lidar_parser .Direction .DOWN
@@ -110,7 +136,9 @@ def test_oscillation_reset_after_detection(
110
136
higher_angle_detection_up : lidar_detection .LidarDetection ,
111
137
lidar_detection_down : lidar_detection .LidarDetection ,
112
138
) -> None :
113
-
139
+ """
140
+ Test that the parser resets its readings after detecting an oscillation.
141
+ """
114
142
lidar_parser_instance .run (lidar_detection_up )
115
143
lidar_parser_instance .run (higher_angle_detection_up )
116
144
result , oscillation = lidar_parser_instance .run (lidar_detection_down )
@@ -129,8 +157,6 @@ def test_alternating_up_down_oscillations(
129
157
Test the parser with alternating up and down angles to simulate multiple oscillations.
130
158
"""
131
159
oscillation_count = 0
132
-
133
- # Detect the first oscillation with three readings: two up, one down
134
160
lidar_parser_instance .run (lidar_detection_up )
135
161
lidar_parser_instance .run (higher_angle_detection_up )
136
162
result , oscillation = lidar_parser_instance .run (lidar_detection_down )
@@ -139,25 +165,26 @@ def test_alternating_up_down_oscillations(
139
165
oscillation_count += 1
140
166
assert oscillation is not None
141
167
142
- # Subsequent oscillations should alternate and be detected on every change of angle
143
168
for i in range (4 ):
144
- if i % 2 == 0 : # Every even index provides an upward reading
169
+ if i % 2 == 0 :
145
170
result , oscillation = lidar_parser_instance .run (higher_angle_detection_up )
146
- else : # Every odd index provides a downward reading
171
+ else :
147
172
result , oscillation = lidar_parser_instance .run (lidar_detection_down )
148
173
149
174
if result :
150
175
oscillation_count += 1
151
176
assert oscillation is not None
152
177
153
- # Verify that five oscillations were detected in total
154
178
assert oscillation_count == 5
155
179
156
180
def test_no_oscillation_with_single_reading (
157
181
self ,
158
182
lidar_parser_instance : lidar_parser .LidarParser ,
159
183
lidar_detection_up : lidar_detection .LidarDetection ,
160
184
) -> None :
185
+ """
186
+ Test that a single detection does not produce an oscillation.
187
+ """
161
188
result , oscillation = lidar_parser_instance .run (lidar_detection_up )
162
189
assert not result
163
190
assert oscillation is None
@@ -169,6 +196,9 @@ def test_oscillation_on_direction_change_after_none(
169
196
higher_angle_detection_up : lidar_detection .LidarDetection ,
170
197
lidar_detection_down : lidar_detection .LidarDetection ,
171
198
) -> None :
199
+ """
200
+ Test that direction changes after Direction.NONE trigger the first oscillation properly.
201
+ """
172
202
lidar_parser_instance .run (lidar_detection_up )
173
203
lidar_parser_instance .run (higher_angle_detection_up )
174
204
assert lidar_parser_instance .direction == lidar_parser .Direction .UP
0 commit comments