langtools/test/jdk/jshell/CommandCompletionTest.java
author jlahoda
Mon, 05 Dec 2016 19:42:42 +0100
changeset 42415 85388bae723d
parent 42265 b36ad5a64e75
child 42843 a8d83044a192
permissions -rw-r--r--
8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows Summary: Ignoring non-existent default FileSystem roots. Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35359
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
/*
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
 * @test
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    26
 * @bug 8144095 8164825 8169818 8153402
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
 * @summary Test Command Completion
35359
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 34570
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 34570
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.main
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36494
diff changeset
    30
 *          jdk.jdeps/com.sun.tools.javap
35359
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 34570
diff changeset
    31
 *          jdk.jshell/jdk.internal.jshell.tool
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
 * @library /tools/lib
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    33
 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    34
 * @build ReplToolTesting TestingInputStream Compiler
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
 * @run testng CommandCompletionTest
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import java.nio.file.FileSystems;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import java.nio.file.Files;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import java.nio.file.Path;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import java.nio.file.Paths;
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
    43
import java.util.Arrays;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import java.util.Collections;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import java.util.function.Predicate;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import java.util.stream.Collectors;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import java.util.stream.Stream;
42415
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
    49
import java.util.stream.StreamSupport;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import org.testng.annotations.Test;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
@Test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
public class CommandCompletionTest extends ReplToolTesting {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
    public void testCommand() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
        assertCompletion("/deb|", false);
41635
cb3d04878117 8163840: jshell tool: provide way to display configuration settings
rfield
parents: 40765
diff changeset
    58
        assertCompletion("/re|", false, "/reload ", "/reset ");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
        assertCompletion("/h|", false, "/help ", "/history ");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
    public void testList() {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 38531
diff changeset
    63
        test(false, new String[] {"--no-startup"},
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34475
diff changeset
    64
                a -> assertCompletion(a, "/l|", false, "/list "),
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    65
                a -> assertCompletion(a, "/list |", false, "-all", "-history", "-start "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    66
                a -> assertCompletion(a, "/list -h|", false, "-history"),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34475
diff changeset
    67
                a -> assertCompletion(a, "/list q|", false),
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34475
diff changeset
    68
                a -> assertVariable(a, "int", "xray"),
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    69
                a -> assertCompletion(a, "/list |", false, "-all", "-history", "-start ", "1 ", "xray "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    70
                a -> assertCompletion(a, "/list x|", false, "xray "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    71
                a -> assertCompletion(a, "/list xray |", false)
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34475
diff changeset
    72
        );
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
    public void testDrop() {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 38531
diff changeset
    76
        test(false, new String[] {"--no-startup"},
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34475
diff changeset
    77
                a -> assertCompletion(a, "/d|", false, "/drop "),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
                a -> assertClass(a, "class cTest {}", "class", "cTest"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
                a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
                a -> assertVariable(a, "int", "fTest"),
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    81
                a -> assertCompletion(a, "/drop |", false, "1 ", "2 ", "3 ", "cTest ", "fTest ", "mTest "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    82
                a -> assertCompletion(a, "/drop f|", false, "fTest ")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
    public void testEdit() {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 38531
diff changeset
    87
        test(false, new String[]{"--no-startup"},
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34475
diff changeset
    88
                a -> assertCompletion(a, "/e|", false, "/edit ", "/exit "),
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34475
diff changeset
    89
                a -> assertCompletion(a, "/ed|", false, "/edit "),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
                a -> assertClass(a, "class cTest {}", "class", "cTest"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
                a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
                a -> assertVariable(a, "int", "fTest"),
42265
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    93
                a -> assertCompletion(a, "/edit |", false,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    94
                        "-all" , "-start " , "1 ", "2 ", "3 ", "cTest ", "fTest ", "mTest "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    95
                a -> assertCompletion(a, "/edit cTest |", false,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    96
                        "2 ", "3 ", "fTest ", "mTest "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    97
                a -> assertCompletion(a, "/edit 1 fTest |", false,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    98
                        "2 ", "mTest "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
    99
                a -> assertCompletion(a, "/edit f|", false, "fTest "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   100
                a -> assertCompletion(a, "/edit mTest f|", false, "fTest ")
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   101
        );
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   102
    }
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   103
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   104
    public void testHelp() {
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   105
        assertCompletion("/help |", false,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   106
                "/! ", "/-<n> ", "/<id> ", "/? ", "/classpath ", "/drop ",
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   107
                "/edit ", "/exit ", "/help ", "/history ", "/imports ",
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   108
                "/list ", "/methods ", "/open ", "/reload ", "/reset ",
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   109
                "/save ", "/set ", "/types ", "/vars ", "intro ", "shortcuts ");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   110
        assertCompletion("/? |", false,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   111
                "/! ", "/-<n> ", "/<id> ", "/? ", "/classpath ", "/drop ",
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   112
                "/edit ", "/exit ", "/help ", "/history ", "/imports ",
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   113
                "/list ", "/methods ", "/open ", "/reload ", "/reset ",
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   114
                "/save ", "/set ", "/types ", "/vars ", "intro ", "shortcuts ");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   115
        assertCompletion("/help /s|", false,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   116
                "/save ", "/set ");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   117
        assertCompletion("/help /set |", false,
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   118
                "editor", "feedback", "format", "mode", "prompt", "start", "truncation");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   119
        assertCompletion("/help /edit |", false);
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   120
    }
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   121
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   122
    public void testReload() {
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   123
        assertCompletion("/reload |", false, "-quiet ", "-restore ");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   124
        assertCompletion("/reload -restore |", false, "-quiet");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   125
        assertCompletion("/reload -quiet |", false, "-restore");
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   126
        assertCompletion("/reload -restore -quiet |", false);
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   127
    }
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   128
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   129
    public void testVarsMethodsTypes() {
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   130
        test(false, new String[]{"--no-startup"},
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   131
                a -> assertCompletion(a, "/v|", false, "/vars "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   132
                a -> assertCompletion(a, "/m|", false, "/methods "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   133
                a -> assertCompletion(a, "/t|", false, "/types "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   134
                a -> assertClass(a, "class cTest {}", "class", "cTest"),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   135
                a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   136
                a -> assertVariable(a, "int", "fTest"),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   137
                a -> assertCompletion(a, "/vars |", false, "-all", "-start ", "3 ", "fTest "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   138
                a -> assertCompletion(a, "/meth |", false, "-all", "-start ", "2 ", "mTest "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   139
                a -> assertCompletion(a, "/typ |", false, "-all", "-start ", "1 ", "cTest "),
b36ad5a64e75 8153402: jshell tool: completion provider for /help
rfield
parents: 41635
diff changeset
   140
                a -> assertCompletion(a, "/var f|", false, "fTest ")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   143
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
    public void testOpen() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
        assertCompletion("/o|", false, "/open ");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
        List<String> p1 = listFiles(Paths.get(""));
42415
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   148
        getRootDirectories().forEach(s -> p1.add(s.toString()));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
        Collections.sort(p1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
        assertCompletion("/open |", false, p1.toArray(new String[p1.size()]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
        Path classDir = compiler.getClassDir();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
        List<String> p2 = listFiles(classDir);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   153
        assertCompletion("/open " + classDir + "/|", false, p2.toArray(new String[p2.size()]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   156
    public void testSave() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   157
        Compiler compiler = new Compiler();
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35359
diff changeset
   158
        assertCompletion("/s|", false, "/save ", "/set ");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
        List<String> p1 = listFiles(Paths.get(""));
38520
17e72b872ffd 8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents: 38514
diff changeset
   160
        Collections.addAll(p1, "-all ", "-history ", "-start ");
42415
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   161
        getRootDirectories().forEach(s -> p1.add(s.toString()));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
        Collections.sort(p1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
        assertCompletion("/save |", false, p1.toArray(new String[p1.size()]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
        Path classDir = compiler.getClassDir();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
        List<String> p2 = listFiles(classDir);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
        assertCompletion("/save " + classDir + "/|",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
                false, p2.toArray(new String[p2.size()]));
38520
17e72b872ffd 8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents: 38514
diff changeset
   168
        assertCompletion("/save -all " + classDir + "/|",
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
                false, p2.toArray(new String[p2.size()]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   170
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   171
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   172
    public void testClassPath() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
        assertCompletion("/classp|", false, "/classpath ");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
        Path outDir = compiler.getPath("testClasspathCompletion");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
        Files.createDirectories(outDir);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
        Files.createDirectories(outDir.resolve("dir"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
        createIfNeeded(outDir.resolve("test.jar"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
        createIfNeeded(outDir.resolve("test.zip"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
        compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
        String jarName = "test.jar";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
        compiler.jar(outDir, jarName, "pkg/A.class");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
        compiler.getPath(outDir).resolve(jarName);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        List<String> paths = listFiles(outDir, CLASSPATH_FILTER);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
        assertCompletion("/classpath " + outDir + "/|", false, paths.toArray(new String[paths.size()]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
    public void testUserHome() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
        List<String> completions;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
        Path home = Paths.get(System.getProperty("user.home"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
        try (Stream<Path> content = Files.list(home)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
            completions = content.filter(CLASSPATH_FILTER)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
                                 .map(file -> file.getFileName().toString() + (Files.isDirectory(file) ? "/" : ""))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
                                 .sorted()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
                                 .collect(Collectors.toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
        assertCompletion("/classpath ~/|", false, completions.toArray(new String[completions.size()]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   200
    public void testSet() throws IOException {
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   201
        List<String> p1 = listFiles(Paths.get(""));
42415
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   202
        getRootDirectories().forEach(s -> p1.add(s.toString()));
40765
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   203
        Collections.sort(p1);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   204
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   205
        String[] modes = {"concise ", "normal ", "silent ", "verbose "};
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   206
        String[] options = {"-command", "-delete", "-quiet"};
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   207
        String[] modesWithOptions = Stream.concat(Arrays.stream(options), Arrays.stream(modes)).sorted().toArray(String[]::new);
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   208
        test(false, new String[] {"--no-startup"},
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   209
                a -> assertCompletion(a, "/se|", false, "/set "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   210
                a -> assertCompletion(a, "/set |", false, "editor ", "feedback ", "format ", "mode ", "prompt ", "start ", "truncation "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   211
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   212
                // /set editor
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   213
                a -> assertCompletion(a, "/set e|", false, "editor "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   214
                a -> assertCompletion(a, "/set editor |", false, p1.toArray(new String[p1.size()])),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   215
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   216
                // /set feedback
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   217
                a -> assertCompletion(a, "/set fe|", false, "feedback "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   218
                a -> assertCompletion(a, "/set fe |", false, modes),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   219
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   220
                // /set format
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   221
                a -> assertCompletion(a, "/set fo|", false, "format "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   222
                a -> assertCompletion(a, "/set fo |", false, modes),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   223
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   224
                // /set mode
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   225
                a -> assertCompletion(a, "/set mo|", false, "mode "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   226
                a -> assertCompletion(a, "/set mo |", false),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   227
                a -> assertCompletion(a, "/set mo newmode |", false, modesWithOptions),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   228
                a -> assertCompletion(a, "/set mo newmode -|", false, options),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   229
                a -> assertCompletion(a, "/set mo newmode -command |", false),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   230
                a -> assertCompletion(a, "/set mo newmode normal |", false, options),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   231
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   232
                // /set prompt
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   233
                a -> assertCompletion(a, "/set pro|", false, "prompt "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   234
                a -> assertCompletion(a, "/set pro |", false, modes),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   235
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   236
                // /set start
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   237
                a -> assertCompletion(a, "/set st|", false, "start "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   238
                a -> assertCompletion(a, "/set st |", false, p1.toArray(new String[p1.size()])),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   239
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   240
                // /set truncation
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   241
                a -> assertCompletion(a, "/set tr|", false, "truncation "),
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   242
                a -> assertCompletion(a, "/set tr |", false, modes)
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   243
        );
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   244
    }
6f9556cf4404 8164825: jshell tool: Completion for subcommand
shinyafox
parents: 40588
diff changeset
   245
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
    private void createIfNeeded(Path file) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
        if (!Files.exists(file))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
            Files.createFile(file);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   249
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
    private List<String> listFiles(Path path) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
        return listFiles(path, ACCEPT_ALL);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   253
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
    private List<String> listFiles(Path path, Predicate<? super Path> filter) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
        try (Stream<Path> stream = Files.list(path)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
            return stream.filter(filter)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
                         .map(p -> p.getFileName().toString() + (Files.isDirectory(p) ? "/" : ""))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
                         .sorted()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
                         .collect(Collectors.toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
    private static final Predicate<? super Path> ACCEPT_ALL =
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
            (file) -> !file.endsWith(".") && !file.endsWith("..");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
    private static final Predicate<? super Path> CLASSPATH_FILTER =
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
            (file) -> ACCEPT_ALL.test(file) &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
                    (Files.isDirectory(file) ||
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
                     file.getFileName().toString().endsWith(".jar") ||
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   270
                     file.getFileName().toString().endsWith(".zip"));
42415
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   271
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   272
    private static Iterable<? extends Path> getRootDirectories() {
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   273
        return StreamSupport.stream(FileSystems.getDefault()
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   274
                                               .getRootDirectories()
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   275
                                               .spliterator(),
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   276
                                    false)
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   277
                            .filter(p -> Files.exists(p))
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   278
                            .collect(Collectors.toList());
85388bae723d 8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents: 42265
diff changeset
   279
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   280
}