File: test_message.py

package info (click to toggle)
prospector 1.10.3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,440 kB
  • sloc: python: 4,200; makefile: 181
file content (91 lines) | stat: -rw-r--r-- 3,415 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
# flake8: noqa
from pathlib import Path
from unittest import TestCase

from prospector.message import Location


class LocationPathTest(TestCase):
    def test_paths(self):
        """
        Tests the absolute and relative path conversion
        """
        root = Path(__file__).parent.parent
        loc = Location(__file__, "module", "func", 1, 2)
        self.assertEqual(loc.relative_path(root), Path("tests/test_message.py"))
        absolute = root / "tests/test_message.py"
        self.assertEqual(loc.absolute_path(), absolute)

    def test_strings_or_paths(self):
        """
        For ease of use the Location object can accept a path as a Path or a string
        """
        path = "/tmp/path/module1.py"
        args = ["module1", "somefunc", 12, 2]
        self.assertEqual(Location("/tmp/path/module1.py", *args), Location(Path(path), *args))

    def test_bad_path_input(self):
        self.assertRaises(ValueError, Location, 3.2, "module", "func", 1, 2)
        self.assertRaises(ValueError, Location, None, "module", "func", 1, 2)


class LocationOrderTest(TestCase):
    def test_path_order(self):
        locs = [
            Location(Path("/tmp/path/module3.py"), "module3", "somefunc", 15, 0),
            Location(Path("/tmp/path/module1.py"), "module1", "somefunc", 10, 0),
            Location("/tmp/path/module2.py", "module2", "somefunc", 9, 0),
        ]

        paths = [loc.path for loc in locs]
        expected = sorted(paths)

        self.assertEqual(expected, [loc.path for loc in sorted(locs)])

    def test_line_order(self):
        locs = [
            Location("/tmp/path/module1.py", "module1", "somefunc", 15, 0),
            Location("/tmp/path/module1.py", "module1", "somefunc", 10, 0),
            Location("/tmp/path/module1.py", "module1", "somefunc", 12, 0),
        ]

        lines = [loc.line for loc in locs]
        expected = sorted(lines)

        self.assertEqual(expected, [loc.line for loc in sorted(locs)])

    def test_sort_between_none_lines(self):
        locs = [
            Location("/tmp/path/module1.py", "module1", "somefunc", 15, 0),
            Location("/tmp/path/module1.py", "module1", "somefunc", 10, 0),
            Location("/tmp/path/module1.py", "module1", "somefunc", -1, 0),
        ]

        lines = [(loc.line or -1) for loc in locs]
        expected = [None if l == -1 else l for l in sorted(lines)]

        self.assertEqual(expected, [loc.line for loc in sorted(locs)])

    def test_char_order(self):
        locs = [
            Location("/tmp/path/module1.py", "module1", "somefunc", 10, 7),
            Location("/tmp/path/module1.py", "module1", "somefunc", 10, 0),
            Location("/tmp/path/module1.py", "module1", "somefunc", 10, 2),
        ]

        chars = [loc.character for loc in locs]
        expected = sorted(chars)

        self.assertEqual(expected, [loc.character for loc in sorted(locs)])

    def test_sort_between_none_chars(self):
        locs = [
            Location("/tmp/path/module1.py", "module1", "somefunc", 10, -1),
            Location("/tmp/path/module1.py", "module1", "somefunc", 10, 1),
            Location("/tmp/path/module1.py", "module1", "somefunc", 10, 2),
        ]

        chars = [(loc.character or -1) for loc in locs]
        expected = [None if c == -1 else c for c in sorted(chars)]

        self.assertEqual(expected, [loc.character for loc in sorted(locs)])