Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:04
created

ResultTest.test_json_diff()   A

Complexity

Conditions 1

Size

Total Lines 21

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