|
| 1 | +import unittest |
| 2 | +from aprslib.parsing import parse |
| 3 | +from aprslib.parsing.misc import parse_maidenhead_locator |
| 4 | +from aprslib.exceptions import ParseError |
| 5 | + |
| 6 | + |
| 7 | +class ParseMaidenheadLocator(unittest.TestCase): |
| 8 | + """Test suite for APRS maidenhead locator beacon parsing""" |
| 9 | + |
| 10 | + def test_valid_4_char_locator(self): |
| 11 | + """Test 4-character maidenhead locator (field + square)""" |
| 12 | + packet = "TEST>APRS:[FN31]" |
| 13 | + result = parse(packet) |
| 14 | + |
| 15 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 16 | + self.assertEqual(result['locator'], 'FN31') |
| 17 | + self.assertEqual(result['locator_precision'], 4) |
| 18 | + |
| 19 | + def test_valid_6_char_locator(self): |
| 20 | + """Test 6-character maidenhead locator (field + square + subsquare)""" |
| 21 | + packet = "TEST>APRS:[IO91SX]" |
| 22 | + result = parse(packet) |
| 23 | + |
| 24 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 25 | + self.assertEqual(result['locator'], 'IO91SX') |
| 26 | + self.assertEqual(result['locator_precision'], 6) |
| 27 | + |
| 28 | + def test_valid_8_char_locator(self): |
| 29 | + """Test 8-character maidenhead locator (field + square + subsquare + extended)""" |
| 30 | + packet = "TEST>APRS:[FN31pr45]" |
| 31 | + result = parse(packet) |
| 32 | + |
| 33 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 34 | + self.assertEqual(result['locator'], 'FN31PR45') |
| 35 | + self.assertEqual(result['locator_precision'], 8) |
| 36 | + |
| 37 | + def test_locator_with_symbol(self): |
| 38 | + """Test locator with symbol table and symbol""" |
| 39 | + packet = "TEST>APRS:[IO91SX/-]" |
| 40 | + result = parse(packet) |
| 41 | + |
| 42 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 43 | + self.assertEqual(result['locator'], 'IO91SX') |
| 44 | + self.assertEqual(result['locator_precision'], 6) |
| 45 | + self.assertEqual(result['symbol_table'], '/') |
| 46 | + self.assertEqual(result['symbol'], '-') |
| 47 | + |
| 48 | + def test_locator_with_comment(self): |
| 49 | + """Test locator with comment""" |
| 50 | + packet = "TEST>APRS:[IO91SX]Test comment" |
| 51 | + result = parse(packet) |
| 52 | + |
| 53 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 54 | + self.assertEqual(result['locator'], 'IO91SX') |
| 55 | + self.assertEqual(result['comment'], 'Test comment') |
| 56 | + |
| 57 | + def test_locator_with_symbol_and_comment(self): |
| 58 | + """Test locator with symbol and comment""" |
| 59 | + packet = "TEST>APRS:[IO91SX/-]Test comment" |
| 60 | + result = parse(packet) |
| 61 | + |
| 62 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 63 | + self.assertEqual(result['locator'], 'IO91SX') |
| 64 | + self.assertEqual(result['symbol_table'], '/') |
| 65 | + self.assertEqual(result['symbol'], '-') |
| 66 | + self.assertEqual(result['comment'], 'Test comment') |
| 67 | + |
| 68 | + def test_locator_with_slash_in_comment(self): |
| 69 | + """Test locator where / is part of comment, not symbol table""" |
| 70 | + packet = "TEST>APRS:[FN31pr/Test comment]" |
| 71 | + result = parse(packet) |
| 72 | + |
| 73 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 74 | + self.assertEqual(result['locator'], 'FN31PR') |
| 75 | + self.assertNotIn('symbol', result) |
| 76 | + self.assertEqual(result['comment'], '/Test comment') |
| 77 | + |
| 78 | + def test_locator_case_insensitive(self): |
| 79 | + """Test that locator letters are case-insensitive and normalized to uppercase""" |
| 80 | + packet = "TEST>APRS:[io91sx]" |
| 81 | + result = parse(packet) |
| 82 | + |
| 83 | + self.assertEqual(result['locator'], 'IO91SX') |
| 84 | + self.assertEqual(result['locator_precision'], 6) |
| 85 | + |
| 86 | + def test_locator_mixed_case(self): |
| 87 | + """Test locator with mixed case letters""" |
| 88 | + packet = "TEST>APRS:[Fn31Pr]" |
| 89 | + result = parse(packet) |
| 90 | + |
| 91 | + self.assertEqual(result['locator'], 'FN31PR') |
| 92 | + self.assertEqual(result['locator_precision'], 6) |
| 93 | + |
| 94 | + def test_locator_with_backslash_symbol_table(self): |
| 95 | + """Test locator with backslash symbol table""" |
| 96 | + packet = "TEST>APRS:[IO91SX\\-]" |
| 97 | + result = parse(packet) |
| 98 | + |
| 99 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 100 | + self.assertEqual(result['locator'], 'IO91SX') |
| 101 | + self.assertEqual(result['symbol_table'], '\\') |
| 102 | + self.assertEqual(result['symbol'], '-') |
| 103 | + |
| 104 | + def test_locator_no_closing_bracket(self): |
| 105 | + """Test locator without closing bracket""" |
| 106 | + packet = "TEST>APRS:[IO91SX" |
| 107 | + result = parse(packet) |
| 108 | + |
| 109 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 110 | + self.assertEqual(result['locator'], 'IO91SX') |
| 111 | + |
| 112 | + def test_locator_empty_body(self): |
| 113 | + """Test locator with empty body""" |
| 114 | + packet = "TEST>APRS:[" |
| 115 | + result = parse(packet) |
| 116 | + |
| 117 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 118 | + self.assertNotIn('locator', result) |
| 119 | + |
| 120 | + def test_invalid_locator_too_short(self): |
| 121 | + """Test that invalid locator (too short) raises error""" |
| 122 | + packet = "TEST>APRS:[FN3]" |
| 123 | + |
| 124 | + with self.assertRaises(ParseError) as context: |
| 125 | + parse(packet) |
| 126 | + |
| 127 | + self.assertIn("invalid maidenhead locator format", str(context.exception)) |
| 128 | + |
| 129 | + def test_invalid_locator_wrong_format(self): |
| 130 | + """Test that invalid locator format raises error""" |
| 131 | + packet = "TEST>APRS:[1234]" |
| 132 | + |
| 133 | + with self.assertRaises(ParseError) as context: |
| 134 | + parse(packet) |
| 135 | + |
| 136 | + self.assertIn("invalid maidenhead locator format", str(context.exception)) |
| 137 | + |
| 138 | + def test_invalid_locator_letters_out_of_range(self): |
| 139 | + """Test that locator with letters outside A-R range raises error""" |
| 140 | + packet = "TEST>APRS:[ZZ31]" |
| 141 | + |
| 142 | + with self.assertRaises(ParseError) as context: |
| 143 | + parse(packet) |
| 144 | + |
| 145 | + self.assertIn("invalid maidenhead locator format", str(context.exception)) |
| 146 | + |
| 147 | + def test_parse_maidenhead_locator_function_direct(self): |
| 148 | + """Test parse_maidenhead_locator function directly""" |
| 149 | + body = "IO91SX/-]Comment" |
| 150 | + remaining, result = parse_maidenhead_locator(body) |
| 151 | + |
| 152 | + self.assertEqual(remaining, '') |
| 153 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 154 | + self.assertEqual(result['locator'], 'IO91SX') |
| 155 | + self.assertEqual(result['locator_precision'], 6) |
| 156 | + self.assertEqual(result['symbol_table'], '/') |
| 157 | + self.assertEqual(result['symbol'], '-') |
| 158 | + self.assertEqual(result['comment'], 'Comment') |
| 159 | + |
| 160 | + def test_parse_maidenhead_locator_4_char_direct(self): |
| 161 | + """Test parse_maidenhead_locator function with 4-char locator""" |
| 162 | + body = "FN31]" |
| 163 | + remaining, result = parse_maidenhead_locator(body) |
| 164 | + |
| 165 | + self.assertEqual(remaining, '') |
| 166 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 167 | + self.assertEqual(result['locator'], 'FN31') |
| 168 | + self.assertEqual(result['locator_precision'], 4) |
| 169 | + |
| 170 | + def test_parse_maidenhead_locator_8_char_direct(self): |
| 171 | + """Test parse_maidenhead_locator function with 8-char locator""" |
| 172 | + body = "FN31pr45]" |
| 173 | + remaining, result = parse_maidenhead_locator(body) |
| 174 | + |
| 175 | + self.assertEqual(remaining, '') |
| 176 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 177 | + self.assertEqual(result['locator'], 'FN31PR45') |
| 178 | + self.assertEqual(result['locator_precision'], 8) |
| 179 | + |
| 180 | + def test_locator_with_symbol_only_no_comment(self): |
| 181 | + """Test locator with symbol but no comment""" |
| 182 | + packet = "TEST>APRS:[IO91SX/-]" |
| 183 | + result = parse(packet) |
| 184 | + |
| 185 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 186 | + self.assertEqual(result['locator'], 'IO91SX') |
| 187 | + self.assertEqual(result['symbol_table'], '/') |
| 188 | + self.assertEqual(result['symbol'], '-') |
| 189 | + self.assertNotIn('comment', result) |
| 190 | + |
| 191 | + def test_locator_with_trailing_bracket_in_comment(self): |
| 192 | + """Test locator where closing bracket appears in comment""" |
| 193 | + packet = "TEST>APRS:[IO91SX/- Test]" |
| 194 | + result = parse(packet) |
| 195 | + |
| 196 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 197 | + self.assertEqual(result['locator'], 'IO91SX') |
| 198 | + self.assertEqual(result['symbol_table'], '/') |
| 199 | + self.assertEqual(result['symbol'], '-') |
| 200 | + self.assertEqual(result['comment'], 'Test') |
| 201 | + |
| 202 | + def test_real_world_winlink_packet(self): |
| 203 | + """Test real-world packet from error log: WinLink RMS Packet Node""" |
| 204 | + packet = "J73GPG-10>WL2K,STSHD,WIDE2*,qAR,J73Z-10:[FK95HI] J73GPG-10 WinLink RMS Packet Node" |
| 205 | + result = parse(packet) |
| 206 | + |
| 207 | + self.assertEqual(result['format'], 'maidenhead-locator') |
| 208 | + self.assertEqual(result['locator'], 'FK95HI') |
| 209 | + self.assertEqual(result['locator_precision'], 6) |
| 210 | + self.assertEqual(result['comment'], 'J73GPG-10 WinLink RMS Packet Node') |
| 211 | + # Verify header parsing |
| 212 | + self.assertEqual(result['from'], 'J73GPG-10') |
| 213 | + self.assertEqual(result['to'], 'WL2K') |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == '__main__': |
| 217 | + unittest.main() |
0 commit comments