|
| 1 | +# pylint: disable=missing-function-docstring,missing-module-docstring |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2021 Jonas Kittner |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: MIT |
| 6 | +import pytest |
| 7 | + |
| 8 | +from adafruit_gps import _parse_degrees |
| 9 | +from adafruit_gps import _parse_int |
| 10 | +from adafruit_gps import _parse_float |
| 11 | +from adafruit_gps import _read_degrees |
| 12 | +from adafruit_gps import _parse_talker |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + ("val", "exp"), |
| 17 | + ( |
| 18 | + pytest.param("0023.456", 0.390933, id="leading zero"), |
| 19 | + pytest.param("6413.9369", 64.23228, id="regular value"), |
| 20 | + pytest.param("2747.416122087989", 27.79027, id="long value"), |
| 21 | + ), |
| 22 | +) |
| 23 | +def test_parse_degrees(val, exp): |
| 24 | + assert _parse_degrees(val) == pytest.approx(exp) |
| 25 | + |
| 26 | + |
| 27 | +def test_parse_degrees_too_short(): |
| 28 | + assert _parse_degrees("12") is None |
| 29 | + |
| 30 | + |
| 31 | +def test_parse_int(): |
| 32 | + assert _parse_int("456") == 456 |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize( |
| 36 | + "val", |
| 37 | + (None, ""), |
| 38 | +) |
| 39 | +def test_parse_int_invalid(val): |
| 40 | + assert _parse_int(val) is None |
| 41 | + |
| 42 | + |
| 43 | +def test_parse_float(): |
| 44 | + assert _parse_float("456") == 456 |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "val", |
| 49 | + (None, ""), |
| 50 | +) |
| 51 | +def test_parse_float_invalid(val): |
| 52 | + assert _parse_float(val) is None |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize( |
| 56 | + ("data", "neg", "exp"), |
| 57 | + ( |
| 58 | + pytest.param([27.79027, "S"], "s", -27.79027, id="south negative"), |
| 59 | + pytest.param([64.23228, "N"], "s", 64.23228, id="north not negative"), |
| 60 | + pytest.param([123.4567, "W"], "w", -123.4567, id="west negative"), |
| 61 | + pytest.param([10.7891, "E"], "w", 10.7891, id="east not negative"), |
| 62 | + ), |
| 63 | +) |
| 64 | +def test_read_degrees(data, neg, exp): |
| 65 | + assert _read_degrees(data, 0, neg) == exp |
| 66 | + |
| 67 | + |
| 68 | +def test_parse_talker_prop_code(): |
| 69 | + assert _parse_talker(b"PMTK001") == (b"P", b"MTK001") |
| 70 | + |
| 71 | + |
| 72 | +def test_parse_talker_regular(): |
| 73 | + assert _parse_talker(b"GPRMC") == (b"GP", b"RMC") |
0 commit comments