/* * Copyright (c) 2016, 2021, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
@Test publicvoid defineUnresolvedVar() {
test(
(a) -> assertCommand(a, "undefined x", "| created variable x, however, it cannot be referenced until class undefined is declared"),
(a) -> assertCommand(a, "/vars", "| undefined x = (not-active)")
);
}
@Test publicvoid testUnresolved() {
test(
(a) -> assertCommand(a, "int f() { return g() + x + new A().a; }", "| 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"),
(a) -> assertCommandOutputStartsWith(a, "int g() { return x; }", "| 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")
);
}
@Test publicvoid testAbstractMethod() {
test(
(a) -> assertCommand(a, "abstract int f(int x);", "| created method f(int), however, it cannot be invoked until method f(int) is declared"),
(a) -> assertCommand(a, "f(13)", "| attempted to call method f(int) which cannot be invoked until method f(int) is declared"),
(a) -> assertCommand(a, " abstract void m(Blah b);", "| created method m(Blah), however, it cannot be referenced until class Blah, and method m(Blah) are declared")
);
}
// 8199623
@Test publicvoid testTwoForkedDrop() {
test(
(a) -> assertCommand(a, "void p() throws Exception { ((String) null).toString(); }", "| created method p()"),
(a) -> assertCommand(a, "void n() throws Exception { try { p(); } catch (Exception ex) { throw new IOException(\"bar\", ex); }} ", "| created method n()"),
(a) -> assertCommand(a, "void m() { try { n(); } catch (Exception ex) { throw new RuntimeException(\"foo\", ex); }}", "| created method m()"),
(a) -> assertCommand(a, "void c() throws Throwable { p(); }", "| created method c()"),
(a) -> assertCommand(a, "/drop p", "| dropped method p()"),
(a) -> assertCommand(a, "m()", "| attempted to call method n() which cannot be invoked until method p() is declared"),
(a) -> assertCommand(a, "/meth n", "| void n()\n" + "| which cannot be invoked until method p() is declared"),
(a) -> assertCommand(a, "/meth m", "| void m()"),
(a) -> assertCommand(a, "/meth c", "| void c()\n" + "| which cannot be invoked until method p() is declared")
);
}
@Test publicvoid testUnknownCommand() {
test((a) -> assertCommand(a, "/unknown", "| Invalid command: /unknown\n" + "| Type /help for help."));
}
@Test publicvoid testInvalidClassPath() {
test(
a -> assertCommand(a, "/env --class-path snurgefusal", "| File 'snurgefusal' for '--class-path' is not found."),
a -> assertCommand(a, "/env --class-path ?", "| File '?' for '--class-path' is not found.")
);
}
@Test publicvoid testNoArgument() {
test(
(a) -> assertCommand(a, "/save", "| '/save' requires a filename argument."),
(a) -> assertCommand(a, "/open", "| '/open' requires a filename argument."),
(a) -> assertCommandOutputStartsWith(a, "/drop", "| In the /drop argument, please specify an import, variable, method, or class to drop.")
);
}
@Test publicvoid testDrop() {
test(false, new String[]{"--no-startup"},
a -> assertVariable(a, "int", "a"),
a -> dropVariable(a, "/drop 1", "int a = 0", "| dropped variable a"),
a -> assertMethod(a, "int b() { return 0; }", "()int", "b"),
a -> dropMethod(a, "/drop 2", "int b()", "| 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 -> assertCommand(a, "for (int i = 0; i < 10; ++i) {}", ""),
a -> assertCommand(a, "/drop 5", ""),
a -> assertCommand(a, "/list", ""),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a -> assertCommandCheckOutput(a, "/types", assertClasses()),
a -> assertCommandCheckOutput(a, "/imports", assertImports())
);
test(false, new String[]{"--no-startup"},
a -> assertVariable(a, "int", "a"),
a -> dropVariable(a, "/drop a", "int a = 0", "| dropped variable a"),
a -> assertMethod(a, "int b() { return 0; }", "()int", "b"),
a -> dropMethod(a, "/drop b", "int b()", "| 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, "/types", assertClasses()),
a -> assertCommandCheckOutput(a, "/imports", assertImports())
);
}
@Test publicvoid testDropRange() {
test(false, new String[]{"--no-startup"},
a -> assertVariable(a, "int", "a"),
a -> assertMethod(a, "int b() { return 0; }", "()int", "b"),
a -> assertClass(a, "class A {}", "class", "A"),
a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
a -> assertCommand(a, "for (int i = 0; i < 10; ++i) {}", ""),
a -> assertCommand(a, "/drop 3-5 b 1", "| dropped class A\n" + "| dropped method b()\n" + "| dropped variable a\n"),
a -> assertCommand(a, "/list", "")
);
}
@Test publicvoid testDropNegative() {
test(false, new String[]{"--no-startup"},
a -> assertCommandOutputStartsWith(a, "/drop 0", "| No snippet with ID: 0"),
a -> assertCommandOutputStartsWith(a, "/drop a", "| No such snippet: 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", ""),
a -> assertCommand(a, "/drop 2", "| This command does not accept the snippet '2' : a\n" + "| See /types, /methods, /vars, or /list")
);
}
@Test publicvoid testAmbiguousDrop() {
test(
a -> assertVariable(a, "int", "a"),
a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
a -> assertClass(a, "class a {}", "class", "a"),
a -> assertCommand(a, "/drop a", "| dropped variable a\n" + "| dropped method a()\n" + "| dropped class a")
);
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 -> assertCommand(a, "/drop a", "| dropped method a()\n" + "| dropped method a(int)\n" + "| dropped method a(double)\n")
);
}
@Test publicvoid testApplicationOfPost() {
test(
(a) -> assertCommand(a, "/set mode t normal -command", "| Created new feedback mode: t"),
(a) -> assertCommand(a, "/set feedback t", "| Feedback mode: t"),
(a) -> assertCommand(a, "/set format t post \"$%n\"", ""),
(a) -> assertCommand(a, "/set prompt t \"+\" \"-\"", ""),
(a) -> assertCommand(a, "/set prompt t", "| /set prompt t \"+\" \"-\"$")
);
}
privatevoid assertHelp(boolean a, String command, String... find) {
assertCommandCheckOutput(a, command, s -> { for (String f : find) {
assertTrue(s.contains(f), "Expected output of " + command + " to contain: " + f
+ "\n" + s);
}
});
}
// Check that each line of output contains the corresponding string from the list privatevoid checkLineToList(String in, List<String> match) {
String trimmed = in.trim();
String[] res = trimmed.isEmpty()
? new String[0]
: trimmed.split("\n");
assertEquals(res.length, match.size(), "Got: " + Arrays.asList(res)); for (int i = 0; i < match.size(); ++i) {
assertTrue(res[i].contains(match.get(i)));
}
}
@Test publicvoid testListArgs() {
String arg = "qqqq";
List<String> startVarList = new ArrayList<>(START_UP);
startVarList.add("int aardvark");
startVarList.add("int weevil");
test(
a -> assertCommandCheckOutput(a, "/list -all",
s -> checkLineToList(s, START_UP)),
a -> assertCommandOutputStartsWith(a, "/list " + arg, "| No such snippet: " + arg),
a -> assertVariable(a, "int", "aardvark"),
a -> assertVariable(a, "int", "weevil"),
a -> assertCommandOutputContains(a, "/list aardvark", "aardvark"),
a -> assertCommandCheckOutput(a, "/list -start",
s -> checkLineToList(s, START_UP)),
a -> assertCommandCheckOutput(a, "/list -all",
s -> checkLineToList(s, startVarList)),
a -> assertCommandOutputStartsWith(a, "/list s3", "s3 : import"),
a -> assertCommandCheckOutput(a, "/list 1-2 s3",
s -> {
assertTrue(Pattern.matches(".*aardvark.*\\R.*weevil.*\\R.*s3.*import.*", s.trim()), "No match: " + s);
}),
a -> assertCommandOutputStartsWith(a, "/list " + arg, "| No such snippet: " + arg)
);
}
@Test publicvoid testVarsArgs() {
String arg = "qqqq";
List<String> startVarList = new ArrayList<>();
test(
a -> assertCommandCheckOutput(a, "/vars -all",
s -> checkLineToList(s, startVarList)),
a -> assertCommand(a, "/vars " + arg, "| No such snippet: " + arg),
a -> assertVariable(a, "int", "aardvark"),
a -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
a -> assertVariable(a, "int", "a"),
a -> assertVariable(a, "double", "a", "1", "1.0"),
a -> assertCommandOutputStartsWith(a, "/vars aardvark", "| int aardvark = 0"),
a -> assertCommandCheckOutput(a, "/vars -start",
s -> checkLineToList(s, startVarList)),
a -> assertCommandOutputStartsWith(a, "/vars -all", "| int aardvark = 0\n| int a = "),
a -> assertCommandOutputStartsWith(a, "/vars 1-4", "| int aardvark = 0\n| int a = "),
a -> assertCommandOutputStartsWith(a, "/vars f", "| This command does not accept the snippet 'f'"),
a -> assertCommand(a, "/var " + arg, "| No such snippet: " + arg)
);
}
@Test publicvoid testMethodsArgs() {
String arg = "qqqq";
List<String> printingMethodList = new ArrayList<>(PRINTING_CMD_METHOD);
test(new String[]{"--startup", "PRINTING"},
a -> assertCommandCheckOutput(a, "/methods -all",
s -> checkLineToList(s, printingMethodList)),
a -> assertCommandCheckOutput(a, "/methods -start",
s -> checkLineToList(s, printingMethodList)),
a -> assertCommandCheckOutput(a, "/methods print println printf",
s -> checkLineToList(s, printingMethodList)),
a -> assertCommandCheckOutput(a, "/methods println",
s -> assertEquals(s.trim().split("\n").length, 10)),
a -> assertCommandCheckOutput(a, "/methods",
s -> checkLineToList(s, printingMethodList)),
a -> assertCommandOutputStartsWith(a, "/methods " + arg, "| No such snippet: " + arg),
a -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
a -> assertVariable(a, "int", "aardvark"),
a -> assertMethod(a, "void f(int a) { g(); }", "(int)void", "f"),
a -> assertMethod(a, "void g() {}", "()void", "g"),
a -> assertCommandOutputStartsWith(a, "/methods " + arg, "| No such snippet: " + arg),
a -> assertCommandOutputStartsWith(a, "/methods aardvark", "| This command does not accept the snippet 'aardvark' : int aardvark"),
a -> assertCommandCheckOutput(a, "/methods -start",
s -> checkLineToList(s, printingMethodList)),
a -> assertCommandCheckOutput(a, "/methods print println printf",
s -> checkLineToList(s, printingMethodList)),
a -> assertCommandOutputStartsWith(a, "/methods g", "| void g()"),
a -> assertCommandOutputStartsWith(a, "/methods f", "| int f()\n" + "| void f(int)")
);
}
@Test publicvoid testMethodsWithErrors() {
test(new String[]{"--no-startup"},
a -> assertCommand(a, "double m(int x) { return x; }", "| created method m(int)"),
a -> assertCommand(a, "GARBAGE junk() { return TRASH; }", "| created method junk(), however, it cannot be referenced until class GARBAGE, and variable TRASH are declared"),
a -> assertCommand(a, "int w = 5;", "w ==> 5"),
a -> assertCommand(a, "int tyer() { return w; }", "| created method tyer()"),
a -> assertCommand(a, "String w = \"hi\";", "w ==> \"hi\""),
a -> assertCommand(a, "/methods", "| double m(int)\n" + "| GARBAGE junk()\n" + "| which cannot be referenced until class GARBAGE, and variable TRASH are declared\n" + "| int tyer()\n" + "| which cannot be invoked until this error is corrected: \n" + "| incompatible types: java.lang.String cannot be converted to int\n" + "| int tyer() { return w; }\n" + "| ^\n")
);
}
@Test publicvoid testTypesWithErrors() {
test(new String[]{"--no-startup"},
a -> assertCommand(a, "class C extends NONE { int x; }", "| created class C, however, it cannot be referenced until class NONE is declared"),
a -> assertCommand(a, "class D { void m() { System.out.println(nada); } }", "| created class D, however, it cannot be instantiated or its methods invoked until variable nada is declared"),
a -> assertCommand(a, "/types", "| class C\n" + "| which cannot be referenced until class NONE is declared\n" + "| class D\n" + "| which cannot be instantiated or its methods invoked until variable nada is declared\n")
);
}
@Test publicvoid testTypesArgs() {
String arg = "qqqq";
List<String> startTypeList = new ArrayList<>();
test(
a -> assertCommandCheckOutput(a, "/types -all",
s -> checkLineToList(s, startTypeList)),
a -> assertCommandCheckOutput(a, "/types -start",
s -> checkLineToList(s, startTypeList)),
a -> assertCommandOutputStartsWith(a, "/types " + arg, "| No such snippet: " + arg),
a -> assertVariable(a, "int", "aardvark"),
(a) -> assertClass(a, "class A { }", "class", "A"),
(a) -> assertClass(a, "interface A { }", "interface", "A"),
a -> assertCommandOutputStartsWith(a, "/types -all", "| class A\n" + "| interface A"),
(a) -> assertClass(a, "enum E { }", "enum", "E"),
(a) -> assertClass(a, "@interface B { }", "@interface", "B"),
a -> assertCommand(a, "/types aardvark", "| This command does not accept the snippet 'aardvark' : int aardvark;"),
a -> assertCommandOutputStartsWith(a, "/types A", "| interface A"),
a -> assertCommandOutputStartsWith(a, "/types E", "| enum E"),
a -> assertCommandOutputStartsWith(a, "/types B", "| @interface B"),
a -> assertCommandOutputStartsWith(a, "/types " + arg, "| No such snippet: " + arg),
a -> assertCommandCheckOutput(a, "/types -start",
s -> checkLineToList(s, startTypeList))
);
}
@Test publicvoid testBlankLinesInSnippetContinuation() {
test(Locale.ROOT, false, new String[]{"--no-startup"}, "",
a -> assertCommand(a, "class C {", ""),
a -> assertCommand(a, "", ""),
a -> assertCommand(a, "", ""),
a -> assertCommand(a, " int x;", ""),
a -> assertCommand(a, "", ""),
a -> assertCommand(a, "", ""),
a -> assertCommand(a, "}", "| created class C"),
a -> assertCommand(a, "/list", "\n" + " 1 : class C {\n" + " \n" + " \n" + " int x;\n" + " \n" + " \n" + " }")
);
}
// This is mainly interesting in the TestLocalSimpleTest case (8198573)
@Test publicvoid testUpdateFalsePositive() {
test(
a -> assertClass(a, "class A { int a() { int error = 0; return error; } }", "class", "A"),
a -> assertVariable(a, "A", "a", "new A()", "A@.+"),
a -> assertVariable(a, "int", "error", "4711", "4711"),
a -> assertCommandOutputContains(a, "a", "A@")
);
}
@Test publicvoid testRecords() {
test(new String[] {},
(a) -> assertCommandOutputContains(a, "record R(int i) { public int g() { return j; } }", "| created record R, however, it cannot be instantiated or its methods invoked until variable j is declared"),
(a) -> assertCommandOutputContains(a, "new R(0)", "| attempted to use record R which cannot be instantiated or its methods invoked until variable j is declared")
);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.