Completed
Pull Request — master (#2655)
by
unknown
01:49
created

ResultTest.test_add()   B

Complexity

Conditions 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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