File: test_parse.py

package info (click to toggle)
python-aprslib 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: python: 2,973; makefile: 216
file content (128 lines) | stat: -rw-r--r-- 3,494 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# encoding: utf-8

import sys
import unittest
from mox3 import mox

from aprslib import parse
from aprslib import parsing
from aprslib.exceptions import ParseError, UnknownFormat


def _u(text, c='utf8'):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.decode(c)


class ParseTestCase(unittest.TestCase):
    def test_unicode(self):
        _unicode = str if sys.version_info[0] >= 3 else unicode

        # 7bit ascii
        result = parse("A>B:>status")

        self.assertIsInstance(result['status'], _unicode)
        self.assertEqual(result['status'], _u("status"))

        # string with degree sign
        result = parse("A>B:>status\xb0")

        self.assertIsInstance(result['status'], _unicode)
        self.assertEqual(result['status'], _u("status\xb0", 'latin-1'))

        # str with utf8
        result = parse("A>B:>статус")

        self.assertIsInstance(result['status'], _unicode)
        self.assertEqual(result['status'], _u("статус"))

        # unicode input
        result = parse(_u("A>B:>статус"))

        self.assertIsInstance(result['status'], _unicode)
        self.assertEqual(result['status'], _u("статус"))

    def test_empty_packet(self):
        self.assertRaises(ParseError, parse, "")

    def test_no_body(self):
        self.assertRaises(ParseError, parse, "A>B")

    def test_empty_body(self):
        self.assertRaises(ParseError, parse, "A>B:")

    def testparse_header_exception(self):
        self.assertRaises(ParseError, parse, "A:asd")

    def test_empty_body_of_format_that_is_not_status(self):
        self.assertRaises(ParseError, parse, "A>B:!")

        try:
            parse("A>B:>")
        except:
            self.fail("empty status packet shouldn't raise exception")

    def test_unsupported_formats_raising(self):
        for packet_type in parsing.unsupported_formats:
            with self.assertRaises(UnknownFormat):
                packet = "A>B:%saaa" % packet_type

                try:
                    parse(packet)
                except UnknownFormat as exp:
                    self.assertEqual(exp.packet, packet)
                    raise


class ParseBranchesTestCase(unittest.TestCase):
    def setUp(self):
        self.m = mox.Mox()

    def tearDown(self):
        self.m.UnsetStubs()

    def test_status_format_branch(self):
        def _u(text, c='utf8'):
            if sys.version_info[0] >= 3:
                return text
            else:
                return text.decode(c)

        expected = {
            'status': 'test',
            'raw': _u('A>B:>test'),
            'via': '',
            'from': _u('A'),
            'to': _u('B'),
            'path': [],
            'format': 'status'
        }
        result = parse("A>B:>test")

        self.assertEqual(result, expected)

    def test_mice_format_branch(self):
        self.m.StubOutWithMock(parsing, "parse_mice")
        parsing.parse_mice("B", "test").AndReturn(('', {'format': ''}))
        parsing.parse_mice("D", "test").AndReturn(('', {'format': ''}))
        self.m.ReplayAll()

        parse("A>B:`test")
        parse("C>D:'test")

        self.m.VerifyAll()

    def test_message_format_branch(self):
        self.m.StubOutWithMock(parsing, "parse_message")
        parsing.parse_message("test").AndReturn(('', {'format': ''}))
        self.m.ReplayAll()

        parse("A>B::test")

        self.m.VerifyAll()


if __name__ == '__main__':
    unittest.main()