langtools/test/jdk/jshell/ToolBasicTest.java
changeset 34477 64001b0533a2
parent 34475 7af94fd75ede
child 34481 e0ff9821f1e8
equal deleted inserted replaced
34476:9f12a05b4786 34477:64001b0533a2
    39 import java.nio.file.Paths;
    39 import java.nio.file.Paths;
    40 import java.util.ArrayList;
    40 import java.util.ArrayList;
    41 import java.util.Arrays;
    41 import java.util.Arrays;
    42 import java.util.List;
    42 import java.util.List;
    43 import java.util.Scanner;
    43 import java.util.Scanner;
       
    44 import java.util.function.BiFunction;
    44 import java.util.function.Consumer;
    45 import java.util.function.Consumer;
       
    46 import java.util.function.Function;
    45 import java.util.prefs.BackingStoreException;
    47 import java.util.prefs.BackingStoreException;
    46 import java.util.prefs.Preferences;
    48 import java.util.prefs.Preferences;
    47 import java.util.stream.Collectors;
    49 import java.util.stream.Collectors;
    48 import java.util.stream.Stream;
    50 import java.util.stream.Stream;
    49 
    51 
   213     }
   215     }
   214 
   216 
   215     @Test(enabled = false) // TODO 8130450
   217     @Test(enabled = false) // TODO 8130450
   216     public void testRerun() {
   218     public void testRerun() {
   217         test(false, new String[] {"-nostartup"},
   219         test(false, new String[] {"-nostartup"},
   218                 (a) -> assertCommand(a, "/0", "|  Cannot find snippet 0\n"),
   220                 (a) -> assertCommand(a, "/0", "|  No such command or snippet id: /0\n|  Type /help for help.\n"),
   219                 (a) -> assertCommand(a, "/5", "|  Cannot find snippet 5\n")
   221                 (a) -> assertCommand(a, "/5", "|  No such command or snippet id: /5\n|  Type /help for help.\n")
   220         );
   222         );
   221         String[] codes = new String[] {
   223         String[] codes = new String[] {
   222                 "int a = 0;", // var
   224                 "int a = 0;", // var
   223                 "class A {}", // class
   225                 "class A {}", // class
   224                 "void f() {}", // method
   226                 "void f() {}", // method
   248             tests.add((a) -> assertCommandCheckOutput(a, "/-" + (finalI + 1), check));
   250             tests.add((a) -> assertCommandCheckOutput(a, "/-" + (finalI + 1), check));
   249         }
   251         }
   250         tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("void g() { h(); }")));
   252         tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("void g() { h(); }")));
   251         test(false, new String[]{"-nostartup"},
   253         test(false, new String[]{"-nostartup"},
   252                 tests.toArray(new ReplTest[tests.size()]));
   254                 tests.toArray(new ReplTest[tests.size()]));
       
   255     }
       
   256 
       
   257     public void test8142447() {
       
   258         Function<String, BiFunction<String, Integer, ReplTest>> assertRerun = cmd -> (code, assertionCount) ->
       
   259                 (a) -> assertCommandCheckOutput(a, cmd, s -> {
       
   260                             String[] ss = s.split("\n");
       
   261                             assertEquals(ss[0], code);
       
   262                             loadVariable(a, "int", "assertionCount", Integer.toString(assertionCount), Integer.toString(assertionCount));
       
   263                         });
       
   264         ReplTest assertVariables = (a) -> assertCommandCheckOutput(a, "/v", assertVariables());
       
   265 
       
   266         Compiler compiler = new Compiler();
       
   267         Path startup = compiler.getPath("StartupFileOption/startup.txt");
       
   268         compiler.writeToFile(startup, "int assertionCount = 0;\n" + // id: s1
       
   269                 "void add(int n) { assertionCount += n; }");
       
   270         test(new String[]{"-startup", startup.toString()},
       
   271                 (a) -> assertCommand(a, "add(1)", ""), // id: 1
       
   272                 (a) -> assertCommandCheckOutput(a, "add(ONE)", s -> assertEquals(s.split("\n")[0], "|  Error:")), // id: e1
       
   273                 (a) -> assertVariable(a, "int", "ONE", "1", "1"),
       
   274                 assertRerun.apply("/1").apply("add(1)", 2), assertVariables,
       
   275                 assertRerun.apply("/e1").apply("add(ONE)", 3), assertVariables,
       
   276                 assertRerun.apply("/s1").apply("int assertionCount = 0;", 0), assertVariables
       
   277         );
       
   278 
       
   279         test(false, new String[] {"-nostartup"},
       
   280                 (a) -> assertCommand(a, "/s1", "|  No such command or snippet id: /s1\n|  Type /help for help.\n"),
       
   281                 (a) -> assertCommand(a, "/1", "|  No such command or snippet id: /1\n|  Type /help for help.\n"),
       
   282                 (a) -> assertCommand(a, "/e1", "|  No such command or snippet id: /e1\n|  Type /help for help.\n")
       
   283         );
   253     }
   284     }
   254 
   285 
   255     public void testRemaining() {
   286     public void testRemaining() {
   256         test(
   287         test(
   257                 (a) -> assertCommand(a, "int z; z =", "|  Added variable z of type int\n"),
   288                 (a) -> assertCommand(a, "int z; z =", "|  Added variable z of type int\n"),
   572         }
   603         }
   573     }
   604     }
   574 
   605 
   575     public void testUnknownCommand() {
   606     public void testUnknownCommand() {
   576         test((a) -> assertCommand(a, "/unknown",
   607         test((a) -> assertCommand(a, "/unknown",
   577                 "|  No such command: /unknown\n" +
   608                 "|  No such command or snippet id: /unknown\n" +
   578                 "|  Type /help for help.\n"));
   609                 "|  Type /help for help.\n"));
   579     }
   610     }
   580 
   611 
   581     public void testEmptyClassPath() {
   612     public void testEmptyClassPath() {
   582         test(after -> assertCommand(after, "/classpath", "|  /classpath requires a path argument\n"));
   613         test(after -> assertCommand(after, "/classpath", "|  /classpath requires a path argument\n"));