1
|
|
|
import unittest |
2
|
|
|
import json |
3
|
|
|
from os.path import abspath |
4
|
|
|
|
5
|
|
|
from coalib.results.Diff import Diff |
6
|
|
|
from coalib.results.Result import RESULT_SEVERITY, Result |
7
|
|
|
from coalib.results.SourceRange import SourceRange |
8
|
|
|
from coalib.output.JSONEncoder import create_json_encoder |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class ResultTest(unittest.TestCase): |
12
|
|
|
|
13
|
|
|
def test_origin(self): |
14
|
|
|
uut = Result("origin", "msg") |
15
|
|
|
self.assertEqual(uut.origin, "origin") |
16
|
|
|
|
17
|
|
|
uut = Result(self, "msg") |
18
|
|
|
self.assertEqual(uut.origin, "ResultTest") |
19
|
|
|
|
20
|
|
|
uut = Result(None, "msg") |
21
|
|
|
self.assertEqual(uut.origin, "") |
22
|
|
|
|
23
|
|
|
def test_invalid_severity(self): |
24
|
|
|
with self.assertRaises(ValueError): |
25
|
|
|
Result("o", "m", severity=-5) |
26
|
|
|
|
27
|
|
|
def test_invalid_confidence(self): |
28
|
|
|
with self.assertRaises(ValueError): |
29
|
|
|
Result("o", "m", confidence=-1) |
30
|
|
|
with self.assertRaises(ValueError): |
31
|
|
|
Result("o", "m", confidence=101) |
32
|
|
|
|
33
|
|
|
def test_string_dict(self): |
34
|
|
|
uut = Result(None, "") |
35
|
|
|
output = uut.to_string_dict() |
36
|
|
|
self.assertEqual(output, {"id": str(uut.id), |
37
|
|
|
"origin": "", |
38
|
|
|
"message": "", |
39
|
|
|
"file": "", |
40
|
|
|
"line_nr": "", |
41
|
|
|
"severity": "NORMAL", |
42
|
|
|
"debug_msg": "", |
43
|
|
|
"additional_info": "", |
44
|
|
|
"confidence": "100"}) |
45
|
|
|
|
46
|
|
|
uut = Result.from_values(origin="origin", |
47
|
|
|
message="msg", |
48
|
|
|
file="file", |
49
|
|
|
line=2, |
50
|
|
|
severity=RESULT_SEVERITY.INFO, |
51
|
|
|
additional_info="hi!", |
52
|
|
|
debug_msg="dbg", |
53
|
|
|
confidence=50) |
54
|
|
|
output = uut.to_string_dict() |
55
|
|
|
self.assertEqual(output, {"id": str(uut.id), |
56
|
|
|
"origin": "origin", |
57
|
|
|
"message": "msg", |
58
|
|
|
"file": abspath("file"), |
59
|
|
|
"line_nr": "2", |
60
|
|
|
"severity": "INFO", |
61
|
|
|
"debug_msg": "dbg", |
62
|
|
|
"additional_info": "hi!", |
63
|
|
|
"confidence": "50"}) |
64
|
|
|
|
65
|
|
|
uut = Result.from_values(origin="o", message="m", file="f", line=5) |
66
|
|
|
output = uut.to_string_dict() |
67
|
|
|
self.assertEqual(output["line_nr"], "5") |
68
|
|
|
|
69
|
|
|
def test_apply(self): |
70
|
|
|
file_dict = { |
71
|
|
|
"f_a": ["1", "2", "3"], |
72
|
|
|
"f_b": ["1", "2", "3"] |
73
|
|
|
} |
74
|
|
|
expected_file_dict = { |
75
|
|
|
"f_a": ["1", "3_changed"], |
76
|
|
|
"f_b": ["1", "2", "3"] |
77
|
|
|
} |
78
|
|
|
diff = Diff(file_dict['f_a']) |
79
|
|
|
diff.delete_line(2) |
80
|
|
|
diff.change_line(3, "3", "3_changed") |
81
|
|
|
|
82
|
|
|
uut = Result("origin", "msg", diffs={"f_a": diff}) |
83
|
|
|
uut.apply(file_dict) |
84
|
|
|
|
85
|
|
|
self.assertEqual(file_dict, expected_file_dict) |
86
|
|
|
|
87
|
|
|
def test_add(self): |
88
|
|
|
file_dict = { |
89
|
|
|
"f_a": ["1", "2", "3"], |
90
|
|
|
"f_b": ["1", "2", "3"], |
91
|
|
|
"f_c": ["1", "2", "3"] |
92
|
|
|
} |
93
|
|
|
expected_file_dict = { |
94
|
|
|
"f_a": ["1", "3_changed"], |
95
|
|
|
"f_b": ["1", "2", "3_changed"], |
96
|
|
|
"f_c": ["1", "2", "3"] |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
diff = Diff(file_dict['f_a']) |
100
|
|
|
diff.delete_line(2) |
101
|
|
|
uut1 = Result("origin", "msg", diffs={"f_a": diff}) |
102
|
|
|
|
103
|
|
|
diff = Diff(file_dict['f_a']) |
104
|
|
|
diff.change_line(3, "3", "3_changed") |
105
|
|
|
uut2 = Result("origin", "msg", diffs={"f_a": diff}) |
106
|
|
|
|
107
|
|
|
diff = Diff(file_dict['f_b']) |
108
|
|
|
diff.change_line(3, "3", "3_changed") |
109
|
|
|
uut3 = Result("origin", "msg", diffs={"f_b": diff}) |
110
|
|
|
|
111
|
|
|
uut1 += uut2 + uut3 |
112
|
|
|
uut1.apply(file_dict) |
113
|
|
|
|
114
|
|
|
self.assertEqual(file_dict, expected_file_dict) |
115
|
|
|
|
116
|
|
|
def test_overlaps(self): |
117
|
|
|
overlapping_range = SourceRange.from_values("file1", 1, 1, 2, 2) |
118
|
|
|
nonoverlapping_range = SourceRange.from_values("file2", 1, 1, 2, 2) |
119
|
|
|
uut = Result.from_values("origin", |
120
|
|
|
"message", |
121
|
|
|
file="file1", |
122
|
|
|
line=1, |
123
|
|
|
column=1, |
124
|
|
|
end_line=2, |
125
|
|
|
end_column=2) |
126
|
|
|
self.assertTrue(uut.overlaps(overlapping_range)) |
127
|
|
|
self.assertTrue(uut.overlaps([overlapping_range])) |
128
|
|
|
self.assertFalse(uut.overlaps(nonoverlapping_range)) |
129
|
|
|
|
130
|
|
|
def test_location_repr(self): |
131
|
|
|
result_a = Result(origin="o", message="m") |
132
|
|
|
self.assertEqual(result_a.location_repr(), "the whole project") |
133
|
|
|
|
134
|
|
|
result_b = Result.from_values("o", "m", file="e") |
135
|
|
|
self.assertEqual(result_b.location_repr(), "'e'") |
136
|
|
|
|
137
|
|
|
affected_code = (SourceRange.from_values('f'), |
138
|
|
|
SourceRange.from_values('g')) |
139
|
|
|
result_c = Result("o", "m", affected_code=affected_code) |
140
|
|
|
self.assertEqual(result_c.location_repr(), "'f', 'g'") |
141
|
|
|
|
142
|
|
|
affected_code = (SourceRange.from_values('f'), |
143
|
|
|
SourceRange.from_values('f')) |
144
|
|
|
result_d = Result("o", "m", affected_code=affected_code) |
145
|
|
|
self.assertEqual(result_d.location_repr(), "'f'") |
146
|
|
|
|
147
|
|
|
def test_json_diff(self): |
148
|
|
|
file_dict = { |
149
|
|
|
"f_a": ["1", "2", "3"], |
150
|
|
|
"f_b": ["1", "2", "3"] |
151
|
|
|
} |
152
|
|
|
expected_file = { |
153
|
|
|
"f_a": ["1", "3_changed"], |
154
|
|
|
"f_b": ["1", "2", "3"] |
155
|
|
|
} |
156
|
|
|
diff = Diff(file_dict['f_a']) |
157
|
|
|
diff.delete_line(2) |
158
|
|
|
diff.change_line(3, "3", "3_changed") |
159
|
|
|
uut = Result("origin", "msg", diffs={"f_a": diff}).__json__(True) |
160
|
|
|
self.assertEqual(uut["diffs"]['f_a'].__json__(), "--- \n" |
161
|
|
|
"+++ \n" |
162
|
|
|
"@@ -1,3 +1,2 @@\n" |
163
|
|
|
" 1-2-3+3_changed") |
164
|
|
|
JSONEncoder = create_json_encoder(use_relpath=True) |
165
|
|
|
json_dump = json.dumps(diff, cls=JSONEncoder, sort_keys=True) |
166
|
|
|
self.assertEqual( |
167
|
|
|
json_dump, '"--- \\n+++ \\n@@ -1,3 +1,2 @@\\n 1-2-3+3_changed"') |
168
|
|
|
|