8143955: JShell tool (UX): Output structure

8143956: JShell tool (UX): default prompts

Reviewed-by: jlahoda
This commit is contained in:
Robert Field 2016-04-12 22:23:11 -07:00
parent 70dbeeb6df
commit 1889a6c4fa
11 changed files with 440 additions and 397 deletions

View File

@ -1077,12 +1077,12 @@ public class JShellTool {
ed.add(n);
}
editor = ed.toArray(new String[ed.size()]);
fluffmsg("jshell.msg.set.editor.set", arg);
fluffmsg("jshell.msg.set.editor.set", prog);
return true;
}
}
case "start": {
String init = readFile(at.next(), "'/set start'");
String init = readFile(at.next(), "/set start");
if (init == null) {
return false;
} else {
@ -1351,7 +1351,7 @@ public class JShellTool {
.filter(sn -> state.status(sn).isActive && sn instanceof PersistentSnippet)
.collect(toList());
if (snippets.isEmpty()) {
errormsg("jshell.err.drop.active");
errormsg("jshell.err.drop.not.active");
return false;
}
if (snippets.size() > 1) {
@ -1499,7 +1499,7 @@ public class JShellTool {
}
private boolean cmdOpen(String filename) {
return runFile(filename, "'/open'");
return runFile(filename, "/open");
}
private boolean runFile(String filename, String context) {
@ -1533,7 +1533,7 @@ public class JShellTool {
} catch (AccessDeniedException e) {
errormsg("jshell.err.file.not.accessible", context, filename, e.getMessage());
} catch (NoSuchFileException e) {
errormsg("jshell.err.file.not.found", context, filename, e.getMessage());
errormsg("jshell.err.file.not.found", context, filename);
} catch (Exception e) {
errormsg("jshell.err.file.exception", context, filename, e);
}

View File

@ -42,7 +42,7 @@ jshell.err.arg = Invalid ''{0}'' argument: {1}
jshell.msg.see = See {0} for help.
jshell.err.file.not.accessible = File ''{1}'' for ''{0}'' is not accessible: {2}
jshell.err.file.not.found = File ''{1}'' for ''{0}'' is not found: {2}
jshell.err.file.not.found = File ''{1}'' for ''{0}'' is not found.
jshell.err.file.exception = File ''{1}'' for ''{0}'' threw exception: {2}
jshell.err.file.filename = ''{0}'' requires a filename argument.
@ -90,7 +90,7 @@ For example ''/help /list'' or ''/help intro''. Subjects:\n
jshell.err.drop.arg =\
In the /drop argument, please specify an import, variable, method, or class to drop.\n\
Specify by id or name. Use /list to see ids. Use /reset to reset all state.
jshell.msg.drop.not.active = The argument did not specify an active import, variable, method, or class to drop.
jshell.err.drop.not.active = The argument did not specify an active import, variable, method, or class to drop.
jshell.err.drop.ambiguous = The argument references more than one import, variable, method, or class.
jshell.err.failed = Failed.
jshell.msg.native.method = Native Method
@ -503,74 +503,86 @@ The contents of the specified <file> become the default start-up snippets and co
which are run when the jshell tool is started or reset.
startup.feedback = \
/set newmode normal command \n\
/set prompt normal '\\n-> ' '>> ' \n\
/set format normal pre '| ' \n\
/set format normal post '%n' \n\
/set format normal errorpre '| ' \n\
/set format normal errorpost '%n' \n\
\n\
/set format normal errorline '{post}{pre} {err}' \n\
\n\
/set format normal action 'Added' added-primary \n\
/set format normal action 'Modified' modified-primary \n\
/set format normal action 'Replaced' replaced-primary \n\
/set format normal action 'Overwrote' overwrote-primary \n\
/set format normal action 'Dropped' dropped-primary \n\
/set format normal action ' Update added' added-update \n\
/set format normal action ' Update modified' modified-update \n\
/set format normal action ' Update replaced' replaced-update \n\
/set format normal action ' Update overwrote' overwrote-update \n\
/set format normal action ' Update dropped' dropped-update \n\
\n\
/set format normal until ', however, it cannot be instanciated or its methods invoked until' defined-class-primary \n\
/set format normal until ', however, its methods cannot be invoked until' defined-interface-primary \n\
/set format normal until ', however, it cannot be used until' defined-enum,annotation-primary \n\
/set format normal until ', however, it cannot be invoked until' defined-method-primary \n\
/set format normal until ', however, it cannot be referenced until' notdefined-primary \n\
/set format normal until ' which cannot be instanciated or its methods invoked until' defined-class-update \n\
/set format normal until ' whose methods cannot be invoked until' defined-interface-update \n\
/set format normal until ' which cannot be invoked until' defined-method-update \n\
/set format normal until ' which cannot be referenced until' notdefined-update \n\
\n\
/set format normal unrerr '{unresolved} is declared' unresolved1-error0 \n\
/set format normal unrerr '{unresolved} are declared' unresolved2-error0 \n\
/set format normal unrerr ' this error is corrected: {errors}' unresolved0-error1 \n\
/set format normal unrerr '{unresolved} is declared and this error is corrected: {errors}' unresolved1-error1 \n\
/set format normal unrerr '{unresolved} are declared and this error is corrected: {errors}' unresolved2-error1 \n\
/set format normal unrerr ' these errors are corrected: {errors}' unresolved0-error2 \n\
/set format normal unrerr '{unresolved} is declared and these errors are corrected: {errors}' unresolved1-error2 \n\
/set format normal unrerr '{unresolved} are declared and these errors are corrected: {errors}' unresolved2-error2 \n\
\n\
/set format normal resolve '{until}{unrerr}' added,modified,replaced,used \n\
\n\
/set format normal typeKind 'class' class \n\
/set format normal typeKind 'interface' interface \n\
/set format normal typeKind 'enum' enum \n\
/set format normal typeKind 'annotation interface' annotation \n\
\n\
/set format normal display '{pre}{action} {typeKind} {name}{resolve}{post}' class,interface,enum,annotation \n\
/set format normal display '{pre}{action} method {name}({type}){resolve}{post}' method \n\
\n\
/set format normal display '{pre}{action} variable {name} of type {type}{resolve}{post}' vardecl \n\
/set format normal display '{pre}{action} variable {name} of type {type} with initial value {value}{resolve}{post}' varinit \n\
/set newmode verbose command \n\
\n\
/set prompt verbose '\\njshell> ' ' ...> ' \n\
\n\
/set format verbose pre '| ' \n\
/set format verbose post '%n' \n\
/set format verbose errorpre '| ' \n\
/set format verbose errorpost '%n' \n\
\n\
/set format verbose errorline '{pre} {err}' \n\
\n\
/set format verbose action 'created' added-primary \n\
/set format verbose action 'modified' modified-primary \n\
/set format verbose action 'replaced' replaced-primary \n\
/set format verbose action 'overwrote' overwrote-primary \n\
/set format verbose action 'dropped' dropped-primary \n\
/set format verbose action ' update created' added-update \n\
/set format verbose action ' update modified' modified-update \n\
/set format verbose action ' update replaced' replaced-update \n\
/set format verbose action ' update overwrote' overwrote-update \n\
/set format verbose action ' update dropped' dropped-update \n\
\n\
/set format verbose until ', however, it cannot be instanciated or its methods invoked until' defined-class-primary \n\
/set format verbose until ', however, its methods cannot be invoked until' defined-interface-primary \n\
/set format verbose until ', however, it cannot be used until' defined-enum,annotation-primary \n\
/set format verbose until ', however, it cannot be invoked until' defined-method-primary \n\
/set format verbose until ', however, it cannot be referenced until' notdefined-primary \n\
/set format verbose until ' which cannot be instanciated or its methods invoked until' defined-class-update \n\
/set format verbose until ' whose methods cannot be invoked until' defined-interface-update \n\
/set format verbose until ' which cannot be invoked until' defined-method-update \n\
/set format verbose until ' which cannot be referenced until' notdefined-update \n\
\n\
/set format verbose unrerr '{unresolved} is declared' unresolved1-error0 \n\
/set format verbose unrerr '{unresolved} are declared' unresolved2-error0 \n\
/set format verbose unrerr ' this error is corrected: {errors}' unresolved0-error1 \n\
/set format verbose unrerr '{unresolved} is declared and this error is corrected: {errors}' unresolved1-error1 \n\
/set format verbose unrerr '{unresolved} are declared and this error is corrected: {errors}' unresolved2-error1 \n\
/set format verbose unrerr ' these errors are corrected: {errors}' unresolved0-error2 \n\
/set format verbose unrerr '{unresolved} is declared and these errors are corrected: {errors}' unresolved1-error2 \n\
/set format verbose unrerr '{unresolved} are declared and these errors are corrected: {errors}' unresolved2-error2 \n\
\n\
/set format verbose resolve '{until}{unrerr}' added,modified,replaced,used \n\
\n\
/set format verbose typeKind 'class' class \n\
/set format verbose typeKind 'interface' interface \n\
/set format verbose typeKind 'enum' enum \n\
/set format verbose typeKind 'annotation interface' annotation \n\
\n\
/set format verbose result '{name} ==> {value}{post}' added,modified,replaced-ok-primary \n\
\n\
/set format verbose display '{result}{pre}created scratch variable {name} : {type}{post}' expression-primary \n\
/set format verbose display '{result}{pre}value of {name} : {type}{post}' varvalue-primary \n\
/set format verbose display '{result}{pre}assigned to {name} : {type}{post}' assignment-primary \n\
/set format verbose display '{result}{pre}{action} variable {name} : {type}{resolve}{post}' varinit,vardecl \n\
/set format verbose display '{pre}{action} variable {name}{resolve}{post}' vardecl,varinit-notdefined \n\
/set format verbose display '{pre}{action} variable {name}{post}' dropped-vardecl,varinit \n\
/set format verbose display '{pre}{action} variable {name}, reset to null{post}' replaced-vardecl,varinit-ok-update \n\
\n\
/set format verbose display '{pre}{action} {typeKind} {name}{resolve}{post}' class,interface,enum,annotation \n\
/set format verbose display '{pre}{action} method {name}({type}){resolve}{post}' method \n\
\n\
/set format verbose display '{pre}attempted to use {typeKind} {name}{resolve}{post}' used-class,interface,enum,annotation \n\
/set format verbose display '{pre}attempted to call method {name}({type}){resolve}{post}' used-method \n\
\n\
/set newmode normal command verbose \n\
/set format normal display '' added,modified,replaced,overwrote,dropped-update \n\
/set format normal display '{pre}{action} variable {name}, reset to null{post}' replaced-vardecl,varinit-ok-update \n\
/set format normal display '{pre}{action} variable {name}{resolve}{post}' vardecl,varinit-notdefined \n\
/set format normal display '{pre}{action} variable {name}{post}' overwrote,dropped-vardecl,varinit \n\
\n\
/set format normal display '{pre}Expression value is: {value}{post}{pre} assigned to temporary variable {name} of type {type}{post}' expression \n\
/set format normal display '{pre}Variable {name} of type {type} has value {value}{post}' varvalue \n\
/set format normal display '{pre}Variable {name} has been assigned the value {value}{post}' assignment \n\
\n\
/set format normal display '{pre}Attempted to use {typeKind} {name}{resolve}{post}' used-class,interface,enum,annotation \n\
/set format normal display '{pre}Attempted to call method {name}({type}){resolve}{post}' used-method \n\
\n\
/set format normal display '{result}' added,modified,replaced-expression,varvalue,assignment,varinit,vardecl-ok-primary \n\
/set newmode concise quiet normal \n\
\n\
/set prompt concise 'jshell> ' ' ...> ' \n\
\n\
/set format concise display '' class,interface,enum,annotation,method,assignment,varinit,vardecl-ok \n\
\n\
/set feedback normal \n\
\n\
/set newmode off quiet \n\
/set prompt off '-> ' '>> ' \n\
/set format off pre '| ' \n\
/set format off post '%n' \n\
/set format off errorpre '| ' \n\
/set format off errorpost '%n' \n\
/set format off display '' \n
\n\
/set newmode silent quiet \n\
/set prompt silent '-> ' '>> ' \n\
/set format silent pre '| ' \n\
/set format silent post '%n' \n\
/set format silent errorpre '| ' \n\
/set format silent errorpost '%n' \n\
/set format silent display '' \n

View File

@ -465,16 +465,40 @@ class Eval {
// If appropriate, execute the snippet
String value = null;
Exception exception = null;
if (si.isExecutable() && si.status().isDefined) {
try {
value = state.executionControl().commandInvoke(state.maps.classFullName(si));
value = si.subKind().hasValue()
? expunge(value)
: "";
} catch (EvalException ex) {
exception = translateExecutionException(ex);
} catch (UnresolvedReferenceException ex) {
exception = ex;
if (si.status().isDefined) {
if (si.isExecutable()) {
try {
value = state.executionControl().commandInvoke(state.maps.classFullName(si));
value = si.subKind().hasValue()
? expunge(value)
: "";
} catch (EvalException ex) {
exception = translateExecutionException(ex);
} catch (UnresolvedReferenceException ex) {
exception = ex;
}
} else if (si.subKind() == SubKind.VAR_DECLARATION_SUBKIND) {
switch (((VarSnippet) si).typeName()) {
case "byte":
case "short":
case "int":
case "long":
value = "0";
break;
case "float":
case "double":
value = "0.0";
break;
case "boolean":
value = "false";
break;
case "char":
value = "''";
break;
default:
value = "null";
break;
}
}
}
return events(c, outs, value, exception);

View File

@ -25,6 +25,7 @@
* @test
* @summary Testing built-in editor.
* @ignore 8139872
* @modules jdk.jshell/jdk.internal.jshell.tool
* @build ReplToolTesting EditorTestBase
* @run testng EditorPadTest
*/
@ -67,7 +68,7 @@ public class EditorPadTest extends EditorTestBase {
private static JButton exit = null;
@BeforeClass
public static void setUp() {
public static void setUpEditorPadTest() {
try {
robot = new Robot();
robot.setAutoWaitForIdle(true);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -65,19 +65,19 @@ public abstract class EditorTestBase extends ReplToolTesting {
}
public void assertEditOutput(boolean after, String cmd, String output, Action action) {
assertEditOutput(after, cmd, s -> assertEquals(s, output, "command"), action);
assertEditOutput(after, cmd, s -> assertEquals(s.trim(), output.trim(), "command"), action);
}
@Test
public void testEditNegative() {
for (String edit : new String[] {"/e", "/edit"}) {
for (String edit : new String[] {"/ed", "/edit"}) {
test(new String[]{"-nostartup"},
a -> assertCommand(a, edit + " 1",
"| No definition or id named 1 found. See /classes, /methods, /vars, or /list\n"),
a -> assertCommand(a, edit + " -1",
"| No definition or id named -1 found. See /classes, /methods, /vars, or /list\n"),
a -> assertCommand(a, edit + " unknown",
"| No definition or id named unknown found. See /classes, /methods, /vars, or /list\n")
a -> assertCommandOutputStartsWith(a, edit + " 1",
"| No definition or id found named: 1"),
a -> assertCommandOutputStartsWith(a, edit + " -1",
"| No definition or id found named: -1"),
a -> assertCommandOutputStartsWith(a, edit + " unknown",
"| No definition or id found named: unknown")
);
}
}
@ -86,7 +86,7 @@ public abstract class EditorTestBase extends ReplToolTesting {
public void testDoNothing() {
testEditor(
a -> assertVariable(a, "int", "a", "0", "0"),
a -> assertEditOutput(a, "/e 1", "", this::exit),
a -> assertEditOutput(a, "/ed 1", "", this::exit),
a -> assertCommandCheckOutput(a, "/v", assertVariables())
);
}
@ -95,12 +95,12 @@ public abstract class EditorTestBase extends ReplToolTesting {
public void testEditVariable1() {
testEditor(
a -> assertVariable(a, "int", "a", "0", "0"),
a -> assertEditOutput(a, "/e 1", "| Modified variable a of type int with initial value 10\n", () -> {
a -> assertEditOutput(a, "/ed 1", "a ==> 10", () -> {
writeSource("\n\n\nint a = 10;\n\n\n");
exit();
loadVariable(true, "int", "a", "10", "10");
}),
a -> assertEditOutput(a, "/e 1", "| Modified variable a of type int with initial value 15\n", () -> {
a -> assertEditOutput(a, "/ed 1", "a ==> 15", () -> {
writeSource("int a = 15;");
exit();
loadVariable(true, "int", "a", "15", "15");
@ -113,12 +113,12 @@ public abstract class EditorTestBase extends ReplToolTesting {
public void testEditVariable2() {
testEditor(
a -> assertVariable(a, "int", "a", "0", "0"),
a -> assertEditOutput(a, "/e 1", "| Added variable b of type int with initial value 10\n", () -> {
a -> assertEditOutput(a, "/ed 1", "b ==> 10", () -> {
writeSource("int b = 10;");
exit();
loadVariable(true, "int", "b", "10", "10");
}),
a -> assertEditOutput(a, "/e 1", "| Modified variable a of type int with initial value 15\n", () -> {
a -> assertEditOutput(a, "/ed 1", "a ==> 15", () -> {
writeSource("int a = 15;");
exit();
loadVariable(true, "int", "a", "15", "15");
@ -131,19 +131,18 @@ public abstract class EditorTestBase extends ReplToolTesting {
public void testEditClass1() {
testEditor(
a -> assertClass(a, "class A {}", "class", "A"),
a -> assertEditOutput(a, "/e 1", "", () -> {
a -> assertEditOutput(a, "/ed 1", "", () -> {
writeSource("\n\n\nclass A {}\n\n\n");
exit();
loadClass(true, "class A {}", "class", "A");
}),
a -> assertEditOutput(a, "/e 1",
"| Replaced enum A\n" +
"| Update overwrote class A\n", () -> {
a -> assertEditOutput(a, "/ed 1",
"| replaced enum A", () -> {
writeSource("enum A {}");
exit();
loadClass(true, "enum A {}", "enum", "A");
}),
a -> assertCommandCheckOutput(a, "/c", assertClasses())
a -> assertCommandCheckOutput(a, "/classes", assertClasses())
);
}
@ -151,19 +150,18 @@ public abstract class EditorTestBase extends ReplToolTesting {
public void testEditClass2() {
testEditor(
a -> assertClass(a, "class A {}", "class", "A"),
a -> assertEditOutput(a, "/e 1", "| Added class B\n", () -> {
a -> assertEditOutput(a, "/ed 1", "| created class B", () -> {
writeSource("class B { }");
exit();
loadClass(true, "class B {}", "class", "B");
}),
a -> assertEditOutput(a, "/e 1",
"| Replaced enum A\n" +
"| Update overwrote class A\n", () -> {
a -> assertEditOutput(a, "/ed 1",
"| replaced enum A", () -> {
writeSource("enum A {}");
exit();
loadClass(true, "enum A {}", "enum", "A");
}),
a -> assertCommandCheckOutput(a, "/c", assertClasses())
a -> assertCommandCheckOutput(a, "/classes", assertClasses())
);
}
@ -171,14 +169,13 @@ public abstract class EditorTestBase extends ReplToolTesting {
public void testEditMethod1() {
testEditor(
a -> assertMethod(a, "void f() {}", "()void", "f"),
a -> assertEditOutput(a, "/e 1", "", () -> {
a -> assertEditOutput(a, "/ed 1", "", () -> {
writeSource("\n\n\nvoid f() {}\n\n\n");
exit();
loadMethod(true, "void f() {}", "()void", "f");
}),
a -> assertEditOutput(a, "/e 1",
"| Replaced method f()\n" +
"| Update overwrote method f()\n", () -> {
a -> assertEditOutput(a, "/ed 1",
"| replaced method f()", () -> {
writeSource("double f() { return 0; }");
exit();
loadMethod(true, "double f() { return 0; }", "()double", "f");
@ -191,14 +188,13 @@ public abstract class EditorTestBase extends ReplToolTesting {
public void testEditMethod2() {
testEditor(
a -> assertMethod(a, "void f() {}", "()void", "f"),
a -> assertEditOutput(a, "/e 1", "| Added method g()\n", () -> {
a -> assertEditOutput(a, "/ed 1", "| created method g()", () -> {
writeSource("void g() {}");
exit();
loadMethod(true, "void g() {}", "()void", "g");
}),
a -> assertEditOutput(a, "/e 1",
"| Replaced method f()\n" +
"| Update overwrote method f()\n", () -> {
a -> assertEditOutput(a, "/ed 1",
"| replaced method f()", () -> {
writeSource("double f() { return 0; }");
exit();
loadMethod(true, "double f() { return 0; }", "()double", "f");
@ -213,7 +209,7 @@ public abstract class EditorTestBase extends ReplToolTesting {
a -> assertVariable(a, "int", "a"),
a -> assertMethod(a, "void f() {}", "()void", "f"),
a -> assertClass(a, "class A {}", "class", "A"),
a -> assertEditInput(a, "/e", s -> {
a -> assertEditInput(a, "/ed", s -> {
String[] ss = s.split("\n");
assertEquals(ss.length, 3, "Expected 3 lines: " + s);
assertEquals(ss[0], "int a;");
@ -226,15 +222,15 @@ public abstract class EditorTestBase extends ReplToolTesting {
@Test
public void testStartup() {
testEditor(true, new String[0],
a -> assertEditInput(a, "/e", s -> assertTrue(s.isEmpty(), "Checking of startup: " + s), this::cancel),
a -> assertEditInput(a, "/e printf", assertStartsWith("void printf"), this::cancel));
a -> assertEditInput(a, "/ed", s -> assertTrue(s.isEmpty(), "Checking of startup: " + s), this::cancel),
a -> assertEditInput(a, "/ed printf", assertStartsWith("void printf"), this::cancel));
}
@Test
public void testCancel() {
testEditor(
a -> assertVariable(a, "int", "a"),
a -> assertEditOutput(a, "/e a", "", () -> {
a -> assertEditOutput(a, "/ed a", "", () -> {
writeSource("int b = 10");
cancel();
})
@ -245,7 +241,7 @@ public abstract class EditorTestBase extends ReplToolTesting {
public void testAccept() {
testEditor(
a -> assertVariable(a, "int", "a"),
a -> assertEditOutput(a, "/e a", "| Added variable b of type int with initial value 10\n", () -> {
a -> assertEditOutput(a, "/ed a", "b ==> 10", () -> {
writeSource("int b = 10");
accept();
exit();

View File

@ -24,8 +24,9 @@
/*
* @test
* @summary Testing external editor.
* @bug 8080843
* @bug 8080843 8143955
* @ignore 8080843
* @modules jdk.jshell/jdk.internal.jshell.tool
* @build ReplToolTesting CustomEditor EditorTestBase
* @run testng ExternalEditorTest
*/
@ -124,7 +125,7 @@ public class ExternalEditorTest extends EditorTestBase {
}
@BeforeClass
public static void setUp() throws IOException {
public static void setUpExternalEditorTest() throws IOException {
listener = new ServerSocket(0);
listener.setSoTimeout(30000);
int localPort = listener.getLocalPort();
@ -193,11 +194,11 @@ public class ExternalEditorTest extends EditorTestBase {
@Test
public void setUnknownEditor() {
test(
a -> assertCommand(a, "/set editor", "| /set editor requires a path argument\n"),
a -> assertCommand(a, "/set editor UNKNOWN", "| Editor set to: UNKNOWN\n"),
a -> assertCommand(a, "/set editor", "| The '/set editor' command requires a path argument"),
a -> assertCommand(a, "/set editor UNKNOWN", "| Editor set to: UNKNOWN"),
a -> assertCommand(a, "int a;", null),
a -> assertCommand(a, "/e 1",
"| Edit Error: process IO failure: Cannot run program \"UNKNOWN\": error=2, No such file or directory\n")
a -> assertCommand(a, "/ed 1",
"| Edit Error: process IO failure: Cannot run program \"UNKNOWN\": error=2, No such file or directory")
);
}

View File

@ -273,8 +273,7 @@ public class ReplToolTesting {
}
public void evaluateExpression(boolean after, String type, String expr, String value) {
String output = String.format("\\| *Expression values is: %s\n|" +
" *.*temporary variable (\\$\\d+) of type %s", value, type);
String output = String.format("(\\$\\d+) ==> %s", value);
Pattern outputPattern = Pattern.compile(output);
assertCommandCheckOutput(after, expr, s -> {
Matcher matcher = outputPattern.matcher(s);
@ -558,14 +557,19 @@ public class ReplToolTesting {
@Override
public Consumer<String> checkOutput() {
String pattern = String.format("\\| *\\w+ variable %s of type %s", name, type);
if (initialValue != null) {
pattern += " with initial value " + initialValue;
}
Predicate<String> checkOutput = Pattern.compile(pattern).asPredicate();
final String finalPattern = pattern;
return output -> assertTrue(checkOutput.test(output),
"Output: " + output + " does not fit pattern: " + finalPattern);
String arrowPattern = String.format("%s ==> %s", name, value);
Predicate<String> arrowCheckOutput = Pattern.compile(arrowPattern).asPredicate();
String howeverPattern = String.format("\\| *\\w+ variable %s, however*.", name);
Predicate<String> howeverCheckOutput = Pattern.compile(howeverPattern).asPredicate();
return output -> {
if (output.startsWith("| ")) {
assertTrue(howeverCheckOutput.test(output),
"Output: " + output + " does not fit pattern: " + howeverPattern);
} else {
assertTrue(arrowCheckOutput.test(output),
"Output: " + output + " does not fit pattern: " + arrowPattern);
}
};
}
@Override

View File

@ -37,8 +37,8 @@ import org.testng.annotations.Test;
public class JShellToolTest8146368 extends ReplToolTesting {
public void test() {
test(
a -> assertCommand(a, "class A extends B {}", "| Added class A, however, it cannot be referenced until class B is declared\n"),
a -> assertCommand(a, "und m() { return new und(); }", "| Added method m(), however, it cannot be referenced until class und is declared\n")
a -> assertCommand(a, "class A extends B {}", "| created class A, however, it cannot be referenced until class B is declared\n"),
a -> assertCommand(a, "und m() { return new und(); }", "| created method m(), however, it cannot be referenced until class und is declared\n")
);
}
}

View File

@ -23,9 +23,13 @@
/*
* @test
* @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317
* @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955
* @summary Tests for Basic tests for REPL tool
* @requires os.family != "solaris"
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jdeps/com.sun.tools.javap
* jdk.jshell/jdk.internal.jshell.tool
* @library /tools/lib
* @ignore 8139873
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
@ -176,23 +180,23 @@ public class ToolBasicTest extends ReplToolTesting {
}
assertOutput(getCommandOutput(), "", "command");
assertOutput(getCommandErrorOutput(), "", "command error");
assertOutput(getUserOutput(), output, "user");
assertOutput(getUserOutput().trim(), output, "user");
assertOutput(getUserErrorOutput(), "", "user error");
}
}
public void testStop() {
test(
(a) -> assertStop(a, "while (true) {}", "Killed.\n"),
(a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "Killed.\n")
(a) -> assertStop(a, "while (true) {}", "Killed."),
(a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "Killed.")
);
}
@Test(enabled = false) // TODO 8130450
public void testRerun() {
test(false, new String[] {"-nostartup"},
(a) -> assertCommand(a, "/0", "| No such command or snippet id: /0\n| Type /help for help.\n"),
(a) -> assertCommand(a, "/5", "| No such command or snippet id: /5\n| Type /help for help.\n")
(a) -> assertCommand(a, "/0", "| No such command or snippet id: /0\n| Type /help for help."),
(a) -> assertCommand(a, "/5", "| No such command or snippet id: /5\n| Type /help for help.")
);
String[] codes = new String[] {
"int a = 0;", // var
@ -251,99 +255,9 @@ public class ToolBasicTest extends ReplToolTesting {
);
test(false, new String[] {"-nostartup"},
(a) -> assertCommand(a, "/s1", "| No such command or snippet id: /s1\n| Type /help for help.\n"),
(a) -> assertCommand(a, "/1", "| No such command or snippet id: /1\n| Type /help for help.\n"),
(a) -> assertCommand(a, "/e1", "| No such command or snippet id: /e1\n| Type /help for help.\n")
);
}
public void testRemaining() {
test(
(a) -> assertCommand(a, "int z; z =", "| Added variable z of type int\n"),
(a) -> assertCommand(a, "5", "| Variable z has been assigned the value 5\n"),
(a) -> assertCommand(a, "/*nada*/; int q =", ""),
(a) -> assertCommand(a, "77", "| Added variable q of type int with initial value 77\n"),
(a) -> assertCommand(a, "//comment;", ""),
(a) -> assertCommand(a, "int v;", "| Added variable v of type int\n"),
(a) -> assertCommand(a, "int v; int c", "| Added variable c of type int\n")
);
}
public void oneLineOfError() {
test(
(a) -> assertCommand(a, "12+", null),
(a) -> assertCommandCheckOutput(a, " true", (s) ->
assertTrue(s.contains("12+") && !s.contains("true"), "Output: '" + s + "'"))
);
}
public void defineVariables() {
test(
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertVariable(a, "int", "a"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertVariable(a, "double", "a", "1", "1.0"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> evaluateExpression(a, "double", "2 * a", "2.0"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables())
);
}
public void defineMethods() {
test(
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertMethod(a, "void f(int a) { g(); }", "(int)void", "f"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertMethod(a, "void g() {}", "()void", "g"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods())
);
}
public void defineClasses() {
test(
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
(a) -> assertClass(a, "class A { }", "class", "A"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
(a) -> assertClass(a, "interface A { }", "interface", "A"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
(a) -> assertClass(a, "enum A { }", "enum", "A"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
(a) -> assertClass(a, "@interface A { }", "@interface", "A"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses())
);
}
public void defineImports() {
test(
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import java.util.stream.Stream;", "", "java.util.stream.Stream"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import static java.lang.Math.PI;", "static", "java.lang.Math.PI"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import static java.lang.Math.*;", "static", "java.lang.Math.*"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports())
(a) -> assertCommand(a, "/s1", "| No such command or snippet id: /s1\n| Type /help for help."),
(a) -> assertCommand(a, "/1", "| No such command or snippet id: /1\n| Type /help for help."),
(a) -> assertCommand(a, "/e1", "| No such command or snippet id: /e1\n| Type /help for help.")
);
}
@ -353,14 +267,14 @@ public class ToolBasicTest extends ReplToolTesting {
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
Path classpath = compiler.getPath(outDir);
test(
(a) -> assertCommand(a, "/classpath " + classpath, String.format("| Path %s added to classpath\n", classpath)),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
(a) -> assertCommand(a, "/classpath " + classpath, String.format("| Path '%s' added to classpath", classpath)),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
test(new String[] { "-cp", classpath.toString() },
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
test(new String[] { "-classpath", classpath.toString() },
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
}
@ -372,14 +286,14 @@ public class ToolBasicTest extends ReplToolTesting {
compiler.jar(outDir, jarName, "pkg/A.class");
Path jarPath = compiler.getPath(outDir).resolve(jarName);
test(
(a) -> assertCommand(a, "/classpath " + jarPath, String.format("| Path %s added to classpath\n", jarPath)),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
(a) -> assertCommand(a, "/classpath " + jarPath, String.format("| Path '%s' added to classpath", jarPath)),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
test(new String[] { "-cp", jarPath.toString() },
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
test(new String[] { "-classpath", jarPath.toString() },
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
}
@ -389,7 +303,7 @@ public class ToolBasicTest extends ReplToolTesting {
Path startup = compiler.getPath("StartupFileOption/startup.txt");
compiler.writeToFile(startup, "class A { public String toString() { return \"A\"; } }");
test(new String[]{"-startup", startup.toString()},
(a) -> evaluateExpression(a, "A", "new A()", "\"A\"\n")
(a) -> evaluateExpression(a, "A", "new A()", "A")
);
test(new String[]{"-nostartup"},
(a) -> assertCommandCheckOutput(a, "printf(\"\")", assertStartsWith("| Error:\n| cannot find symbol"))
@ -406,18 +320,18 @@ public class ToolBasicTest extends ReplToolTesting {
Path path = compiler.getPath("loading.repl");
compiler.writeToFile(path, "int a = 10; double x = 20; double a = 10;");
test(new String[] { path.toString() },
(a) -> assertCommand(a, "x", "| Variable x of type double has value 20.0\n"),
(a) -> assertCommand(a, "a", "| Variable a of type double has value 10.0\n")
(a) -> assertCommand(a, "x", "x ==> 20.0"),
(a) -> assertCommand(a, "a", "a ==> 10.0")
);
Path unknown = compiler.getPath("UNKNOWN.jar");
test(Locale.ROOT, true, new String[]{unknown.toString()},
"| File " + unknown
+ " is not found: " + unresolvableMessage(unknown) + "\n");
+ " is not found: " + unresolvableMessage(unknown));
}
public void testReset() {
test(
(a) -> assertReset(a, "/r"),
(a) -> assertReset(a, "/res"),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertVariable(a, "int", "x"),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
@ -444,8 +358,8 @@ public class ToolBasicTest extends ReplToolTesting {
for (String s : new String[]{"/o", "/open"}) {
test(
(a) -> assertCommand(a, s + " " + path.toString(), ""),
(a) -> assertCommand(a, "a", "| Variable a of type double has value 10.0\n"),
(a) -> evaluateExpression(a, "A", "new A();", "\"A\""),
(a) -> assertCommand(a, "a", "a ==> 10.0"),
(a) -> evaluateExpression(a, "A", "new A();", "A"),
(a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"),
(a) -> {
loadVariable(a, "double", "x", "20.0", "20.0");
@ -464,8 +378,7 @@ public class ToolBasicTest extends ReplToolTesting {
Path unknown = compiler.getPath("UNKNOWN.repl");
test(
(a) -> assertCommand(a, s + " " + unknown,
"| File '" + unknown
+ "' is not found: " + unresolvableMessage(unknown) + "\n")
"| File '" + unknown + "' for '/open' is not found.")
);
}
}
@ -529,8 +442,8 @@ public class ToolBasicTest extends ReplToolTesting {
);
Path unknown = compiler.getPath("UNKNOWN");
test(
(a) -> assertCommand(a, "/set start " + unknown.toString(),
"| File '" + unknown + "' for /set start is not found.\n")
(a) -> assertCommandOutputStartsWith(a, "/set start " + unknown.toString(),
"| File '" + unknown + "' for '/set start' is not found.")
);
test(false, new String[0],
(a) -> {
@ -556,28 +469,6 @@ public class ToolBasicTest extends ReplToolTesting {
}
}
public void testUnknownCommand() {
test((a) -> assertCommand(a, "/unknown",
"| No such command or snippet id: /unknown\n" +
"| Type /help for help.\n"));
}
public void testEmptyClassPath() {
test(after -> assertCommand(after, "/classpath", "| /classpath requires a path argument\n"));
}
public void testNoArgument() {
String[] commands = {"/save", "/open", "/set start"};
test(Stream.of(commands)
.map(cmd -> {
String c = cmd;
final String finalC = c;
return (ReplTest) after -> assertCommand(after, cmd,
"| The " + finalC + " command requires a filename argument.\n");
})
.toArray(ReplTest[]::new));
}
public void testStartSave() throws IOException {
Compiler compiler = new Compiler();
Path startSave = compiler.getPath("startSave.txt");
@ -614,8 +505,8 @@ public class ToolBasicTest extends ReplToolTesting {
assertStartsWith("| Does not match any current feedback mode")));
}
public void testFeedbackOff() {
for (String off : new String[]{"o", "off"}) {
public void testFeedbackSilent() {
for (String off : new String[]{"s", "silent"}) {
test(
a -> assertCommand(a, "/set feedback " + off, ""),
a -> assertCommand(a, "int a", ""),
@ -632,16 +523,16 @@ public class ToolBasicTest extends ReplToolTesting {
String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"};
String[] sources2 = new String[] {"int a //again", "void f() {int y = 4;}", "class A {} //again", "a = 10"};
String[] output = new String[] {
"| Added variable a of type int\n",
"| Added method f()\n",
"| Added class A\n",
"| Variable a has been assigned the value 10\n"
"a ==> 0",
"| created method f()",
"| created class A",
"a ==> 10"
};
compiler.writeToFile(testNormalFile, sources2);
for (String feedback : new String[]{"/set f", "/set feedback"}) {
for (String feedbackState : new String[]{"n", "normal", "o", "off"}) {
for (String feedback : new String[]{"/set fe", "/set feedback"}) {
for (String feedbackState : new String[]{"n", "normal"}) {
test(
a -> assertCommand(a, feedback + " " + feedbackState, "| Feedback mode: normal\n"),
a -> assertCommand(a, feedback + " " + feedbackState, "| Feedback mode: normal"),
a -> assertCommand(a, sources[0], output[0]),
a -> assertCommand(a, sources[1], output[1]),
a -> assertCommand(a, sources[2], output[2]),
@ -652,87 +543,21 @@ public class ToolBasicTest extends ReplToolTesting {
}
}
public void testDrop() {
test(false, new String[]{"-nostartup"},
a -> assertVariable(a, "int", "a"),
a -> dropVariable(a, "/drop 1", "int a = 0", "| Dropped variable a\n"),
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
a -> dropMethod(a, "/drop 2", "b ()I", "| Dropped method b()\n"),
a -> assertClass(a, "class A {}", "class", "A"),
a -> dropClass(a, "/drop 3", "class A", "| Dropped class A\n"),
a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
a -> dropImport(a, "/drop 4", "import java.util.stream.*", ""),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
a -> assertCommandCheckOutput(a, "/imports", assertImports())
);
test(false, new String[]{"-nostartup"},
a -> assertVariable(a, "int", "a"),
a -> dropVariable(a, "/drop a", "int a = 0", "| Dropped variable a\n"),
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
a -> dropMethod(a, "/drop b", "b ()I", "| Dropped method b()\n"),
a -> assertClass(a, "class A {}", "class", "A"),
a -> dropClass(a, "/drop A", "class A", "| Dropped class A\n"),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
a -> assertCommandCheckOutput(a, "/imports", assertImports())
);
}
public void testDropNegative() {
test(false, new String[]{"-nostartup"},
a -> assertCommand(a, "/drop 0", "| No definition or id named 0 found. See /classes, /methods, /vars, or /list\n"),
a -> assertCommand(a, "/drop a", "| No definition or id named a found. See /classes, /methods, /vars, or /list\n"),
a -> assertCommandCheckOutput(a, "/drop",
assertStartsWith("| In the /drop argument, please specify an import, variable, method, or class to drop.")),
a -> assertVariable(a, "int", "a"),
a -> assertCommand(a, "a", "| Variable a of type int has value 0\n"),
a -> assertCommand(a, "/drop 2", "| The argument did not specify an active import, variable, method, or class to drop.\n")
);
}
public void testAmbiguousDrop() {
Consumer<String> check = s -> {
assertTrue(s.startsWith("| The argument references more than one import, variable, method, or class"), s);
int lines = s.split("\n").length;
assertEquals(lines, 5, "Expected 3 ambiguous keys, but found: " + (lines - 2) + "\n" + s);
};
test(
a -> assertVariable(a, "int", "a"),
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
a -> assertClass(a, "class a {}", "class", "a"),
a -> assertCommandCheckOutput(a, "/drop a", check),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
a -> assertCommandCheckOutput(a, "/imports", assertImports())
);
test(
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
a -> assertMethod(a, "double a(int a) { return 0; }", "(int)double", "a"),
a -> assertMethod(a, "double a(double a) { return 0; }", "(double)double", "a"),
a -> assertCommandCheckOutput(a, "/drop a", check),
a -> assertCommandCheckOutput(a, "/methods", assertMethods())
);
}
public void testHistoryReference() {
test(false, new String[]{"-nostartup"},
a -> assertCommand(a, "System.err.println(1)", "", "", null, "", "1\n"),
a -> assertCommand(a, "System.err.println(2)", "", "", null, "", "2\n"),
a -> assertCommand(a, "/-2", "System.err.println(1)\n", "", null, "", "1\n"),
a -> assertCommand(a, "/history", "\n" +
a -> assertCommand(a, "/-2", "System.err.println(1)", "", null, "", "1\n"),
a -> assertCommand(a, "/history",
"/debug 0\n" +
"System.err.println(1)\n" +
"System.err.println(2)\n" +
"System.err.println(1)\n" +
"/history\n"),
a -> assertCommand(a, "/-2", "System.err.println(2)\n", "", null, "", "2\n"),
a -> assertCommand(a, "/!", "System.err.println(2)\n", "", null, "", "2\n"),
a -> assertCommand(a, "/2", "System.err.println(2)\n", "", null, "", "2\n"),
a -> assertCommand(a, "/1", "System.err.println(1)\n", "", null, "", "1\n")
a -> assertCommand(a, "/-2", "System.err.println(2)", "", null, "", "2\n"),
a -> assertCommand(a, "/!", "System.err.println(2)", "", null, "", "2\n"),
a -> assertCommand(a, "/2", "System.err.println(2)", "", null, "", "2\n"),
a -> assertCommand(a, "/1", "System.err.println(1)", "", null, "", "1\n")
);
}
@ -744,14 +569,4 @@ public class ToolBasicTest extends ReplToolTesting {
return ex.getMessage();
}
}
public void testCommandPrefix() {
test(a -> assertCommandCheckOutput(a, "/s",
assertStartsWith("| Command: /s is ambiguous: /save, /set")),
a -> assertCommand(a, "int var", "| Added variable var of type int\n"),
a -> assertCommandCheckOutput(a, "/va",
assertStartsWith("| int var = 0")),
a -> assertCommandCheckOutput(a, "/save",
assertStartsWith("| The /save command requires a filename argument.")));
}
}

View File

@ -24,7 +24,7 @@
/*
* @test
* @key intermittent
* @bug 8081845 8147898
* @bug 8081845 8147898 8143955
* @summary Tests for /reload in JShell tool
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -71,7 +71,7 @@ public class ToolReloadTest extends ReplToolTesting {
Path classpath = compiler.getPath(outDir);
test(
(a) -> assertCommand(a, "/classpath " + classpath,
String.format("| Path '%s' added to classpath\n", classpath)),
String.format("| Path '%s' added to classpath", classpath)),
(a) -> assertMethod(a, "String foo() { return (new pkg.A()).toString(); }",
"()String", "foo"),
(a) -> assertVariable(a, "String", "v", "foo()", "\"A\""),
@ -83,20 +83,20 @@ public class ToolReloadTest extends ReplToolTesting {
"-: String foo() { return (new pkg.A()).toString(); }\n" +
"-: String v = foo();\n");
},
(a) -> assertCommand(a, "v", "| Variable v of type String has value \"Aprime\"\n"),
(a) -> assertCommand(a, "v", "v ==> \"Aprime\""),
(a) -> evaluateExpression(a, "String", "foo()", "\"Aprime\""),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"Aprime\"")
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "Aprime")
);
}
public void testReloadDrop() {
test(false, new String[]{"-nostartup"},
a -> assertVariable(a, "int", "a"),
a -> dropVariable(a, "/dr 1", "int a = 0", "| Dropped variable a\n"),
a -> dropVariable(a, "/dr 1", "int a = 0", "| dropped variable a"),
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
a -> dropMethod(a, "/drop b", "b ()I", "| Dropped method b()\n"),
a -> dropMethod(a, "/drop b", "b ()I", "| dropped method b()"),
a -> assertClass(a, "class A {}", "class", "A"),
a -> dropClass(a, "/dr A", "class A", "| Dropped class A\n"),
a -> dropClass(a, "/dr A", "class A", "| dropped class A"),
a -> assertCommand(a, "/reload",
"| Restarting and restoring state.\n" +
"-: int a;\n" +
@ -115,13 +115,13 @@ public class ToolReloadTest extends ReplToolTesting {
public void testReloadQuiet() {
test(false, new String[]{"-nostartup"},
a -> assertVariable(a, "int", "a"),
a -> dropVariable(a, "/dr 1", "int a = 0", "| Dropped variable a\n"),
a -> dropVariable(a, "/dr 1", "int a = 0", "| dropped variable a"),
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
a -> dropMethod(a, "/drop b", "b ()I", "| Dropped method b()\n"),
a -> dropMethod(a, "/drop b", "b ()I", "| dropped method b()"),
a -> assertClass(a, "class A {}", "class", "A"),
a -> dropClass(a, "/dr A", "class A", "| Dropped class A\n"),
a -> dropClass(a, "/dr A", "class A", "| dropped class A"),
a -> assertCommand(a, "/reload quiet",
"| Restarting and restoring state.\n"),
"| Restarting and restoring state."),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
@ -144,8 +144,8 @@ public class ToolReloadTest extends ReplToolTesting {
"-: ++c\n" +
"-: ++c\n"
),
(a) -> assertCommand(a, "c", "| Variable c of type int has value 11\n"),
(a) -> assertCommand(a, "$4", "| Variable $4 of type int has value 10\n")
(a) -> assertCommand(a, "c", "c ==> 11"),
(a) -> assertCommand(a, "$4", "$4 ==> 10")
);
}
@ -158,7 +158,7 @@ public class ToolReloadTest extends ReplToolTesting {
(a) -> assertCommand(a, "/vars", null),
(a) -> assertCommand(a, "/save abcd", null),
(a) -> assertCommand(a, "/reload",
"| Restarting and restoring state.\n")
"| Restarting and restoring state.")
);
}
@ -168,7 +168,7 @@ public class ToolReloadTest extends ReplToolTesting {
(a) -> assertMethod(a, "int m(int z) { return z * z; }",
"(int)int", "m"),
(a) -> evaluateExpression(a, "int", "m(x)", "25"),
(a) -> assertCommand(a, "/reset", "| Resetting state.\n"),
(a) -> assertCommand(a, "/reset", "| Resetting state."),
(a) -> assertCommand(a, "/reload restore",
"| Restarting and restoring from previous state.\n" +
"-: int x = 5;\n" +
@ -188,7 +188,7 @@ public class ToolReloadTest extends ReplToolTesting {
(a) -> evaluateExpression(a, "int", "m(x)", "25"),
(a) -> assertCommand(a, "System.exit(1);",
"| State engine terminated.\n" +
"| Restore definitions with: /reload restore\n"),
"| Restore definitions with: /reload restore"),
(a) -> assertCommand(a, "/reload restore",
"| Restarting and restoring from previous state.\n" +
"-: int x = 5;\n" +

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8153716
* @bug 8153716 8143955
* @summary Simple jshell tool tests
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -47,10 +47,102 @@ import static org.testng.Assert.assertTrue;
@Test
public class ToolSimpleTest extends ReplToolTesting {
public void testRemaining() {
test(
(a) -> assertCommand(a, "int z; z =", "z ==> 0"),
(a) -> assertCommand(a, "5", "z ==> 5"),
(a) -> assertCommand(a, "/*nada*/; int q =", ""),
(a) -> assertCommand(a, "77", "q ==> 77"),
(a) -> assertCommand(a, "//comment;", ""),
(a) -> assertCommand(a, "int v;", "v ==> 0"),
(a) -> assertCommand(a, "int v; int c",
"v ==> 0\n" +
"c ==> 0")
);
}
public void oneLineOfError() {
test(
(a) -> assertCommand(a, "12+", null),
(a) -> assertCommandCheckOutput(a, " true", (s) ->
assertTrue(s.contains("12+") && !s.contains("true"), "Output: '" + s + "'"))
);
}
public void defineVariables() {
test(
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertVariable(a, "int", "a"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertVariable(a, "double", "a", "1", "1.0"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> evaluateExpression(a, "double", "2 * a", "2.0"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables())
);
}
public void defineMethods() {
test(
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertMethod(a, "void f(int a) { g(); }", "(int)void", "f"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertMethod(a, "void g() {}", "()void", "g"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods())
);
}
public void defineClasses() {
test(
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
(a) -> assertClass(a, "class A { }", "class", "A"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
(a) -> assertClass(a, "interface A { }", "interface", "A"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
(a) -> assertClass(a, "enum A { }", "enum", "A"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
(a) -> assertClass(a, "@interface A { }", "@interface", "A"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/classes", assertClasses())
);
}
public void defineImports() {
test(
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import java.util.stream.Stream;", "", "java.util.stream.Stream"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import static java.lang.Math.PI;", "static", "java.lang.Math.PI"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import static java.lang.Math.*;", "static", "java.lang.Math.*"),
(a) -> assertCommandCheckOutput(a, "/list", assertList()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports())
);
}
public void defineVar() {
test(
(a) -> assertCommand(a, "int x = 72", "| Added variable x of type int with initial value 72"),
(a) -> assertCommand(a, "x", "| Variable x of type int has value 72"),
(a) -> assertCommand(a, "int x = 72", "x ==> 72"),
(a) -> assertCommand(a, "x", "x ==> 72"),
(a) -> assertCommand(a, "/vars", "| int x = 72")
);
}
@ -59,7 +151,7 @@ public class ToolSimpleTest extends ReplToolTesting {
public void defineUnresolvedVar() {
test(
(a) -> assertCommand(a, "undefined x",
"| Added variable x, however, it cannot be referenced until class undefined is declared"),
"| created variable x, however, it cannot be referenced until class undefined is declared"),
(a) -> assertCommand(a, "/vars", "| undefined x = (not-active)")
);
}
@ -67,15 +159,37 @@ public class ToolSimpleTest extends ReplToolTesting {
public void testUnresolved() {
test(
(a) -> assertCommand(a, "int f() { return g() + x + new A().a; }",
"| Added method f(), however, it cannot be invoked until method g(), variable x, and class A are declared"),
"| created method f(), however, it cannot be invoked until method g(), variable x, and class A are declared"),
(a) -> assertCommand(a, "f()",
"| Attempted to call method f() which cannot be invoked until method g(), variable x, and class A are declared"),
"| attempted to call method f() which cannot be invoked until method g(), variable x, and class A are declared"),
(a) -> assertCommandOutputStartsWith(a, "int g() { return x; }",
"| Added method g(), however, it cannot be invoked until variable x is declared"),
(a) -> assertCommand(a, "g()", "| Attempted to call method g() which cannot be invoked until variable x is declared")
"| created method g(), however, it cannot be invoked until variable x is declared"),
(a) -> assertCommand(a, "g()", "| attempted to call method g() which cannot be invoked until variable x is declared")
);
}
public void testUnknownCommand() {
test((a) -> assertCommand(a, "/unknown",
"| No such command or snippet id: /unknown\n" +
"| Type /help for help."));
}
public void testEmptyClassPath() {
test(after -> assertCommand(after, "/classpath", "| The /classpath command requires a path argument."));
}
public void testNoArgument() {
String[] commands = {"/save", "/open", "/set start"};
test(Stream.of(commands)
.map(cmd -> {
String c = cmd;
final String finalC = c;
return (ReplTest) after -> assertCommand(after, cmd,
"| '" + finalC + "' requires a filename argument.");
})
.toArray(ReplTest[]::new));
}
public void testDebug() {
test(
(a) -> assertCommand(a, "/deb", "| Debugging on"),
@ -85,6 +199,72 @@ public class ToolSimpleTest extends ReplToolTesting {
);
}
public void testDrop() {
test(false, new String[]{"-nostartup"},
a -> assertVariable(a, "int", "a"),
a -> dropVariable(a, "/drop 1", "int a = 0", "| dropped variable a"),
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
a -> dropMethod(a, "/drop 2", "b ()I", "| dropped method b()"),
a -> assertClass(a, "class A {}", "class", "A"),
a -> dropClass(a, "/drop 3", "class A", "| dropped class A"),
a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
a -> dropImport(a, "/drop 4", "import java.util.stream.*", ""),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
a -> assertCommandCheckOutput(a, "/imports", assertImports())
);
test(false, new String[]{"-nostartup"},
a -> assertVariable(a, "int", "a"),
a -> dropVariable(a, "/drop a", "int a = 0", "| dropped variable a"),
a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
a -> dropMethod(a, "/drop b", "b ()I", "| dropped method b()"),
a -> assertClass(a, "class A {}", "class", "A"),
a -> dropClass(a, "/drop A", "class A", "| dropped class A"),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
a -> assertCommandCheckOutput(a, "/imports", assertImports())
);
}
public void testDropNegative() {
test(false, new String[]{"-nostartup"},
a -> assertCommandOutputStartsWith(a, "/drop 0", "| No definition or id found named: 0"),
a -> assertCommandOutputStartsWith(a, "/drop a", "| No definition or id found named: a"),
a -> assertCommandCheckOutput(a, "/drop",
assertStartsWith("| In the /drop argument, please specify an import, variable, method, or class to drop.")),
a -> assertVariable(a, "int", "a"),
a -> assertCommand(a, "a", "a ==> 0"),
a -> assertCommand(a, "/drop 2", "| The argument did not specify an active import, variable, method, or class to drop.")
);
}
public void testAmbiguousDrop() {
Consumer<String> check = s -> {
assertTrue(s.startsWith("| The argument references more than one import, variable, method, or class"), s);
int lines = s.split("\n").length;
assertEquals(lines, 5, "Expected 3 ambiguous keys, but found: " + (lines - 2) + "\n" + s);
};
test(
a -> assertVariable(a, "int", "a"),
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
a -> assertClass(a, "class a {}", "class", "a"),
a -> assertCommandCheckOutput(a, "/drop a", check),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
a -> assertCommandCheckOutput(a, "/imports", assertImports())
);
test(
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
a -> assertMethod(a, "double a(int a) { return 0; }", "(int)double", "a"),
a -> assertMethod(a, "double a(double a) { return 0; }", "(double)double", "a"),
a -> assertCommandCheckOutput(a, "/drop a", check),
a -> assertCommandCheckOutput(a, "/methods", assertMethods())
);
}
public void testHelpLength() {
Consumer<String> testOutput = (s) -> {
List<String> ss = Stream.of(s.split("\n"))
@ -148,6 +328,16 @@ public class ToolSimpleTest extends ReplToolTesting {
);
}
public void testCommandPrefix() {
test(a -> assertCommandCheckOutput(a, "/s",
assertStartsWith("| Command: '/s' is ambiguous: /save, /set")),
a -> assertCommand(a, "int var", "var ==> 0"),
a -> assertCommandCheckOutput(a, "/va",
assertStartsWith("| int var = 0")),
a -> assertCommandCheckOutput(a, "/save",
assertStartsWith("| '/save' requires a filename argument.")));
}
public void testHeadlessEditPad() {
String prevHeadless = System.getProperty("java.awt.headless");
try {