author | rfield |
Fri, 12 Feb 2016 10:51:36 -0800 | |
changeset 35812 | 6a6ca0bd3c14 |
parent 35002 | 209d72239196 |
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 |
/* |
|
25 |
* @test |
|
35812 | 26 |
* @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 |
35002 | 27 |
* @requires os.family != "solaris" |
33362 | 28 |
* @summary Tests for Basic tests for REPL tool |
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 |
33362 | 31 |
* @build KullaTesting TestingInputStream ToolBox Compiler |
35002 | 32 |
* @run testng/timeout=600 ToolBasicTest |
33362 | 33 |
*/ |
34 |
||
35002 | 35 |
import java.io.FileInputStream; |
33362 | 36 |
import java.io.IOException; |
37 |
import java.io.PrintWriter; |
|
38 |
import java.io.StringWriter; |
|
39 |
import java.nio.file.Files; |
|
40 |
import java.nio.file.Path; |
|
41 |
import java.nio.file.Paths; |
|
42 |
import java.util.ArrayList; |
|
43 |
import java.util.Arrays; |
|
44 |
import java.util.List; |
|
45 |
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
|
46 |
import java.util.function.BiFunction; |
33362 | 47 |
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
|
48 |
import java.util.function.Function; |
33362 | 49 |
import java.util.prefs.BackingStoreException; |
50 |
import java.util.prefs.Preferences; |
|
51 |
import java.util.stream.Collectors; |
|
52 |
import java.util.stream.Stream; |
|
53 |
||
54 |
import org.testng.annotations.Test; |
|
55 |
||
56 |
import static org.testng.Assert.assertEquals; |
|
57 |
import static org.testng.Assert.assertTrue; |
|
58 |
import static org.testng.Assert.fail; |
|
59 |
||
60 |
@Test |
|
61 |
public class ToolBasicTest extends ReplToolTesting { |
|
62 |
||
63 |
public void defineVar() { |
|
64 |
test( |
|
65 |
(a) -> assertCommand(a, "int x = 72", "| Added variable x of type int with initial value 72\n"), |
|
66 |
(a) -> assertCommand(a, "x", "| Variable x of type int has value 72\n"), |
|
67 |
(a) -> assertCommand(a, "/vars", "| int x = 72\n") |
|
68 |
); |
|
69 |
} |
|
70 |
||
71 |
public void defineUnresolvedVar() { |
|
72 |
test( |
|
73 |
(a) -> assertCommand(a, "undefined x", |
|
74 |
"| Added variable x, however, it cannot be referenced until class undefined is declared\n"), |
|
75 |
(a) -> assertCommand(a, "/vars", "| undefined x = (not-active)\n") |
|
76 |
); |
|
77 |
} |
|
78 |
||
79 |
public void testUnresolved() { |
|
80 |
test( |
|
81 |
(a) -> assertCommand(a, "int f() { return g() + x + new A().a; }", |
|
82 |
"| Added method f(), however, it cannot be invoked until method g(), variable x, and class A are declared\n"), |
|
83 |
(a) -> assertCommand(a, "f()", |
|
84 |
"| Attempted to call f which cannot be invoked until method g(), variable x, and class A are declared\n"), |
|
85 |
(a) -> assertCommand(a, "int g() { return x; }", |
|
86 |
"| Added method g(), however, it cannot be invoked until variable x is declared\n"), |
|
87 |
(a) -> assertCommand(a, "g()", "| Attempted to call g which cannot be invoked until variable x is declared\n") |
|
88 |
); |
|
89 |
} |
|
90 |
||
91 |
public void elideStartUpFromList() { |
|
92 |
test( |
|
93 |
(a) -> assertCommandCheckOutput(a, "123", (s) -> |
|
94 |
assertTrue(s.contains("type int"), s)), |
|
95 |
(a) -> assertCommandCheckOutput(a, "/list", (s) -> { |
|
96 |
int cnt; |
|
97 |
try (Scanner scanner = new Scanner(s)) { |
|
98 |
cnt = 0; |
|
99 |
while (scanner.hasNextLine()) { |
|
100 |
String line = scanner.nextLine(); |
|
101 |
if (!line.trim().isEmpty()) { |
|
102 |
++cnt; |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
assertEquals(cnt, 1, "Expected only one listed line"); |
|
107 |
}) |
|
108 |
); |
|
109 |
} |
|
110 |
||
111 |
public void elideStartUpFromSave() throws IOException { |
|
112 |
Compiler compiler = new Compiler(); |
|
113 |
Path path = compiler.getPath("myfile"); |
|
114 |
test( |
|
115 |
(a) -> assertCommandCheckOutput(a, "123", |
|
116 |
(s) -> assertTrue(s.contains("type int"), s)), |
|
117 |
(a) -> assertCommand(a, "/save " + path.toString(), "") |
|
118 |
); |
|
119 |
try (Stream<String> lines = Files.lines(path)) { |
|
120 |
assertEquals(lines.count(), 1, "Expected only one saved line"); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
public void testInterrupt() { |
|
125 |
ReplTest interrupt = (a) -> assertCommand(a, "\u0003", ""); |
|
126 |
for (String s : new String[] { "", "\u0003" }) { |
|
127 |
test(false, new String[]{"-nostartup"}, |
|
128 |
(a) -> assertCommand(a, "int a = 2 +" + s, ""), |
|
129 |
interrupt, |
|
130 |
(a) -> assertCommand(a, "int a\u0003", ""), |
|
131 |
(a) -> assertCommand(a, "int a = 2 + 2\u0003", ""), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
132 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 133 |
(a) -> evaluateExpression(a, "int", "2", "2"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
134 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 135 |
(a) -> assertCommand(a, "void f() {", ""), |
136 |
(a) -> assertCommand(a, "int q = 10;" + s, ""), |
|
137 |
interrupt, |
|
138 |
(a) -> assertCommand(a, "void f() {}\u0003", ""), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
139 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 140 |
(a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
141 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 142 |
(a) -> assertCommand(a, "class A {" + s, ""), |
143 |
interrupt, |
|
144 |
(a) -> assertCommand(a, "class A {}\u0003", ""), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
145 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
33362 | 146 |
(a) -> assertClass(a, "interface A {}", "interface", "A"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
147 |
(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
|
148 |
(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
|
149 |
interrupt, |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
150 |
(a) -> assertCommand(a, "import java.util.stream.\u0003", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
151 |
(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
|
152 |
(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
|
153 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 154 |
); |
155 |
} |
|
156 |
} |
|
157 |
||
158 |
private final Object lock = new Object(); |
|
159 |
private PrintWriter out; |
|
160 |
private boolean isStopped; |
|
161 |
private Thread t; |
|
162 |
private void assertStop(boolean after, String cmd, String output) { |
|
163 |
if (!after) { |
|
164 |
isStopped = false; |
|
165 |
StringWriter writer = new StringWriter(); |
|
166 |
out = new PrintWriter(writer); |
|
167 |
setCommandInput(cmd + "\n"); |
|
168 |
t = new Thread(() -> { |
|
169 |
try { |
|
170 |
// no chance to know whether cmd is being evaluated |
|
171 |
Thread.sleep(5000); |
|
172 |
} catch (InterruptedException ignored) { |
|
173 |
} |
|
174 |
int i = 1; |
|
175 |
int n = 30; |
|
176 |
synchronized (lock) { |
|
177 |
do { |
|
178 |
setCommandInput("\u0003"); |
|
179 |
if (!isStopped) { |
|
180 |
out.println("Not stopped. Try again: " + i); |
|
181 |
try { |
|
182 |
lock.wait(1000); |
|
183 |
} catch (InterruptedException ignored) { |
|
184 |
} |
|
185 |
} |
|
186 |
} while (i++ < n && !isStopped); |
|
187 |
if (!isStopped) { |
|
188 |
System.err.println(writer.toString()); |
|
189 |
fail("Evaluation was not stopped: '" + cmd + "'"); |
|
190 |
} |
|
191 |
} |
|
192 |
}); |
|
193 |
t.start(); |
|
194 |
} else { |
|
195 |
synchronized (lock) { |
|
196 |
out.println("Evaluation was stopped successfully: '" + cmd + "'"); |
|
197 |
isStopped = true; |
|
198 |
lock.notify(); |
|
199 |
} |
|
200 |
try { |
|
201 |
t.join(); |
|
202 |
t = null; |
|
203 |
} catch (InterruptedException ignored) { |
|
204 |
} |
|
205 |
assertOutput(getCommandOutput(), "", "command"); |
|
206 |
assertOutput(getCommandErrorOutput(), "", "command error"); |
|
207 |
assertOutput(getUserOutput(), output, "user"); |
|
208 |
assertOutput(getUserErrorOutput(), "", "user error"); |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
public void testStop() { |
|
213 |
test( |
|
214 |
(a) -> assertStop(a, "while (true) {}", "Killed.\n"), |
|
215 |
(a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "Killed.\n") |
|
216 |
); |
|
217 |
} |
|
218 |
||
219 |
@Test(enabled = false) // TODO 8130450 |
|
220 |
public void testRerun() { |
|
221 |
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
|
222 |
(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
|
223 |
(a) -> assertCommand(a, "/5", "| No such command or snippet id: /5\n| Type /help for help.\n") |
33362 | 224 |
); |
225 |
String[] codes = new String[] { |
|
226 |
"int a = 0;", // var |
|
227 |
"class A {}", // class |
|
228 |
"void f() {}", // method |
|
229 |
"bool b;", // active failed |
|
230 |
"void g() { h(); }", // active corralled |
|
231 |
}; |
|
232 |
List<ReplTest> tests = new ArrayList<>(); |
|
233 |
for (String s : codes) { |
|
234 |
tests.add((a) -> assertCommand(a, s, null)); |
|
235 |
} |
|
236 |
for (int i = 0; i < codes.length; ++i) { |
|
237 |
final int finalI = i; |
|
238 |
Consumer<String> check = (s) -> { |
|
239 |
String[] ss = s.split("\n"); |
|
240 |
assertEquals(ss[0], codes[finalI]); |
|
241 |
assertTrue(ss.length > 1, s); |
|
242 |
}; |
|
243 |
tests.add((a) -> assertCommandCheckOutput(a, "/" + (finalI + 1), check)); |
|
244 |
} |
|
245 |
for (int i = 0; i < codes.length; ++i) { |
|
246 |
final int finalI = i; |
|
247 |
Consumer<String> check = (s) -> { |
|
248 |
String[] ss = s.split("\n"); |
|
249 |
assertEquals(ss[0], codes[codes.length - finalI - 1]); |
|
250 |
assertTrue(ss.length > 1, s); |
|
251 |
}; |
|
252 |
tests.add((a) -> assertCommandCheckOutput(a, "/-" + (finalI + 1), check)); |
|
253 |
} |
|
254 |
tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("void g() { h(); }"))); |
|
255 |
test(false, new String[]{"-nostartup"}, |
|
256 |
tests.toArray(new ReplTest[tests.size()])); |
|
257 |
} |
|
258 |
||
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
259 |
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
|
260 |
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
|
261 |
(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
|
262 |
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
|
263 |
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
|
264 |
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
|
265 |
}); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
266 |
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
|
267 |
|
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
268 |
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
|
269 |
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
|
270 |
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
|
271 |
"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
|
272 |
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
|
273 |
(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
|
274 |
(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
|
275 |
(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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
280 |
|
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
281 |
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
|
282 |
(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
|
283 |
(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
|
284 |
(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
|
285 |
); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
286 |
} |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
287 |
|
33362 | 288 |
public void testRemaining() { |
289 |
test( |
|
290 |
(a) -> assertCommand(a, "int z; z =", "| Added variable z of type int\n"), |
|
291 |
(a) -> assertCommand(a, "5", "| Variable z has been assigned the value 5\n"), |
|
292 |
(a) -> assertCommand(a, "/*nada*/; int q =", ""), |
|
293 |
(a) -> assertCommand(a, "77", "| Added variable q of type int with initial value 77\n"), |
|
294 |
(a) -> assertCommand(a, "//comment;", ""), |
|
295 |
(a) -> assertCommand(a, "int v;", "| Added variable v of type int\n"), |
|
296 |
(a) -> assertCommand(a, "int v; int c", "| Added variable c of type int\n") |
|
297 |
); |
|
298 |
} |
|
299 |
||
300 |
public void testDebug() { |
|
301 |
test( |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
302 |
(a) -> assertCommand(a, "/deb", "| Debugging on\n"), |
33362 | 303 |
(a) -> assertCommand(a, "/debug", "| Debugging off\n"), |
304 |
(a) -> assertCommand(a, "/debug", "| Debugging on\n"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
305 |
(a) -> assertCommand(a, "/deb", "| Debugging off\n") |
33362 | 306 |
); |
307 |
} |
|
308 |
||
35812 | 309 |
public void testHelpLength() { |
33362 | 310 |
Consumer<String> testOutput = (s) -> { |
311 |
List<String> ss = Stream.of(s.split("\n")) |
|
312 |
.filter(l -> !l.isEmpty()) |
|
313 |
.collect(Collectors.toList()); |
|
35812 | 314 |
assertTrue(ss.size() >= 10, "Help does not print enough lines:\n" + s); |
33362 | 315 |
}; |
316 |
test( |
|
317 |
(a) -> assertCommandCheckOutput(a, "/?", testOutput), |
|
35812 | 318 |
(a) -> assertCommandCheckOutput(a, "/help", testOutput), |
319 |
(a) -> assertCommandCheckOutput(a, "/help /list", testOutput) |
|
33362 | 320 |
); |
321 |
} |
|
322 |
||
35812 | 323 |
public void testHelp() { |
324 |
test( |
|
325 |
(a) -> assertHelp(a, "/?", "/list", "/help", "/exit", "intro"), |
|
326 |
(a) -> assertHelp(a, "/help", "/list", "/help", "/exit", "intro"), |
|
327 |
(a) -> assertHelp(a, "/help short", "shortcuts", "<tab>"), |
|
328 |
(a) -> assertHelp(a, "/? /li", "/list all", "snippets"), |
|
329 |
(a) -> assertHelp(a, "/help /help", "/help <command>") |
|
330 |
); |
|
331 |
} |
|
332 |
||
333 |
private void assertHelp(boolean a, String command, String... find) { |
|
334 |
assertCommandCheckOutput(a, command, s -> { |
|
335 |
for (String f : find) { |
|
336 |
assertTrue(s.contains(f), "Expected output of " + command + " to contain: " + f); |
|
337 |
} |
|
338 |
}); |
|
339 |
} |
|
340 |
||
33362 | 341 |
public void oneLineOfError() { |
342 |
test( |
|
343 |
(a) -> assertCommand(a, "12+", null), |
|
344 |
(a) -> assertCommandCheckOutput(a, " true", (s) -> |
|
345 |
assertTrue(s.contains("12+") && !s.contains("true"), "Output: '" + s + "'")) |
|
346 |
); |
|
347 |
} |
|
348 |
||
349 |
public void defineVariables() { |
|
350 |
test( |
|
351 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
352 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
|
353 |
(a) -> assertVariable(a, "int", "a"), |
|
354 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
355 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
|
356 |
(a) -> assertVariable(a, "double", "a", "1", "1.0"), |
|
357 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
358 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
|
359 |
(a) -> evaluateExpression(a, "double", "2 * a", "2.0"), |
|
360 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
361 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()) |
|
362 |
); |
|
363 |
} |
|
364 |
||
365 |
public void defineMethods() { |
|
366 |
test( |
|
367 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
368 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
|
369 |
(a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"), |
|
370 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
371 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
|
372 |
(a) -> assertMethod(a, "void f(int a) { g(); }", "(int)void", "f"), |
|
373 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
374 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
|
375 |
(a) -> assertMethod(a, "void g() {}", "()void", "g"), |
|
376 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
377 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()) |
|
378 |
); |
|
379 |
} |
|
380 |
||
381 |
public void defineClasses() { |
|
382 |
test( |
|
383 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
384 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
|
385 |
(a) -> assertClass(a, "class A { }", "class", "A"), |
|
386 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
387 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
|
388 |
(a) -> assertClass(a, "interface A { }", "interface", "A"), |
|
389 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
390 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
|
391 |
(a) -> assertClass(a, "enum A { }", "enum", "A"), |
|
392 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
393 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
|
394 |
(a) -> assertClass(a, "@interface A { }", "@interface", "A"), |
|
395 |
(a) -> assertCommandCheckOutput(a, "/list", assertList()), |
|
396 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()) |
|
397 |
); |
|
398 |
} |
|
399 |
||
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
400 |
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
|
401 |
test( |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
402 |
(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
|
403 |
(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
|
404 |
(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
|
405 |
(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
|
406 |
(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
|
407 |
(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
|
408 |
(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
|
409 |
(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
|
410 |
(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
|
411 |
(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
|
412 |
(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
|
413 |
(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
|
414 |
(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
|
415 |
(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
|
416 |
); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
417 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
418 |
|
33362 | 419 |
public void testClasspathDirectory() { |
420 |
Compiler compiler = new Compiler(); |
|
421 |
Path outDir = Paths.get("testClasspathDirectory"); |
|
422 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
423 |
Path classpath = compiler.getPath(outDir); |
|
424 |
test( |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
425 |
(a) -> assertCommand(a, "/classpath " + classpath, String.format("| Path %s added to classpath\n", classpath)), |
33362 | 426 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
427 |
); |
|
428 |
test(new String[] { "-cp", classpath.toString() }, |
|
429 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
430 |
); |
|
431 |
test(new String[] { "-classpath", classpath.toString() }, |
|
432 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
433 |
); |
|
434 |
} |
|
435 |
||
436 |
public void testClasspathJar() { |
|
437 |
Compiler compiler = new Compiler(); |
|
438 |
Path outDir = Paths.get("testClasspathJar"); |
|
439 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
440 |
String jarName = "test.jar"; |
|
441 |
compiler.jar(outDir, jarName, "pkg/A.class"); |
|
442 |
Path jarPath = compiler.getPath(outDir).resolve(jarName); |
|
443 |
test( |
|
444 |
(a) -> assertCommand(a, "/classpath " + jarPath, String.format("| Path %s added to classpath\n", jarPath)), |
|
445 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
446 |
); |
|
447 |
test(new String[] { "-cp", jarPath.toString() }, |
|
448 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
449 |
); |
|
450 |
test(new String[] { "-classpath", jarPath.toString() }, |
|
451 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"") |
|
452 |
); |
|
453 |
} |
|
454 |
||
455 |
public void testStartupFileOption() { |
|
456 |
try { |
|
457 |
Compiler compiler = new Compiler(); |
|
458 |
Path startup = compiler.getPath("StartupFileOption/startup.txt"); |
|
459 |
compiler.writeToFile(startup, "class A { public String toString() { return \"A\"; } }"); |
|
460 |
test(new String[]{"-startup", startup.toString()}, |
|
461 |
(a) -> evaluateExpression(a, "A", "new A()", "\"A\"\n") |
|
462 |
); |
|
463 |
test(new String[]{"-nostartup"}, |
|
464 |
(a) -> assertCommandCheckOutput(a, "printf(\"\")", assertStartsWith("| Error:\n| cannot find symbol")) |
|
465 |
); |
|
466 |
test((a) -> assertCommand(a, "printf(\"A\")", "", "", null, "A", "")); |
|
467 |
test(false, new String[]{"-startup", "UNKNOWN"}, "| File 'UNKNOWN' for start-up is not found."); |
|
468 |
} finally { |
|
469 |
removeStartup(); |
|
470 |
} |
|
471 |
} |
|
472 |
||
473 |
public void testLoadingFromArgs() { |
|
474 |
Compiler compiler = new Compiler(); |
|
475 |
Path path = compiler.getPath("loading.repl"); |
|
476 |
compiler.writeToFile(path, "int a = 10; double x = 20; double a = 10;"); |
|
477 |
test(new String[] { path.toString() }, |
|
478 |
(a) -> assertCommand(a, "x", "| Variable x of type double has value 20.0\n"), |
|
479 |
(a) -> assertCommand(a, "a", "| Variable a of type double has value 10.0\n") |
|
480 |
); |
|
481 |
Path unknown = compiler.getPath("UNKNOWN.jar"); |
|
482 |
test(true, new String[]{unknown.toString()}, |
|
483 |
"| File '" + unknown |
|
35002 | 484 |
+ "' is not found: " + unresolvableMessage(unknown) + "\n"); |
33362 | 485 |
} |
486 |
||
487 |
public void testReset() { |
|
488 |
test( |
|
489 |
(a) -> assertReset(a, "/r"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
490 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 491 |
(a) -> assertVariable(a, "int", "x"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
492 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 493 |
(a) -> assertMethod(a, "void f() { }", "()void", "f"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
494 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 495 |
(a) -> assertClass(a, "class A { }", "class", "A"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
496 |
(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
|
497 |
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
498 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
33362 | 499 |
(a) -> assertReset(a, "/reset"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
500 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
501 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
502 |
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
503 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 504 |
); |
505 |
} |
|
506 |
||
507 |
public void testOpen() { |
|
508 |
Compiler compiler = new Compiler(); |
|
509 |
Path path = compiler.getPath("testOpen.repl"); |
|
510 |
compiler.writeToFile(path, |
|
511 |
"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
|
512 |
"class A { public String toString() { return \"A\"; } }\nimport java.util.stream.*;"); |
33362 | 513 |
for (String s : new String[]{"/o", "/open"}) { |
514 |
test( |
|
515 |
(a) -> assertCommand(a, s + " " + path.toString(), ""), |
|
516 |
(a) -> assertCommand(a, "a", "| Variable a of type double has value 10.0\n"), |
|
517 |
(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
|
518 |
(a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"), |
33362 | 519 |
(a) -> { |
520 |
loadVariable(a, "double", "x", "20.0", "20.0"); |
|
521 |
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
|
522 |
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
|
523 |
loadVariable(a, "long", "$8", "Stream.of(\"A\").count();", "1"); |
33362 | 524 |
loadClass(a, "class A { public String toString() { return \"A\"; } }", |
525 |
"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
|
526 |
loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*"); |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
527 |
assertCommandCheckOutput(a, "/classes", assertClasses()); |
33362 | 528 |
}, |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
529 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
530 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
531 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 532 |
); |
533 |
Path unknown = compiler.getPath("UNKNOWN.repl"); |
|
534 |
test( |
|
535 |
(a) -> assertCommand(a, s + " " + unknown, |
|
536 |
"| File '" + unknown |
|
35002 | 537 |
+ "' is not found: " + unresolvableMessage(unknown) + "\n") |
33362 | 538 |
); |
539 |
} |
|
540 |
} |
|
541 |
||
542 |
public void testSave() throws IOException { |
|
543 |
Compiler compiler = new Compiler(); |
|
544 |
Path path = compiler.getPath("testSave.repl"); |
|
545 |
List<String> list = Arrays.asList( |
|
546 |
"int a;", |
|
547 |
"class A { public String toString() { return \"A\"; } }" |
|
548 |
); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
549 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
550 |
(a) -> assertVariable(a, "int", "a"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
551 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
552 |
(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
|
553 |
(a) -> assertCommand(a, "/save " + path.toString(), "") |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
554 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
555 |
assertEquals(Files.readAllLines(path), list); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
556 |
{ |
33362 | 557 |
List<String> output = new ArrayList<>(); |
558 |
test( |
|
559 |
(a) -> assertCommand(a, "int a;", null), |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
560 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
33362 | 561 |
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"), |
562 |
(a) -> assertCommandCheckOutput(a, "/list all", (out) -> |
|
563 |
output.addAll(Stream.of(out.split("\n")) |
|
564 |
.filter(str -> !str.isEmpty()) |
|
565 |
.map(str -> str.substring(str.indexOf(':') + 2)) |
|
566 |
.filter(str -> !str.startsWith("/")) |
|
567 |
.collect(Collectors.toList()))), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
568 |
(a) -> assertCommand(a, "/save all " + path.toString(), "") |
33362 | 569 |
); |
570 |
assertEquals(Files.readAllLines(path), output); |
|
571 |
} |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
572 |
List<String> output = new ArrayList<>(); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
573 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
574 |
(a) -> assertVariable(a, "int", "a"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
575 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
576 |
(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
|
577 |
(a) -> assertCommandCheckOutput(a, "/history", (out) -> |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
578 |
output.addAll(Stream.of(out.split("\n")) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
579 |
.filter(str -> !str.isEmpty()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
580 |
.collect(Collectors.toList()))), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
581 |
(a) -> assertCommand(a, "/save history " + path.toString(), "") |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
582 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
583 |
output.add("/save history " + path.toString()); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
584 |
assertEquals(Files.readAllLines(path), output); |
33362 | 585 |
} |
586 |
||
587 |
public void testStartSet() throws BackingStoreException { |
|
588 |
try { |
|
589 |
Compiler compiler = new Compiler(); |
|
590 |
Path startUpFile = compiler.getPath("startUp.txt"); |
|
591 |
test( |
|
592 |
(a) -> assertVariable(a, "int", "a"), |
|
593 |
(a) -> assertVariable(a, "double", "b", "10", "10.0"), |
|
594 |
(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
|
595 |
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
596 |
(a) -> assertCommand(a, "/save " + startUpFile.toString(), null), |
33362 | 597 |
(a) -> assertCommand(a, "/setstart " + startUpFile.toString(), null) |
598 |
); |
|
599 |
Path unknown = compiler.getPath("UNKNOWN"); |
|
600 |
test( |
|
601 |
(a) -> assertCommand(a, "/setstart " + unknown.toString(), |
|
602 |
"| File '" + unknown + "' for /setstart is not found.\n") |
|
603 |
); |
|
604 |
test(false, new String[0], |
|
605 |
(a) -> { |
|
606 |
loadVariable(a, "int", "a"); |
|
607 |
loadVariable(a, "double", "b", "10.0", "10.0"); |
|
608 |
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
|
609 |
loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*"); |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
610 |
assertCommandCheckOutput(a, "/classes", assertClasses()); |
33362 | 611 |
}, |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
612 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
613 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
614 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 615 |
); |
616 |
} finally { |
|
617 |
removeStartup(); |
|
618 |
} |
|
619 |
} |
|
620 |
||
621 |
private void removeStartup() { |
|
622 |
Preferences preferences = Preferences.userRoot().node("tool/REPL"); |
|
623 |
if (preferences != null) { |
|
624 |
preferences.remove("STARTUP"); |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
public void testUnknownCommand() { |
|
629 |
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
|
630 |
"| No such command or snippet id: /unknown\n" + |
33362 | 631 |
"| Type /help for help.\n")); |
632 |
} |
|
633 |
||
634 |
public void testEmptyClassPath() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
635 |
test(after -> assertCommand(after, "/classpath", "| /classpath requires a path argument\n")); |
33362 | 636 |
} |
637 |
||
638 |
public void testNoArgument() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
639 |
String[] commands = {"/save", "/open", "/setstart"}; |
33362 | 640 |
test(Stream.of(commands) |
641 |
.map(cmd -> { |
|
642 |
String c = cmd; |
|
643 |
final String finalC = c; |
|
644 |
return (ReplTest) after -> assertCommand(after, cmd, |
|
645 |
"| The " + finalC + " command requires a filename argument.\n"); |
|
646 |
}) |
|
647 |
.toArray(ReplTest[]::new)); |
|
648 |
} |
|
649 |
||
650 |
public void testStartSave() throws IOException { |
|
651 |
Compiler compiler = new Compiler(); |
|
652 |
Path startSave = compiler.getPath("startSave.txt"); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
653 |
test(a -> assertCommand(a, "/save start " + startSave.toString(), null)); |
33362 | 654 |
List<String> lines = Files.lines(startSave) |
655 |
.filter(s -> !s.isEmpty()) |
|
656 |
.collect(Collectors.toList()); |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
657 |
assertEquals(lines, START_UP); |
33362 | 658 |
} |
659 |
||
660 |
public void testConstrainedUpdates() { |
|
661 |
test( |
|
662 |
a -> assertClass(a, "class XYZZY { }", "class", "XYZZY"), |
|
663 |
a -> assertVariable(a, "XYZZY", "xyzzy"), |
|
664 |
a -> assertCommandCheckOutput(a, "import java.util.stream.*", |
|
665 |
(out) -> assertTrue(out.trim().isEmpty(), "Expected no output, got: " + out)) |
|
666 |
); |
|
667 |
} |
|
668 |
||
669 |
public void testRemoteExit() { |
|
670 |
test( |
|
671 |
a -> assertVariable(a, "int", "x"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
672 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 673 |
a -> assertCommandCheckOutput(a, "System.exit(5);", s -> |
674 |
assertTrue(s.contains("terminated"), s)), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
675 |
a -> assertCommandCheckOutput(a, "/vars", s -> |
33362 | 676 |
assertTrue(s.trim().isEmpty(), s)), |
677 |
a -> assertMethod(a, "void f() { }", "()void", "f"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
678 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()) |
33362 | 679 |
); |
680 |
} |
|
681 |
||
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
682 |
// Check that each line of output contains the corresponding string from the list |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
683 |
private void checkLineToList(String in, List<String> match) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
684 |
String[] res = in.trim().split("\n"); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
685 |
assertEquals(res.length, match.size(), "Got: " + Arrays.asList(res)); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
686 |
for (int i = 0; i < match.size(); ++i) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
687 |
assertTrue(res[i].contains(match.get(i))); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
688 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
689 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
690 |
|
33362 | 691 |
public void testListArgs() { |
692 |
String arg = "qqqq"; |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
693 |
List<String> startVarList = new ArrayList<>(START_UP); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
694 |
startVarList.add("int aardvark"); |
33362 | 695 |
test( |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
696 |
a -> assertCommandCheckOutput(a, "/list all", |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
697 |
s -> checkLineToList(s, START_UP)), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
698 |
a -> assertCommandCheckOutput(a, "/list " + arg, |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
699 |
s -> assertEquals(s, "| No definition or id named " + arg + |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
700 |
" found. There are no active definitions.\n")), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
701 |
a -> assertVariable(a, "int", "aardvark"), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
702 |
a -> assertCommandCheckOutput(a, "/list aardvark", |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
703 |
s -> assertTrue(s.contains("aardvark"))), |
35812 | 704 |
a -> assertCommandCheckOutput(a, "/list start", |
705 |
s -> checkLineToList(s, START_UP)), |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
706 |
a -> assertCommandCheckOutput(a, "/list all", |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
707 |
s -> checkLineToList(s, startVarList)), |
35812 | 708 |
a -> assertCommandCheckOutput(a, "/list printf", |
709 |
s -> assertTrue(s.contains("void printf"))), |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
710 |
a -> assertCommandCheckOutput(a, "/list " + arg, |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
711 |
s -> assertEquals(s, "| No definition or id named " + arg + |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
712 |
" found. Try /list without arguments.\n")) |
33362 | 713 |
); |
714 |
} |
|
715 |
||
716 |
public void testFeedbackNegative() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
717 |
test(a -> assertCommandCheckOutput(a, "/feedback aaaa", |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
718 |
assertStartsWith("| Follow /feedback with of the following"))); |
33362 | 719 |
} |
720 |
||
721 |
public void testFeedbackOff() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
722 |
for (String off : new String[]{"o", "off"}) { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
723 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
724 |
a -> assertCommand(a, "/feedback " + off, ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
725 |
a -> assertCommand(a, "int a", ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
726 |
a -> assertCommand(a, "void f() {}", ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
727 |
a -> assertCommandCheckOutput(a, "aaaa", assertStartsWith("| Error:")), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
728 |
a -> assertCommandCheckOutput(a, "public void f() {}", assertStartsWith("| Warning:")) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
729 |
); |
33362 | 730 |
} |
731 |
} |
|
732 |
||
733 |
public void testFeedbackConcise() { |
|
734 |
Compiler compiler = new Compiler(); |
|
735 |
Path testConciseFile = compiler.getPath("testConciseFeedback"); |
|
736 |
String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"}; |
|
737 |
compiler.writeToFile(testConciseFile, sources); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
738 |
for (String concise : new String[]{"c", "concise"}) { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
739 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
740 |
a -> assertCommand(a, "/feedback " + concise, ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
741 |
a -> assertCommand(a, sources[0], ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
742 |
a -> assertCommand(a, sources[1], ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
743 |
a -> assertCommand(a, sources[2], ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
744 |
a -> assertCommand(a, sources[3], "| a : 10\n"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
745 |
a -> assertCommand(a, "/o " + testConciseFile.toString(), "| a : 10\n") |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
746 |
); |
33362 | 747 |
} |
748 |
} |
|
749 |
||
750 |
public void testFeedbackNormal() { |
|
751 |
Compiler compiler = new Compiler(); |
|
752 |
Path testNormalFile = compiler.getPath("testConciseNormal"); |
|
753 |
String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"}; |
|
754 |
String[] sources2 = new String[] {"int a //again", "void f() {int y = 4;}", "class A {} //again", "a = 10"}; |
|
755 |
String[] output = new String[] { |
|
756 |
"| Added variable a of type int\n", |
|
757 |
"| Added method f()\n", |
|
758 |
"| Added class A\n", |
|
759 |
"| Variable a has been assigned the value 10\n" |
|
760 |
}; |
|
761 |
compiler.writeToFile(testNormalFile, sources2); |
|
762 |
for (String feedback : new String[]{"/f", "/feedback"}) { |
|
763 |
for (String feedbackState : new String[]{"n", "normal", "v", "verbose"}) { |
|
764 |
String f = null; |
|
765 |
if (feedbackState.startsWith("n")) { |
|
766 |
f = "normal"; |
|
767 |
} else if (feedbackState.startsWith("v")) { |
|
768 |
f = "verbose"; |
|
769 |
} |
|
770 |
final String finalF = f; |
|
771 |
test( |
|
772 |
a -> assertCommand(a, feedback + " " + feedbackState, "| Feedback mode: " + finalF +"\n"), |
|
773 |
a -> assertCommand(a, sources[0], output[0]), |
|
774 |
a -> assertCommand(a, sources[1], output[1]), |
|
775 |
a -> assertCommand(a, sources[2], output[2]), |
|
776 |
a -> assertCommand(a, sources[3], output[3]), |
|
777 |
a -> assertCommand(a, "/o " + testNormalFile.toString(), |
|
778 |
"| Modified variable a of type int\n" + |
|
779 |
"| Modified method f()\n" + |
|
780 |
"| Update overwrote method f()\n" + |
|
781 |
"| Modified class A\n" + |
|
782 |
"| Update overwrote class A\n" + |
|
783 |
"| Variable a has been assigned the value 10\n") |
|
784 |
); |
|
785 |
} |
|
786 |
} |
|
787 |
} |
|
788 |
||
789 |
public void testFeedbackDefault() { |
|
790 |
Compiler compiler = new Compiler(); |
|
791 |
Path testDefaultFile = compiler.getPath("testDefaultFeedback"); |
|
792 |
String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"}; |
|
793 |
String[] output = new String[] { |
|
794 |
"| Added variable a of type int\n", |
|
795 |
"| Added method f()\n", |
|
796 |
"| Added class A\n", |
|
797 |
"| Variable a has been assigned the value 10\n" |
|
798 |
}; |
|
799 |
compiler.writeToFile(testDefaultFile, sources); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
800 |
for (String defaultFeedback : new String[]{"", "d", "default"}) { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
801 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
802 |
a -> assertCommand(a, "/feedback o", ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
803 |
a -> assertCommand(a, "int x", ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
804 |
a -> assertCommand(a, "/feedback " + defaultFeedback, "| Feedback mode: default\n"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
805 |
a -> assertCommand(a, sources[0], output[0]), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
806 |
a -> assertCommand(a, sources[1], output[1]), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
807 |
a -> assertCommand(a, sources[2], output[2]), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
808 |
a -> assertCommand(a, sources[3], output[3]), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
809 |
a -> assertCommand(a, "/o " + testDefaultFile.toString(), "") |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
810 |
); |
33362 | 811 |
} |
812 |
} |
|
813 |
||
814 |
public void testDrop() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
815 |
test(false, new String[]{"-nostartup"}, |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
816 |
a -> assertVariable(a, "int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
817 |
a -> dropVariable(a, "/drop 1", "int a = 0"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
818 |
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
819 |
a -> dropMethod(a, "/drop 2", "b ()I"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
820 |
a -> assertClass(a, "class A {}", "class", "A"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
821 |
a -> dropClass(a, "/drop 3", "class A"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
822 |
a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
823 |
a -> dropImport(a, "/drop 4", "import java.util.stream.*"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
824 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
825 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
826 |
a -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
827 |
a -> assertCommandCheckOutput(a, "/imports", assertImports()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
828 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
829 |
test(false, new String[]{"-nostartup"}, |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
830 |
a -> assertVariable(a, "int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
831 |
a -> dropVariable(a, "/drop a", "int a = 0"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
832 |
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
833 |
a -> dropMethod(a, "/drop b", "b ()I"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
834 |
a -> assertClass(a, "class A {}", "class", "A"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
835 |
a -> dropClass(a, "/drop A", "class A"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
836 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
837 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
838 |
a -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
839 |
a -> assertCommandCheckOutput(a, "/imports", assertImports()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
840 |
); |
33362 | 841 |
} |
842 |
||
843 |
public void testDropNegative() { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
844 |
test(false, new String[]{"-nostartup"}, |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
845 |
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
|
846 |
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
|
847 |
a -> assertCommandCheckOutput(a, "/drop", |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
848 |
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
|
849 |
a -> assertVariable(a, "int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
850 |
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
|
851 |
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
|
852 |
); |
33362 | 853 |
} |
854 |
||
855 |
public void testAmbiguousDrop() { |
|
856 |
Consumer<String> check = s -> { |
|
857 |
assertTrue(s.startsWith("| The argument references more than one import, variable, method, or class"), s); |
|
858 |
int lines = s.split("\n").length; |
|
859 |
assertEquals(lines, 5, "Expected 3 ambiguous keys, but found: " + (lines - 2) + "\n" + s); |
|
860 |
}; |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
861 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
862 |
a -> assertVariable(a, "int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
863 |
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
864 |
a -> assertClass(a, "class a {}", "class", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
865 |
a -> assertCommandCheckOutput(a, "/drop a", check), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
866 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
867 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
868 |
a -> assertCommandCheckOutput(a, "/classes", assertClasses()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
869 |
a -> assertCommandCheckOutput(a, "/imports", assertImports()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
870 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
871 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
872 |
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
873 |
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
|
874 |
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
|
875 |
a -> assertCommandCheckOutput(a, "/drop a", check), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
876 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
877 |
); |
33362 | 878 |
} |
879 |
||
880 |
public void testHistoryReference() { |
|
881 |
test(false, new String[]{"-nostartup"}, |
|
882 |
a -> assertCommand(a, "System.err.println(1)", "", "", null, "", "1\n"), |
|
883 |
a -> assertCommand(a, "System.err.println(2)", "", "", null, "", "2\n"), |
|
884 |
a -> assertCommand(a, "/-2", "System.err.println(1)\n", "", null, "", "1\n"), |
|
885 |
a -> assertCommand(a, "/history", "\n" + |
|
886 |
"/debug 0\n" + |
|
887 |
"System.err.println(1)\n" + |
|
888 |
"System.err.println(2)\n" + |
|
889 |
"System.err.println(1)\n" + |
|
890 |
"/history\n"), |
|
891 |
a -> assertCommand(a, "/-2", "System.err.println(2)\n", "", null, "", "2\n"), |
|
892 |
a -> assertCommand(a, "/!", "System.err.println(2)\n", "", null, "", "2\n"), |
|
893 |
a -> assertCommand(a, "/2", "System.err.println(2)\n", "", null, "", "2\n"), |
|
894 |
a -> assertCommand(a, "/1", "System.err.println(1)\n", "", null, "", "1\n") |
|
895 |
); |
|
896 |
} |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
897 |
|
35002 | 898 |
private String unresolvableMessage(Path p) { |
899 |
try { |
|
900 |
new FileInputStream(p.toFile()); |
|
901 |
throw new AssertionError("Expected exception did not occur."); |
|
902 |
} catch (IOException ex) { |
|
903 |
return ex.getMessage(); |
|
904 |
} |
|
905 |
} |
|
906 |
||
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
907 |
public void testCommandPrefix() { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
908 |
test(a -> assertCommandCheckOutput(a, "/s", |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
909 |
assertStartsWith("| Command: /s is ambiguous: /seteditor, /save, /setstart")), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
910 |
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
|
911 |
a -> assertCommandCheckOutput(a, "/va", |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
912 |
assertStartsWith("| int var = 0")), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
913 |
a -> assertCommandCheckOutput(a, "/save", |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
914 |
assertStartsWith("| The /save command requires a filename argument."))); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
915 |
} |
33362 | 916 |
} |