|
| 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") == "" |
0 commit comments