Completed
Pull Request — master (#2077)
by Lasse
02:11
created

DiffTest.test_addition_rename()   A

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
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
The variable unittest does not seem to be defined.
Loading history...
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
The variable self does not seem to be defined.
Loading history...
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
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable ConflictError does not seem to be defined.
Loading history...
22
        self.assertRaises(ValueError, self.uut.add_lines, -1, ["t"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
23
        self.assertRaises(TypeError, self.uut.add_lines, "str", ["t"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
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
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
29
30
    def test_change_line(self):
31
        self.assertEqual(len(self.uut), 0)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
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
The variable ConflictError does not seem to be defined.
Loading history...
35
        self.assertRaises(ValueError, self.uut.change_line, 0, "1", "2")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
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
The variable affected_code does not seem to be defined.
Loading history...
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
81
        # If delete is True then modified returns an empty list
82
        self.uut.delete = True
83
        self.assertEqual(self.uut.modified, [])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
84
        self.uut.delete = False
85
86
        self.assertEqual(self.uut.modified, result_file)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable result_file does not seem to be defined.
Loading history...
87
        self.assertEqual(self.uut.original, self.file)
88
89
        self.uut.delete_line(len(self.file))
90
        del result_file[len(result_file) - 1]
91
        self.assertEqual(self.uut.modified, result_file)
92
93
        self.uut.delete_line(1)
94
        del result_file[2]
95
        self.assertEqual(self.uut.modified, result_file)
96
97
    def test_addition(self):
98
        self.assertRaises(TypeError, self.uut.__add__, 5)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
99
100
        result_file = ["1",
101
                       "2",
102
                       "2"]
103
104
        other = Diff(self.file)
105
        other.delete_line(1)
106
        other.change_line(2, "1", "2")
107
        other.add_lines(0, ["1"])
108
109
        self.uut.delete_line(1)
110
        self.uut.delete_line(3)
111
        self.uut.change_line(4, "4", "2")
112
        result = self.uut + other
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable other does not seem to be defined.
Loading history...
113
114
        self.assertEqual(result.modified, result_file)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable result_file does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable result does not seem to be defined.
Loading history...
115
        # Make sure it didn't happen in place!
116
        self.assertNotEqual(self.uut.modified, result_file)
117
118
    def test_addition_rename(self):
119
        uut = Diff(self.file, rename=False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
120
        other = Diff(self.file, rename=False)
121
        self.assertEqual((other + uut).rename, False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable other does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
122
123
        other.rename = "some.py"
124
        self.assertEqual((other + uut).rename, "some.py")
125
126
        uut.rename = "some.py"
127
        self.assertEqual((other + uut).rename, "some.py")
128
129
        uut.rename = "other.py"
130
        self.assertRaises(ConflictError, other.__add__, uut)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ConflictError does not seem to be defined.
Loading history...
131
132
    def test_from_string_arrays(self):
133
        a = ["q", "a", "b", "x", "c", "d"]
134
        b = ["a", "b", "y", "c", "d", "f"]
135
        self.uut = Diff.from_string_arrays(a, b)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable a does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable b does not seem to be defined.
Loading history...
136
        self.assertEqual(self.uut.modified, b)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
137
138
        a = ["first", "fourth"]
139
        b = ["first", "second", "third", "fourth"]
140
        self.uut = Diff.from_string_arrays(a, b)
141
        self.assertEqual(self.uut.modified, b)
142
143
        a = ["first", "fourth"]
144
        b = ["first_changed", "second", "third", "fourth"]
145
        self.uut = Diff.from_string_arrays(a, b)
146
        self.assertEqual(self.uut.modified, b)
147
148
        a = ["first", "second", "third", "fourth"]
149
        b = ["first", "fourth"]
150
        self.uut = Diff.from_string_arrays(a, b)
151
        self.assertEqual(self.uut.modified, b)
152
153
        a = ["first", "second", "third", "fourth"]
154
        b = ["first_changed", "second_changed", "fourth"]
155
        self.uut = Diff.from_string_arrays(a, b)
156
        self.assertEqual(self.uut.modified, b)
157
158
    def test_from_clang_fixit(self):
159
        try:
160
            from clang.cindex import Index, LibclangError
161
        except ImportError as err:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ImportError does not seem to be defined.
Loading history...
162
            raise SkipTest(str(err))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable err does not seem to be defined.
Loading history...
163
164
        joined_file = 'struct { int f0; }\nx = { f0 :1 };\n'
165
        file = joined_file.splitlines(True)
166
        fixed_file = ['struct { int f0; }\n', 'x = { .f0 = 1 };\n']
167
        try:
168
            tu = Index.create().parse('t.c', unsaved_files=[
169
                ('t.c', joined_file)])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable joined_file does not seem to be defined.
Loading history...
170
        except LibclangError as err:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable LibclangError does not seem to be defined.
Loading history...
171
            raise SkipTest(str(err))
172
173
        fixit = tu.diagnostics[0].fixits[0]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tu does not seem to be defined.
Loading history...
174
        clang_fixed_file = Diff.from_clang_fixit(fixit, file).modified
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable fixit does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
175
        self.assertEqual(fixed_file, clang_fixed_file)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable clang_fixed_file does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable fixed_file does not seem to be defined.
Loading history...
176
177
    def test_equality(self):
178
        a = ["first", "second", "third"]
179
        b = ["first", "third"]
180
        diff_1 = Diff.from_string_arrays(a, b)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable b does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable a does not seem to be defined.
Loading history...
181
182
        a[1] = "else"
183
        diff_2 = Diff.from_string_arrays(a, b)
184
        self.assertEqual(diff_1, diff_2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable diff_2 does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable diff_1 does not seem to be defined.
Loading history...
185
186
        diff_1.rename = "abcd"
187
        self.assertNotEqual(diff_1, diff_2)
188
        diff_1.rename = False
189
190
        diff_1.delete = True
191
        self.assertNotEqual(diff_1, diff_2)
192
        diff_1.delete = False
193
194
        diff_1.add_lines(1, ["1"])
195
        self.assertNotEqual(diff_1, diff_2)
196
197
    def test_json_export(self):
198
        JSONEncoder = create_json_encoder()
199
        a = ["first\n", "second\n", "third\n"]
200
        b = ["first\n", "third\n"]
201
        diff = Diff.from_string_arrays(a, b)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable a does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable b does not seem to be defined.
Loading history...
202
        self.assertEqual(
203
            json.dumps(diff, cls=JSONEncoder, sort_keys=True),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable diff does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable JSONEncoder does not seem to be defined.
Loading history...
204
            '"--- \\n'
205
            '+++ \\n'
206
            '@@ -1,3 +1,2 @@\\n'
207
            ' first\\n'
208
            '-second\\n'
209
            ' third\\n"')
210
211
    def test_rename(self):
212
        self.uut.rename = False
213
        self.uut.rename = "1234"
214
        with self.assertRaises(TypeError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
215
            self.uut.rename = True
216
        with self.assertRaises(TypeError):
217
            self.uut.rename = 1234
218
219
    def test_delete(self):
220
        self.uut.delete = True
221
        self.uut.delete = False
222
        # Double deletion is allowed
223
        self.uut.delete = False
224
        with self.assertRaises(TypeError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
225
            self.uut.delete = "abcd"
226