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