langtools/test/jdk/jshell/ToolSimpleTest.java
changeset 45215 c9477e22877f
parent 44454 74af976d6798
child 45602 7aeef976cb06
equal deleted inserted replaced
45093:c42dc7b58b4d 45215:c9477e22877f
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103  8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079
    26  * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103  8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079 8180508
    27  * @summary Simple jshell tool tests
    27  * @summary Simple jshell tool tests
    28  * @modules jdk.compiler/com.sun.tools.javac.api
    28  * @modules jdk.compiler/com.sun.tools.javac.api
    29  *          jdk.compiler/com.sun.tools.javac.main
    29  *          jdk.compiler/com.sun.tools.javac.main
    30  *          jdk.jdeps/com.sun.tools.javap
    30  *          jdk.jdeps/com.sun.tools.javap
    31  *          jdk.jshell/jdk.internal.jshell.tool
    31  *          jdk.jshell/jdk.internal.jshell.tool
    35 import java.util.Arrays;
    35 import java.util.Arrays;
    36 import java.util.ArrayList;
    36 import java.util.ArrayList;
    37 import java.util.List;
    37 import java.util.List;
    38 import java.util.Locale;
    38 import java.util.Locale;
    39 import java.util.function.Consumer;
    39 import java.util.function.Consumer;
       
    40 import java.util.regex.Pattern;
    40 import java.util.stream.Collectors;
    41 import java.util.stream.Collectors;
    41 import java.util.stream.Stream;
    42 import java.util.stream.Stream;
    42 
    43 
    43 import org.testng.annotations.Test;
    44 import org.testng.annotations.Test;
    44 
    45 
   200     }
   201     }
   201 
   202 
   202     @Test
   203     @Test
   203     public void testUnknownCommand() {
   204     public void testUnknownCommand() {
   204         test((a) -> assertCommand(a, "/unknown",
   205         test((a) -> assertCommand(a, "/unknown",
   205                 "|  No such command or snippet id: /unknown\n" +
   206                 "|  Invalid command: /unknown\n" +
   206                 "|  Type /help for help."));
   207                 "|  Type /help for help."));
   207     }
   208     }
   208 
   209 
   209     @Test
   210     @Test
   210     public void testEmptyClassPath() {
   211     public void testEmptyClassPath() {
   273                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
   274                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
   274         );
   275         );
   275     }
   276     }
   276 
   277 
   277     @Test
   278     @Test
       
   279     public void testDropRange() {
       
   280         test(false, new String[]{"--no-startup"},
       
   281                 a -> assertVariable(a, "int", "a"),
       
   282                 a -> assertMethod(a, "int b() { return 0; }", "()int", "b"),
       
   283                 a -> assertClass(a, "class A {}", "class", "A"),
       
   284                 a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
       
   285                 a -> assertCommand(a, "for (int i = 0; i < 10; ++i) {}", ""),
       
   286                 a -> assertCommand(a, "/drop 3-5 b 1",
       
   287                         "|  dropped class A\n" +
       
   288                         "|  dropped method b()\n" +
       
   289                         "|  dropped variable a\n"),
       
   290                 a -> assertCommand(a, "/list", "")
       
   291         );
       
   292     }
       
   293 
       
   294     @Test
   278     public void testDropNegative() {
   295     public void testDropNegative() {
   279         test(false, new String[]{"--no-startup"},
   296         test(false, new String[]{"--no-startup"},
   280                 a -> assertCommandOutputStartsWith(a, "/drop 0", "|  No such snippet: 0"),
   297                 a -> assertCommandOutputStartsWith(a, "/drop 0", "|  No snippet with id: 0"),
   281                 a -> assertCommandOutputStartsWith(a, "/drop a", "|  No such snippet: a"),
   298                 a -> assertCommandOutputStartsWith(a, "/drop a", "|  No such snippet: a"),
   282                 a -> assertCommandCheckOutput(a, "/drop",
   299                 a -> assertCommandCheckOutput(a, "/drop",
   283                         assertStartsWith("|  In the /drop argument, please specify an import, variable, method, or class to drop.")),
   300                         assertStartsWith("|  In the /drop argument, please specify an import, variable, method, or class to drop.")),
   284                 a -> assertVariable(a, "int", "a"),
   301                 a -> assertVariable(a, "int", "a"),
   285                 a -> assertCommand(a, "a", "a ==> 0"),
   302                 a -> assertCommand(a, "a", "a ==> 0"),
   290         );
   307         );
   291     }
   308     }
   292 
   309 
   293     @Test
   310     @Test
   294     public void testAmbiguousDrop() {
   311     public void testAmbiguousDrop() {
   295         Consumer<String> check = s -> {
       
   296             assertTrue(s.startsWith("|  The argument references more than one import, variable, method, or class"), s);
       
   297             int lines = s.split("\n").length;
       
   298             assertEquals(lines, 5, "Expected 3 ambiguous keys, but found: " + (lines - 2) + "\n" + s);
       
   299         };
       
   300         test(
   312         test(
   301                 a -> assertVariable(a, "int", "a"),
   313                 a -> assertVariable(a, "int", "a"),
   302                 a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
   314                 a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
   303                 a -> assertClass(a, "class a {}", "class", "a"),
   315                 a -> assertClass(a, "class a {}", "class", "a"),
   304                 a -> assertCommandCheckOutput(a, "/drop a", check),
   316                 a -> assertCommand(a, "/drop a",
   305                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
   317                         "|  dropped variable a\n" +
   306                 a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
   318                         "|  dropped method a()\n" +
   307                 a -> assertCommandCheckOutput(a, "/types", assertClasses()),
   319                         "|  dropped class a")
   308                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
       
   309         );
   320         );
   310         test(
   321         test(
   311                 a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
   322                 a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
   312                 a -> assertMethod(a, "double a(int a) { return 0; }", "(int)double", "a"),
   323                 a -> assertMethod(a, "double a(int a) { return 0; }", "(int)double", "a"),
   313                 a -> assertMethod(a, "double a(double a) { return 0; }", "(double)double", "a"),
   324                 a -> assertMethod(a, "double a(double a) { return 0; }", "(double)double", "a"),
   314                 a -> assertCommandCheckOutput(a, "/drop a", check),
   325                 a -> assertCommand(a, "/drop a",
   315                 a -> assertCommandCheckOutput(a, "/methods", assertMethods())
   326                         "|  dropped method a()\n" +
       
   327                         "|  dropped method a(int)\n" +
       
   328                         "|  dropped method a(double)\n")
   316         );
   329         );
   317     }
   330     }
   318 
   331 
   319     @Test
   332     @Test
   320     public void testApplicationOfPost() {
   333     public void testApplicationOfPost() {
   400     @Test
   413     @Test
   401     public void testListArgs() {
   414     public void testListArgs() {
   402         String arg = "qqqq";
   415         String arg = "qqqq";
   403         List<String> startVarList = new ArrayList<>(START_UP);
   416         List<String> startVarList = new ArrayList<>(START_UP);
   404         startVarList.add("int aardvark");
   417         startVarList.add("int aardvark");
       
   418         startVarList.add("int weevil");
   405         test(
   419         test(
   406                 a -> assertCommandCheckOutput(a, "/list -all",
   420                 a -> assertCommandCheckOutput(a, "/list -all",
   407                         s -> checkLineToList(s, START_UP)),
   421                         s -> checkLineToList(s, START_UP)),
   408                 a -> assertCommandOutputStartsWith(a, "/list " + arg,
   422                 a -> assertCommandOutputStartsWith(a, "/list " + arg,
   409                         "|  No such snippet: " + arg),
   423                         "|  No such snippet: " + arg),
   410                 a -> assertVariable(a, "int", "aardvark"),
   424                 a -> assertVariable(a, "int", "aardvark"),
       
   425                 a -> assertVariable(a, "int", "weevil"),
   411                 a -> assertCommandOutputContains(a, "/list aardvark", "aardvark"),
   426                 a -> assertCommandOutputContains(a, "/list aardvark", "aardvark"),
   412                 a -> assertCommandCheckOutput(a, "/list -start",
   427                 a -> assertCommandCheckOutput(a, "/list -start",
   413                         s -> checkLineToList(s, START_UP)),
   428                         s -> checkLineToList(s, START_UP)),
   414                 a -> assertCommandCheckOutput(a, "/list -all",
   429                 a -> assertCommandCheckOutput(a, "/list -all",
   415                         s -> checkLineToList(s, startVarList)),
   430                         s -> checkLineToList(s, startVarList)),
   416                 a -> assertCommandOutputStartsWith(a, "/list s3",
   431                 a -> assertCommandOutputStartsWith(a, "/list s3",
   417                         "s3 : import"),
   432                         "s3 : import"),
       
   433                 a -> assertCommandCheckOutput(a, "/list 1-2 s3",
       
   434                         s -> {
       
   435                             assertTrue(Pattern.matches(".*aardvark.*\\R.*weevil.*\\R.*s3.*import.*", s.trim()),
       
   436                                     "No match: " + s);
       
   437                         }),
   418                 a -> assertCommandOutputStartsWith(a, "/list " + arg,
   438                 a -> assertCommandOutputStartsWith(a, "/list " + arg,
   419                         "|  No such snippet: " + arg)
   439                         "|  No such snippet: " + arg)
   420         );
   440         );
   421     }
   441     }
   422 
   442 
   436                 a -> assertCommandOutputStartsWith(a, "/vars aardvark",
   456                 a -> assertCommandOutputStartsWith(a, "/vars aardvark",
   437                         "|    int aardvark = 0"),
   457                         "|    int aardvark = 0"),
   438                 a -> assertCommandCheckOutput(a, "/vars -start",
   458                 a -> assertCommandCheckOutput(a, "/vars -start",
   439                         s -> checkLineToList(s, startVarList)),
   459                         s -> checkLineToList(s, startVarList)),
   440                 a -> assertCommandOutputStartsWith(a, "/vars -all",
   460                 a -> assertCommandOutputStartsWith(a, "/vars -all",
       
   461                         "|    int aardvark = 0\n|    int a = "),
       
   462                 a -> assertCommandOutputStartsWith(a, "/vars 1-4",
   441                         "|    int aardvark = 0\n|    int a = "),
   463                         "|    int aardvark = 0\n|    int a = "),
   442                 a -> assertCommandOutputStartsWith(a, "/vars f",
   464                 a -> assertCommandOutputStartsWith(a, "/vars f",
   443                         "|  This command does not accept the snippet 'f'"),
   465                         "|  This command does not accept the snippet 'f'"),
   444                 a -> assertCommand(a, "/var " + arg,
   466                 a -> assertCommand(a, "/var " + arg,
   445                         "|  No such snippet: " + arg)
   467                         "|  No such snippet: " + arg)