Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:11
created

tests/results/SourceRangeTest.py (31 issues)

1
import unittest
2
from collections import namedtuple
3
4
from coalib.results.SourcePosition import SourcePosition
5
from coalib.results.SourceRange import SourceRange
6
from coalib.results.AbsolutePosition import AbsolutePosition
7
8
9
class SourceRangeTest(unittest.TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
10
11
    def setUp(self):
12
        self.result_fileA_noline = SourcePosition("A")
13
        self.result_fileA_line2 = SourcePosition("A", 2)
14
        self.result_fileB_noline = SourcePosition("B")
15
        self.result_fileB_line2 = SourcePosition("B", 2)
16
        self.result_fileB_line4 = SourcePosition("B", 4)
17
18
    def test_construction(self):
19
        uut1 = SourceRange(self.result_fileA_noline)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
20
        self.assertEqual(uut1.end, self.result_fileA_noline)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut1 does not seem to be defined.
Loading history...
21
22
        uut2 = SourceRange.from_values("A")
23
        self.assertEqual(uut1, uut2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut2 does not seem to be defined.
Loading history...
24
25
        uut = SourceRange.from_values("B", start_line=2, end_line=4)
26
        self.assertEqual(uut.start, self.result_fileB_line2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
27
        self.assertEqual(uut.end, self.result_fileB_line4)
28
29
    def test_from_clang_range(self):
30
        # Simulating a clang SourceRange is easier than setting one up without
31
        # actually parsing a complete C file.
32
        ClangRange = namedtuple("ClangRange", "start end")
33
        ClangPosition = namedtuple("ClangPosition", "file line column")
34
        ClangFile = namedtuple("ClangFile", "name")
35
        file = ClangFile("t.c")
36
        start = ClangPosition(file, 1, 2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
37
        end = ClangPosition(file, 3, 4)
38
39
        uut = SourceRange.from_clang_range(ClangRange(start, end))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable end does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable start does not seem to be defined.
Loading history...
40
        compare = SourceRange.from_values("t.c", 1, 2, 3, 4)
41
        self.assertEqual(uut, compare)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable compare does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
42
43
    def test_from_absolute_position(self):
44
        text = ("a\n", "b\n")
45
        start = AbsolutePosition(text, 0)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable text does not seem to be defined.
Loading history...
46
        end = AbsolutePosition(text, 2)
47
48
        uut = SourceRange.from_absolute_position("F", start, end)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable start does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable end does not seem to be defined.
Loading history...
49
        compare = SourceRange.from_values("F", 1, 1, 2, 1)
50
        self.assertEqual(uut, compare)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable compare does not seem to be defined.
Loading history...
51
52
        uut = SourceRange.from_absolute_position("F", start, None)
53
        compare = SourceRange(SourcePosition("F", 1, 1), None)
54
        self.assertEqual(uut, compare)
55
56
    def test_file_property(self):
57
        uut = SourceRange(self.result_fileA_line2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
58
        self.assertRegex(uut.file, ".*A")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
59
60
    def test_invalid_arguments(self):
61
        # arguments must be SourceRanges
62
        with self.assertRaises(TypeError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
63
            SourceRange(1, self.result_fileA_noline)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
64
65
        with self.assertRaises(TypeError):
66
            SourceRange(self.result_fileA_line2, 1)
67
68
    def test_argument_file(self):
69
        # both Source_Positions should describe the same file
70
        with self.assertRaises(ValueError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
71
            SourceRange(self.result_fileA_noline, self.result_fileB_noline)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
72
73
    def test_argument_order(self):
74
        # end should come after the start
75
        with self.assertRaises(ValueError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
76
            SourceRange(self.result_fileA_line2, self.result_fileA_noline)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
77
78
    def test_invalid_comparison(self):
79
        with self.assertRaises(TypeError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
80
            SourceRange(self.result_fileB_noline, self.result_fileB_line2) < 1
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
81
82
    def test_json(self):
83
        uut = SourceRange.from_values("B", start_line=2,
84
                                      end_line=4).__json__(use_relpath=True)
85
        self.assertEqual(uut['start'], self.result_fileB_line2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
86
87
88
class SourceRangeExpandTest(unittest.TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
89
90
    def test_expand(self):
91
        empty_position = SourcePosition("filename")
92
        file = ["abc\n", "def\n", "ghi\n"]
93
        empty_range = SourceRange(empty_position, empty_position)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable empty_position does not seem to be defined.
Loading history...
94
        full_range = SourceRange.from_values("filename", 1, 1, 3, 4)
95
        self.assertEqual(empty_range.expand(file), full_range)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable full_range does not seem to be defined.
Loading history...
96