21
21
22
22
import org .assertj .core .api .Assertions ;
23
23
import org .junit .Test ;
24
+ import org .sonar .plugins .python .api .PythonCheck ;
24
25
import org .sonar .python .checks .quickfix .PythonQuickFixVerifier ;
25
26
import org .sonar .python .checks .utils .PythonCheckVerifier ;
26
27
27
28
import static org .assertj .core .api .Assertions .assertThat ;
28
29
29
30
public class BooleanCheckNotInvertedCheckTest {
30
31
32
+ private final PythonCheck check = new BooleanCheckNotInvertedCheck ();
33
+
31
34
@ Test
32
35
public void test () {
33
- PythonCheckVerifier .verify ("src/test/resources/checks/booleanCheckNotInverted.py" , new BooleanCheckNotInvertedCheck () );
36
+ PythonCheckVerifier .verify ("src/test/resources/checks/booleanCheckNotInverted.py" , check );
34
37
}
35
38
36
39
@ Test
@@ -54,30 +57,149 @@ public void operatorStringTest() {
54
57
public void test_quickfix () {
55
58
String codeWithIssue = "a = not(b == c)" ;
56
59
String codeFixed = "a = b != c" ;
57
- PythonQuickFixVerifier .verify (new BooleanCheckNotInvertedCheck (), codeWithIssue , codeFixed );
60
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
61
+
62
+ codeWithIssue = "a = not b != c" ;
63
+ codeFixed = "a = b == c" ;
64
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
58
65
59
66
codeWithIssue = "a = not (b != c)" ;
60
67
codeFixed = "a = b == c" ;
61
- PythonQuickFixVerifier .verify (new BooleanCheckNotInvertedCheck () , codeWithIssue , codeFixed );
68
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
62
69
63
70
codeWithIssue = "a = not (b < c)" ;
64
71
codeFixed = "a = b >= c" ;
65
- PythonQuickFixVerifier .verify (new BooleanCheckNotInvertedCheck () , codeWithIssue , codeFixed );
72
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
66
73
67
74
codeWithIssue = "a = not (b is c)" ;
68
75
codeFixed = "a = b is not c" ;
69
- PythonQuickFixVerifier .verify (new BooleanCheckNotInvertedCheck () , codeWithIssue , codeFixed );
76
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
70
77
71
78
codeWithIssue = "a = not (b is not c)" ;
72
79
codeFixed = "a = b is c" ;
73
- PythonQuickFixVerifier .verify (new BooleanCheckNotInvertedCheck () , codeWithIssue , codeFixed );
80
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
74
81
75
82
codeWithIssue = "a = not (b in c)" ;
76
83
codeFixed = "a = b not in c" ;
77
- PythonQuickFixVerifier .verify (new BooleanCheckNotInvertedCheck () , codeWithIssue , codeFixed );
84
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
78
85
79
86
codeWithIssue = "a = not (b not in c)" ;
80
87
codeFixed = "a = b in c" ;
81
- PythonQuickFixVerifier .verify (new BooleanCheckNotInvertedCheck (), codeWithIssue , codeFixed );
88
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
89
+ }
90
+
91
+ @ Test
92
+ public void not_in_if () {
93
+ String codeWithIssue = "" +
94
+ "def func():\n " +
95
+ " if not a == 2:\n " +
96
+ " b = 10\n " +
97
+ " return \" item1\" \" item2\" " ;
98
+ String codeFixed = "" +
99
+ "def func():\n " +
100
+ " if a != 2:\n " +
101
+ " b = 10\n " +
102
+ " return \" item1\" \" item2\" " ;
103
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
104
+
105
+ codeWithIssue = "" +
106
+ "def func():\n " +
107
+ " if not a == 2 and b == 9:\n " +
108
+ " b = 10\n " +
109
+ " return \" item1\" \" item2\" " ;
110
+ codeFixed = "" +
111
+ "def func():\n " +
112
+ " if a != 2 and b == 9:\n " +
113
+ " b = 10\n " +
114
+ " return \" item1\" \" item2\" " ;
115
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
116
+
117
+ codeWithIssue = "" +
118
+ "def func():\n " +
119
+ " if a != 2 and not b == 9:\n " +
120
+ " b = 10\n " +
121
+ " return \" item1\" \" item2\" " ;
122
+ codeFixed = "" +
123
+ "def func():\n " +
124
+ " if a != 2 and b != 9:\n " +
125
+ " b = 10\n " +
126
+ " return \" item1\" \" item2\" " ;
127
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
128
+ }
129
+
130
+ @ Test
131
+ public void not_parentheses () {
132
+ String codeWithIssue = "a = not ((((((b > c))))))" ;
133
+ String codeFixed = "a = b <= c" ;
134
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
135
+
136
+ codeWithIssue = "a = not (not (b == c))" ;
137
+ codeFixed = "a = not (b != c)" ;
138
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
139
+
140
+ codeWithIssue = "a = not (a is (not b))" ;
141
+ codeFixed = "a = a is not (not b)" ;
142
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
143
+
144
+ codeWithIssue = "a = not (1 == b == c) == 2" ;
145
+ codeFixed = "a = (1 == b == c) != 2" ;
146
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
147
+
148
+ codeWithIssue = "a = not (1 == b == c) == (d == 2)" ;
149
+ codeFixed = "a = (1 == b == c) != (d == 2)" ;
150
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
151
+
152
+ codeWithIssue = "a = not ((1 == b == c) and a) == (d == 2)" ;
153
+ codeFixed = "a = ((1 == b == c) and a) != (d == 2)" ;
154
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
155
+
156
+ codeWithIssue = "a = not (b < foo())" ;
157
+ codeFixed = "a = b >= foo()" ;
158
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
159
+ }
160
+
161
+ @ Test
162
+ public void expression_list () {
163
+ String codeWithIssue = "a = not [] < (c,d)" ;
164
+ String codeFixed = "a = [] >= (c, d)" ;
165
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
166
+
167
+ codeWithIssue = "a = not [a, (a,b)] is c" ;
168
+ codeFixed = "a = [a, (a, b)] is not c" ;
169
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
170
+
171
+ codeWithIssue = "a = not (foo((1)) < c)" ;
172
+ codeFixed = "a = foo((1)) >= c" ;
173
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
174
+
175
+ codeWithIssue = "a = not (foo(()) < c)" ;
176
+ codeFixed = "a = foo(()) >= c" ;
177
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
178
+
179
+ codeWithIssue = "a = not (() in c)" ;
180
+ codeFixed = "a = () not in c" ;
181
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
182
+
183
+ codeWithIssue = "a = not ((1,) in c)" ;
184
+ codeFixed = "a = (1,) not in c" ;
185
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
186
+
187
+ codeWithIssue = "a = not ((1,2,3,4) in c)" ;
188
+ codeFixed = "a = (1, 2, 3, 4) not in c" ;
189
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
190
+ }
191
+
192
+ @ Test
193
+ public void fstring () {
194
+ String codeWithIssue = "x = not (a == f'foo${b}')" ;
195
+ String codeFixed = "x = a != f'foo${b}'" ;
196
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
197
+
198
+ }
199
+ @ Test
200
+ public void brackets (){
201
+ String codeWithIssue = "x = not ( ham[1] in a)" ;
202
+ String codeFixed = "x = ham[1] not in a" ;
203
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
82
204
}
83
205
}
0 commit comments