1 | import json |
||
2 | import unittest |
||
3 | from unittest.case import SkipTest |
||
4 | |||
5 | from coalib.output.JSONEncoder import create_json_encoder |
||
6 | from coalib.results.Diff import ConflictError, Diff, SourceRange |
||
7 | |||
8 | |||
9 | class DiffTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
10 | |||
11 | def setUp(self): |
||
12 | self.file = ["1", "2", "3", "4"] |
||
13 | self.uut = Diff(self.file) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
14 | |||
15 | def test_add_lines(self): |
||
16 | self.uut.add_lines(0, []) |
||
17 | self.uut.add_lines(0, ["t"]) |
||
18 | self.uut.add_lines(0, []) |
||
19 | |||
20 | # No double addition allowed |
||
21 | self.assertRaises(ConflictError, self.uut.add_lines, 0, ["t"]) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
22 | self.assertRaises(ValueError, self.uut.add_lines, -1, ["t"]) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
23 | self.assertRaises(TypeError, self.uut.add_lines, "str", ["t"]) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
24 | |||
25 | def test_delete_line(self): |
||
26 | self.uut.delete_line(1) |
||
27 | self.uut.delete_line(1) # Double deletion possible without conflict |
||
28 | self.assertRaises(ValueError, self.uut.delete_line, 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
29 | |||
30 | def test_change_line(self): |
||
31 | self.assertEqual(len(self.uut), 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
32 | self.uut.change_line(2, "1", "2") |
||
33 | self.assertEqual(len(self.uut), 1) |
||
34 | self.assertRaises(ConflictError, self.uut.change_line, 2, "1", "3") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
35 | self.assertRaises(ValueError, self.uut.change_line, 0, "1", "2") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
36 | |||
37 | self.uut.delete_line(1) |
||
38 | # Line was deleted, unchangeable |
||
39 | self.assertRaises(ConflictError, self.uut.change_line, 1, "1", "2") |
||
40 | |||
41 | def test_affected_code(self): |
||
42 | self.assertEqual(self.uut.affected_code("file"), []) |
||
43 | |||
44 | self.uut.add_lines(0, ["test"]) |
||
45 | affected_code = [ |
||
46 | SourceRange.from_values("file", start_line=1)] |
||
47 | self.assertEqual(self.uut.affected_code("file"), affected_code) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
48 | |||
49 | self.uut.delete_line(2) |
||
50 | affected_code = [ |
||
51 | SourceRange.from_values("file", start_line=1), |
||
52 | SourceRange.from_values("file", start_line=2)] |
||
53 | self.assertEqual(self.uut.affected_code("file"), affected_code) |
||
54 | |||
55 | self.uut.delete_line(3) |
||
56 | affected_code = [ |
||
57 | SourceRange.from_values("file", start_line=1), |
||
58 | SourceRange.from_values("file", start_line=2, end_line=3)] |
||
59 | self.assertEqual(self.uut.affected_code("file"), affected_code) |
||
60 | |||
61 | self.uut.delete_line(6) |
||
62 | affected_code = [ |
||
63 | SourceRange.from_values("file", start_line=1), |
||
64 | SourceRange.from_values("file", start_line=2, end_line=3), |
||
65 | SourceRange.from_values('file', start_line=6)] |
||
66 | self.assertEqual(self.uut.affected_code("file"), affected_code) |
||
67 | |||
68 | def test_modified(self): |
||
69 | result_file = ["0.1", |
||
70 | "0.2", |
||
71 | "1", |
||
72 | "1.1", |
||
73 | "3.changed", |
||
74 | "4"] |
||
75 | |||
76 | self.uut.delete_line(2) |
||
77 | self.uut.add_lines(0, ["0.1", "0.2"]) |
||
78 | self.uut.add_lines(1, ["1.1"]) |
||
79 | self.uut.change_line(3, "3", "3.changed") |
||
80 | self.assertEqual(self.uut.modified, result_file) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
81 | self.assertEqual(self.uut.original, self.file) |
||
82 | |||
83 | self.uut.delete_line(len(self.file)) |
||
84 | del result_file[len(result_file) - 1] |
||
85 | self.assertEqual(self.uut.modified, result_file) |
||
86 | |||
87 | self.uut.delete_line(1) |
||
88 | del result_file[2] |
||
89 | self.assertEqual(self.uut.modified, result_file) |
||
90 | |||
91 | def test_addition(self): |
||
92 | self.assertRaises(TypeError, self.uut.__add__, 5) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
93 | |||
94 | result_file = ["1", |
||
95 | "2", |
||
96 | "2"] |
||
97 | |||
98 | other = Diff(self.file) |
||
99 | other.delete_line(1) |
||
100 | other.change_line(2, "1", "2") |
||
101 | other.add_lines(0, ["1"]) |
||
102 | |||
103 | self.uut.delete_line(1) |
||
104 | self.uut.delete_line(3) |
||
105 | self.uut.change_line(4, "4", "2") |
||
106 | result = self.uut + other |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
107 | |||
108 | self.assertEqual(result.modified, result_file) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
109 | # Make sure it didn't happen in place! |
||
110 | self.assertNotEqual(self.uut.modified, result_file) |
||
111 | |||
112 | def test_from_string_arrays(self): |
||
113 | a = ["q", "a", "b", "x", "c", "d"] |
||
114 | b = ["a", "b", "y", "c", "d", "f"] |
||
115 | self.uut = Diff.from_string_arrays(a, b) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
116 | self.assertEqual(self.uut.modified, b) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
117 | |||
118 | a = ["first", "fourth"] |
||
119 | b = ["first", "second", "third", "fourth"] |
||
120 | self.uut = Diff.from_string_arrays(a, b) |
||
121 | self.assertEqual(self.uut.modified, b) |
||
122 | |||
123 | a = ["first", "fourth"] |
||
124 | b = ["first_changed", "second", "third", "fourth"] |
||
125 | self.uut = Diff.from_string_arrays(a, b) |
||
126 | self.assertEqual(self.uut.modified, b) |
||
127 | |||
128 | a = ["first", "second", "third", "fourth"] |
||
129 | b = ["first", "fourth"] |
||
130 | self.uut = Diff.from_string_arrays(a, b) |
||
131 | self.assertEqual(self.uut.modified, b) |
||
132 | |||
133 | a = ["first", "second", "third", "fourth"] |
||
134 | b = ["first_changed", "second_changed", "fourth"] |
||
135 | self.uut = Diff.from_string_arrays(a, b) |
||
136 | self.assertEqual(self.uut.modified, b) |
||
137 | |||
138 | def test_from_clang_fixit(self): |
||
139 | try: |
||
140 | from clang.cindex import Index, LibclangError |
||
141 | except ImportError as err: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
142 | raise SkipTest(str(err)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
143 | |||
144 | joined_file = 'struct { int f0; }\nx = { f0 :1 };\n' |
||
145 | file = joined_file.splitlines(True) |
||
146 | fixed_file = ['struct { int f0; }\n', 'x = { .f0 = 1 };\n'] |
||
147 | try: |
||
148 | tu = Index.create().parse('t.c', unsaved_files=[ |
||
149 | ('t.c', joined_file)]) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
150 | except LibclangError as err: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
151 | raise SkipTest(str(err)) |
||
152 | |||
153 | fixit = tu.diagnostics[0].fixits[0] |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
154 | clang_fixed_file = Diff.from_clang_fixit(fixit, file).modified |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
155 | self.assertEqual(fixed_file, clang_fixed_file) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
156 | |||
157 | def test_equality(self): |
||
158 | a = ["first", "second", "third"] |
||
159 | b = ["first", "third"] |
||
160 | diff_1 = Diff.from_string_arrays(a, b) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
161 | |||
162 | a[1] = "else" |
||
163 | diff_2 = Diff.from_string_arrays(a, b) |
||
164 | self.assertEqual(diff_1, diff_2) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
165 | |||
166 | diff_1.add_lines(1, ["1"]) |
||
167 | self.assertNotEqual(diff_1, diff_2) |
||
168 | |||
169 | def test_json_export(self): |
||
170 | JSONEncoder = create_json_encoder() |
||
171 | a = ["first\n", "second\n", "third\n"] |
||
172 | b = ["first\n", "third\n"] |
||
173 | diff = Diff.from_string_arrays(a, b) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
174 | self.assertEqual( |
||
175 | json.dumps(diff, cls=JSONEncoder, sort_keys=True), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
176 | '"--- \\n' |
||
177 | '+++ \\n' |
||
178 | '@@ -1,3 +1,2 @@\\n' |
||
179 | ' first\\n' |
||
180 | '-second\\n' |
||
181 | ' third\\n"') |
||
182 |