author | rfield |
Sat, 09 Apr 2016 11:03:48 -0700 | |
changeset 37007 | 6023a9a9d58a |
parent 36990 | ec0b843a7af5 |
child 37389 | 9c137b83a8b8 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
36494 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
/* |
|
25 |
* @test |
|
36494 | 26 |
* @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36718
diff
changeset
|
27 |
* @summary Tests for Basic tests for REPL tool |
35002 | 28 |
* @requires os.family != "solaris" |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
29 |
* @library /tools/lib |
33918
6d7a40b2a54b
8143334: @ignore langtools/test/jdk/jshell/ToolBasicTest.java
jlahoda
parents:
33714
diff
changeset
|
30 |
* @ignore 8139873 |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36718
diff
changeset
|
31 |
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36718
diff
changeset
|
32 |
* @build KullaTesting TestingInputStream Compiler |
35002 | 33 |
* @run testng/timeout=600 ToolBasicTest |
33362 | 34 |
*/ |
35 |
||
35002 | 36 |
import java.io.FileInputStream; |
33362 | 37 |
import java.io.IOException; |
38 |
import java.io.PrintWriter; |
|
39 |
import java.io.StringWriter; |
|
40 |
import java.nio.file.Files; |
|
41 |
import java.nio.file.Path; |
|
42 |
import java.nio.file.Paths; |
|
43 |
import java.util.ArrayList; |
|
44 |
import java.util.Arrays; |
|
45 |
import java.util.List; |
|
36990 | 46 |
import java.util.Locale; |
33362 | 47 |
import java.util.Scanner; |
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
48 |
import java.util.function.BiFunction; |
33362 | 49 |
import java.util.function.Consumer; |
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
50 |
import java.util.function.Function; |
33362 | 51 |
import java.util.prefs.BackingStoreException; |
52 |
import java.util.prefs.Preferences; |
|
53 |
import java.util.stream.Collectors; |
|
54 |
import java.util.stream.Stream; |
|
55 |
||
56 |
import org.testng.annotations.Test; |
|
57 |
||
58 |
import static org.testng.Assert.assertEquals; |
|
59 |
import static org.testng.Assert.assertTrue; |
|
60 |
import static org.testng.Assert.fail; |
|
61 |
||
62 |
@Test |
|
63 |
public class ToolBasicTest extends ReplToolTesting { |
|
64 |
||
65 |
public void elideStartUpFromList() { |
|
66 |
test( |
|
36494 | 67 |
(a) -> assertCommandOutputContains(a, "123", "type int"), |
33362 | 68 |
(a) -> assertCommandCheckOutput(a, "/list", (s) -> { |
69 |
int cnt; |
|
70 |
try (Scanner scanner = new Scanner(s)) { |
|
71 |
cnt = 0; |
|
72 |
while (scanner.hasNextLine()) { |
|
73 |
String line = scanner.nextLine(); |
|
74 |
if (!line.trim().isEmpty()) { |
|
75 |
++cnt; |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
assertEquals(cnt, 1, "Expected only one listed line"); |
|
80 |
}) |
|
81 |
); |
|
82 |
} |
|
83 |
||
84 |
public void elideStartUpFromSave() throws IOException { |
|
85 |
Compiler compiler = new Compiler(); |
|
86 |
Path path = compiler.getPath("myfile"); |
|
87 |
test( |
|
36494 | 88 |
(a) -> assertCommandOutputContains(a, "123", "type int"), |
33362 | 89 |
(a) -> assertCommand(a, "/save " + path.toString(), "") |
90 |
); |
|
91 |
try (Stream<String> lines = Files.lines(path)) { |
|
92 |
assertEquals(lines.count(), 1, "Expected only one saved line"); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
public void testInterrupt() { |
|
97 |
ReplTest interrupt = (a) -> assertCommand(a, "\u0003", ""); |
|
98 |
for (String s : new String[] { "", "\u0003" }) { |
|
99 |
test(false, new String[]{"-nostartup"}, |
|
100 |
(a) -> assertCommand(a, "int a = 2 +" + s, ""), |
|
101 |
interrupt, |
|
102 |
(a) -> assertCommand(a, "int a\u0003", ""), |
|
103 |
(a) -> assertCommand(a, "int a = 2 + 2\u0003", ""), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
104 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 105 |
(a) -> evaluateExpression(a, "int", "2", "2"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
106 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 107 |
(a) -> assertCommand(a, "void f() {", ""), |
108 |
(a) -> assertCommand(a, "int q = 10;" + s, ""), |
|
109 |
interrupt, |
|
110 |
(a) -> assertCommand(a, "void f() {}\u0003", ""), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
111 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 112 |
(a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
113 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 114 |
(a) -> assertCommand(a, "class A {" + s, ""), |
115 |
interrupt, |
|
116 |
(a) -> assertCommand(a, "class A {}\u0003", ""), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
117 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
33362 | 118 |
(a) -> assertClass(a, "interface A {}", "interface", "A"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
119 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
120 |
(a) -> assertCommand(a, "import java.util.stream." + s, ""), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
121 |
interrupt, |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
122 |
(a) -> assertCommand(a, "import java.util.stream.\u0003", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
123 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
124 |
(a) -> assertImport(a, "import java.util.stream.Stream", "", "java.util.stream.Stream"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
125 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 126 |
); |
127 |
} |
|
128 |
} |
|
129 |
||
130 |
private final Object lock = new Object(); |
|
131 |
private PrintWriter out; |
|
132 |
private boolean isStopped; |
|
133 |
private Thread t; |
|
134 |
private void assertStop(boolean after, String cmd, String output) { |
|
135 |
if (!after) { |
|
136 |
isStopped = false; |
|
137 |
StringWriter writer = new StringWriter(); |
|
138 |
out = new PrintWriter(writer); |
|
139 |
setCommandInput(cmd + "\n"); |
|
140 |
t = new Thread(() -> { |
|
141 |
try { |
|
142 |
// no chance to know whether cmd is being evaluated |
|
143 |
Thread.sleep(5000); |
|
144 |
} catch (InterruptedException ignored) { |
|
145 |
} |
|
146 |
int i = 1; |
|
147 |
int n = 30; |
|
148 |
synchronized (lock) { |
|
149 |
do { |
|
150 |
setCommandInput("\u0003"); |
|
151 |
if (!isStopped) { |
|
152 |
out.println("Not stopped. Try again: " + i); |
|
153 |
try { |
|
154 |
lock.wait(1000); |
|
155 |
} catch (InterruptedException ignored) { |
|
156 |
} |
|
157 |
} |
|
158 |
} while (i++ < n && !isStopped); |
|
159 |
if (!isStopped) { |
|
160 |
System.err.println(writer.toString()); |
|
161 |
fail("Evaluation was not stopped: '" + cmd + "'"); |
|
162 |
} |
|
163 |
} |
|
164 |
}); |
|
165 |
t.start(); |
|
166 |
} else { |
|
167 |
synchronized (lock) { |
|
168 |
out.println("Evaluation was stopped successfully: '" + cmd + "'"); |
|
169 |
isStopped = true; |
|
170 |
lock.notify(); |
|
171 |
} |
|
172 |
try { |
|
173 |
t.join(); |
|
174 |
t = null; |
|
175 |
} catch (InterruptedException ignored) { |
|
176 |
} |
|
177 |
assertOutput(getCommandOutput(), "", "command"); |
|
178 |
assertOutput(getCommandErrorOutput(), "", "command error"); |
|
179 |
assertOutput(getUserOutput(), output, "user"); |
|
180 |
assertOutput(getUserErrorOutput(), "", "user error"); |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
public void testStop() { |
|
185 |
test( |
|
186 |
(a) -> assertStop(a, "while (true) {}", "Killed.\n"), |
|
187 |
(a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "Killed.\n") |
|
188 |
); |
|
189 |
} |
|
190 |
||
191 |
@Test(enabled = false) // TODO 8130450 |
|
192 |
public void testRerun() { |
|
193 |
test(false, new String[] {"-nostartup"}, |
|
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
194 |
(a) -> assertCommand(a, "/0", "| No such command or snippet id: /0\n| Type /help for help.\n"), |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
195 |
(a) -> assertCommand(a, "/5", "| No such command or snippet id: /5\n| Type /help for help.\n") |
33362 | 196 |
); |
197 |
String[] codes = new String[] { |
|
198 |
"int a = 0;", // var |
|
199 |
"class A {}", // class |
|
200 |
"void f() {}", // method |
|
201 |
"bool b;", // active failed |
|
202 |
"void g() { h(); }", // active corralled |
|
203 |
}; |
|
204 |
List<ReplTest> tests = new ArrayList<>(); |
|
205 |
for (String s : codes) { |
|
206 |
tests.add((a) -> assertCommand(a, s, null)); |
|
207 |
} |
|
208 |
for (int i = 0; i < codes.length; ++i) { |
|
209 |
final int finalI = i; |
|
210 |
Consumer<String> check = (s) -> { |
|
211 |
String[] ss = s.split("\n"); |
|
212 |
assertEquals(ss[0], codes[finalI]); |
|
213 |
assertTrue(ss.length > 1, s); |
|
214 |
}; |
|
215 |
tests.add((a) -> assertCommandCheckOutput(a, "/" + (finalI + 1), check)); |
|
216 |
} |
|
217 |
for (int i = 0; i < codes.length; ++i) { |
|
218 |
final int finalI = i; |
|
219 |
Consumer<String> check = (s) -> { |
|
220 |
String[] ss = s.split("\n"); |
|
221 |
assertEquals(ss[0], codes[codes.length - finalI - 1]); |
|
222 |
assertTrue(ss.length > 1, s); |
|
223 |
}; |
|
224 |
tests.add((a) -> assertCommandCheckOutput(a, "/-" + (finalI + 1), check)); |
|
225 |
} |
|
226 |
tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("void g() { h(); }"))); |
|
227 |
test(false, new String[]{"-nostartup"}, |
|
228 |
tests.toArray(new ReplTest[tests.size()])); |
|
229 |
} |
|
230 |
||
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
231 |
public void test8142447() { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
232 |
Function<String, BiFunction<String, Integer, ReplTest>> assertRerun = cmd -> (code, assertionCount) -> |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
233 |
(a) -> assertCommandCheckOutput(a, cmd, s -> { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
234 |
String[] ss = s.split("\n"); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
235 |
assertEquals(ss[0], code); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
236 |
loadVariable(a, "int", "assertionCount", Integer.toString(assertionCount), Integer.toString(assertionCount)); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
237 |
}); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
238 |
ReplTest assertVariables = (a) -> assertCommandCheckOutput(a, "/v", assertVariables()); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
239 |
|
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
240 |
Compiler compiler = new Compiler(); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
241 |
Path startup = compiler.getPath("StartupFileOption/startup.txt"); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
242 |
compiler.writeToFile(startup, "int assertionCount = 0;\n" + // id: s1 |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
243 |
"void add(int n) { assertionCount += n; }"); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
244 |
test(new String[]{"-startup", startup.toString()}, |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
245 |
(a) -> assertCommand(a, "add(1)", ""), // id: 1 |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
246 |
(a) -> assertCommandCheckOutput(a, "add(ONE)", s -> assertEquals(s.split("\n")[0], "| Error:")), // id: e1 |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
247 |
(a) -> assertVariable(a, "int", "ONE", "1", "1"), |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
248 |
assertRerun.apply("/1").apply("add(1)", 2), assertVariables, |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
249 |
assertRerun.apply("/e1").apply("add(ONE)", 3), assertVariables, |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
250 |
assertRerun.apply("/s1").apply("int assertionCount = 0;", 0), assertVariables |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
251 |
); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
252 |
|
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
253 |
test(false, new String[] {"-nostartup"}, |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
254 |
(a) -> assertCommand(a, "/s1", "| No such command or snippet id: /s1\n| Type /help for help.\n"), |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
255 |
(a) -> assertCommand(a, "/1", "| No such command or snippet id: /1\n| Type /help for help.\n"), |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
256 |
(a) -> assertCommand(a, "/e1", "| No such command or snippet id: /e1\n| Type /help for help.\n") |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
257 |
); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
258 |
} |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
259 |
|
33362 | 260 |
public void testRemaining() { |
261 |
test( |
|
262 |
(a) -> assertCommand(a, "int z; z =", "| Added variable z of type int\n"), |
|
263 |
(a) -> assertCommand(a, "5", "| Variable z has been assigned the value 5\n"), |
|
264 |
(a) -> assertCommand(a, "/*nada*/; int q =", ""), |
|
265 |
(a) -> assertCommand(a, "77", "| Added variable q of type int with initial value 77\n"), |
|
266 |
(a) -> assertCommand(a, "//comment;", ""), |
|
267 |
(a) -> assertCommand(a, "int v;", "| Added variable v of type int\n"), |
|
268 |
(a) -> assertCommand(a, "int v; int c", "| Added variable c of type int\n") |
|
269 |
); |
|
270 |
} |
|
271 |
||
272 |
public void oneLineOfError() { |
|
273 |
test( |
|
274 |
(a) -> assertCommand(a, "12+", null), |
|
275 |
(a) -> assertCommandCheckOutput(a, " true", (s) -> |
|
276 |
assertTrue(s.contains("12+") && !s.contains("true"), "Output: '" + s + "'")) |
|
277 |
); |
|
278 |
} |
|
279 |
||
280 |
public void defineVariables() { |
|
281 |
test( |
|
282 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
283 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
|
284 |
(a) -> assertVariable(a, "int", "a"), |
|
285 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
286 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
|
287 |
(a) -> assertVariable(a, "double", "a", "1", "1.0"), |
|
288 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
289 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
|
290 |
(a) -> evaluateExpression(a, "double", "2 * a", "2.0"), |
|
291 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
292 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()) |
|
293 |
); |
|
294 |
} |
|
295 |
||
296 |
public void defineMethods() { |
|
297 |
test( |
|
298 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
299 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
|
300 |
(a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"), |
|
301 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
302 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
|
303 |
(a) -> assertMethod(a, "void f(int a) { g(); }", "(int)void", "f"), |
|
304 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
305 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
|
306 |
(a) -> assertMethod(a, "void g() {}", "()void", "g"), |
|
307 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
308 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()) |
|
309 |
); |
|
310 |
} |
|
311 |
||
312 |
public void defineClasses() { |
|
313 |
test( |
|
314 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
315 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
|
316 |
(a) -> assertClass(a, "class A { }", "class", "A"), |
|
317 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
318 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
|
319 |
(a) -> assertClass(a, "interface A { }", "interface", "A"), |
|
320 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
321 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
|
322 |
(a) -> assertClass(a, "enum A { }", "enum", "A"), |
|
323 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
324 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
|
325 |
(a) -> assertClass(a, "@interface A { }", "@interface", "A"), |
|
326 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
327 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()) |
|
328 |
); |
|
329 |
} |
|
330 |
||
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
331 |
public void defineImports() { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
332 |
test( |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
333 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
334 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
335 |
(a) -> assertImport(a, "import java.util.stream.Stream;", "", "java.util.stream.Stream"), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
336 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
337 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
338 |
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
339 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
340 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
341 |
(a) -> assertImport(a, "import static java.lang.Math.PI;", "static", "java.lang.Math.PI"), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
342 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
343 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
344 |
(a) -> assertImport(a, "import static java.lang.Math.*;", "static", "java.lang.Math.*"), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
345 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
346 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
347 |
); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
348 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
349 |
|
33362 | 350 |
public void testClasspathDirectory() { |
351 |
Compiler compiler = new Compiler(); |
|
352 |
Path outDir = Paths.get("testClasspathDirectory"); |
|
353 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
354 |
Path classpath = compiler.getPath(outDir); |
|
355 |
test( |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
356 |
(a) -> assertCommand(a, "/classpath " + classpath, String.format("| Path %s added to classpath\n", classpath)), |
33362 | 357 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
358 |
); |
|
359 |
test(new String[] { "-cp", classpath.toString() }, |
|
360 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
361 |
); |
|
362 |
test(new String[] { "-classpath", classpath.toString() }, |
|
363 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
364 |
); |
|
365 |
} |
|
366 |
||
367 |
public void testClasspathJar() { |
|
368 |
Compiler compiler = new Compiler(); |
|
369 |
Path outDir = Paths.get("testClasspathJar"); |
|
370 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
371 |
String jarName = "test.jar"; |
|
372 |
compiler.jar(outDir, jarName, "pkg/A.class"); |
|
373 |
Path jarPath = compiler.getPath(outDir).resolve(jarName); |
|
374 |
test( |
|
375 |
(a) -> assertCommand(a, "/classpath " + jarPath, String.format("| Path %s added to classpath\n", jarPath)), |
|
376 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
377 |
); |
|
378 |
test(new String[] { "-cp", jarPath.toString() }, |
|
379 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
380 |
); |
|
381 |
test(new String[] { "-classpath", jarPath.toString() }, |
|
382 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
383 |
); |
|
384 |
} |
|
385 |
||
386 |
public void testStartupFileOption() { |
|
387 |
try { |
|
388 |
Compiler compiler = new Compiler(); |
|
389 |
Path startup = compiler.getPath("StartupFileOption/startup.txt"); |
|
390 |
compiler.writeToFile(startup, "class A { public String toString() { return \"A\"; } }"); |
|
391 |
test(new String[]{"-startup", startup.toString()}, |
|
392 |
(a) -> evaluateExpression(a, "A", "new A()", "\"A\"\n") |
|
393 |
); |
|
394 |
test(new String[]{"-nostartup"}, |
|
395 |
(a) -> assertCommandCheckOutput(a, "printf(\"\")", assertStartsWith("| Error:\n| cannot find symbol")) |
|
396 |
); |
|
397 |
test((a) -> assertCommand(a, "printf(\"A\")", "", "", null, "A", "")); |
|
36990 | 398 |
test(Locale.ROOT, false, new String[]{"-startup", "UNKNOWN"}, "| File 'UNKNOWN' for start-up is not found."); |
33362 | 399 |
} finally { |
400 |
removeStartup(); |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
public void testLoadingFromArgs() { |
|
405 |
Compiler compiler = new Compiler(); |
|
406 |
Path path = compiler.getPath("loading.repl"); |
|
407 |
compiler.writeToFile(path, "int a = 10; double x = 20; double a = 10;"); |
|
408 |
test(new String[] { path.toString() }, |
|
409 |
(a) -> assertCommand(a, "x", "| Variable x of type double has value 20.0\n"), |
|
410 |
(a) -> assertCommand(a, "a", "| Variable a of type double has value 10.0\n") |
|
411 |
); |
|
412 |
Path unknown = compiler.getPath("UNKNOWN.jar"); |
|
36990 | 413 |
test(Locale.ROOT, true, new String[]{unknown.toString()}, |
414 |
"| File " + unknown |
|
415 |
+ " is not found: " + unresolvableMessage(unknown) + "\n"); |
|
33362 | 416 |
} |
417 |
||
418 |
public void testReset() { |
|
419 |
test( |
|
420 |
(a) -> assertReset(a, "/r"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
421 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 422 |
(a) -> assertVariable(a, "int", "x"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
423 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 424 |
(a) -> assertMethod(a, "void f() { }", "()void", "f"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
425 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 426 |
(a) -> assertClass(a, "class A { }", "class", "A"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
427 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
428 |
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
429 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
33362 | 430 |
(a) -> assertReset(a, "/reset"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
431 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
432 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
433 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
434 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 435 |
); |
436 |
} |
|
437 |
||
438 |
public void testOpen() { |
|
439 |
Compiler compiler = new Compiler(); |
|
440 |
Path path = compiler.getPath("testOpen.repl"); |
|
441 |
compiler.writeToFile(path, |
|
442 |
"int a = 10;\ndouble x = 20;\ndouble a = 10;\n" + |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
443 |
"class A { public String toString() { return \"A\"; } }\nimport java.util.stream.*;"); |
33362 | 444 |
for (String s : new String[]{"/o", "/open"}) { |
445 |
test( |
|
446 |
(a) -> assertCommand(a, s + " " + path.toString(), ""), |
|
447 |
(a) -> assertCommand(a, "a", "| Variable a of type double has value 10.0\n"), |
|
448 |
(a) -> evaluateExpression(a, "A", "new A();", "\"A\""), |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
449 |
(a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"), |
33362 | 450 |
(a) -> { |
451 |
loadVariable(a, "double", "x", "20.0", "20.0"); |
|
452 |
loadVariable(a, "double", "a", "10.0", "10.0"); |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
453 |
loadVariable(a, "A", "$7", "new A();", "A"); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
454 |
loadVariable(a, "long", "$8", "Stream.of(\"A\").count();", "1"); |
33362 | 455 |
loadClass(a, "class A { public String toString() { return \"A\"; } }", |
456 |
"class", "A"); |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
457 |
loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*"); |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
458 |
assertCommandCheckOutput(a, "/classes", assertClasses()); |
33362 | 459 |
}, |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
460 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
461 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
462 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 463 |
); |
464 |
Path unknown = compiler.getPath("UNKNOWN.repl"); |
|
465 |
test( |
|
466 |
(a) -> assertCommand(a, s + " " + unknown, |
|
467 |
"| File '" + unknown |
|
35002 | 468 |
+ "' is not found: " + unresolvableMessage(unknown) + "\n") |
33362 | 469 |
); |
470 |
} |
|
471 |
} |
|
472 |
||
473 |
public void testSave() throws IOException { |
|
474 |
Compiler compiler = new Compiler(); |
|
475 |
Path path = compiler.getPath("testSave.repl"); |
|
476 |
List<String> list = Arrays.asList( |
|
477 |
"int a;", |
|
478 |
"class A { public String toString() { return \"A\"; } }" |
|
479 |
); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
480 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
481 |
(a) -> assertVariable(a, "int", "a"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
482 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
483 |
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
484 |
(a) -> assertCommand(a, "/save " + path.toString(), "") |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
485 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
486 |
assertEquals(Files.readAllLines(path), list); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
487 |
{ |
33362 | 488 |
List<String> output = new ArrayList<>(); |
489 |
test( |
|
490 |
(a) -> assertCommand(a, "int a;", null), |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
491 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
33362 | 492 |
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"), |
493 |
(a) -> assertCommandCheckOutput(a, "/list all", (out) -> |
|
494 |
output.addAll(Stream.of(out.split("\n")) |
|
495 |
.filter(str -> !str.isEmpty()) |
|
496 |
.map(str -> str.substring(str.indexOf(':') + 2)) |
|
497 |
.filter(str -> !str.startsWith("/")) |
|
498 |
.collect(Collectors.toList()))), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
499 |
(a) -> assertCommand(a, "/save all " + path.toString(), "") |
33362 | 500 |
); |
501 |
assertEquals(Files.readAllLines(path), output); |
|
502 |
} |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
503 |
List<String> output = new ArrayList<>(); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
504 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
505 |
(a) -> assertVariable(a, "int", "a"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
506 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
507 |
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
508 |
(a) -> assertCommandCheckOutput(a, "/history", (out) -> |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
509 |
output.addAll(Stream.of(out.split("\n")) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
510 |
.filter(str -> !str.isEmpty()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
511 |
.collect(Collectors.toList()))), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
512 |
(a) -> assertCommand(a, "/save history " + path.toString(), "") |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
513 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
514 |
output.add("/save history " + path.toString()); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
515 |
assertEquals(Files.readAllLines(path), output); |
33362 | 516 |
} |
517 |
||
518 |
public void testStartSet() throws BackingStoreException { |
|
519 |
try { |
|
520 |
Compiler compiler = new Compiler(); |
|
521 |
Path startUpFile = compiler.getPath("startUp.txt"); |
|
522 |
test( |
|
523 |
(a) -> assertVariable(a, "int", "a"), |
|
524 |
(a) -> assertVariable(a, "double", "b", "10", "10.0"), |
|
525 |
(a) -> assertMethod(a, "void f() {}", "()V", "f"), |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
526 |
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
527 |
(a) -> assertCommand(a, "/save " + startUpFile.toString(), null), |
36494 | 528 |
(a) -> assertCommand(a, "/set start " + startUpFile.toString(), null) |
33362 | 529 |
); |
530 |
Path unknown = compiler.getPath("UNKNOWN"); |
|
531 |
test( |
|
36494 | 532 |
(a) -> assertCommand(a, "/set start " + unknown.toString(), |
533 |
"| File '" + unknown + "' for /set start is not found.\n") |
|
33362 | 534 |
); |
535 |
test(false, new String[0], |
|
536 |
(a) -> { |
|
537 |
loadVariable(a, "int", "a"); |
|
538 |
loadVariable(a, "double", "b", "10.0", "10.0"); |
|
539 |
loadMethod(a, "void f() {}", "()void", "f"); |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
540 |
loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*"); |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
541 |
assertCommandCheckOutput(a, "/classes", assertClasses()); |
33362 | 542 |
}, |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
543 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
544 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
545 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 546 |
); |
547 |
} finally { |
|
548 |
removeStartup(); |
|
549 |
} |
|
550 |
} |
|
551 |
||
552 |
private void removeStartup() { |
|
36494 | 553 |
Preferences preferences = Preferences.userRoot().node("tool/JShell"); |
33362 | 554 |
if (preferences != null) { |
555 |
preferences.remove("STARTUP"); |
|
556 |
} |
|
557 |
} |
|
558 |
||
559 |
public void testUnknownCommand() { |
|
560 |
test((a) -> assertCommand(a, "/unknown", |
|
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
561 |
"| No such command or snippet id: /unknown\n" + |
33362 | 562 |
"| Type /help for help.\n")); |
563 |
} |
|
564 |
||
565 |
public void testEmptyClassPath() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
566 |
test(after -> assertCommand(after, "/classpath", "| /classpath requires a path argument\n")); |
33362 | 567 |
} |
568 |
||
569 |
public void testNoArgument() { |
|
36494 | 570 |
String[] commands = {"/save", "/open", "/set start"}; |
33362 | 571 |
test(Stream.of(commands) |
572 |
.map(cmd -> { |
|
573 |
String c = cmd; |
|
574 |
final String finalC = c; |
|
575 |
return (ReplTest) after -> assertCommand(after, cmd, |
|
576 |
"| The " + finalC + " command requires a filename argument.\n"); |
|
577 |
}) |
|
578 |
.toArray(ReplTest[]::new)); |
|
579 |
} |
|
580 |
||
581 |
public void testStartSave() throws IOException { |
|
582 |
Compiler compiler = new Compiler(); |
|
583 |
Path startSave = compiler.getPath("startSave.txt"); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
584 |
test(a -> assertCommand(a, "/save start " + startSave.toString(), null)); |
33362 | 585 |
List<String> lines = Files.lines(startSave) |
586 |
.filter(s -> !s.isEmpty()) |
|
587 |
.collect(Collectors.toList()); |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
588 |
assertEquals(lines, START_UP); |
33362 | 589 |
} |
590 |
||
591 |
public void testConstrainedUpdates() { |
|
592 |
test( |
|
593 |
a -> assertClass(a, "class XYZZY { }", "class", "XYZZY"), |
|
594 |
a -> assertVariable(a, "XYZZY", "xyzzy"), |
|
595 |
a -> assertCommandCheckOutput(a, "import java.util.stream.*", |
|
596 |
(out) -> assertTrue(out.trim().isEmpty(), "Expected no output, got: " + out)) |
|
597 |
); |
|
598 |
} |
|
599 |
||
600 |
public void testRemoteExit() { |
|
601 |
test( |
|
602 |
a -> assertVariable(a, "int", "x"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
603 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
36494 | 604 |
a -> assertCommandOutputContains(a, "System.exit(5);", "terminated"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
605 |
a -> assertCommandCheckOutput(a, "/vars", s -> |
33362 | 606 |
assertTrue(s.trim().isEmpty(), s)), |
607 |
a -> assertMethod(a, "void f() { }", "()void", "f"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
608 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()) |
33362 | 609 |
); |
610 |
} |
|
611 |
||
612 |
public void testFeedbackNegative() { |
|
36494 | 613 |
test(a -> assertCommandCheckOutput(a, "/set feedback aaaa", |
614 |
assertStartsWith("| Does not match any current feedback mode"))); |
|
33362 | 615 |
} |
616 |
||
617 |
public void testFeedbackOff() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
618 |
for (String off : new String[]{"o", "off"}) { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
619 |
test( |
36494 | 620 |
a -> assertCommand(a, "/set feedback " + off, ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
621 |
a -> assertCommand(a, "int a", ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
622 |
a -> assertCommand(a, "void f() {}", ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
623 |
a -> assertCommandCheckOutput(a, "aaaa", assertStartsWith("| Error:")), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
624 |
a -> assertCommandCheckOutput(a, "public void f() {}", assertStartsWith("| Warning:")) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
625 |
); |
33362 | 626 |
} |
627 |
} |
|
628 |
||
629 |
public void testFeedbackNormal() { |
|
630 |
Compiler compiler = new Compiler(); |
|
631 |
Path testNormalFile = compiler.getPath("testConciseNormal"); |
|
632 |
String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"}; |
|
633 |
String[] sources2 = new String[] {"int a //again", "void f() {int y = 4;}", "class A {} //again", "a = 10"}; |
|
634 |
String[] output = new String[] { |
|
635 |
"| Added variable a of type int\n", |
|
636 |
"| Added method f()\n", |
|
637 |
"| Added class A\n", |
|
638 |
"| Variable a has been assigned the value 10\n" |
|
639 |
}; |
|
640 |
compiler.writeToFile(testNormalFile, sources2); |
|
36494 | 641 |
for (String feedback : new String[]{"/set f", "/set feedback"}) { |
642 |
for (String feedbackState : new String[]{"n", "normal", "o", "off"}) { |
|
33362 | 643 |
test( |
36494 | 644 |
a -> assertCommand(a, feedback + " " + feedbackState, "| Feedback mode: normal\n"), |
33362 | 645 |
a -> assertCommand(a, sources[0], output[0]), |
646 |
a -> assertCommand(a, sources[1], output[1]), |
|
647 |
a -> assertCommand(a, sources[2], output[2]), |
|
648 |
a -> assertCommand(a, sources[3], output[3]), |
|
36494 | 649 |
a -> assertCommand(a, "/o " + testNormalFile.toString(), "") |
33362 | 650 |
); |
651 |
} |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
public void testDrop() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
656 |
test(false, new String[]{"-nostartup"}, |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
657 |
a -> assertVariable(a, "int", "a"), |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36494
diff
changeset
|
658 |
a -> dropVariable(a, "/drop 1", "int a = 0", "| Dropped variable a\n"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
659 |
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"), |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36494
diff
changeset
|
660 |
a -> dropMethod(a, "/drop 2", "b ()I", "| Dropped method b()\n"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
661 |
a -> assertClass(a, "class A {}", "class", "A"), |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36494
diff
changeset
|
662 |
a -> dropClass(a, "/drop 3", "class A", "| Dropped class A\n"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
663 |
a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36494
diff
changeset
|
664 |
a -> dropImport(a, "/drop 4", "import java.util.stream.*", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
665 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
666 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
667 |
a -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
668 |
a -> assertCommandCheckOutput(a, "/imports", assertImports()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
669 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
670 |
test(false, new String[]{"-nostartup"}, |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
671 |
a -> assertVariable(a, "int", "a"), |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36494
diff
changeset
|
672 |
a -> dropVariable(a, "/drop a", "int a = 0", "| Dropped variable a\n"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
673 |
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"), |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36494
diff
changeset
|
674 |
a -> dropMethod(a, "/drop b", "b ()I", "| Dropped method b()\n"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
675 |
a -> assertClass(a, "class A {}", "class", "A"), |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36494
diff
changeset
|
676 |
a -> dropClass(a, "/drop A", "class A", "| Dropped class A\n"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
677 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
678 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
679 |
a -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
680 |
a -> assertCommandCheckOutput(a, "/imports", assertImports()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
681 |
); |
33362 | 682 |
} |
683 |
||
684 |
public void testDropNegative() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
685 |
test(false, new String[]{"-nostartup"}, |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
686 |
a -> assertCommand(a, "/drop 0", "| No definition or id named 0 found. See /classes, /methods, /vars, or /list\n"), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
687 |
a -> assertCommand(a, "/drop a", "| No definition or id named a found. See /classes, /methods, /vars, or /list\n"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
688 |
a -> assertCommandCheckOutput(a, "/drop", |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
689 |
assertStartsWith("| In the /drop argument, please specify an import, variable, method, or class to drop.")), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
690 |
a -> assertVariable(a, "int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
691 |
a -> assertCommand(a, "a", "| Variable a of type int has value 0\n"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
692 |
a -> assertCommand(a, "/drop 2", "| The argument did not specify an active import, variable, method, or class to drop.\n") |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
693 |
); |
33362 | 694 |
} |
695 |
||
696 |
public void testAmbiguousDrop() { |
|
697 |
Consumer<String> check = s -> { |
|
698 |
assertTrue(s.startsWith("| The argument references more than one import, variable, method, or class"), s); |
|
699 |
int lines = s.split("\n").length; |
|
700 |
assertEquals(lines, 5, "Expected 3 ambiguous keys, but found: " + (lines - 2) + "\n" + s); |
|
701 |
}; |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
702 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
703 |
a -> assertVariable(a, "int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
704 |
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
705 |
a -> assertClass(a, "class a {}", "class", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
706 |
a -> assertCommandCheckOutput(a, "/drop a", check), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
707 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
708 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
709 |
a -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
710 |
a -> assertCommandCheckOutput(a, "/imports", assertImports()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
711 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
712 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
713 |
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
714 |
a -> assertMethod(a, "double a(int a) { return 0; }", "(int)double", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
715 |
a -> assertMethod(a, "double a(double a) { return 0; }", "(double)double", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
716 |
a -> assertCommandCheckOutput(a, "/drop a", check), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
717 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
718 |
); |
33362 | 719 |
} |
720 |
||
721 |
public void testHistoryReference() { |
|
722 |
test(false, new String[]{"-nostartup"}, |
|
723 |
a -> assertCommand(a, "System.err.println(1)", "", "", null, "", "1\n"), |
|
724 |
a -> assertCommand(a, "System.err.println(2)", "", "", null, "", "2\n"), |
|
725 |
a -> assertCommand(a, "/-2", "System.err.println(1)\n", "", null, "", "1\n"), |
|
726 |
a -> assertCommand(a, "/history", "\n" + |
|
727 |
"/debug 0\n" + |
|
728 |
"System.err.println(1)\n" + |
|
729 |
"System.err.println(2)\n" + |
|
730 |
"System.err.println(1)\n" + |
|
731 |
"/history\n"), |
|
732 |
a -> assertCommand(a, "/-2", "System.err.println(2)\n", "", null, "", "2\n"), |
|
733 |
a -> assertCommand(a, "/!", "System.err.println(2)\n", "", null, "", "2\n"), |
|
734 |
a -> assertCommand(a, "/2", "System.err.println(2)\n", "", null, "", "2\n"), |
|
735 |
a -> assertCommand(a, "/1", "System.err.println(1)\n", "", null, "", "1\n") |
|
736 |
); |
|
737 |
} |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
738 |
|
35002 | 739 |
private String unresolvableMessage(Path p) { |
740 |
try { |
|
741 |
new FileInputStream(p.toFile()); |
|
742 |
throw new AssertionError("Expected exception did not occur."); |
|
743 |
} catch (IOException ex) { |
|
744 |
return ex.getMessage(); |
|
745 |
} |
|
746 |
} |
|
747 |
||
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
748 |
public void testCommandPrefix() { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
749 |
test(a -> assertCommandCheckOutput(a, "/s", |
36494 | 750 |
assertStartsWith("| Command: /s is ambiguous: /save, /set")), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
751 |
a -> assertCommand(a, "int var", "| Added variable var of type int\n"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
752 |
a -> assertCommandCheckOutput(a, "/va", |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
753 |
assertStartsWith("| int var = 0")), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
754 |
a -> assertCommandCheckOutput(a, "/save", |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
755 |
assertStartsWith("| The /save command requires a filename argument."))); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
756 |
} |
33362 | 757 |
} |