author | shinyafox |
Thu, 12 Nov 2015 08:48:42 +0100 | |
changeset 33714 | 8064f484590e |
parent 33362 | 65ec6de1d6b4 |
child 34570 | 8a8f52a733dd |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 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.OutputStream; |
|
26 |
import java.io.PrintStream; |
|
27 |
import java.util.ArrayList; |
|
28 |
import java.util.Arrays; |
|
29 |
import java.util.HashMap; |
|
30 |
import java.util.List; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.function.Consumer; |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
33 |
import java.util.function.Function; |
33362 | 34 |
import java.util.function.Predicate; |
35 |
import java.util.regex.Matcher; |
|
36 |
import java.util.regex.Pattern; |
|
37 |
import java.util.stream.Collectors; |
|
38 |
import java.util.stream.Stream; |
|
39 |
||
40 |
import jdk.internal.jshell.tool.JShellTool; |
|
41 |
import jdk.jshell.SourceCodeAnalysis.Suggestion; |
|
42 |
||
43 |
import static org.testng.Assert.assertEquals; |
|
44 |
import static org.testng.Assert.assertNotNull; |
|
45 |
import static org.testng.Assert.assertTrue; |
|
46 |
import static org.testng.Assert.fail; |
|
47 |
||
48 |
public class ReplToolTesting { |
|
49 |
||
50 |
private final static String DEFAULT_STARTUP_MESSAGE = "| Welcome to"; |
|
51 |
||
52 |
private WaitingTestingInputStream cmdin = null; |
|
53 |
private ByteArrayOutputStream cmdout = null; |
|
54 |
private ByteArrayOutputStream cmderr = null; |
|
55 |
private PromptedCommandOutputStream console = null; |
|
56 |
private TestingInputStream userin = null; |
|
57 |
private ByteArrayOutputStream userout = null; |
|
58 |
private ByteArrayOutputStream usererr = null; |
|
59 |
||
60 |
private List<MemberInfo> keys; |
|
61 |
private Map<String, VariableInfo> variables; |
|
62 |
private Map<String, MethodInfo> methods; |
|
63 |
private Map<String, ClassInfo> classes; |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
64 |
private Map<String, ImportInfo> imports; |
33362 | 65 |
private boolean isDefaultStartUp = true; |
66 |
||
67 |
public JShellTool repl = null; |
|
68 |
||
69 |
public interface ReplTest { |
|
70 |
void run(boolean after); |
|
71 |
} |
|
72 |
||
73 |
public void setCommandInput(String s) { |
|
74 |
cmdin.setInput(s); |
|
75 |
} |
|
76 |
||
77 |
public final static Pattern idPattern = Pattern.compile("^\\s+(\\d+)"); |
|
78 |
public Consumer<String> assertList() { |
|
79 |
return s -> { |
|
80 |
List<String> lines = Stream.of(s.split("\n")) |
|
81 |
.filter(l -> !l.isEmpty()) |
|
82 |
.collect(Collectors.toList()); |
|
83 |
int previousId = Integer.MIN_VALUE; |
|
84 |
assertEquals(lines.size(), keys.size(), "Number of keys"); |
|
85 |
for (int i = 0; i < lines.size(); ++i) { |
|
86 |
String line = lines.get(i); |
|
87 |
Matcher matcher = idPattern.matcher(line); |
|
88 |
assertTrue(matcher.find(), "Snippet id not found: " + line); |
|
89 |
String src = keys.get(i).getSource(); |
|
90 |
assertTrue(line.endsWith(src), "Line '" + line + "' does not end with: " + src); |
|
91 |
int id = Integer.parseInt(matcher.group(1)); |
|
92 |
assertTrue(previousId < id, |
|
93 |
String.format("The previous id is not less than the next one: previous: %d, next: %d", |
|
94 |
previousId, id)); |
|
95 |
previousId = id; |
|
96 |
} |
|
97 |
}; |
|
98 |
} |
|
99 |
||
100 |
private final static Pattern extractPattern = Pattern.compile("^\\| *(.*)$"); |
|
101 |
private Consumer<String> assertMembers(String message, Map<String, ? extends MemberInfo> set) { |
|
102 |
return s -> { |
|
103 |
List<String> lines = Stream.of(s.split("\n")) |
|
104 |
.filter(l -> !l.isEmpty()) |
|
105 |
.collect(Collectors.toList()); |
|
106 |
assertEquals(lines.size(), set.size(), message + " : expected: " + set.keySet() + "\ngot:\n" + lines); |
|
107 |
for (String line : lines) { |
|
108 |
Matcher matcher = extractPattern.matcher(line); |
|
109 |
assertTrue(matcher.find(), line); |
|
110 |
String src = matcher.group(1); |
|
111 |
MemberInfo info = set.get(src); |
|
112 |
assertNotNull(info, "Not found snippet with signature: " + src + ", line: " |
|
113 |
+ line + ", keys: " + set.keySet() + "\n"); |
|
114 |
} |
|
115 |
}; |
|
116 |
} |
|
117 |
||
118 |
public Consumer<String> assertVariables() { |
|
119 |
return assertMembers("Variables", variables); |
|
120 |
} |
|
121 |
||
122 |
public Consumer<String> assertMethods() { |
|
123 |
return assertMembers("Methods", methods); |
|
124 |
} |
|
125 |
||
126 |
public Consumer<String> assertClasses() { |
|
127 |
return assertMembers("Classes", classes); |
|
128 |
} |
|
129 |
||
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
130 |
public Consumer<String> assertImports() { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
131 |
return assertMembers("Imports", imports); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
132 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
133 |
|
33362 | 134 |
public String getCommandOutput() { |
135 |
String s = cmdout.toString(); |
|
136 |
cmdout.reset(); |
|
137 |
return s; |
|
138 |
} |
|
139 |
||
140 |
public String getCommandErrorOutput() { |
|
141 |
String s = cmderr.toString(); |
|
142 |
cmderr.reset(); |
|
143 |
return s; |
|
144 |
} |
|
145 |
||
146 |
public void setUserInput(String s) { |
|
147 |
userin.setInput(s); |
|
148 |
} |
|
149 |
||
150 |
public String getUserOutput() { |
|
151 |
String s = userout.toString(); |
|
152 |
userout.reset(); |
|
153 |
return s; |
|
154 |
} |
|
155 |
||
156 |
public String getUserErrorOutput() { |
|
157 |
String s = usererr.toString(); |
|
158 |
usererr.reset(); |
|
159 |
return s; |
|
160 |
} |
|
161 |
||
162 |
public void test(ReplTest... tests) { |
|
163 |
test(new String[0], tests); |
|
164 |
} |
|
165 |
||
166 |
public void test(String[] args, ReplTest... tests) { |
|
167 |
test(true, args, tests); |
|
168 |
} |
|
169 |
||
170 |
public void test(boolean isDefaultStartUp, String[] args, ReplTest... tests) { |
|
171 |
test(isDefaultStartUp, args, DEFAULT_STARTUP_MESSAGE, tests); |
|
172 |
} |
|
173 |
||
174 |
public void test(boolean isDefaultStartUp, String[] args, String startUpMessage, ReplTest... tests) { |
|
175 |
this.isDefaultStartUp = isDefaultStartUp; |
|
176 |
initSnippets(); |
|
177 |
ReplTest[] wtests = new ReplTest[tests.length + 3]; |
|
178 |
wtests[0] = a -> assertCommandCheckOutput(a, "<start>", |
|
179 |
s -> assertTrue(s.startsWith(startUpMessage), "Expected start-up message '" + startUpMessage + "' Got: " + s)); |
|
180 |
wtests[1] = a -> assertCommand(a, "/debug 0", null); |
|
181 |
System.arraycopy(tests, 0, wtests, 2, tests.length); |
|
182 |
wtests[tests.length + 2] = a -> assertCommand(a, "/exit", null); |
|
183 |
testRaw(args, wtests); |
|
184 |
} |
|
185 |
||
186 |
private void initSnippets() { |
|
187 |
keys = new ArrayList<>(); |
|
188 |
variables = new HashMap<>(); |
|
189 |
methods = new HashMap<>(); |
|
190 |
classes = new HashMap<>(); |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
191 |
imports = new HashMap<>(); |
33362 | 192 |
if (isDefaultStartUp) { |
193 |
methods.put("printf (String,Object...)void", |
|
194 |
new MethodInfo("", "(String,Object...)void", "printf")); |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
195 |
imports.putAll( |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
196 |
Stream.of( |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
197 |
"java.util.*", |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
198 |
"java.io.*", |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
199 |
"java.math.*", |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
200 |
"java.net.*", |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
201 |
"java.util.concurrent.*", |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
202 |
"java.util.prefs.*", |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
203 |
"java.util.regex.*") |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
204 |
.map(s -> new ImportInfo("", "", s)) |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
205 |
.collect(Collectors.toMap(Object::toString, Function.identity()))); |
33362 | 206 |
} |
207 |
} |
|
208 |
||
209 |
public void testRaw(String[] args, ReplTest... tests) { |
|
210 |
cmdin = new WaitingTestingInputStream(); |
|
211 |
cmdout = new ByteArrayOutputStream(); |
|
212 |
cmderr = new ByteArrayOutputStream(); |
|
213 |
console = new PromptedCommandOutputStream(tests); |
|
214 |
userin = new TestingInputStream(); |
|
215 |
userout = new ByteArrayOutputStream(); |
|
216 |
usererr = new ByteArrayOutputStream(); |
|
217 |
repl = new JShellTool( |
|
218 |
cmdin, |
|
219 |
new PrintStream(cmdout), |
|
220 |
new PrintStream(cmderr), |
|
221 |
new PrintStream(console), |
|
222 |
userin, |
|
223 |
new PrintStream(userout), |
|
224 |
new PrintStream(usererr)); |
|
225 |
repl.testPrompt = true; |
|
226 |
try { |
|
227 |
repl.start(args); |
|
228 |
} catch (Exception ex) { |
|
229 |
fail("Repl tool died with exception", ex); |
|
230 |
} |
|
231 |
// perform internal consistency checks on state, if desired |
|
232 |
String cos = getCommandOutput(); |
|
233 |
String ceos = getCommandErrorOutput(); |
|
234 |
String uos = getUserOutput(); |
|
235 |
String ueos = getUserErrorOutput(); |
|
236 |
assertTrue((cos.isEmpty() || cos.startsWith("| Goodbye")), |
|
237 |
"Expected a goodbye, but got: " + cos); |
|
238 |
assertTrue(ceos.isEmpty(), "Expected empty error output, got: " + ceos); |
|
239 |
assertTrue(uos.isEmpty(), "Expected empty output, got: " + uos); |
|
240 |
assertTrue(ueos.isEmpty(), "Expected empty error output, got: " + ueos); |
|
241 |
} |
|
242 |
||
243 |
public void assertReset(boolean after, String cmd) { |
|
244 |
assertCommand(after, cmd, "| Resetting state.\n"); |
|
245 |
initSnippets(); |
|
246 |
} |
|
247 |
||
248 |
public void evaluateExpression(boolean after, String type, String expr, String value) { |
|
249 |
String output = String.format("\\| *Expression values is: %s\n|" + |
|
250 |
" *.*temporary variable (\\$\\d+) of type %s", value, type); |
|
251 |
Pattern outputPattern = Pattern.compile(output); |
|
252 |
assertCommandCheckOutput(after, expr, s -> { |
|
253 |
Matcher matcher = outputPattern.matcher(s); |
|
254 |
assertTrue(matcher.find(), "Output: '" + s + "' does not fit pattern: '" + output + "'"); |
|
255 |
String name = matcher.group(1); |
|
256 |
VariableInfo tempVar = new TempVariableInfo(expr, type, name, value); |
|
257 |
variables.put(tempVar.toString(), tempVar); |
|
258 |
addKey(after, tempVar); |
|
259 |
}); |
|
260 |
} |
|
261 |
||
262 |
public void loadVariable(boolean after, String type, String name) { |
|
263 |
loadVariable(after, type, name, null, null); |
|
264 |
} |
|
265 |
||
266 |
public void loadVariable(boolean after, String type, String name, String expr, String value) { |
|
267 |
String src = expr == null |
|
268 |
? String.format("%s %s", type, name) |
|
269 |
: String.format("%s %s = %s", type, name, expr); |
|
270 |
VariableInfo var = expr == null |
|
271 |
? new VariableInfo(src, type, name) |
|
272 |
: new VariableInfo(src, type, name, value); |
|
273 |
addKey(after, var, variables); |
|
274 |
addKey(after, var); |
|
275 |
} |
|
276 |
||
277 |
public void assertVariable(boolean after, String type, String name) { |
|
278 |
assertVariable(after, type, name, null, null); |
|
279 |
} |
|
280 |
||
281 |
public void assertVariable(boolean after, String type, String name, String expr, String value) { |
|
282 |
String src = expr == null |
|
283 |
? String.format("%s %s", type, name) |
|
284 |
: String.format("%s %s = %s", type, name, expr); |
|
285 |
VariableInfo var = expr == null |
|
286 |
? new VariableInfo(src, type, name) |
|
287 |
: new VariableInfo(src, type, name, value); |
|
288 |
assertCommandCheckOutput(after, src, var.checkOutput()); |
|
289 |
addKey(after, var, variables); |
|
290 |
addKey(after, var); |
|
291 |
} |
|
292 |
||
293 |
public void loadMethod(boolean after, String src, String signature, String name) { |
|
294 |
MethodInfo method = new MethodInfo(src, signature, name); |
|
295 |
addKey(after, method, methods); |
|
296 |
addKey(after, method); |
|
297 |
} |
|
298 |
||
299 |
public void assertMethod(boolean after, String src, String signature, String name) { |
|
300 |
MethodInfo method = new MethodInfo(src, signature, name); |
|
301 |
assertCommandCheckOutput(after, src, method.checkOutput()); |
|
302 |
addKey(after, method, methods); |
|
303 |
addKey(after, method); |
|
304 |
} |
|
305 |
||
306 |
public void loadClass(boolean after, String src, String type, String name) { |
|
307 |
ClassInfo clazz = new ClassInfo(src, type, name); |
|
308 |
addKey(after, clazz, classes); |
|
309 |
addKey(after, clazz); |
|
310 |
} |
|
311 |
||
312 |
public void assertClass(boolean after, String src, String type, String name) { |
|
313 |
ClassInfo clazz = new ClassInfo(src, type, name); |
|
314 |
assertCommandCheckOutput(after, src, clazz.checkOutput()); |
|
315 |
addKey(after, clazz, classes); |
|
316 |
addKey(after, clazz); |
|
317 |
} |
|
318 |
||
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
319 |
public void loadImport(boolean after, String src, String type, String name) { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
320 |
ImportInfo i = new ImportInfo(src, type, name); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
321 |
addKey(after, i, imports); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
322 |
addKey(after, i); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
323 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
324 |
|
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
325 |
public void assertImport(boolean after, String src, String type, String name) { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
326 |
ImportInfo i = new ImportInfo(src, type, name); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
327 |
assertCommandCheckOutput(after, src, i.checkOutput()); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
328 |
addKey(after, i, imports); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
329 |
addKey(after, i); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
330 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
331 |
|
33362 | 332 |
private <T extends MemberInfo> void addKey(boolean after, T memberInfo, Map<String, T> map) { |
333 |
if (after) { |
|
334 |
map.entrySet().removeIf(e -> e.getValue().equals(memberInfo)); |
|
335 |
map.put(memberInfo.toString(), memberInfo); |
|
336 |
} |
|
337 |
} |
|
338 |
||
339 |
private <T extends MemberInfo> void addKey(boolean after, T memberInfo) { |
|
340 |
if (after) { |
|
341 |
for (int i = 0; i < keys.size(); ++i) { |
|
342 |
MemberInfo m = keys.get(i); |
|
343 |
if (m.equals(memberInfo)) { |
|
344 |
keys.set(i, memberInfo); |
|
345 |
return; |
|
346 |
} |
|
347 |
} |
|
348 |
keys.add(memberInfo); |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
private void dropKey(boolean after, String cmd, String name, Map<String, ? extends MemberInfo> map) { |
|
353 |
assertCommand(after, cmd, ""); |
|
354 |
if (after) { |
|
355 |
map.remove(name); |
|
356 |
for (int i = 0; i < keys.size(); ++i) { |
|
357 |
MemberInfo m = keys.get(i); |
|
358 |
if (m.toString().equals(name)) { |
|
359 |
keys.remove(i); |
|
360 |
return; |
|
361 |
} |
|
362 |
} |
|
363 |
throw new AssertionError("Key not found: " + name + ", keys: " + keys); |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
public void dropVariable(boolean after, String cmd, String name) { |
|
368 |
dropKey(after, cmd, name, variables); |
|
369 |
} |
|
370 |
||
371 |
public void dropMethod(boolean after, String cmd, String name) { |
|
372 |
dropKey(after, cmd, name, methods); |
|
373 |
} |
|
374 |
||
375 |
public void dropClass(boolean after, String cmd, String name) { |
|
376 |
dropKey(after, cmd, name, classes); |
|
377 |
} |
|
378 |
||
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
379 |
public void dropImport(boolean after, String cmd, String name) { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
380 |
dropKey(after, cmd, name, imports); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
381 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
382 |
|
33362 | 383 |
public void assertCommand(boolean after, String cmd, String out) { |
384 |
assertCommand(after, cmd, out, "", null, "", ""); |
|
385 |
} |
|
386 |
||
387 |
public void assertCommandCheckOutput(boolean after, String cmd, Consumer<String> check) { |
|
388 |
if (!after) { |
|
389 |
assertCommand(false, cmd, null); |
|
390 |
} else { |
|
391 |
String got = getCommandOutput(); |
|
392 |
check.accept(got); |
|
393 |
assertCommand(true, cmd, null); |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
public void assertCommand(boolean after, String cmd, String out, String err, |
|
398 |
String userinput, String print, String usererr) { |
|
399 |
if (!after) { |
|
400 |
if (userinput != null) { |
|
401 |
setUserInput(userinput); |
|
402 |
} |
|
403 |
setCommandInput(cmd + "\n"); |
|
404 |
} else { |
|
405 |
assertOutput(getCommandOutput(), out, "command"); |
|
406 |
assertOutput(getCommandErrorOutput(), err, "command error"); |
|
407 |
assertOutput(getUserOutput(), print, "user"); |
|
408 |
assertOutput(getUserErrorOutput(), usererr, "user error"); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
public void assertCompletion(boolean after, String code, boolean isSmart, String... expected) { |
|
413 |
if (!after) { |
|
414 |
setCommandInput("\n"); |
|
415 |
} else { |
|
416 |
assertCompletion(code, isSmart, expected); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
public void assertCompletion(String code, boolean isSmart, String... expected) { |
|
421 |
List<String> completions = computeCompletions(code, isSmart); |
|
422 |
assertEquals(completions, Arrays.asList(expected), "Command: " + code + ", output: " + |
|
423 |
completions.toString()); |
|
424 |
} |
|
425 |
||
426 |
private List<String> computeCompletions(String code, boolean isSmart) { |
|
427 |
JShellTool repl = this.repl != null ? this.repl |
|
428 |
: new JShellTool(null, null, null, null, null, null, null); |
|
429 |
int cursor = code.indexOf('|'); |
|
430 |
code = code.replace("|", ""); |
|
431 |
assertTrue(cursor > -1, "'|' not found: " + code); |
|
432 |
List<Suggestion> completions = |
|
433 |
repl.commandCompletionSuggestions(code, cursor, new int[1]); //XXX: ignoring anchor for now |
|
434 |
return completions.stream() |
|
435 |
.filter(s -> isSmart == s.isSmart) |
|
436 |
.map(s -> s.continuation) |
|
437 |
.distinct() |
|
438 |
.collect(Collectors.toList()); |
|
439 |
} |
|
440 |
||
441 |
public Consumer<String> assertStartsWith(String prefix) { |
|
442 |
return (output) -> assertTrue(output.startsWith(prefix), "Output: \'" + output + "' does not start with: " + prefix); |
|
443 |
} |
|
444 |
||
445 |
public void assertOutput(String got, String expected, String kind) { |
|
446 |
if (expected != null) { |
|
447 |
assertEquals(got, expected, "Kind: " + kind + ".\n"); |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
public static abstract class MemberInfo { |
|
452 |
public final String source; |
|
453 |
public final String type; |
|
454 |
public final String name; |
|
455 |
||
456 |
public MemberInfo(String source, String type, String name) { |
|
457 |
this.source = source; |
|
458 |
this.type = type; |
|
459 |
this.name = name; |
|
460 |
} |
|
461 |
||
462 |
@Override |
|
463 |
public int hashCode() { |
|
464 |
return name.hashCode(); |
|
465 |
} |
|
466 |
||
467 |
public abstract Consumer<String> checkOutput(); |
|
468 |
||
469 |
public String getSource() { |
|
470 |
return source; |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
public static class VariableInfo extends MemberInfo { |
|
475 |
||
476 |
public final String value; |
|
477 |
public final String initialValue; |
|
478 |
||
479 |
public VariableInfo(String src, String type, String name) { |
|
480 |
super(src, type, name); |
|
481 |
this.initialValue = null; |
|
482 |
switch (type) { |
|
483 |
case "byte": |
|
484 |
case "short": |
|
485 |
case "int": |
|
486 |
case "long": |
|
487 |
value = "0"; |
|
488 |
break; |
|
489 |
case "boolean": |
|
490 |
value = "false"; |
|
491 |
break; |
|
492 |
case "char": |
|
493 |
value = "''"; |
|
494 |
break; |
|
495 |
case "float": |
|
496 |
case "double": |
|
497 |
value = "0.0"; |
|
498 |
break; |
|
499 |
default: |
|
500 |
value = "null"; |
|
501 |
} |
|
502 |
} |
|
503 |
||
504 |
public VariableInfo(String src, String type, String name, String value) { |
|
505 |
super(src, type, name); |
|
506 |
this.value = value; |
|
507 |
this.initialValue = value; |
|
508 |
} |
|
509 |
||
510 |
@Override |
|
511 |
public Consumer<String> checkOutput() { |
|
512 |
String pattern = String.format("\\| *\\w+ variable %s of type %s", name, type); |
|
513 |
if (initialValue != null) { |
|
514 |
pattern += " with initial value " + initialValue; |
|
515 |
} |
|
516 |
Predicate<String> checkOutput = Pattern.compile(pattern).asPredicate(); |
|
517 |
final String finalPattern = pattern; |
|
518 |
return output -> assertTrue(checkOutput.test(output), |
|
519 |
"Output: " + output + " does not fit pattern: " + finalPattern); |
|
520 |
} |
|
521 |
||
522 |
@Override |
|
523 |
public boolean equals(Object o) { |
|
524 |
if (o instanceof VariableInfo) { |
|
525 |
VariableInfo v = (VariableInfo) o; |
|
526 |
return name.equals(v.name); |
|
527 |
} |
|
528 |
return false; |
|
529 |
} |
|
530 |
||
531 |
@Override |
|
532 |
public String toString() { |
|
533 |
return String.format("%s %s = %s", type, name, value); |
|
534 |
} |
|
535 |
||
536 |
@Override |
|
537 |
public String getSource() { |
|
538 |
String src = super.getSource(); |
|
539 |
return src.endsWith(";") ? src : src + ";"; |
|
540 |
} |
|
541 |
} |
|
542 |
||
543 |
public static class TempVariableInfo extends VariableInfo { |
|
544 |
||
545 |
public TempVariableInfo(String src, String type, String name, String value) { |
|
546 |
super(src, type, name, value); |
|
547 |
} |
|
548 |
||
549 |
@Override |
|
550 |
public String getSource() { |
|
551 |
return source; |
|
552 |
} |
|
553 |
} |
|
554 |
||
555 |
public static class MethodInfo extends MemberInfo { |
|
556 |
||
557 |
public final String signature; |
|
558 |
||
559 |
public MethodInfo(String source, String signature, String name) { |
|
560 |
super(source, signature.substring(0, signature.lastIndexOf(')') + 1), name); |
|
561 |
this.signature = signature; |
|
562 |
} |
|
563 |
||
564 |
@Override |
|
565 |
public Consumer<String> checkOutput() { |
|
566 |
String expectedOutput = String.format("\\| *\\w+ method %s", name); |
|
567 |
Predicate<String> checkOutput = Pattern.compile(expectedOutput).asPredicate(); |
|
568 |
return s -> assertTrue(checkOutput.test(s), "Expected: '" + expectedOutput + "', actual: " + s); |
|
569 |
} |
|
570 |
||
571 |
||
572 |
@Override |
|
573 |
public boolean equals(Object o) { |
|
574 |
if (o instanceof MemberInfo) { |
|
575 |
MemberInfo m = (MemberInfo) o; |
|
576 |
return name.equals(m.name) && type.equals(m.type); |
|
577 |
} |
|
578 |
return false; |
|
579 |
} |
|
580 |
||
581 |
@Override |
|
582 |
public String toString() { |
|
583 |
return String.format("%s %s", name, signature); |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
public static class ClassInfo extends MemberInfo { |
|
588 |
||
589 |
public ClassInfo(String source, String type, String name) { |
|
590 |
super(source, type, name); |
|
591 |
} |
|
592 |
||
593 |
@Override |
|
594 |
public Consumer<String> checkOutput() { |
|
595 |
String fullType = type.equals("@interface")? "annotation interface" : type; |
|
596 |
String expectedOutput = String.format("\\| *\\w+ %s %s", fullType, name); |
|
597 |
Predicate<String> checkOutput = Pattern.compile(expectedOutput).asPredicate(); |
|
598 |
return s -> assertTrue(checkOutput.test(s), "Expected: '" + expectedOutput + "', actual: " + s); |
|
599 |
} |
|
600 |
||
601 |
@Override |
|
602 |
public boolean equals(Object o) { |
|
603 |
if (o instanceof ClassInfo) { |
|
604 |
ClassInfo c = (ClassInfo) o; |
|
605 |
return name.equals(c.name); |
|
606 |
} |
|
607 |
return false; |
|
608 |
} |
|
609 |
||
610 |
@Override |
|
611 |
public String toString() { |
|
612 |
return String.format("%s %s", type, name); |
|
613 |
} |
|
614 |
} |
|
615 |
||
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
616 |
public static class ImportInfo extends MemberInfo { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
617 |
public ImportInfo(String source, String type, String fullname) { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
618 |
super(source, type, fullname); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
619 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
620 |
|
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
621 |
@Override |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
622 |
public Consumer<String> checkOutput() { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
623 |
return s -> assertTrue("".equals(s), "Expected: '', actual: " + s); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
624 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
625 |
|
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
626 |
@Override |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
627 |
public boolean equals(Object o) { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
628 |
if (o instanceof ImportInfo) { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
629 |
ImportInfo i = (ImportInfo) o; |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
630 |
return name.equals(i.name) && type.equals(i.type); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
631 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
632 |
return false; |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
633 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
634 |
|
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
635 |
@Override |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
636 |
public String toString() { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
637 |
return String.format("import %s%s", type.equals("static") ? "static " : "", name); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
638 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
639 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
640 |
|
33362 | 641 |
class WaitingTestingInputStream extends TestingInputStream { |
642 |
||
643 |
@Override |
|
644 |
synchronized void setInput(String s) { |
|
645 |
super.setInput(s); |
|
646 |
notify(); |
|
647 |
} |
|
648 |
||
649 |
synchronized void waitForInput() { |
|
650 |
boolean interrupted = false; |
|
651 |
try { |
|
652 |
while (available() == 0) { |
|
653 |
try { |
|
654 |
wait(); |
|
655 |
} catch (InterruptedException e) { |
|
656 |
interrupted = true; |
|
657 |
// fall through and retry |
|
658 |
} |
|
659 |
} |
|
660 |
} finally { |
|
661 |
if (interrupted) { |
|
662 |
Thread.currentThread().interrupt(); |
|
663 |
} |
|
664 |
} |
|
665 |
} |
|
666 |
||
667 |
@Override |
|
668 |
public int read() { |
|
669 |
waitForInput(); |
|
670 |
return super.read(); |
|
671 |
} |
|
672 |
||
673 |
@Override |
|
674 |
public int read(byte b[], int off, int len) { |
|
675 |
waitForInput(); |
|
676 |
return super.read(b, off, len); |
|
677 |
} |
|
678 |
} |
|
679 |
||
680 |
class PromptedCommandOutputStream extends OutputStream { |
|
681 |
private final ReplTest[] tests; |
|
682 |
private int index = 0; |
|
683 |
PromptedCommandOutputStream(ReplTest[] tests) { |
|
684 |
this.tests = tests; |
|
685 |
} |
|
686 |
||
687 |
@Override |
|
688 |
public synchronized void write(int b) { |
|
689 |
if (b == 5 || b == 6) { |
|
690 |
if (index < (tests.length - 1)) { |
|
691 |
tests[index].run(true); |
|
692 |
tests[index + 1].run(false); |
|
693 |
} else { |
|
694 |
fail("Did not exit Repl tool after test"); |
|
695 |
} |
|
696 |
++index; |
|
697 |
} // For now, anything else is thrown away |
|
698 |
} |
|
699 |
||
700 |
@Override |
|
701 |
public synchronized void write(byte b[], int off, int len) { |
|
702 |
if ((off < 0) || (off > b.length) || (len < 0) |
|
703 |
|| ((off + len) - b.length > 0)) { |
|
704 |
throw new IndexOutOfBoundsException(); |
|
705 |
} |
|
706 |
for (int i = 0; i < len; ++i) { |
|
707 |
write(b[off + i]); |
|
708 |
} |
|
709 |
} |
|
710 |
} |
|
711 |
} |