author | rfield |
Sat, 21 May 2016 22:32:08 -0700 | |
changeset 38535 | 4a25025e0b0d |
parent 37644 | 33cf53901cac |
child 38908 | f0c186d76c8a |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.io.ByteArrayOutputStream; |
|
25 |
import java.io.PrintStream; |
|
26 |
import java.io.StringWriter; |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
27 |
import java.lang.reflect.Method; |
33362 | 28 |
import java.nio.file.Path; |
29 |
import java.util.ArrayList; |
|
30 |
import java.util.Arrays; |
|
31 |
import java.util.Collection; |
|
32 |
import java.util.Collections; |
|
33 |
import java.util.HashMap; |
|
34 |
import java.util.LinkedHashMap; |
|
35 |
import java.util.LinkedHashSet; |
|
36 |
import java.util.List; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.Set; |
|
39 |
import java.util.TreeMap; |
|
40 |
import java.util.function.Predicate; |
|
41 |
import java.util.function.Supplier; |
|
42 |
import java.util.stream.Collectors; |
|
43 |
import java.util.stream.Stream; |
|
44 |
||
45 |
import javax.tools.Diagnostic; |
|
46 |
||
47 |
import jdk.jshell.EvalException; |
|
48 |
import jdk.jshell.JShell; |
|
49 |
import jdk.jshell.JShell.Subscription; |
|
50 |
import jdk.jshell.Snippet; |
|
51 |
import jdk.jshell.DeclarationSnippet; |
|
52 |
import jdk.jshell.ExpressionSnippet; |
|
53 |
import jdk.jshell.ImportSnippet; |
|
54 |
import jdk.jshell.Snippet.Kind; |
|
55 |
import jdk.jshell.MethodSnippet; |
|
56 |
import jdk.jshell.PersistentSnippet; |
|
57 |
import jdk.jshell.Snippet.Status; |
|
58 |
import jdk.jshell.Snippet.SubKind; |
|
59 |
import jdk.jshell.TypeDeclSnippet; |
|
60 |
import jdk.jshell.VarSnippet; |
|
61 |
import jdk.jshell.SnippetEvent; |
|
62 |
import jdk.jshell.SourceCodeAnalysis; |
|
63 |
import jdk.jshell.SourceCodeAnalysis.CompletionInfo; |
|
64 |
import jdk.jshell.SourceCodeAnalysis.Completeness; |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
65 |
import jdk.jshell.SourceCodeAnalysis.QualifiedNames; |
33362 | 66 |
import jdk.jshell.SourceCodeAnalysis.Suggestion; |
67 |
import jdk.jshell.UnresolvedReferenceException; |
|
68 |
import org.testng.annotations.AfterMethod; |
|
69 |
import org.testng.annotations.BeforeMethod; |
|
70 |
||
71 |
import jdk.jshell.Diag; |
|
72 |
import static jdk.jshell.Snippet.Status.*; |
|
73 |
import static org.testng.Assert.*; |
|
74 |
import static jdk.jshell.Snippet.SubKind.METHOD_SUBKIND; |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
75 |
import jdk.jshell.spi.ExecutionControl; |
33362 | 76 |
|
77 |
public class KullaTesting { |
|
78 |
||
79 |
public static final String IGNORE_VALUE = "<ignore-value>"; |
|
80 |
public static final Class<? extends Throwable> IGNORE_EXCEPTION = (new Throwable() {}).getClass(); |
|
81 |
public static final Snippet MAIN_SNIPPET; |
|
82 |
||
83 |
private SourceCodeAnalysis analysis = null; |
|
84 |
private JShell state = null; |
|
85 |
private TestingInputStream inStream = null; |
|
86 |
private ByteArrayOutputStream outStream = null; |
|
87 |
private ByteArrayOutputStream errStream = null; |
|
88 |
||
89 |
private Map<String, Snippet> idToSnippet = new LinkedHashMap<>(); |
|
90 |
private Set<Snippet> allSnippets = new LinkedHashSet<>(); |
|
91 |
private List<String> classpath; |
|
92 |
||
93 |
static { |
|
94 |
JShell js = JShell.create(); |
|
95 |
MAIN_SNIPPET = js.eval("MAIN_SNIPPET").get(0).snippet(); |
|
96 |
js.close(); |
|
97 |
assertTrue(MAIN_SNIPPET != null, "Bad MAIN_SNIPPET set-up -- must not be null"); |
|
98 |
} |
|
99 |
||
100 |
public enum DiagCheck { |
|
101 |
DIAG_OK, |
|
102 |
DIAG_WARNING, |
|
103 |
DIAG_ERROR, |
|
104 |
DIAG_IGNORE |
|
105 |
} |
|
106 |
||
107 |
public void setInput(String s) { |
|
108 |
inStream.setInput(s); |
|
109 |
} |
|
110 |
||
111 |
public String getOutput() { |
|
112 |
String s = outStream.toString(); |
|
113 |
outStream.reset(); |
|
114 |
return s; |
|
115 |
} |
|
116 |
||
117 |
public String getErrorOutput() { |
|
118 |
String s = errStream.toString(); |
|
119 |
errStream.reset(); |
|
120 |
return s; |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* @return the analysis |
|
125 |
*/ |
|
126 |
public SourceCodeAnalysis getAnalysis() { |
|
127 |
if (analysis == null) { |
|
128 |
analysis = state.sourceCodeAnalysis(); |
|
129 |
} |
|
130 |
return analysis; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* @return the state |
|
135 |
*/ |
|
136 |
public JShell getState() { |
|
137 |
return state; |
|
138 |
} |
|
139 |
||
140 |
public List<Snippet> getActiveKeys() { |
|
141 |
return allSnippets.stream() |
|
142 |
.filter(k -> getState().status(k).isActive) |
|
143 |
.collect(Collectors.toList()); |
|
144 |
} |
|
145 |
||
146 |
public void addToClasspath(String path) { |
|
147 |
classpath.add(path); |
|
148 |
getState().addToClasspath(path); |
|
149 |
} |
|
150 |
||
151 |
public void addToClasspath(Path path) { |
|
152 |
addToClasspath(path.toString()); |
|
153 |
} |
|
154 |
||
155 |
@BeforeMethod |
|
156 |
public void setUp() { |
|
157 |
inStream = new TestingInputStream(); |
|
158 |
outStream = new ByteArrayOutputStream(); |
|
159 |
errStream = new ByteArrayOutputStream(); |
|
160 |
state = JShell.builder() |
|
161 |
.in(inStream) |
|
162 |
.out(new PrintStream(outStream)) |
|
163 |
.err(new PrintStream(errStream)) |
|
164 |
.build(); |
|
165 |
allSnippets = new LinkedHashSet<>(); |
|
166 |
idToSnippet = new LinkedHashMap<>(); |
|
167 |
classpath = new ArrayList<>(); |
|
168 |
} |
|
169 |
||
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
170 |
public void setUp(ExecutionControl ec) { |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
171 |
inStream = new TestingInputStream(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
172 |
outStream = new ByteArrayOutputStream(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
173 |
errStream = new ByteArrayOutputStream(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
174 |
state = JShell.builder() |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
175 |
.executionEngine(ec) |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
176 |
.in(inStream) |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
177 |
.out(new PrintStream(outStream)) |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
178 |
.err(new PrintStream(errStream)) |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
179 |
.build(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
180 |
allSnippets = new LinkedHashSet<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
181 |
idToSnippet = new LinkedHashMap<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
182 |
classpath = new ArrayList<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
183 |
} |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
184 |
|
33362 | 185 |
@AfterMethod |
186 |
public void tearDown() { |
|
187 |
if (state != null) state.close(); |
|
188 |
state = null; |
|
189 |
analysis = null; |
|
190 |
allSnippets = null; |
|
191 |
idToSnippet = null; |
|
192 |
classpath = null; |
|
193 |
} |
|
194 |
||
195 |
public List<String> assertUnresolvedDependencies(DeclarationSnippet key, int unresolvedSize) { |
|
196 |
List<String> unresolved = getState().unresolvedDependencies(key); |
|
197 |
assertEquals(unresolved.size(), unresolvedSize, "Input: " + key.source() + ", checking unresolved: "); |
|
198 |
return unresolved; |
|
199 |
} |
|
200 |
||
201 |
public DeclarationSnippet assertUnresolvedDependencies1(DeclarationSnippet key, Status status, String name) { |
|
202 |
List<String> unresolved = assertUnresolvedDependencies(key, 1); |
|
203 |
String input = key.source(); |
|
204 |
assertEquals(unresolved.size(), 1, "Given input: " + input + ", checking unresolved"); |
|
205 |
assertEquals(unresolved.get(0), name, "Given input: " + input + ", checking unresolved: "); |
|
206 |
assertEquals(getState().status(key), status, "Given input: " + input + ", checking status: "); |
|
207 |
return key; |
|
208 |
} |
|
209 |
||
36499 | 210 |
public DeclarationSnippet assertEvalUnresolvedException(String input, String name, int unresolvedSize, int diagnosticsSize) { |
33362 | 211 |
List<SnippetEvent> events = assertEval(input, null, UnresolvedReferenceException.class, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null); |
212 |
SnippetEvent ste = events.get(0); |
|
36499 | 213 |
DeclarationSnippet sn = ((UnresolvedReferenceException) ste.exception()).getSnippet(); |
214 |
assertEquals(sn.name(), name, "Given input: " + input + ", checking name"); |
|
215 |
assertEquals(getState().unresolvedDependencies(sn).size(), unresolvedSize, "Given input: " + input + ", checking unresolved"); |
|
216 |
assertEquals(getState().diagnostics(sn).size(), diagnosticsSize, "Given input: " + input + ", checking diagnostics"); |
|
217 |
return sn; |
|
33362 | 218 |
} |
219 |
||
220 |
public Snippet assertKeyMatch(String input, boolean isExecutable, SubKind expectedSubKind, STEInfo mainInfo, STEInfo... updates) { |
|
221 |
Snippet key = key(assertEval(input, IGNORE_VALUE, mainInfo, updates)); |
|
222 |
String source = key.source(); |
|
223 |
assertEquals(source, input, "Key \"" + input + "\" source mismatch, got: " + source + ", expected: " + input); |
|
224 |
SubKind subkind = key.subKind(); |
|
225 |
assertEquals(subkind, expectedSubKind, "Key \"" + input + "\" subkind mismatch, got: " |
|
226 |
+ subkind + ", expected: " + expectedSubKind); |
|
227 |
assertEquals(subkind.isExecutable(), isExecutable, "Key \"" + input + "\", expected isExecutable: " |
|
228 |
+ isExecutable + ", got: " + subkind.isExecutable()); |
|
229 |
Snippet.Kind expectedKind = getKind(key); |
|
230 |
assertEquals(key.kind(), expectedKind, "Checking kind: "); |
|
231 |
assertEquals(expectedSubKind.kind(), expectedKind, "Checking kind: "); |
|
232 |
return key; |
|
233 |
} |
|
234 |
||
235 |
private Kind getKind(Snippet key) { |
|
236 |
SubKind expectedSubKind = key.subKind(); |
|
237 |
Kind expectedKind; |
|
238 |
switch (expectedSubKind) { |
|
239 |
case SINGLE_TYPE_IMPORT_SUBKIND: |
|
240 |
case SINGLE_STATIC_IMPORT_SUBKIND: |
|
241 |
case TYPE_IMPORT_ON_DEMAND_SUBKIND: |
|
242 |
case STATIC_IMPORT_ON_DEMAND_SUBKIND: |
|
243 |
expectedKind = Kind.IMPORT; |
|
244 |
break; |
|
245 |
case CLASS_SUBKIND: |
|
246 |
case INTERFACE_SUBKIND: |
|
247 |
case ENUM_SUBKIND: |
|
248 |
case ANNOTATION_TYPE_SUBKIND: |
|
249 |
expectedKind = Kind.TYPE_DECL; |
|
250 |
break; |
|
251 |
case METHOD_SUBKIND: |
|
252 |
expectedKind = Kind.METHOD; |
|
253 |
break; |
|
254 |
case VAR_DECLARATION_SUBKIND: |
|
255 |
case TEMP_VAR_EXPRESSION_SUBKIND: |
|
256 |
case VAR_DECLARATION_WITH_INITIALIZER_SUBKIND: |
|
257 |
expectedKind = Kind.VAR; |
|
258 |
break; |
|
259 |
case VAR_VALUE_SUBKIND: |
|
260 |
case ASSIGNMENT_SUBKIND: |
|
261 |
expectedKind = Kind.EXPRESSION; |
|
262 |
break; |
|
263 |
case STATEMENT_SUBKIND: |
|
264 |
expectedKind = Kind.STATEMENT; |
|
265 |
break; |
|
266 |
case UNKNOWN_SUBKIND: |
|
267 |
expectedKind = Kind.ERRONEOUS; |
|
268 |
break; |
|
269 |
default: |
|
270 |
throw new AssertionError("Unsupported key: " + key.getClass().getCanonicalName()); |
|
271 |
} |
|
272 |
return expectedKind; |
|
273 |
} |
|
274 |
||
275 |
public ImportSnippet assertImportKeyMatch(String input, String name, SubKind subkind, STEInfo mainInfo, STEInfo... updates) { |
|
276 |
Snippet key = assertKeyMatch(input, false, subkind, mainInfo, updates); |
|
277 |
||
278 |
assertTrue(key instanceof ImportSnippet, "Expected an ImportKey, got: " + key.getClass().getName()); |
|
279 |
ImportSnippet importKey = (ImportSnippet) key; |
|
280 |
assertEquals(importKey.name(), name, "Input \"" + input + |
|
281 |
"\" name mismatch, got: " + importKey.name() + ", expected: " + name); |
|
282 |
assertEquals(importKey.kind(), Kind.IMPORT, "Checking kind: "); |
|
283 |
return importKey; |
|
284 |
} |
|
285 |
||
286 |
public DeclarationSnippet assertDeclarationKeyMatch(String input, boolean isExecutable, String name, SubKind subkind, STEInfo mainInfo, STEInfo... updates) { |
|
287 |
Snippet key = assertKeyMatch(input, isExecutable, subkind, mainInfo, updates); |
|
288 |
||
289 |
assertTrue(key instanceof DeclarationSnippet, "Expected a DeclarationKey, got: " + key.getClass().getName()); |
|
290 |
DeclarationSnippet declKey = (DeclarationSnippet) key; |
|
291 |
assertEquals(declKey.name(), name, "Input \"" + input + |
|
292 |
"\" name mismatch, got: " + declKey.name() + ", expected: " + name); |
|
293 |
return declKey; |
|
294 |
} |
|
295 |
||
296 |
public VarSnippet assertVarKeyMatch(String input, boolean isExecutable, String name, SubKind kind, String typeName, STEInfo mainInfo, STEInfo... updates) { |
|
297 |
Snippet sn = assertDeclarationKeyMatch(input, isExecutable, name, kind, mainInfo, updates); |
|
298 |
assertTrue(sn instanceof VarSnippet, "Expected a VarKey, got: " + sn.getClass().getName()); |
|
299 |
VarSnippet variableKey = (VarSnippet) sn; |
|
300 |
String signature = variableKey.typeName(); |
|
301 |
assertEquals(signature, typeName, "Key \"" + input + |
|
302 |
"\" typeName mismatch, got: " + signature + ", expected: " + typeName); |
|
303 |
assertEquals(variableKey.kind(), Kind.VAR, "Checking kind: "); |
|
304 |
return variableKey; |
|
305 |
} |
|
306 |
||
307 |
public void assertExpressionKeyMatch(String input, String name, SubKind kind, String typeName) { |
|
308 |
Snippet key = assertKeyMatch(input, true, kind, added(VALID)); |
|
309 |
assertTrue(key instanceof ExpressionSnippet, "Expected a ExpressionKey, got: " + key.getClass().getName()); |
|
310 |
ExpressionSnippet exprKey = (ExpressionSnippet) key; |
|
311 |
assertEquals(exprKey.name(), name, "Input \"" + input + |
|
312 |
"\" name mismatch, got: " + exprKey.name() + ", expected: " + name); |
|
313 |
assertEquals(exprKey.typeName(), typeName, "Key \"" + input + |
|
314 |
"\" typeName mismatch, got: " + exprKey.typeName() + ", expected: " + typeName); |
|
315 |
assertEquals(exprKey.kind(), Kind.EXPRESSION, "Checking kind: "); |
|
316 |
} |
|
317 |
||
318 |
// For expressions throwing an EvalException |
|
319 |
public SnippetEvent assertEvalException(String input) { |
|
320 |
List<SnippetEvent> events = assertEval(input, null, EvalException.class, |
|
321 |
DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null); |
|
322 |
return events.get(0); |
|
323 |
} |
|
324 |
||
325 |
||
326 |
public List<SnippetEvent> assertEvalFail(String input) { |
|
327 |
return assertEval(input, null, null, |
|
328 |
DiagCheck.DIAG_ERROR, DiagCheck.DIAG_IGNORE, added(REJECTED)); |
|
329 |
} |
|
330 |
||
331 |
public List<SnippetEvent> assertEval(String input) { |
|
332 |
return assertEval(input, IGNORE_VALUE, null, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, added(VALID)); |
|
333 |
} |
|
334 |
||
335 |
public List<SnippetEvent> assertEval(String input, String value) { |
|
336 |
return assertEval(input, value, null, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, added(VALID)); |
|
337 |
} |
|
338 |
||
339 |
public List<SnippetEvent> assertEval(String input, STEInfo mainInfo, STEInfo... updates) { |
|
340 |
return assertEval(input, IGNORE_VALUE, null, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, mainInfo, updates); |
|
341 |
} |
|
342 |
||
343 |
public List<SnippetEvent> assertEval(String input, String value, |
|
344 |
STEInfo mainInfo, STEInfo... updates) { |
|
345 |
return assertEval(input, value, null, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, mainInfo, updates); |
|
346 |
} |
|
347 |
||
348 |
public List<SnippetEvent> assertEval(String input, DiagCheck diagMain, DiagCheck diagUpdates) { |
|
349 |
return assertEval(input, IGNORE_VALUE, null, diagMain, diagUpdates, added(VALID)); |
|
350 |
} |
|
351 |
||
352 |
public List<SnippetEvent> assertEval(String input, DiagCheck diagMain, DiagCheck diagUpdates, |
|
353 |
STEInfo mainInfo, STEInfo... updates) { |
|
354 |
return assertEval(input, IGNORE_VALUE, null, diagMain, diagUpdates, mainInfo, updates); |
|
355 |
} |
|
356 |
||
357 |
public List<SnippetEvent> assertEval(String input, |
|
358 |
String value, Class<? extends Throwable> exceptionClass, |
|
359 |
DiagCheck diagMain, DiagCheck diagUpdates, |
|
360 |
STEInfo mainInfo, STEInfo... updates) { |
|
361 |
return assertEval(input, diagMain, diagUpdates, new EventChain(mainInfo, value, exceptionClass, updates)); |
|
362 |
} |
|
363 |
||
364 |
// Use this directly or usually indirectly for all non-empty calls to eval() |
|
365 |
public List<SnippetEvent> assertEval(String input, |
|
366 |
DiagCheck diagMain, DiagCheck diagUpdates, EventChain... eventChains) { |
|
367 |
return checkEvents(() -> getState().eval(input), "eval(" + input + ")", diagMain, diagUpdates, eventChains); |
|
368 |
} |
|
369 |
||
370 |
private Map<Snippet, Snippet> closure(List<SnippetEvent> events) { |
|
371 |
Map<Snippet, Snippet> transitions = new HashMap<>(); |
|
372 |
for (SnippetEvent event : events) { |
|
373 |
transitions.put(event.snippet(), event.causeSnippet()); |
|
374 |
} |
|
375 |
Map<Snippet, Snippet> causeSnippets = new HashMap<>(); |
|
376 |
for (Map.Entry<Snippet, Snippet> entry : transitions.entrySet()) { |
|
377 |
Snippet snippet = entry.getKey(); |
|
378 |
Snippet cause = getInitialCause(transitions, entry.getValue()); |
|
379 |
causeSnippets.put(snippet, cause); |
|
380 |
} |
|
381 |
return causeSnippets; |
|
382 |
} |
|
383 |
||
384 |
private Snippet getInitialCause(Map<Snippet, Snippet> transitions, Snippet snippet) { |
|
385 |
Snippet result; |
|
386 |
while ((result = transitions.get(snippet)) != null) { |
|
387 |
snippet = result; |
|
388 |
} |
|
389 |
return snippet; |
|
390 |
} |
|
391 |
||
392 |
private Map<Snippet, List<SnippetEvent>> groupByCauseSnippet(List<SnippetEvent> events) { |
|
393 |
Map<Snippet, List<SnippetEvent>> map = new TreeMap<>((a, b) -> a.id().compareTo(b.id())); |
|
394 |
for (SnippetEvent event : events) { |
|
395 |
if (event == null) { |
|
396 |
throw new InternalError("null event found in " + events); |
|
397 |
} |
|
398 |
if (event.snippet() == null) { |
|
399 |
throw new InternalError("null event Snippet found in " + events); |
|
400 |
} |
|
401 |
if (event.snippet().id() == null) { |
|
402 |
throw new InternalError("null event Snippet id() found in " + events); |
|
403 |
} |
|
404 |
} |
|
405 |
for (SnippetEvent event : events) { |
|
406 |
if (event.causeSnippet() == null) { |
|
407 |
map.computeIfAbsent(event.snippet(), ($) -> new ArrayList<>()).add(event); |
|
408 |
} |
|
409 |
} |
|
410 |
Map<Snippet, Snippet> causeSnippets = closure(events); |
|
411 |
for (SnippetEvent event : events) { |
|
412 |
Snippet causeSnippet = causeSnippets.get(event.snippet()); |
|
413 |
if (causeSnippet != null) { |
|
414 |
map.get(causeSnippet).add(event); |
|
415 |
} |
|
416 |
} |
|
417 |
for (Map.Entry<Snippet, List<SnippetEvent>> entry : map.entrySet()) { |
|
418 |
Collections.sort(entry.getValue(), |
|
419 |
(a, b) -> a.causeSnippet() == null |
|
420 |
? -1 : b.causeSnippet() == null |
|
421 |
? 1 : a.snippet().id().compareTo(b.snippet().id())); |
|
422 |
} |
|
423 |
return map; |
|
424 |
} |
|
425 |
||
426 |
private List<STEInfo> getInfos(EventChain... eventChains) { |
|
427 |
List<STEInfo> list = new ArrayList<>(); |
|
428 |
for (EventChain i : eventChains) { |
|
429 |
list.add(i.mainInfo); |
|
430 |
Collections.addAll(list, i.updates); |
|
431 |
} |
|
432 |
return list; |
|
433 |
} |
|
434 |
||
435 |
private List<SnippetEvent> checkEvents(Supplier<List<SnippetEvent>> toTest, |
|
436 |
String descriptor, |
|
437 |
DiagCheck diagMain, DiagCheck diagUpdates, |
|
438 |
EventChain... eventChains) { |
|
439 |
List<SnippetEvent> dispatched = new ArrayList<>(); |
|
440 |
Subscription token = getState().onSnippetEvent(kse -> { |
|
441 |
if (dispatched.size() > 0 && dispatched.get(dispatched.size() - 1) == null) { |
|
442 |
throw new RuntimeException("dispatch event after done"); |
|
443 |
} |
|
444 |
dispatched.add(kse); |
|
445 |
}); |
|
446 |
List<SnippetEvent> events = toTest.get(); |
|
447 |
getState().unsubscribe(token); |
|
448 |
assertEquals(dispatched.size(), events.size(), "dispatched event size not the same as event size"); |
|
449 |
for (int i = events.size() - 1; i >= 0; --i) { |
|
450 |
assertEquals(dispatched.get(i), events.get(i), "Event element " + i + " does not match"); |
|
451 |
} |
|
452 |
dispatched.add(null); // mark end of dispatchs |
|
453 |
||
454 |
for (SnippetEvent evt : events) { |
|
455 |
assertTrue(evt.snippet() != null, "key must never be null, but it was for: " + descriptor); |
|
456 |
assertTrue(evt.previousStatus() != null, "previousStatus must never be null, but it was for: " + descriptor); |
|
457 |
assertTrue(evt.status() != null, "status must never be null, but it was for: " + descriptor); |
|
458 |
assertTrue(evt.status() != NONEXISTENT, "status must not be NONEXISTENT: " + descriptor); |
|
459 |
if (evt.previousStatus() != NONEXISTENT) { |
|
460 |
Snippet old = idToSnippet.get(evt.snippet().id()); |
|
461 |
if (old != null) { |
|
462 |
switch (evt.status()) { |
|
463 |
case DROPPED: |
|
464 |
assertEquals(old, evt.snippet(), |
|
465 |
"Drop: Old snippet must be what is dropped -- input: " + descriptor); |
|
466 |
break; |
|
467 |
case OVERWRITTEN: |
|
468 |
assertEquals(old, evt.snippet(), |
|
469 |
"Overwrite: Old snippet (" + old |
|
470 |
+ ") must be what is overwritten -- input: " |
|
471 |
+ descriptor + " -- " + evt); |
|
472 |
break; |
|
473 |
default: |
|
474 |
if (evt.causeSnippet() == null) { |
|
475 |
// New source |
|
476 |
assertNotEquals(old, evt.snippet(), |
|
477 |
"New source: Old snippet must be different from the replacing -- input: " |
|
478 |
+ descriptor); |
|
479 |
} else { |
|
480 |
// An update (key Overwrite??) |
|
481 |
assertEquals(old, evt.snippet(), |
|
482 |
"Update: Old snippet must be equal to the replacing -- input: " |
|
483 |
+ descriptor); |
|
484 |
} |
|
485 |
break; |
|
486 |
} |
|
487 |
} |
|
488 |
} |
|
489 |
} |
|
490 |
for (SnippetEvent evt : events) { |
|
491 |
if (evt.causeSnippet() == null && evt.status() != DROPPED) { |
|
492 |
allSnippets.add(evt.snippet()); |
|
493 |
idToSnippet.put(evt.snippet().id(), evt.snippet()); |
|
494 |
} |
|
495 |
} |
|
496 |
assertTrue(events.size() >= 1, "Expected at least one event, got none."); |
|
497 |
List<STEInfo> all = getInfos(eventChains); |
|
498 |
if (events.size() != all.size()) { |
|
499 |
StringBuilder sb = new StringBuilder(); |
|
500 |
sb.append("Got events --\n"); |
|
501 |
for (SnippetEvent evt : events) { |
|
502 |
sb.append(" key: ").append(evt.snippet()); |
|
503 |
sb.append(" before: ").append(evt.previousStatus()); |
|
504 |
sb.append(" status: ").append(evt.status()); |
|
505 |
sb.append(" isSignatureChange: ").append(evt.isSignatureChange()); |
|
506 |
sb.append(" cause: "); |
|
507 |
if (evt.causeSnippet() == null) { |
|
508 |
sb.append("direct"); |
|
509 |
} else { |
|
510 |
sb.append(evt.causeSnippet()); |
|
511 |
} |
|
512 |
sb.append("\n"); |
|
513 |
} |
|
514 |
sb.append("Expected ").append(all.size()); |
|
515 |
sb.append(" events, got: ").append(events.size()); |
|
516 |
fail(sb.toString()); |
|
517 |
} |
|
518 |
||
519 |
int impactId = 0; |
|
520 |
Map<Snippet, List<SnippetEvent>> groupedEvents = groupByCauseSnippet(events); |
|
521 |
assertEquals(groupedEvents.size(), eventChains.length, "Number of main events"); |
|
522 |
for (Map.Entry<Snippet, List<SnippetEvent>> entry : groupedEvents.entrySet()) { |
|
523 |
EventChain eventChain = eventChains[impactId++]; |
|
524 |
SnippetEvent main = entry.getValue().get(0); |
|
525 |
Snippet mainKey = main.snippet(); |
|
526 |
if (eventChain.mainInfo != null) { |
|
527 |
eventChain.mainInfo.assertMatch(entry.getValue().get(0), mainKey); |
|
528 |
if (eventChain.updates.length > 0) { |
|
529 |
if (eventChain.updates.length == 1) { |
|
530 |
eventChain.updates[0].assertMatch(entry.getValue().get(1), mainKey); |
|
531 |
} else { |
|
532 |
Arrays.sort(eventChain.updates, (a, b) -> ((a.snippet() == MAIN_SNIPPET) |
|
533 |
? mainKey |
|
534 |
: a.snippet()).id().compareTo(b.snippet().id())); |
|
535 |
List<SnippetEvent> updateEvents = new ArrayList<>(entry.getValue().subList(1, entry.getValue().size())); |
|
536 |
int idx = 0; |
|
537 |
for (SnippetEvent ste : updateEvents) { |
|
538 |
eventChain.updates[idx++].assertMatch(ste, mainKey); |
|
539 |
} |
|
540 |
} |
|
541 |
} |
|
542 |
} |
|
543 |
if (((Object) eventChain.value) != IGNORE_VALUE) { |
|
544 |
assertEquals(main.value(), eventChain.value, "Expected execution value of: " + eventChain.value + |
|
545 |
", but got: " + main.value()); |
|
546 |
} |
|
547 |
if (eventChain.exceptionClass != IGNORE_EXCEPTION) { |
|
548 |
if (main.exception() == null) { |
|
549 |
assertEquals(eventChain.exceptionClass, null, "Expected an exception of class " |
|
550 |
+ eventChain.exceptionClass + " got no exception"); |
|
551 |
} else if (eventChain.exceptionClass == null) { |
|
552 |
fail("Expected no exception but got " + main.exception().toString()); |
|
553 |
} else { |
|
554 |
assertTrue(eventChain.exceptionClass.isInstance(main.exception()), |
|
555 |
"Expected an exception of class " + eventChain.exceptionClass + |
|
556 |
" got: " + main.exception().toString()); |
|
557 |
} |
|
558 |
} |
|
559 |
List<Diag> diagnostics = getState().diagnostics(mainKey); |
|
560 |
switch (diagMain) { |
|
561 |
case DIAG_OK: |
|
562 |
assertEquals(diagnostics.size(), 0, "Expected no diagnostics, got: " + diagnosticsToString(diagnostics)); |
|
563 |
break; |
|
564 |
case DIAG_WARNING: |
|
565 |
assertFalse(hasFatalError(diagnostics), "Expected no errors, got: " + diagnosticsToString(diagnostics)); |
|
566 |
break; |
|
567 |
case DIAG_ERROR: |
|
568 |
assertTrue(hasFatalError(diagnostics), "Expected errors, got: " + diagnosticsToString(diagnostics)); |
|
569 |
break; |
|
570 |
} |
|
571 |
if (eventChain.mainInfo != null) { |
|
572 |
for (STEInfo ste : eventChain.updates) { |
|
573 |
diagnostics = getState().diagnostics(ste.snippet()); |
|
574 |
switch (diagUpdates) { |
|
575 |
case DIAG_OK: |
|
576 |
assertEquals(diagnostics.size(), 0, "Expected no diagnostics, got: " + diagnosticsToString(diagnostics)); |
|
577 |
break; |
|
578 |
case DIAG_WARNING: |
|
579 |
assertFalse(hasFatalError(diagnostics), "Expected no errors, got: " + diagnosticsToString(diagnostics)); |
|
580 |
break; |
|
581 |
} |
|
582 |
} |
|
583 |
} |
|
584 |
} |
|
585 |
return events; |
|
586 |
} |
|
587 |
||
588 |
// Use this for all EMPTY calls to eval() |
|
589 |
public void assertEvalEmpty(String input) { |
|
590 |
List<SnippetEvent> events = getState().eval(input); |
|
591 |
assertEquals(events.size(), 0, "Expected no events, got: " + events.size()); |
|
592 |
} |
|
593 |
||
594 |
public VarSnippet varKey(List<SnippetEvent> events) { |
|
595 |
Snippet key = key(events); |
|
596 |
assertTrue(key instanceof VarSnippet, "Expected a VariableKey, got: " + key); |
|
597 |
return (VarSnippet) key; |
|
598 |
} |
|
599 |
||
600 |
public MethodSnippet methodKey(List<SnippetEvent> events) { |
|
601 |
Snippet key = key(events); |
|
602 |
assertTrue(key instanceof MethodSnippet, "Expected a MethodKey, got: " + key); |
|
603 |
return (MethodSnippet) key; |
|
604 |
} |
|
605 |
||
606 |
public TypeDeclSnippet classKey(List<SnippetEvent> events) { |
|
607 |
Snippet key = key(events); |
|
608 |
assertTrue(key instanceof TypeDeclSnippet, "Expected a ClassKey, got: " + key); |
|
609 |
return (TypeDeclSnippet) key; |
|
610 |
} |
|
611 |
||
612 |
public ImportSnippet importKey(List<SnippetEvent> events) { |
|
613 |
Snippet key = key(events); |
|
614 |
assertTrue(key instanceof ImportSnippet, "Expected a ImportKey, got: " + key); |
|
615 |
return (ImportSnippet) key; |
|
616 |
} |
|
617 |
||
618 |
private Snippet key(List<SnippetEvent> events) { |
|
619 |
assertTrue(events.size() >= 1, "Expected at least one event, got none."); |
|
620 |
return events.get(0).snippet(); |
|
621 |
} |
|
622 |
||
623 |
public void assertVarValue(Snippet key, String expected) { |
|
624 |
String value = state.varValue((VarSnippet) key); |
|
625 |
assertEquals(value, expected, "Expected var value of: " + expected + ", but got: " + value); |
|
626 |
} |
|
627 |
||
628 |
public Snippet assertDeclareFail(String input, String expectedErrorCode) { |
|
629 |
return assertDeclareFail(input, expectedErrorCode, added(REJECTED)); |
|
630 |
} |
|
631 |
||
632 |
public Snippet assertDeclareFail(String input, String expectedErrorCode, |
|
633 |
STEInfo mainInfo, STEInfo... updates) { |
|
634 |
return assertDeclareFail(input, |
|
635 |
new ExpectedDiagnostic(expectedErrorCode, -1, -1, -1, -1, -1, Diagnostic.Kind.ERROR), |
|
636 |
mainInfo, updates); |
|
637 |
} |
|
638 |
||
639 |
public Snippet assertDeclareFail(String input, ExpectedDiagnostic expectedDiagnostic) { |
|
640 |
return assertDeclareFail(input, expectedDiagnostic, added(REJECTED)); |
|
641 |
} |
|
642 |
||
643 |
public Snippet assertDeclareFail(String input, ExpectedDiagnostic expectedDiagnostic, |
|
644 |
STEInfo mainInfo, STEInfo... updates) { |
|
645 |
List<SnippetEvent> events = assertEval(input, null, null, |
|
646 |
DiagCheck.DIAG_ERROR, DiagCheck.DIAG_IGNORE, mainInfo, updates); |
|
647 |
SnippetEvent e = events.get(0); |
|
648 |
Snippet key = e.snippet(); |
|
649 |
assertEquals(getState().status(key), REJECTED); |
|
650 |
List<Diag> diagnostics = getState().diagnostics(e.snippet()); |
|
651 |
assertTrue(diagnostics.size() > 0, "Expected diagnostics, got none"); |
|
652 |
assertDiagnostic(input, diagnostics.get(0), expectedDiagnostic); |
|
653 |
assertTrue(key != null, "key must never be null, but it was for: " + input); |
|
654 |
return key; |
|
655 |
} |
|
656 |
||
657 |
public Snippet assertDeclareWarn1(String input, String expectedErrorCode) { |
|
658 |
return assertDeclareWarn1(input, new ExpectedDiagnostic(expectedErrorCode, -1, -1, -1, -1, -1, Diagnostic.Kind.WARNING)); |
|
659 |
} |
|
660 |
||
661 |
public Snippet assertDeclareWarn1(String input, ExpectedDiagnostic expectedDiagnostic) { |
|
662 |
return assertDeclareWarn1(input, expectedDiagnostic, added(VALID)); |
|
663 |
} |
|
664 |
||
665 |
public Snippet assertDeclareWarn1(String input, ExpectedDiagnostic expectedDiagnostic, STEInfo mainInfo, STEInfo... updates) { |
|
666 |
List<SnippetEvent> events = assertEval(input, IGNORE_VALUE, null, |
|
667 |
DiagCheck.DIAG_WARNING, DiagCheck.DIAG_IGNORE, mainInfo, updates); |
|
668 |
SnippetEvent e = events.get(0); |
|
669 |
List<Diag> diagnostics = getState().diagnostics(e.snippet()); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36499
diff
changeset
|
670 |
if (expectedDiagnostic != null) assertDiagnostic(input, diagnostics.get(0), expectedDiagnostic); |
33362 | 671 |
return e.snippet(); |
672 |
} |
|
673 |
||
674 |
private void assertDiagnostic(String input, Diag diagnostic, ExpectedDiagnostic expectedDiagnostic) { |
|
675 |
if (expectedDiagnostic != null) expectedDiagnostic.assertDiagnostic(diagnostic); |
|
676 |
// assertEquals(diagnostic.getSource(), input, "Diagnostic source"); |
|
677 |
} |
|
678 |
||
679 |
public void assertTypeDeclSnippet(TypeDeclSnippet type, String expectedName, |
|
680 |
Status expectedStatus, SubKind expectedSubKind, |
|
681 |
int unressz, int othersz) { |
|
682 |
assertDeclarationSnippet(type, expectedName, expectedStatus, |
|
683 |
expectedSubKind, unressz, othersz); |
|
684 |
} |
|
685 |
||
686 |
public void assertMethodDeclSnippet(MethodSnippet method, |
|
687 |
String expectedName, String expectedSignature, |
|
688 |
Status expectedStatus, int unressz, int othersz) { |
|
689 |
assertDeclarationSnippet(method, expectedName, expectedStatus, |
|
690 |
METHOD_SUBKIND, unressz, othersz); |
|
691 |
String signature = method.signature(); |
|
692 |
assertEquals(signature, expectedSignature, |
|
693 |
"Expected " + method.source() + " to have the name: " + |
|
694 |
expectedSignature + ", got: " + signature); |
|
695 |
} |
|
696 |
||
697 |
public void assertVariableDeclSnippet(VarSnippet var, |
|
698 |
String expectedName, String expectedTypeName, |
|
699 |
Status expectedStatus, SubKind expectedSubKind, |
|
700 |
int unressz, int othersz) { |
|
701 |
assertDeclarationSnippet(var, expectedName, expectedStatus, |
|
702 |
expectedSubKind, unressz, othersz); |
|
703 |
String signature = var.typeName(); |
|
704 |
assertEquals(signature, expectedTypeName, |
|
705 |
"Expected " + var.source() + " to have the name: " + |
|
706 |
expectedTypeName + ", got: " + signature); |
|
707 |
} |
|
708 |
||
709 |
public void assertDeclarationSnippet(DeclarationSnippet declarationKey, |
|
710 |
String expectedName, |
|
711 |
Status expectedStatus, SubKind expectedSubKind, |
|
712 |
int unressz, int othersz) { |
|
713 |
assertKey(declarationKey, expectedStatus, expectedSubKind); |
|
714 |
String source = declarationKey.source(); |
|
715 |
assertEquals(declarationKey.name(), expectedName, |
|
716 |
"Expected " + source + " to have the name: " + expectedName + ", got: " + declarationKey.name()); |
|
717 |
List<String> unresolved = getState().unresolvedDependencies(declarationKey); |
|
718 |
assertEquals(unresolved.size(), unressz, "Expected " + source + " to have " + unressz |
|
719 |
+ " unresolved symbols, got: " + unresolved.size()); |
|
720 |
List<Diag> otherCorralledErrors = getState().diagnostics(declarationKey); |
|
721 |
assertEquals(otherCorralledErrors.size(), othersz, "Expected " + source + " to have " + othersz |
|
722 |
+ " other errors, got: " + otherCorralledErrors.size()); |
|
723 |
} |
|
724 |
||
725 |
public void assertKey(Snippet key, Status expectedStatus, SubKind expectedSubKind) { |
|
726 |
String source = key.source(); |
|
727 |
SubKind actualSubKind = key.subKind(); |
|
728 |
assertEquals(actualSubKind, expectedSubKind, |
|
729 |
"Expected " + source + " to have the subkind: " + expectedSubKind + ", got: " + actualSubKind); |
|
730 |
Status status = getState().status(key); |
|
731 |
assertEquals(status, expectedStatus, "Expected " + source + " to be " |
|
732 |
+ expectedStatus + ", but it is " + status); |
|
733 |
Snippet.Kind expectedKind = getKind(key); |
|
734 |
assertEquals(key.kind(), expectedKind, "Checking kind: "); |
|
735 |
assertEquals(expectedSubKind.kind(), expectedKind, "Checking kind: "); |
|
736 |
} |
|
737 |
||
738 |
public void assertDrop(PersistentSnippet key, STEInfo mainInfo, STEInfo... updates) { |
|
739 |
assertDrop(key, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, mainInfo, updates); |
|
740 |
} |
|
741 |
||
742 |
public void assertDrop(PersistentSnippet key, DiagCheck diagMain, DiagCheck diagUpdates, STEInfo mainInfo, STEInfo... updates) { |
|
743 |
assertDrop(key, diagMain, diagUpdates, new EventChain(mainInfo, null, null, updates)); |
|
744 |
} |
|
745 |
||
746 |
public void assertDrop(PersistentSnippet key, DiagCheck diagMain, DiagCheck diagUpdates, EventChain... eventChains) { |
|
747 |
checkEvents(() -> getState().drop(key), "drop(" + key + ")", diagMain, diagUpdates, eventChains); |
|
748 |
} |
|
749 |
||
750 |
public void assertAnalyze(String input, String source, String remaining, boolean isComplete) { |
|
751 |
assertAnalyze(input, null, source, remaining, isComplete); |
|
752 |
} |
|
753 |
||
754 |
public void assertAnalyze(String input, Completeness status, String source) { |
|
755 |
assertAnalyze(input, status, source, null, null); |
|
756 |
} |
|
757 |
||
758 |
public void assertAnalyze(String input, Completeness status, String source, String remaining, Boolean isComplete) { |
|
759 |
CompletionInfo ci = getAnalysis().analyzeCompletion(input); |
|
760 |
if (status != null) assertEquals(ci.completeness, status, "Input : " + input + ", status: "); |
|
761 |
if (source != null) assertEquals(ci.source, source, "Input : " + input + ", source: "); |
|
762 |
if (remaining != null) assertEquals(ci.remaining, remaining, "Input : " + input + ", remaining: "); |
|
763 |
if (isComplete != null) { |
|
764 |
boolean isExpectedComplete = isComplete; |
|
765 |
assertEquals(ci.completeness.isComplete, isExpectedComplete, "Input : " + input + ", isComplete: "); |
|
766 |
} |
|
767 |
} |
|
768 |
||
769 |
public void assertNumberOfActiveVariables(int cnt) { |
|
770 |
Collection<VarSnippet> variables = getState().variables(); |
|
771 |
assertEquals(variables.size(), cnt, "Variables : " + variables); |
|
772 |
} |
|
773 |
||
774 |
public void assertNumberOfActiveMethods(int cnt) { |
|
775 |
Collection<MethodSnippet> methods = getState().methods(); |
|
776 |
assertEquals(methods.size(), cnt, "Methods : " + methods); |
|
777 |
} |
|
778 |
||
779 |
public void assertNumberOfActiveClasses(int cnt) { |
|
780 |
Collection<TypeDeclSnippet> classes = getState().types(); |
|
781 |
assertEquals(classes.size(), cnt, "Classes : " + classes); |
|
782 |
} |
|
783 |
||
784 |
public void assertMembers(Collection<? extends Snippet> members, Set<MemberInfo> expected) { |
|
785 |
assertEquals(members.size(), expected.size(), "Expected : " + expected + ", actual : " + members); |
|
786 |
assertEquals(members.stream() |
|
787 |
.map(this::getMemberInfo) |
|
788 |
.collect(Collectors.toSet()), |
|
789 |
expected); |
|
790 |
} |
|
791 |
||
792 |
public void assertKeys(MemberInfo... expected) { |
|
793 |
int index = 0; |
|
794 |
List<Snippet> snippets = getState().snippets(); |
|
795 |
assertEquals(allSnippets.size(), snippets.size()); |
|
796 |
for (Snippet sn : snippets) { |
|
797 |
if (sn.kind().isPersistent && getState().status(sn).isActive) { |
|
798 |
MemberInfo actual = getMemberInfo(sn); |
|
799 |
MemberInfo exp = expected[index]; |
|
800 |
assertEquals(actual, exp, String.format("Difference in #%d. Expected: %s, actual: %s", |
|
801 |
index, exp, actual)); |
|
802 |
++index; |
|
803 |
} |
|
804 |
} |
|
805 |
} |
|
806 |
||
807 |
public void assertActiveKeys() { |
|
808 |
Collection<Snippet> expected = getActiveKeys(); |
|
809 |
assertActiveKeys(expected.toArray(new Snippet[expected.size()])); |
|
810 |
} |
|
811 |
||
812 |
public void assertActiveKeys(Snippet... expected) { |
|
813 |
int index = 0; |
|
814 |
for (Snippet key : getState().snippets()) { |
|
815 |
if (state.status(key).isActive) { |
|
816 |
assertEquals(expected[index], key, String.format("Difference in #%d. Expected: %s, actual: %s", index, key, expected[index])); |
|
817 |
++index; |
|
818 |
} |
|
819 |
} |
|
820 |
} |
|
821 |
||
822 |
private List<Snippet> filterDeclaredKeys(Predicate<Snippet> p) { |
|
823 |
return getActiveKeys().stream() |
|
824 |
.filter(p) |
|
825 |
.collect(Collectors.toList()); |
|
826 |
} |
|
827 |
||
828 |
public void assertVariables() { |
|
829 |
assertEquals(getState().variables(), filterDeclaredKeys((key) -> key instanceof VarSnippet), "Variables"); |
|
830 |
} |
|
831 |
||
832 |
public void assertMethods() { |
|
833 |
assertEquals(getState().methods(), filterDeclaredKeys((key) -> key instanceof MethodSnippet), "Methods"); |
|
834 |
} |
|
835 |
||
836 |
public void assertClasses() { |
|
837 |
assertEquals(getState().types(), filterDeclaredKeys((key) -> key instanceof TypeDeclSnippet), "Classes"); |
|
838 |
} |
|
839 |
||
840 |
public void assertVariables(MemberInfo...expected) { |
|
841 |
assertMembers(getState().variables(), Stream.of(expected).collect(Collectors.toSet())); |
|
842 |
} |
|
843 |
||
844 |
public void assertMethods(MemberInfo...expected) { |
|
845 |
assertMembers(getState().methods(), Stream.of(expected).collect(Collectors.toSet())); |
|
846 |
for (MethodSnippet methodKey : getState().methods()) { |
|
847 |
MemberInfo expectedInfo = null; |
|
848 |
for (MemberInfo info : expected) { |
|
849 |
if (info.name.equals(methodKey.name()) && info.type.equals(methodKey.signature())) { |
|
850 |
expectedInfo = getMemberInfo(methodKey); |
|
851 |
} |
|
852 |
} |
|
853 |
assertNotNull(expectedInfo, "Not found method: " + methodKey.name()); |
|
854 |
int lastIndexOf = expectedInfo.type.lastIndexOf(')'); |
|
855 |
assertEquals(methodKey.parameterTypes(), expectedInfo.type.substring(1, lastIndexOf), "Parameter types"); |
|
856 |
} |
|
857 |
} |
|
858 |
||
859 |
public void assertClasses(MemberInfo...expected) { |
|
860 |
assertMembers(getState().types(), Stream.of(expected).collect(Collectors.toSet())); |
|
861 |
} |
|
862 |
||
863 |
public void assertCompletion(String code, String... expected) { |
|
864 |
assertCompletion(code, null, expected); |
|
865 |
} |
|
866 |
||
867 |
public void assertCompletion(String code, Boolean isSmart, String... expected) { |
|
868 |
List<String> completions = computeCompletions(code, isSmart); |
|
869 |
assertEquals(completions, Arrays.asList(expected), "Input: " + code + ", " + completions.toString()); |
|
870 |
} |
|
871 |
||
872 |
public void assertCompletionIncludesExcludes(String code, Set<String> expected, Set<String> notExpected) { |
|
873 |
assertCompletionIncludesExcludes(code, null, expected, notExpected); |
|
874 |
} |
|
875 |
||
876 |
public void assertCompletionIncludesExcludes(String code, Boolean isSmart, Set<String> expected, Set<String> notExpected) { |
|
877 |
List<String> completions = computeCompletions(code, isSmart); |
|
878 |
assertTrue(completions.containsAll(expected), String.valueOf(completions)); |
|
879 |
assertTrue(Collections.disjoint(completions, notExpected), String.valueOf(completions)); |
|
880 |
} |
|
881 |
||
882 |
private List<String> computeCompletions(String code, Boolean isSmart) { |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
883 |
waitIndexingFinished(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
884 |
|
33362 | 885 |
int cursor = code.indexOf('|'); |
886 |
code = code.replace("|", ""); |
|
887 |
assertTrue(cursor > -1, "'|' expected, but not found in: " + code); |
|
888 |
List<Suggestion> completions = |
|
889 |
getAnalysis().completionSuggestions(code, cursor, new int[1]); //XXX: ignoring anchor for now |
|
890 |
return completions.stream() |
|
891 |
.filter(s -> isSmart == null || isSmart == s.isSmart) |
|
892 |
.map(s -> s.continuation) |
|
893 |
.distinct() |
|
894 |
.collect(Collectors.toList()); |
|
895 |
} |
|
896 |
||
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
897 |
public void assertInferredType(String code, String expectedType) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
898 |
String inferredType = getAnalysis().analyzeType(code, code.length()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
899 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
900 |
assertEquals(inferredType, expectedType, "Input: " + code + ", " + inferredType); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
901 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
902 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
903 |
public void assertInferredFQNs(String code, String... fqns) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
904 |
assertInferredFQNs(code, code.length(), false, fqns); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
905 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
906 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
907 |
public void assertInferredFQNs(String code, int simpleNameLen, boolean resolvable, String... fqns) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
908 |
waitIndexingFinished(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
909 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
910 |
QualifiedNames candidates = getAnalysis().listQualifiedNames(code, code.length()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
911 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
912 |
assertEquals(candidates.getNames(), Arrays.asList(fqns), "Input: " + code + ", candidates=" + candidates.getNames()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
913 |
assertEquals(candidates.getSimpleNameLength(), simpleNameLen, "Input: " + code + ", simpleNameLen=" + candidates.getSimpleNameLength()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
914 |
assertEquals(candidates.isResolvable(), resolvable, "Input: " + code + ", resolvable=" + candidates.isResolvable()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
915 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
916 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
917 |
protected void waitIndexingFinished() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
918 |
try { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
919 |
Method waitBackgroundTaskFinished = getAnalysis().getClass().getDeclaredMethod("waitBackgroundTaskFinished"); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
920 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
921 |
waitBackgroundTaskFinished.setAccessible(true); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
922 |
waitBackgroundTaskFinished.invoke(getAnalysis()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
923 |
} catch (Exception ex) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
924 |
throw new AssertionError("Cannot wait for indexing end.", ex); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
925 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
926 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
927 |
|
33362 | 928 |
public void assertDocumentation(String code, String... expected) { |
929 |
int cursor = code.indexOf('|'); |
|
930 |
code = code.replace("|", ""); |
|
931 |
assertTrue(cursor > -1, "'|' expected, but not found in: " + code); |
|
932 |
String documentation = getAnalysis().documentation(code, cursor); |
|
933 |
Set<String> docSet = Stream.of(documentation.split("\r?\n")).collect(Collectors.toSet()); |
|
934 |
Set<String> expectedSet = Stream.of(expected).collect(Collectors.toSet()); |
|
935 |
assertEquals(docSet, expectedSet, "Input: " + code); |
|
936 |
} |
|
937 |
||
938 |
public enum ClassType { |
|
939 |
CLASS("CLASS_SUBKIND") { |
|
940 |
@Override |
|
941 |
public String toString() { |
|
942 |
return "class"; |
|
943 |
} |
|
944 |
}, |
|
945 |
ENUM("ENUM_SUBKIND") { |
|
946 |
@Override |
|
947 |
public String toString() { |
|
948 |
return "enum"; |
|
949 |
} |
|
950 |
}, |
|
951 |
INTERFACE("INTERFACE_SUBKIND") { |
|
952 |
@Override |
|
953 |
public String toString() { |
|
954 |
return "interface"; |
|
955 |
} |
|
956 |
}, |
|
957 |
ANNOTATION("ANNOTATION_TYPE_SUBKIND") { |
|
958 |
@Override |
|
959 |
public String toString() { |
|
960 |
return "@interface"; |
|
961 |
} |
|
962 |
}; |
|
963 |
||
964 |
private final String classType; |
|
965 |
||
966 |
ClassType(String classType) { |
|
967 |
this.classType = classType; |
|
968 |
} |
|
969 |
||
970 |
public String getClassType() { |
|
971 |
return classType; |
|
972 |
} |
|
973 |
||
974 |
@Override |
|
975 |
public abstract String toString(); |
|
976 |
} |
|
977 |
||
978 |
public static MemberInfo variable(String type, String name) { |
|
979 |
return new MemberInfo(type, name); |
|
980 |
} |
|
981 |
||
982 |
public static MemberInfo method(String signature, String name) { |
|
983 |
return new MemberInfo(signature, name); |
|
984 |
} |
|
985 |
||
986 |
public static MemberInfo clazz(ClassType classType, String className) { |
|
987 |
return new MemberInfo(classType.getClassType(), className); |
|
988 |
} |
|
989 |
||
990 |
public static class MemberInfo { |
|
991 |
public final String type; |
|
992 |
public final String name; |
|
993 |
||
994 |
public MemberInfo(String type, String name) { |
|
995 |
this.type = type; |
|
996 |
this.name = name; |
|
997 |
} |
|
998 |
||
999 |
@Override |
|
1000 |
public int hashCode() { |
|
1001 |
return type.hashCode() + 3 * name.hashCode(); |
|
1002 |
} |
|
1003 |
||
1004 |
@Override |
|
1005 |
public boolean equals(Object o) { |
|
1006 |
if (o instanceof MemberInfo) { |
|
1007 |
MemberInfo other = (MemberInfo) o; |
|
1008 |
return type.equals(other.type) && name.equals(other.name); |
|
1009 |
} |
|
1010 |
return false; |
|
1011 |
} |
|
1012 |
||
1013 |
@Override |
|
1014 |
public String toString() { |
|
1015 |
return String.format("%s %s", type, name); |
|
1016 |
} |
|
1017 |
} |
|
1018 |
||
1019 |
public MemberInfo getMemberInfo(Snippet key) { |
|
1020 |
SubKind subkind = key.subKind(); |
|
1021 |
switch (subkind) { |
|
1022 |
case CLASS_SUBKIND: |
|
1023 |
case INTERFACE_SUBKIND: |
|
1024 |
case ENUM_SUBKIND: |
|
1025 |
case ANNOTATION_TYPE_SUBKIND: |
|
1026 |
return new MemberInfo(subkind.name(), ((DeclarationSnippet) key).name()); |
|
1027 |
case METHOD_SUBKIND: |
|
1028 |
MethodSnippet method = (MethodSnippet) key; |
|
1029 |
return new MemberInfo(method.signature(), method.name()); |
|
1030 |
case VAR_DECLARATION_SUBKIND: |
|
1031 |
case VAR_DECLARATION_WITH_INITIALIZER_SUBKIND: |
|
1032 |
case TEMP_VAR_EXPRESSION_SUBKIND: |
|
1033 |
VarSnippet var = (VarSnippet) key; |
|
1034 |
return new MemberInfo(var.typeName(), var.name()); |
|
1035 |
default: |
|
1036 |
throw new AssertionError("Unknown snippet : " + key.kind() + " in expression " + key.toString()); |
|
1037 |
} |
|
1038 |
} |
|
1039 |
||
1040 |
public String diagnosticsToString(List<Diag> diagnostics) { |
|
1041 |
StringWriter writer = new StringWriter(); |
|
1042 |
for (Diag diag : diagnostics) { |
|
1043 |
writer.write("Error --\n"); |
|
1044 |
for (String line : diag.getMessage(null).split("\\r?\\n")) { |
|
1045 |
writer.write(String.format("%s\n", line)); |
|
1046 |
} |
|
1047 |
} |
|
1048 |
return writer.toString().replace("\n", System.lineSeparator()); |
|
1049 |
} |
|
1050 |
||
1051 |
public boolean hasFatalError(List<Diag> diagnostics) { |
|
1052 |
for (Diag diag : diagnostics) { |
|
1053 |
if (diag.isError()) { |
|
1054 |
return true; |
|
1055 |
} |
|
1056 |
} |
|
1057 |
return false; |
|
1058 |
} |
|
1059 |
||
1060 |
public static EventChain chain(STEInfo mainInfo, STEInfo... updates) { |
|
1061 |
return chain(mainInfo, IGNORE_VALUE, null, updates); |
|
1062 |
} |
|
1063 |
||
1064 |
public static EventChain chain(STEInfo mainInfo, String value, Class<? extends Throwable> exceptionClass, STEInfo... updates) { |
|
1065 |
return new EventChain(mainInfo, value, exceptionClass, updates); |
|
1066 |
} |
|
1067 |
||
1068 |
public static STEInfo ste(Snippet key, Status previousStatus, Status status, |
|
1069 |
Boolean isSignatureChange, Snippet causeKey) { |
|
1070 |
return new STEInfo(key, previousStatus, status, isSignatureChange, causeKey); |
|
1071 |
} |
|
1072 |
||
1073 |
public static STEInfo added(Status status) { |
|
1074 |
return new STEInfo(MAIN_SNIPPET, NONEXISTENT, status, status.isDefined, null); |
|
1075 |
} |
|
1076 |
||
1077 |
public static class EventChain { |
|
1078 |
public final STEInfo mainInfo; |
|
1079 |
public final STEInfo[] updates; |
|
1080 |
public final String value; |
|
1081 |
public final Class<? extends Throwable> exceptionClass; |
|
1082 |
||
1083 |
public EventChain(STEInfo mainInfo, String value, Class<? extends Throwable> exceptionClass, STEInfo... updates) { |
|
1084 |
this.mainInfo = mainInfo; |
|
1085 |
this.updates = updates; |
|
1086 |
this.value = value; |
|
1087 |
this.exceptionClass = exceptionClass; |
|
1088 |
} |
|
1089 |
} |
|
1090 |
||
1091 |
public static class STEInfo { |
|
1092 |
||
1093 |
STEInfo(Snippet snippet, Status previousStatus, Status status, |
|
1094 |
Boolean isSignatureChange, Snippet causeSnippet) { |
|
1095 |
this.snippet = snippet; |
|
1096 |
this.previousStatus = previousStatus; |
|
1097 |
this.status = status; |
|
1098 |
this.checkIsSignatureChange = isSignatureChange != null; |
|
1099 |
this.isSignatureChange = checkIsSignatureChange ? isSignatureChange : false; |
|
1100 |
this.causeSnippet = causeSnippet; |
|
1101 |
assertTrue(snippet != null, "Bad test set-up. The match snippet must not be null"); |
|
1102 |
} |
|
1103 |
||
1104 |
final Snippet snippet; |
|
1105 |
final Status previousStatus; |
|
1106 |
final Status status; |
|
1107 |
final boolean isSignatureChange; |
|
1108 |
final Snippet causeSnippet; |
|
1109 |
||
1110 |
final boolean checkIsSignatureChange; |
|
1111 |
public Snippet snippet() { |
|
1112 |
return snippet; |
|
1113 |
} |
|
1114 |
public Status previousStatus() { |
|
1115 |
return previousStatus; |
|
1116 |
} |
|
1117 |
public Status status() { |
|
1118 |
return status; |
|
1119 |
} |
|
1120 |
public boolean isSignatureChange() { |
|
1121 |
if (!checkIsSignatureChange) { |
|
1122 |
throw new IllegalStateException("isSignatureChange value is undefined"); |
|
1123 |
} |
|
1124 |
return isSignatureChange; |
|
1125 |
} |
|
1126 |
public Snippet causeSnippet() { |
|
1127 |
return causeSnippet; |
|
1128 |
} |
|
1129 |
public String value() { |
|
1130 |
return null; |
|
1131 |
} |
|
1132 |
public Exception exception() { |
|
1133 |
return null; |
|
1134 |
} |
|
1135 |
||
1136 |
public void assertMatch(SnippetEvent ste, Snippet mainSnippet) { |
|
1137 |
assertKeyMatch(ste, ste.snippet(), snippet(), mainSnippet); |
|
1138 |
assertStatusMatch(ste, ste.previousStatus(), previousStatus()); |
|
1139 |
assertStatusMatch(ste, ste.status(), status()); |
|
1140 |
if (checkIsSignatureChange) { |
|
1141 |
assertEquals(ste.isSignatureChange(), isSignatureChange(), |
|
1142 |
"Expected " + |
|
1143 |
(isSignatureChange()? "" : "no ") + |
|
1144 |
"signature-change, got: " + |
|
1145 |
(ste.isSignatureChange()? "" : "no ") + |
|
1146 |
"signature-change" + |
|
1147 |
"\n expected-event: " + this + "\n got-event: " + toString(ste)); |
|
1148 |
} |
|
1149 |
assertKeyMatch(ste, ste.causeSnippet(), causeSnippet(), mainSnippet); |
|
1150 |
} |
|
1151 |
||
1152 |
private void assertKeyMatch(SnippetEvent ste, Snippet sn, Snippet expected, Snippet mainSnippet) { |
|
1153 |
Snippet testKey = expected; |
|
1154 |
if (testKey != null) { |
|
1155 |
if (expected == MAIN_SNIPPET) { |
|
1156 |
assertNotNull(mainSnippet, "MAIN_SNIPPET used, test must pass value to assertMatch"); |
|
1157 |
testKey = mainSnippet; |
|
1158 |
} |
|
1159 |
if (ste.causeSnippet() == null && ste.status() != DROPPED && expected != MAIN_SNIPPET) { |
|
1160 |
// Source change, always new snippet -- only match id() |
|
1161 |
assertTrue(sn != testKey, |
|
1162 |
"Main-event: Expected new snippet to be != : " + testKey |
|
1163 |
+ "\n got-event: " + toString(ste)); |
|
1164 |
assertEquals(sn.id(), testKey.id(), "Expected IDs to match: " + testKey + ", got: " + sn |
|
1165 |
+ "\n expected-event: " + this + "\n got-event: " + toString(ste)); |
|
1166 |
} else { |
|
1167 |
assertEquals(sn, testKey, "Expected key to be: " + testKey + ", got: " + sn |
|
1168 |
+ "\n expected-event: " + this + "\n got-event: " + toString(ste)); |
|
1169 |
} |
|
1170 |
} |
|
1171 |
} |
|
1172 |
||
1173 |
private void assertStatusMatch(SnippetEvent ste, Status status, Status expected) { |
|
1174 |
if (expected != null) { |
|
1175 |
assertEquals(status, expected, "Expected status to be: " + expected + ", got: " + status + |
|
1176 |
"\n expected-event: " + this + "\n got-event: " + toString(ste)); |
|
1177 |
} |
|
1178 |
} |
|
1179 |
||
1180 |
@Override |
|
1181 |
public String toString() { |
|
1182 |
return "STEInfo key: " + |
|
1183 |
(snippet()==MAIN_SNIPPET? "MAIN_SNIPPET" : (snippet()==null? "ignore" : snippet().id())) + |
|
1184 |
" before: " + previousStatus() + |
|
1185 |
" status: " + status() + " sig: " + isSignatureChange() + |
|
1186 |
" cause: " + (causeSnippet()==null? "null" : causeSnippet().id()); |
|
1187 |
} |
|
1188 |
||
1189 |
private String toString(SnippetEvent ste) { |
|
1190 |
return "key: " + (ste.snippet()==MAIN_SNIPPET? "MAIN_SNIPPET" : ste.snippet().id()) + " before: " + ste.previousStatus() |
|
1191 |
+ " status: " + ste.status() + " sig: " + ste.isSignatureChange() |
|
1192 |
+ " cause: " + ste.causeSnippet(); |
|
1193 |
} |
|
1194 |
} |
|
1195 |
} |