langtools/test/jdk/jshell/ToolBasicTest.java
changeset 37007 6023a9a9d58a
parent 36990 ec0b843a7af5
child 37389 9c137b83a8b8
equal deleted inserted replaced
37006:73ca1e844343 37007:6023a9a9d58a
    59 import static org.testng.Assert.assertTrue;
    59 import static org.testng.Assert.assertTrue;
    60 import static org.testng.Assert.fail;
    60 import static org.testng.Assert.fail;
    61 
    61 
    62 @Test
    62 @Test
    63 public class ToolBasicTest extends ReplToolTesting {
    63 public class ToolBasicTest extends ReplToolTesting {
    64 
       
    65     public void defineVar() {
       
    66         test(
       
    67                 (a) -> assertCommand(a, "int x = 72", "|  Added variable x of type int with initial value 72\n"),
       
    68                 (a) -> assertCommand(a, "x", "|  Variable x of type int has value 72\n"),
       
    69                 (a) -> assertCommand(a, "/vars", "|    int x = 72\n")
       
    70         );
       
    71     }
       
    72 
       
    73     public void defineUnresolvedVar() {
       
    74         test(
       
    75                 (a) -> assertCommand(a, "undefined x",
       
    76                         "|  Added variable x, however, it cannot be referenced until class undefined is declared\n"),
       
    77                 (a) -> assertCommand(a, "/vars", "|    undefined x = (not-active)\n")
       
    78         );
       
    79     }
       
    80 
       
    81     public void testUnresolved() {
       
    82         test(
       
    83                 (a) -> assertCommand(a, "int f() { return g() + x + new A().a; }",
       
    84                         "|  Added method f(), however, it cannot be invoked until method g(), variable x, and class A are declared\n"),
       
    85                 (a) -> assertCommand(a, "f()",
       
    86                         "|  Attempted to call f which cannot be invoked until method g(), variable x, and class A are declared\n"),
       
    87                 (a) -> assertCommand(a, "int g() { return x; }",
       
    88                         "|  Added method g(), however, it cannot be invoked until variable x is declared\n"),
       
    89                 (a) -> assertCommand(a, "g()", "|  Attempted to call g which cannot be invoked until variable x is declared\n")
       
    90         );
       
    91     }
       
    92 
    64 
    93     public void elideStartUpFromList() {
    65     public void elideStartUpFromList() {
    94         test(
    66         test(
    95                 (a) -> assertCommandOutputContains(a, "123", "type int"),
    67                 (a) -> assertCommandOutputContains(a, "123", "type int"),
    96                 (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
    68                 (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
   295                 (a) -> assertCommand(a, "int v;", "|  Added variable v of type int\n"),
   267                 (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")
   268                 (a) -> assertCommand(a, "int v; int c", "|  Added variable c of type int\n")
   297         );
   269         );
   298     }
   270     }
   299 
   271 
   300     public void testDebug() {
       
   301         test(
       
   302                 (a) -> assertCommand(a, "/deb", "|  Debugging on\n"),
       
   303                 (a) -> assertCommand(a, "/debug", "|  Debugging off\n"),
       
   304                 (a) -> assertCommand(a, "/debug", "|  Debugging on\n"),
       
   305                 (a) -> assertCommand(a, "/deb", "|  Debugging off\n")
       
   306         );
       
   307     }
       
   308 
       
   309     public void testHelpLength() {
       
   310         Consumer<String> testOutput = (s) -> {
       
   311             List<String> ss = Stream.of(s.split("\n"))
       
   312                     .filter(l -> !l.isEmpty())
       
   313                     .collect(Collectors.toList());
       
   314             assertTrue(ss.size() >= 10, "Help does not print enough lines:\n" + s);
       
   315         };
       
   316         test(
       
   317                 (a) -> assertCommandCheckOutput(a, "/?", testOutput),
       
   318                 (a) -> assertCommandCheckOutput(a, "/help", testOutput),
       
   319                 (a) -> assertCommandCheckOutput(a, "/help /list", testOutput)
       
   320         );
       
   321     }
       
   322 
       
   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 
       
   341     public void oneLineOfError() {
   272     public void oneLineOfError() {
   342         test(
   273         test(
   343                 (a) -> assertCommand(a, "12+", null),
   274                 (a) -> assertCommand(a, "12+", null),
   344                 (a) -> assertCommandCheckOutput(a, "  true", (s) ->
   275                 (a) -> assertCommandCheckOutput(a, "  true", (s) ->
   345                         assertTrue(s.contains("12+") && !s.contains("true"), "Output: '" + s + "'"))
   276                         assertTrue(s.contains("12+") && !s.contains("true"), "Output: '" + s + "'"))
   676                 a -> assertMethod(a, "void f() { }", "()void", "f"),
   607                 a -> assertMethod(a, "void f() { }", "()void", "f"),
   677                 a -> assertCommandCheckOutput(a, "/methods", assertMethods())
   608                 a -> assertCommandCheckOutput(a, "/methods", assertMethods())
   678         );
   609         );
   679     }
   610     }
   680 
   611 
   681     // Check that each line of output contains the corresponding string from the list
       
   682     private void checkLineToList(String in, List<String> match) {
       
   683         String[] res = in.trim().split("\n");
       
   684         assertEquals(res.length, match.size(), "Got: " + Arrays.asList(res));
       
   685         for (int i = 0; i < match.size(); ++i) {
       
   686             assertTrue(res[i].contains(match.get(i)));
       
   687         }
       
   688     }
       
   689 
       
   690     public void testListArgs() {
       
   691         String arg = "qqqq";
       
   692         List<String> startVarList = new ArrayList<>(START_UP);
       
   693         startVarList.add("int aardvark");
       
   694         test(
       
   695                 a -> assertCommandCheckOutput(a, "/list all",
       
   696                         s -> checkLineToList(s, START_UP)),
       
   697                 a -> assertCommandCheckOutput(a, "/list " + arg,
       
   698                         s -> assertEquals(s, "|  No definition or id named " + arg +
       
   699                                 " found.  There are no active definitions.\n")),
       
   700                 a -> assertVariable(a, "int", "aardvark"),
       
   701                 a -> assertCommandOutputContains(a, "/list aardvark", "aardvark"),
       
   702                 a -> assertCommandCheckOutput(a, "/list start",
       
   703                         s -> checkLineToList(s, START_UP)),
       
   704                 a -> assertCommandCheckOutput(a, "/list all",
       
   705                         s -> checkLineToList(s, startVarList)),
       
   706                 a -> assertCommandCheckOutput(a, "/list printf",
       
   707                         s -> assertTrue(s.contains("void printf"))),
       
   708                 a -> assertCommandCheckOutput(a, "/list " + arg,
       
   709                         s -> assertEquals(s, "|  No definition or id named " + arg +
       
   710                                 " found.  Try /list without arguments.\n"))
       
   711         );
       
   712     }
       
   713 
       
   714     public void testFeedbackNegative() {
   612     public void testFeedbackNegative() {
   715         test(a -> assertCommandCheckOutput(a, "/set feedback aaaa",
   613         test(a -> assertCommandCheckOutput(a, "/set feedback aaaa",
   716                 assertStartsWith("|  Does not match any current feedback mode")));
   614                 assertStartsWith("|  Does not match any current feedback mode")));
   717     }
   615     }
   718 
   616