19
19
*/
20
20
package org .sonar .python .checks .quickfix ;
21
21
22
- import com .sonar .sslr .api .AstNode ;
23
22
import java .net .URI ;
24
23
import java .util .ArrayList ;
25
24
import java .util .Arrays ;
26
25
import java .util .Collections ;
27
26
import java .util .Comparator ;
28
27
import java .util .List ;
28
+ import java .util .function .Function ;
29
29
import java .util .stream .Collectors ;
30
30
import java .util .stream .Stream ;
31
31
import org .sonar .plugins .python .api .PythonCheck ;
32
32
import org .sonar .plugins .python .api .PythonCheck .PreciseIssue ;
33
33
import org .sonar .plugins .python .api .PythonFile ;
34
34
import org .sonar .plugins .python .api .PythonSubscriptionCheck ;
35
35
import org .sonar .plugins .python .api .PythonVisitorContext ;
36
- import org .sonar .plugins .python .api .tree .FileInput ;
36
+ import org .sonar .plugins .python .api .quickfix .PythonQuickFix ;
37
+ import org .sonar .plugins .python .api .quickfix .PythonTextEdit ;
37
38
import org .sonar .python .SubscriptionVisitor ;
38
39
import org .sonar .python .caching .CacheContextImpl ;
39
40
import org .sonar .python .parser .PythonParser ;
40
- import org .sonar .plugins .python .api .quickfix .PythonQuickFix ;
41
- import org .sonar .plugins .python .api .quickfix .PythonTextEdit ;
42
41
import org .sonar .python .semantic .ProjectLevelSymbolTable ;
42
+ import org .sonar .python .tree .IPythonTreeMaker ;
43
43
import org .sonar .python .tree .PythonTreeMaker ;
44
44
45
45
import static org .assertj .core .api .Assertions .assertThat ;
@@ -49,8 +49,32 @@ private PythonQuickFixVerifier() {
49
49
}
50
50
51
51
public static void verify (PythonCheck check , String codeWithIssue , String ... codesFixed ) {
52
+ verify (PythonQuickFixVerifier ::createPythonVisitorContext , check , codeWithIssue , codesFixed );
53
+ }
54
+
55
+ public static void verifyNoQuickFixes (PythonCheck check , String codeWithIssue ) {
56
+ verifyNoQuickFixes (PythonQuickFixVerifier ::createPythonVisitorContext , check , codeWithIssue );
57
+ }
58
+
59
+ public static void verifyQuickFixMessages (PythonCheck check , String codeWithIssue , String ... expectedMessages ) {
60
+ verifyQuickFixMessages (PythonQuickFixVerifier ::createPythonVisitorContext , check , codeWithIssue , expectedMessages );
61
+ }
62
+
63
+ public static void verifyIPython (PythonCheck check , String codeWithIssue , String ... codesFixed ) {
64
+ verify (PythonQuickFixVerifier ::createIPythonVisitorContext , check , codeWithIssue , codesFixed );
65
+ }
66
+
67
+ public static void verifyIPythonNoQuickFixes (PythonCheck check , String codeWithIssue ) {
68
+ verifyNoQuickFixes (PythonQuickFixVerifier ::createIPythonVisitorContext , check , codeWithIssue );
69
+ }
70
+
71
+ public static void verifyIPythonQuickFixMessages (PythonCheck check , String codeWithIssue , String ... expectedMessages ) {
72
+ verifyQuickFixMessages (PythonQuickFixVerifier ::createIPythonVisitorContext , check , codeWithIssue , expectedMessages );
73
+ }
74
+
75
+ public static void verify (Function <String , PythonVisitorContext > createVisitorContext , PythonCheck check , String codeWithIssue , String ... codesFixed ) {
52
76
List <PythonCheck .PreciseIssue > issues = PythonQuickFixVerifier
53
- .getIssuesWithQuickFix (check , codeWithIssue );
77
+ .getIssuesWithQuickFix (createVisitorContext , check , codeWithIssue );
54
78
55
79
assertThat (issues )
56
80
.as ("Number of issues" )
@@ -73,9 +97,9 @@ public static void verify(PythonCheck check, String codeWithIssue, String... cod
73
97
.isEqualTo (Arrays .asList (codesFixed ));
74
98
}
75
99
76
- public static void verifyNoQuickFixes (PythonCheck check , String codeWithIssue ) {
100
+ public static void verifyNoQuickFixes (Function < String , PythonVisitorContext > createVisitorContext , PythonCheck check , String codeWithIssue ) {
77
101
List <PythonCheck .PreciseIssue > issues = PythonQuickFixVerifier
78
- .getIssuesWithQuickFix (check , codeWithIssue );
102
+ .getIssuesWithQuickFix (createVisitorContext , check , codeWithIssue );
79
103
80
104
assertThat (issues )
81
105
.as ("Number of issues" )
@@ -89,9 +113,12 @@ public static void verifyNoQuickFixes(PythonCheck check, String codeWithIssue) {
89
113
.isEmpty ();
90
114
}
91
115
92
- public static void verifyQuickFixMessages (PythonCheck check , String codeWithIssue , String ... expectedMessages ) {
116
+ public static void verifyQuickFixMessages (Function <String , PythonVisitorContext > createVisitorContext ,
117
+ PythonCheck check ,
118
+ String codeWithIssue ,
119
+ String ... expectedMessages ) {
93
120
Stream <String > descriptions = PythonQuickFixVerifier
94
- .getIssuesWithQuickFix (check , codeWithIssue )
121
+ .getIssuesWithQuickFix (createVisitorContext , check , codeWithIssue )
95
122
.stream ()
96
123
.flatMap (issue -> issue .quickFixes ().stream ())
97
124
.map (PythonQuickFix ::getDescription );
@@ -107,17 +134,27 @@ private static List<PreciseIssue> scanFileForIssues(PythonCheck check, PythonVis
107
134
return context .getIssues ();
108
135
}
109
136
110
- private static List <PreciseIssue > getIssuesWithQuickFix (PythonCheck check , String codeWithIssue ) {
111
- PythonParser parser = PythonParser .create ();
112
- PythonQuickFixFile pythonFile = new PythonQuickFixFile (codeWithIssue );
113
- AstNode astNode = parser .parse (pythonFile .content ());
114
- FileInput parse = new PythonTreeMaker ().fileInput (astNode );
137
+ private static List <PreciseIssue > getIssuesWithQuickFix (Function <String , PythonVisitorContext > createVisitorContext , PythonCheck check , String codeWithIssue ) {
138
+ var visitorContext = createVisitorContext .apply (codeWithIssue );
139
+ return scanFileForIssues (check , visitorContext );
140
+ }
141
+
142
+ private static PythonVisitorContext createPythonVisitorContext (String code ) {
143
+ return createVisitorContext (PythonParser .create (), new PythonTreeMaker (), code );
144
+ }
145
+
146
+ private static PythonVisitorContext createIPythonVisitorContext (String code ) {
147
+ return createVisitorContext (PythonParser .createIPythonParser (), new IPythonTreeMaker (), code );
148
+ }
115
149
116
- PythonVisitorContext visitorContext = new PythonVisitorContext (parse ,
150
+ private static PythonVisitorContext createVisitorContext (PythonParser parser , PythonTreeMaker treeMaker , String code ) {
151
+ var pythonFile = new PythonQuickFixFile (code );
152
+ var astNode = parser .parse (pythonFile .content ());
153
+ var fileInput = treeMaker .fileInput (astNode );
154
+
155
+ return new PythonVisitorContext (fileInput ,
117
156
pythonFile , null , "" ,
118
157
ProjectLevelSymbolTable .empty (), CacheContextImpl .dummyCache ());
119
-
120
- return scanFileForIssues (check , visitorContext );
121
158
}
122
159
123
160
private static String applyQuickFix (String codeWithIssue , PythonQuickFix quickFix ) {
0 commit comments