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
![]() |
|||
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
|
|||
20 | self.assertEqual(uut1.end, self.result_fileA_noline) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
21 | |||
22 | uut2 = SourceRange.from_values("A") |
||
23 | self.assertEqual(uut1, uut2) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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
|
|||
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
|
|||
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
Comprehensibility
Best Practice
introduced
by
|
|||
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
Comprehensibility
Best Practice
introduced
by
|
|||
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
|
|||
46 | end = AbsolutePosition(text, 2) |
||
47 | |||
48 | uut = SourceRange.from_absolute_position("F", start, end) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
49 | compare = SourceRange.from_values("F", 1, 1, 2, 1) |
||
50 | self.assertEqual(uut, compare) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
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
|
|||
58 | self.assertRegex(uut.file, ".*A") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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
|
|||
63 | SourceRange(1, self.result_fileA_noline) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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
|
|||
71 | SourceRange(self.result_fileA_noline, self.result_fileB_noline) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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
|
|||
76 | SourceRange(self.result_fileA_line2, self.result_fileA_noline) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
77 | |||
78 | def test_invalid_comparison(self): |
||
79 | with self.assertRaises(TypeError): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
80 | SourceRange(self.result_fileB_noline, self.result_fileB_line2) < 1 |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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
Comprehensibility
Best Practice
introduced
by
|
|||
86 | |||
87 | |||
88 | class SourceRangeExpandTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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
|
|||
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
Comprehensibility
Best Practice
introduced
by
|
|||
96 |