Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion tests/test_nmea/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

from typing import Any

import pytest

from nmea.protocols import NMEASentenceHandler


class ConformingNMEADecoder:
"""A dummy class that implements the NMEASentenceHandler protocol."""

def decode(self, sentence: str) -> dict[str, Any]:
return {"talker": "GP", "sentence_id": "RMC"}
if not sentence.startswith("$") and not sentence.startswith("!"):
raise ValueError("Invalid NMEA sentence format")
return {"talker": sentence[1:3], "sentence_id": sentence[3:6], "raw": sentence}

def checksum(self, sentence: str) -> str:
return "00"
Expand All @@ -29,6 +33,13 @@ def checksum(self, sentence: str) -> str:
return "00"


def process_nmea_sentence(
handler: NMEASentenceHandler, sentence: str
) -> dict[str, Any]:
"""Helper function to test protocol-based decoding behavior."""
return handler.decode(sentence)


def test_nmea_sentence_handler_conforming() -> None:
"""Test that a compliant class is recognized by the protocol."""
decoder = ConformingNMEADecoder()
Expand All @@ -42,3 +53,35 @@ def test_nmea_sentence_handler_non_conforming() -> None:

missing_decode = NonConformingNMEADecoderMissingDecode()
assert not isinstance(missing_decode, NMEASentenceHandler)


def test_nmea_sentence_handler_decode_execution() -> None:
"""Test calling the decode method on a conforming NMEASentenceHandler instance."""
decoder: NMEASentenceHandler = ConformingNMEADecoder()
sentence = "$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A"

result = decoder.decode(sentence)

assert isinstance(result, dict)
assert result["talker"] == "GP"
assert result["sentence_id"] == "RMC"
assert result["raw"] == sentence


def test_nmea_sentence_handler_decode_via_protocol_helper() -> None:
"""Test calling decode via a function accepting NMEASentenceHandler protocol type."""
decoder = ConformingNMEADecoder()
sentence = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47"

result = process_nmea_sentence(decoder, sentence)

assert result == {"talker": "GP", "sentence_id": "GGA", "raw": sentence}


def test_nmea_sentence_handler_decode_error_handling() -> None:
"""Test that decode propagates exceptions for invalid inputs when implemented."""
decoder = ConformingNMEADecoder()
invalid_sentence = "INVALID_SENTENCE"

with pytest.raises(ValueError, match="Invalid NMEA sentence format"):
decoder.decode(invalid_sentence)
Loading