Completed
Push — master ( 42cdfe...147541 )
by
unknown
02:00
created

SourceRangeTest.test_invalid_arguments()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
import unittest
2
from collections import namedtuple
3
from os.path import abspath
4
5
from coalib.results.SourcePosition import SourcePosition
6
from coalib.results.SourceRange import SourceRange
7
from coalib.results.AbsolutePosition import AbsolutePosition
8
from coalib.results.Diff import Diff
9
10
11
class SourceRangeTest(unittest.TestCase):
12
13
    def setUp(self):
14
        self.result_fileA_noline = SourcePosition("A")
15
        self.result_fileA_line2 = SourcePosition("A", 2)
16
        self.result_fileB_noline = SourcePosition("B")
17
        self.result_fileB_line2 = SourcePosition("B", 2)
18
        self.result_fileB_line4 = SourcePosition("B", 4)
19
20
    def test_construction(self):
21
        uut1 = SourceRange(self.result_fileA_noline)
22
        self.assertEqual(uut1.end, self.result_fileA_noline)
23
24
        uut2 = SourceRange.from_values("A")
25
        self.assertEqual(uut1, uut2)
26
27
        uut = SourceRange.from_values("B", start_line=2, end_line=4)
28
        self.assertEqual(uut.start, self.result_fileB_line2)
29
        self.assertEqual(uut.end, self.result_fileB_line4)
30
31
    def test_from_clang_range(self):
32
        # Simulating a clang SourceRange is easier than setting one up without
33
        # actually parsing a complete C file.
34
        ClangRange = namedtuple("ClangRange", "start end")
35
        ClangPosition = namedtuple("ClangPosition", "file line column")
36
        ClangFile = namedtuple("ClangFile", "name")
37
        file = ClangFile("t.c")
38
        start = ClangPosition(file, 1, 2)
39
        end = ClangPosition(file, 3, 4)
40
41
        uut = SourceRange.from_clang_range(ClangRange(start, end))
42
        compare = SourceRange.from_values("t.c", 1, 2, 3, 4)
43
        self.assertEqual(uut, compare)
44
45
    def test_from_absolute_position(self):
46
        text = ("a\n", "b\n")
47
        start = AbsolutePosition(text, 0)
48
        end = AbsolutePosition(text, 2)
49
50
        uut = SourceRange.from_absolute_position("F", start, end)
51
        compare = SourceRange.from_values("F", 1, 1, 2, 1)
52
        self.assertEqual(uut, compare)
53
54
        uut = SourceRange.from_absolute_position("F", start, None)
55
        compare = SourceRange(SourcePosition("F", 1, 1), None)
56
        self.assertEqual(uut, compare)
57
58
    def test_file_property(self):
59
        uut = SourceRange(self.result_fileA_line2)
60
        self.assertRegex(uut.file, ".*A")
61
62
    def test_invalid_arguments(self):
63
        # arguments must be SourceRanges
64
        with self.assertRaises(TypeError):
65
            SourceRange(1, self.result_fileA_noline)
66
67
        with self.assertRaises(TypeError):
68
            SourceRange(self.result_fileA_line2, 1)
69
70
    def test_argument_file(self):
71
        # both Source_Positions should describe the same file
72
        with self.assertRaises(ValueError):
73
            SourceRange(self.result_fileA_noline, self.result_fileB_noline)
74
75
    def test_argument_order(self):
76
        # end should come after the start
77
        with self.assertRaises(ValueError):
78
            SourceRange(self.result_fileA_line2, self.result_fileA_noline)
79
80
    def test_invalid_comparison(self):
81
        with self.assertRaises(TypeError):
82
            SourceRange(self.result_fileB_noline, self.result_fileB_line2) < 1
83
84
    def test_json(self):
85
        uut = SourceRange.from_values("B", start_line=2,
86
                                      end_line=4).__json__(use_relpath=True)
87
        self.assertEqual(uut['start'], self.result_fileB_line2)
88
89
    def test_contains(self):
90
        a = SourceRange.from_values("test_file", 1, 2, 1, 20)
91
        b = SourceRange.from_values("test_file", 1, 2, 1, 20)
92
        self.assertIn(a, b)
93
94
        a = SourceRange.from_values("test_file", 1, 2, 2, 20)
95
        b = SourceRange.from_values("test_file", 1, 1, 2, 20)
96
        self.assertIn(a, b)
97
98
        a = SourceRange.from_values("test_file", 1, 2, 1, 20)
99
        b = SourceRange.from_values("test_file2", 1, 2, 1, 20)
100
        self.assertNotIn(a, b)
101
102
        a = SourceRange.from_values("test_file", 2, 2, 64, 20)
103
        b = SourceRange.from_values("test_file", 1, 1, 50, 20)
104
        self.assertNotIn(a, b)
105
106
    def test_renamed_file(self):
107
        src_range = SourceRange(SourcePosition("test_file"))
108
        self.assertEqual(src_range.renamed_file({}), abspath('test_file'))
109
110
        self.assertEqual(
111
            src_range.renamed_file({abspath('test_file'): Diff([])}),
112
            abspath('test_file'))
113
114
        self.assertEqual(
115
            src_range.renamed_file(
116
                {abspath('test_file'): Diff([], rename='another_file')}),
117
            'another_file')
118
119
120
class SourceRangeExpandTest(unittest.TestCase):
121
122
    def test_expand(self):
123
        empty_position = SourcePosition("filename")
124
        file = ["abc\n", "def\n", "ghi\n"]
125
        empty_range = SourceRange(empty_position, empty_position)
126
        full_range = SourceRange.from_values("filename", 1, 1, 3, 4)
127
        self.assertEqual(empty_range.expand(file), full_range)
128