Skip to content

Commit 801df09

Browse files
google-labs-jules[bot]schwehr
authored andcommitted
test(nmea): add dedicated tests for rmc.py
Extracts duplicate rmc tests from `test_rmc_zda_znt.py` and creates a dedicated `test_rmc.py` file with more comprehensive tests for regex extraction and boundary conditions on coordinate processing. Renames old test file to `test_zda_znt.py`.
1 parent f50d003 commit 801df09

2 files changed

Lines changed: 87 additions & 38 deletions

File tree

‎tests/test_nmea/test_rmc.py‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""Unit tests for NMEA RMC sentence parsers."""
2+
3+
import pytest
4+
5+
from nmea import rmc
6+
7+
8+
def test_rmc_match_and_lonlat() -> None:
9+
sentence = "$GPRMC,173011.82,V,4222.8770,N,07103.0096,W,0.00,0.0,151008,14.9,W,N*27"
10+
match = rmc.compile_obj.search(sentence)
11+
assert match is not None
12+
assert match.group("msg_type") == "RMC"
13+
assert match.group("hour") == "17"
14+
assert match.group("minute") == "30"
15+
16+
lon, lat = rmc.lonlat(match)
17+
assert abs(lon - (-71.05016)) < 1e-4
18+
assert abs(lat - 42.3812833) < 1e-4
19+
20+
21+
def test_rmc_lonlat_south_east() -> None:
22+
sentence = (
23+
"$GPRMC,123519.00,A,4807.0380,S,01131.0000,E,022.4,084.4,230394,003.1,W*6A"
24+
)
25+
match = rmc.compile_obj.search(sentence)
26+
assert match is not None
27+
28+
lon, lat = rmc.lonlat(match)
29+
assert abs(lon - 11.516666) < 1e-4
30+
assert abs(lat - (-48.1173)) < 1e-4
31+
32+
33+
def test_rmc_lonlat_zero() -> None:
34+
sentence = (
35+
"$GPRMC,123519.00,A,0000.0000,N,00000.0000,E,022.4,084.4,230394,003.1,W*6A"
36+
)
37+
match = rmc.compile_obj.search(sentence)
38+
assert match is not None
39+
40+
lon, lat = rmc.lonlat(match)
41+
assert lon == 0.0
42+
assert lat == 0.0
43+
44+
45+
def test_rmc_match_all_groups() -> None:
46+
sentence = "$GPRMC,123519.00,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A"
47+
match = rmc.compile_obj.search(sentence)
48+
assert match is not None
49+
assert match.group("prefix") == "GP"
50+
assert match.group("msg_type") == "RMC"
51+
assert match.group("hour") == "12"
52+
assert match.group("minute") == "35"
53+
assert match.group("second") == "19.00"
54+
assert match.group("status") == "A"
55+
assert match.group("latitude") == "4807.038"
56+
assert match.group("lat_deg") == "48"
57+
assert match.group("lat_min") == "07.038"
58+
assert match.group("north_south") == "N"
59+
assert match.group("longitude") == "01131.000"
60+
assert match.group("lon_deg") == "011"
61+
assert match.group("lon_min") == "31.000"
62+
assert match.group("east_west") == "E"
63+
assert match.group("speed_knots") == "022.4"
64+
assert match.group("course_degrees") == "084.4"
65+
assert match.group("day") == "23"
66+
assert match.group("month") == "03"
67+
assert match.group("year") == "94"
68+
assert match.group("magnetic_variation_degrees") == "003.1"
69+
assert match.group("mag_var_east_west") == "W"
70+
assert match.group("checksum") == "*6A"
71+
72+
73+
def test_rmc_lonlat_zero_padding_edge_case() -> None:
74+
# A constructed match where lon/lat have leading zeroes that will be stripped to empty strings
75+
# Simulate the empty string condition in lonlat logic:
76+
# lon_deg = 0 if len(lon_deg) == 0 else int(lon_deg)
77+
sentence = "$GPRMC,123519.00,A,0000.000,N,00000.000,E,022.4,084.4,230394,003.1,W*6A"
78+
match = rmc.compile_obj.search(sentence)
79+
assert match is not None
80+
lon, lat = rmc.lonlat(match)
81+
assert lon == 0.0
82+
assert lat == 0.0
83+
84+
# Ensure it works when strings are all zeros
85+
assert match.group("lon_deg").lstrip("0") == ""
86+
assert match.group("lat_deg").lstrip("0") == ""
Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,10 @@
22

33
import pytest
44

5-
from nmea import rmc, zda, znt
5+
from nmea import zda, znt
66
from nmea.nmea_error import NmeaChecksumError
77

88

9-
def test_rmc_match_and_lonlat() -> None:
10-
sentence = "$GPRMC,173011.82,V,4222.8770,N,07103.0096,W,0.00,0.0,151008,14.9,W,N*27"
11-
match = rmc.compile_obj.search(sentence)
12-
assert match is not None
13-
assert match.group("msg_type") == "RMC"
14-
assert match.group("hour") == "17"
15-
assert match.group("minute") == "30"
16-
17-
lon, lat = rmc.lonlat(match)
18-
assert abs(lon - (-71.05016)) < 1e-4
19-
assert abs(lat - 42.3812833) < 1e-4
20-
21-
22-
def test_rmc_lonlat_south_east() -> None:
23-
sentence = (
24-
"$GPRMC,123519.00,A,4807.0380,S,01131.0000,E,022.4,084.4,230394,003.1,W*6A"
25-
)
26-
match = rmc.compile_obj.search(sentence)
27-
assert match is not None
28-
29-
lon, lat = rmc.lonlat(match)
30-
assert abs(lon - 11.516666) < 1e-4
31-
assert abs(lat - (-48.1173)) < 1e-4
32-
33-
34-
def test_rmc_lonlat_zero() -> None:
35-
sentence = (
36-
"$GPRMC,123519.00,A,0000.0000,N,00000.0000,E,022.4,084.4,230394,003.1,W*6A"
37-
)
38-
match = rmc.compile_obj.search(sentence)
39-
assert match is not None
40-
41-
lon, lat = rmc.lonlat(match)
42-
assert lon == 0.0
43-
assert lat == 0.0
44-
45-
469
def test_zda_decode_and_epoch() -> None:
4710
sentence = "$ZQZDA,110003.00,27,03,2006,-5,00*47"
4811
decoded = zda.zdaDecode(sentence)

0 commit comments

Comments
 (0)