langtools/test/jdk/jshell/ToolBasicTest.java
author rfield
Fri, 25 Mar 2016 18:36:19 -0700
changeset 36718 bf40906bf49d
parent 36494 4175f47b2a50
child 36778 e04318f39f92
permissions -rw-r--r--
8151755: jshell tool: properly cover resolution issues in output configuration 8152246: jshell tool: history overflow Reviewed-by: jlahoda
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: 35812
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
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
    26
 * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
    27
 * @requires os.family != "solaris"
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    28
 * @summary Tests for Basic tests for REPL tool
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
    29
 * @library /tools/lib
33918
6d7a40b2a54b 8143334: @ignore langtools/test/jdk/jshell/ToolBasicTest.java
jlahoda
parents: 33714
diff changeset
    30
 * @ignore 8139873
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
 * @build KullaTesting TestingInputStream ToolBox Compiler
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
    32
 * @run testng/timeout=600 ToolBasicTest
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
    35
import java.io.FileInputStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import java.io.PrintWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import java.io.StringWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import java.nio.file.Files;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import java.nio.file.Path;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import java.nio.file.Paths;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import java.util.ArrayList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import java.util.Arrays;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import java.util.Scanner;
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
    46
import java.util.function.BiFunction;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import java.util.function.Consumer;
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
    48
import java.util.function.Function;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import java.util.prefs.BackingStoreException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import java.util.prefs.Preferences;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import java.util.stream.Collectors;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import java.util.stream.Stream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
import org.testng.annotations.Test;
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
import static org.testng.Assert.assertEquals;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import static org.testng.Assert.assertTrue;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import static org.testng.Assert.fail;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
@Test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
public class ToolBasicTest extends ReplToolTesting {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
    public void defineVar() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
                (a) -> assertCommand(a, "int x = 72", "|  Added variable x of type int with initial value 72\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
                (a) -> assertCommand(a, "x", "|  Variable x of type int has value 72\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
                (a) -> assertCommand(a, "/vars", "|    int x = 72\n")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
    public void defineUnresolvedVar() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
                (a) -> assertCommand(a, "undefined x",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
                        "|  Added variable x, however, it cannot be referenced until class undefined is declared\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
                (a) -> assertCommand(a, "/vars", "|    undefined x = (not-active)\n")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
    public void testUnresolved() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
                (a) -> assertCommand(a, "int f() { return g() + x + new A().a; }",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
                        "|  Added method f(), however, it cannot be invoked until method g(), variable x, and class A are declared\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
                (a) -> assertCommand(a, "f()",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
                        "|  Attempted to call f which cannot be invoked until method g(), variable x, and class A are declared\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
                (a) -> assertCommand(a, "int g() { return x; }",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
                        "|  Added method g(), however, it cannot be invoked until variable x is declared\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
                (a) -> assertCommand(a, "g()", "|  Attempted to call g which cannot be invoked until variable x is declared\n")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
    public void elideStartUpFromList() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
        test(
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
    93
                (a) -> assertCommandOutputContains(a, "123", "type int"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
                (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
                    int cnt;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
                    try (Scanner scanner = new Scanner(s)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
                        cnt = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    98
                        while (scanner.hasNextLine()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
                            String line = scanner.nextLine();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   100
                            if (!line.trim().isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
                                ++cnt;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
                            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
                    assertEquals(cnt, 1, "Expected only one listed line");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
                })
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   107
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   108
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   109
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   110
    public void elideStartUpFromSave() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
        Path path = compiler.getPath("myfile");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
        test(
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   114
                (a) -> assertCommandOutputContains(a, "123", "type int"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   115
                (a) -> assertCommand(a, "/save " + path.toString(), "")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   116
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
        try (Stream<String> lines = Files.lines(path)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
            assertEquals(lines.count(), 1, "Expected only one saved line");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   119
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   120
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   121
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
    public void testInterrupt() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   123
        ReplTest interrupt = (a) -> assertCommand(a, "\u0003", "");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   124
        for (String s : new String[] { "", "\u0003" }) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
            test(false, new String[]{"-nostartup"},
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   126
                    (a) -> assertCommand(a, "int a = 2 +" + s, ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   127
                    interrupt,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   128
                    (a) -> assertCommand(a, "int a\u0003", ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   129
                    (a) -> assertCommand(a, "int a = 2 + 2\u0003", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   130
                    (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
                    (a) -> evaluateExpression(a, "int", "2", "2"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   132
                    (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
                    (a) -> assertCommand(a, "void f() {", ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
                    (a) -> assertCommand(a, "int q = 10;" + s, ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   135
                    interrupt,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
                    (a) -> assertCommand(a, "void f() {}\u0003", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   137
                    (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
                    (a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   139
                    (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
                    (a) -> assertCommand(a, "class A {" + s, ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
                    interrupt,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
                    (a) -> assertCommand(a, "class A {}\u0003", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   143
                    (a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
                    (a) -> assertClass(a, "interface A {}", "interface", "A"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   145
                    (a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   146
                    (a) -> assertCommand(a, "import java.util.stream." + s, ""),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   147
                    interrupt,
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   148
                    (a) -> assertCommand(a, "import java.util.stream.\u0003", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   149
                    (a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   150
                    (a) -> assertImport(a, "import java.util.stream.Stream", "", "java.util.stream.Stream"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   151
                    (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   153
        }
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
    private final Object lock = new Object();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   157
    private PrintWriter out;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   158
    private boolean isStopped;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
    private Thread t;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   160
    private void assertStop(boolean after, String cmd, String output) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
        if (!after) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
            isStopped = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
            StringWriter writer = new StringWriter();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
            out = new PrintWriter(writer);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
            setCommandInput(cmd + "\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
            t = new Thread(() -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
                try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
                    // no chance to know whether cmd is being evaluated
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
                    Thread.sleep(5000);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   170
                } catch (InterruptedException ignored) {
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
                int i = 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
                int n = 30;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
                synchronized (lock) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
                    do {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
                        setCommandInput("\u0003");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
                        if (!isStopped) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
                            out.println("Not stopped. Try again: " + i);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
                            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
                                lock.wait(1000);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
                            } catch (InterruptedException ignored) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
                            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
                    } while (i++ < n && !isStopped);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
                    if (!isStopped) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
                        System.err.println(writer.toString());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
                        fail("Evaluation was not stopped: '" + cmd + "'");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
            });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
            t.start();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
            synchronized (lock)  {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
                out.println("Evaluation was stopped successfully: '" + cmd + "'");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
                isStopped = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
                lock.notify();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
                t.join();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   200
                t = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
            } catch (InterruptedException ignored) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
            assertOutput(getCommandOutput(), "", "command");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
            assertOutput(getCommandErrorOutput(), "", "command error");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
            assertOutput(getUserOutput(), output, "user");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
            assertOutput(getUserErrorOutput(), "", "user error");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
    public void testStop() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
                (a) -> assertStop(a, "while (true) {}", "Killed.\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   213
                (a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "Killed.\n")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   215
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   216
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   217
    @Test(enabled = false) // TODO 8130450
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   218
    public void testRerun() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   219
        test(false, new String[] {"-nostartup"},
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   220
                (a) -> assertCommand(a, "/0", "|  No such command or snippet id: /0\n|  Type /help for help.\n"),
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   221
                (a) -> assertCommand(a, "/5", "|  No such command or snippet id: /5\n|  Type /help for help.\n")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   222
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   223
        String[] codes = new String[] {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   224
                "int a = 0;", // var
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   225
                "class A {}", // class
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   226
                "void f() {}", // method
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   227
                "bool b;", // active failed
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   228
                "void g() { h(); }", // active corralled
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
        List<ReplTest> tests = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
        for (String s : codes) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   232
            tests.add((a) -> assertCommand(a, s, null));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   233
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
        for (int i = 0; i < codes.length; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   235
            final int finalI = i;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   236
            Consumer<String> check = (s) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   237
                String[] ss = s.split("\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
                assertEquals(ss[0], codes[finalI]);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
                assertTrue(ss.length > 1, s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   240
            };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
            tests.add((a) -> assertCommandCheckOutput(a, "/" + (finalI + 1), check));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
        for (int i = 0; i < codes.length; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
            final int finalI = i;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
            Consumer<String> check = (s) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
                String[] ss = s.split("\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
                assertEquals(ss[0], codes[codes.length - finalI - 1]);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
                assertTrue(ss.length > 1, s);
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
            tests.add((a) -> assertCommandCheckOutput(a, "/-" + (finalI + 1), check));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
        tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("void g() { h(); }")));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   253
        test(false, new String[]{"-nostartup"},
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
                tests.toArray(new ReplTest[tests.size()]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   257
    public void test8142447() {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   258
        Function<String, BiFunction<String, Integer, ReplTest>> assertRerun = cmd -> (code, assertionCount) ->
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   259
                (a) -> assertCommandCheckOutput(a, cmd, s -> {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   260
                            String[] ss = s.split("\n");
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   261
                            assertEquals(ss[0], code);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   262
                            loadVariable(a, "int", "assertionCount", Integer.toString(assertionCount), Integer.toString(assertionCount));
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   263
                        });
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   264
        ReplTest assertVariables = (a) -> assertCommandCheckOutput(a, "/v", assertVariables());
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   265
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   266
        Compiler compiler = new Compiler();
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   267
        Path startup = compiler.getPath("StartupFileOption/startup.txt");
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   268
        compiler.writeToFile(startup, "int assertionCount = 0;\n" + // id: s1
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   269
                "void add(int n) { assertionCount += n; }");
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   270
        test(new String[]{"-startup", startup.toString()},
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   271
                (a) -> assertCommand(a, "add(1)", ""), // id: 1
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   272
                (a) -> assertCommandCheckOutput(a, "add(ONE)", s -> assertEquals(s.split("\n")[0], "|  Error:")), // id: e1
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   273
                (a) -> assertVariable(a, "int", "ONE", "1", "1"),
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   274
                assertRerun.apply("/1").apply("add(1)", 2), assertVariables,
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   275
                assertRerun.apply("/e1").apply("add(ONE)", 3), assertVariables,
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   276
                assertRerun.apply("/s1").apply("int assertionCount = 0;", 0), assertVariables
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   277
        );
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   278
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   279
        test(false, new String[] {"-nostartup"},
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   280
                (a) -> assertCommand(a, "/s1", "|  No such command or snippet id: /s1\n|  Type /help for help.\n"),
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   281
                (a) -> assertCommand(a, "/1", "|  No such command or snippet id: /1\n|  Type /help for help.\n"),
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   282
                (a) -> assertCommand(a, "/e1", "|  No such command or snippet id: /e1\n|  Type /help for help.\n")
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   283
        );
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   284
    }
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   285
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
    public void testRemaining() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   287
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   288
                (a) -> assertCommand(a, "int z; z =", "|  Added variable z of type int\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
                (a) -> assertCommand(a, "5", "|  Variable z has been assigned the value 5\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   290
                (a) -> assertCommand(a, "/*nada*/; int q =", ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   291
                (a) -> assertCommand(a, "77", "|  Added variable q of type int with initial value 77\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   292
                (a) -> assertCommand(a, "//comment;", ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
                (a) -> assertCommand(a, "int v;", "|  Added variable v of type int\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   294
                (a) -> assertCommand(a, "int v; int c", "|  Added variable c of type int\n")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   295
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   296
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   297
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   298
    public void testDebug() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   299
        test(
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   300
                (a) -> assertCommand(a, "/deb", "|  Debugging on\n"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   301
                (a) -> assertCommand(a, "/debug", "|  Debugging off\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   302
                (a) -> assertCommand(a, "/debug", "|  Debugging on\n"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   303
                (a) -> assertCommand(a, "/deb", "|  Debugging off\n")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   304
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   305
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   306
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   307
    public void testHelpLength() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   308
        Consumer<String> testOutput = (s) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   309
            List<String> ss = Stream.of(s.split("\n"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
                    .filter(l -> !l.isEmpty())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   311
                    .collect(Collectors.toList());
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   312
            assertTrue(ss.size() >= 10, "Help does not print enough lines:\n" + s);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   313
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   314
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   315
                (a) -> assertCommandCheckOutput(a, "/?", testOutput),
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   316
                (a) -> assertCommandCheckOutput(a, "/help", testOutput),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   317
                (a) -> assertCommandCheckOutput(a, "/help /list", testOutput)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   318
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   319
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   320
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   321
    public void testHelp() {
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   322
        test(
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   323
                (a) -> assertHelp(a, "/?", "/list", "/help", "/exit", "intro"),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   324
                (a) -> assertHelp(a, "/help", "/list", "/help", "/exit", "intro"),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   325
                (a) -> assertHelp(a, "/help short", "shortcuts", "<tab>"),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   326
                (a) -> assertHelp(a, "/? /li", "/list all", "snippets"),
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   327
                (a) -> assertHelp(a, "/help /help", "/help <command>")
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   328
        );
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   329
    }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   330
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   331
    private void assertHelp(boolean a, String command, String... find) {
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   332
        assertCommandCheckOutput(a, command, s -> {
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   333
            for (String f : find) {
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   334
                assertTrue(s.contains(f), "Expected output of " + command + " to contain: " + f);
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   335
            }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   336
        });
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   337
    }
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   338
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   339
    public void oneLineOfError() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   340
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   341
                (a) -> assertCommand(a, "12+", null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   342
                (a) -> assertCommandCheckOutput(a, "  true", (s) ->
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   343
                        assertTrue(s.contains("12+") && !s.contains("true"), "Output: '" + s + "'"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   344
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   345
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   346
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   347
    public void defineVariables() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   348
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   349
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   350
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   351
                (a) -> assertVariable(a, "int", "a"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   352
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   353
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   354
                (a) -> assertVariable(a, "double", "a", "1", "1.0"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   355
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   356
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   357
                (a) -> evaluateExpression(a, "double", "2 * a", "2.0"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   358
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   359
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   360
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   361
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   363
    public void defineMethods() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   364
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   365
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   366
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   367
                (a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   368
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   369
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   370
                (a) -> assertMethod(a, "void f(int a) { g(); }", "(int)void", "f"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   371
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   372
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   373
                (a) -> assertMethod(a, "void g() {}", "()void", "g"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   374
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   375
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   376
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   377
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   378
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   379
    public void defineClasses() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   380
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   381
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   382
                (a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   383
                (a) -> assertClass(a, "class A { }", "class", "A"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   384
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   385
                (a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   386
                (a) -> assertClass(a, "interface A { }", "interface", "A"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   387
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   388
                (a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   389
                (a) -> assertClass(a, "enum A { }", "enum", "A"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   390
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   391
                (a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   392
                (a) -> assertClass(a, "@interface A { }", "@interface", "A"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   393
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   394
                (a) -> assertCommandCheckOutput(a, "/classes", assertClasses())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   395
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   396
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   397
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   398
    public void defineImports() {
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   399
        test(
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   400
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   401
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   402
                (a) -> assertImport(a, "import java.util.stream.Stream;", "", "java.util.stream.Stream"),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   403
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   404
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   405
                (a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   406
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   407
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   408
                (a) -> assertImport(a, "import static java.lang.Math.PI;", "static", "java.lang.Math.PI"),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   409
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   410
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   411
                (a) -> assertImport(a, "import static java.lang.Math.*;", "static", "java.lang.Math.*"),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   412
                (a) -> assertCommandCheckOutput(a, "/list", assertList()),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   413
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   414
        );
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   415
    }
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   416
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   417
    public void testClasspathDirectory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   418
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   419
        Path outDir = Paths.get("testClasspathDirectory");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   420
        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
   421
        Path classpath = compiler.getPath(outDir);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   422
        test(
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   423
                (a) -> assertCommand(a, "/classpath " + classpath, String.format("|  Path %s added to classpath\n", classpath)),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   424
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   425
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   426
        test(new String[] { "-cp", classpath.toString() },
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   427
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   428
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   429
        test(new String[] { "-classpath", classpath.toString() },
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   430
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   431
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   432
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   433
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   434
    public void testClasspathJar() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   435
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   436
        Path outDir = Paths.get("testClasspathJar");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   437
        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
   438
        String jarName = "test.jar";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   439
        compiler.jar(outDir, jarName, "pkg/A.class");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   440
        Path jarPath = compiler.getPath(outDir).resolve(jarName);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   441
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   442
                (a) -> assertCommand(a, "/classpath " + jarPath, String.format("|  Path %s added to classpath\n", jarPath)),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   443
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   444
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   445
        test(new String[] { "-cp", jarPath.toString() },
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   446
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   447
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   448
        test(new String[] { "-classpath", jarPath.toString() },
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   449
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"A\"")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   450
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   451
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   452
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   453
    public void testStartupFileOption() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   454
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   455
            Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   456
            Path startup = compiler.getPath("StartupFileOption/startup.txt");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   457
            compiler.writeToFile(startup, "class A { public String toString() { return \"A\"; } }");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   458
            test(new String[]{"-startup", startup.toString()},
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   459
                    (a) -> evaluateExpression(a, "A", "new A()", "\"A\"\n")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   460
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   461
            test(new String[]{"-nostartup"},
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   462
                    (a) -> assertCommandCheckOutput(a, "printf(\"\")", assertStartsWith("|  Error:\n|  cannot find symbol"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   463
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   464
            test((a) -> assertCommand(a, "printf(\"A\")", "", "", null, "A", ""));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   465
            test(false, new String[]{"-startup", "UNKNOWN"}, "|  File 'UNKNOWN' for start-up is not found.");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   466
        } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   467
            removeStartup();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   468
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   469
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   470
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   471
    public void testLoadingFromArgs() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   472
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   473
        Path path = compiler.getPath("loading.repl");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   474
        compiler.writeToFile(path, "int a = 10; double x = 20; double a = 10;");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   475
        test(new String[] { path.toString() },
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   476
                (a) -> assertCommand(a, "x", "|  Variable x of type double has value 20.0\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   477
                (a) -> assertCommand(a, "a", "|  Variable a of type double has value 10.0\n")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   478
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   479
        Path unknown = compiler.getPath("UNKNOWN.jar");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   480
        test(true, new String[]{unknown.toString()},
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   481
                "|  File '" + unknown
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   482
                + "' is not found: " + unresolvableMessage(unknown) + "\n");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   483
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   484
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   485
    public void testReset() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   486
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   487
                (a) -> assertReset(a, "/r"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   488
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   489
                (a) -> assertVariable(a, "int", "x"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   490
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   491
                (a) -> assertMethod(a, "void f() { }", "()void", "f"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   492
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   493
                (a) -> assertClass(a, "class A { }", "class", "A"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   494
                (a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   495
                (a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   496
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   497
                (a) -> assertReset(a, "/reset"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   498
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   499
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   500
                (a) -> assertCommandCheckOutput(a, "/classes", assertClasses()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   501
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   502
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   503
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   504
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   505
    public void testOpen() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   506
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   507
        Path path = compiler.getPath("testOpen.repl");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   508
        compiler.writeToFile(path,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   509
                "int a = 10;\ndouble x = 20;\ndouble a = 10;\n" +
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   510
                        "class A { public String toString() { return \"A\"; } }\nimport java.util.stream.*;");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   511
        for (String s : new String[]{"/o", "/open"}) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   512
            test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   513
                    (a) -> assertCommand(a, s + " " + path.toString(), ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   514
                    (a) -> assertCommand(a, "a", "|  Variable a of type double has value 10.0\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   515
                    (a) -> evaluateExpression(a, "A", "new A();", "\"A\""),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   516
                    (a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   517
                    (a) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   518
                        loadVariable(a, "double", "x", "20.0", "20.0");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   519
                        loadVariable(a, "double", "a", "10.0", "10.0");
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   520
                        loadVariable(a, "A", "$7", "new A();", "A");
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   521
                        loadVariable(a, "long", "$8", "Stream.of(\"A\").count();", "1");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   522
                        loadClass(a, "class A { public String toString() { return \"A\"; } }",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   523
                                "class", "A");
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   524
                        loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*");
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   525
                        assertCommandCheckOutput(a, "/classes", assertClasses());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   526
                    },
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   527
                    (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   528
                    (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   529
                    (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   530
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   531
            Path unknown = compiler.getPath("UNKNOWN.repl");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   532
            test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   533
                    (a) -> assertCommand(a, s + " " + unknown,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   534
                            "|  File '" + unknown
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   535
                                    + "' is not found: " + unresolvableMessage(unknown) + "\n")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   536
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   537
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   538
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   539
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   540
    public void testSave() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   541
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   542
        Path path = compiler.getPath("testSave.repl");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   543
        List<String> list = Arrays.asList(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   544
                "int a;",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   545
                "class A { public String toString() { return \"A\"; } }"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   546
        );
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   547
        test(
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   548
                (a) -> assertVariable(a, "int", "a"),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   549
                (a) -> assertCommand(a, "()", null, null, null, "", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   550
                (a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   551
                (a) -> assertCommand(a, "/save " + path.toString(), "")
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   552
        );
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   553
        assertEquals(Files.readAllLines(path), list);
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   554
        {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   555
            List<String> output = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   556
            test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   557
                    (a) -> assertCommand(a, "int a;", null),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   558
                    (a) -> assertCommand(a, "()", null, null, null, "", ""),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   559
                    (a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   560
                    (a) -> assertCommandCheckOutput(a, "/list all", (out) ->
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   561
                            output.addAll(Stream.of(out.split("\n"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   562
                                    .filter(str -> !str.isEmpty())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   563
                                    .map(str -> str.substring(str.indexOf(':') + 2))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   564
                                    .filter(str -> !str.startsWith("/"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   565
                                    .collect(Collectors.toList()))),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   566
                    (a) -> assertCommand(a, "/save all " + path.toString(), "")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   567
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   568
            assertEquals(Files.readAllLines(path), output);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   569
        }
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   570
        List<String> output = new ArrayList<>();
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   571
        test(
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   572
                (a) -> assertVariable(a, "int", "a"),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   573
                (a) -> assertCommand(a, "()", null, null, null, "", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   574
                (a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   575
                (a) -> assertCommandCheckOutput(a, "/history", (out) ->
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   576
                        output.addAll(Stream.of(out.split("\n"))
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   577
                                .filter(str -> !str.isEmpty())
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   578
                                .collect(Collectors.toList()))),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   579
                (a) -> assertCommand(a, "/save history " + path.toString(), "")
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   580
        );
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   581
        output.add("/save history " + path.toString());
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   582
        assertEquals(Files.readAllLines(path), output);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   583
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   584
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   585
    public void testStartSet() throws BackingStoreException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   586
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   587
            Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   588
            Path startUpFile = compiler.getPath("startUp.txt");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   589
            test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   590
                    (a) -> assertVariable(a, "int", "a"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   591
                    (a) -> assertVariable(a, "double", "b", "10", "10.0"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   592
                    (a) -> assertMethod(a, "void f() {}", "()V", "f"),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   593
                    (a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   594
                    (a) -> assertCommand(a, "/save " + startUpFile.toString(), null),
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   595
                    (a) -> assertCommand(a, "/set start " + startUpFile.toString(), null)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   596
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   597
            Path unknown = compiler.getPath("UNKNOWN");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   598
            test(
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   599
                    (a) -> assertCommand(a, "/set start " + unknown.toString(),
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   600
                            "|  File '" + unknown + "' for /set start is not found.\n")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   601
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   602
            test(false, new String[0],
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   603
                    (a) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   604
                        loadVariable(a, "int", "a");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   605
                        loadVariable(a, "double", "b", "10.0", "10.0");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   606
                        loadMethod(a, "void f() {}", "()void", "f");
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   607
                        loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*");
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   608
                        assertCommandCheckOutput(a, "/classes", assertClasses());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   609
                    },
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   610
                    (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   611
                    (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   612
                    (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   613
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   614
        } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   615
            removeStartup();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   616
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   617
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   618
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   619
    private void removeStartup() {
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   620
        Preferences preferences = Preferences.userRoot().node("tool/JShell");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   621
        if (preferences != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   622
            preferences.remove("STARTUP");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   623
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   624
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   625
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   626
    public void testUnknownCommand() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   627
        test((a) -> assertCommand(a, "/unknown",
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   628
                "|  No such command or snippet id: /unknown\n" +
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   629
                "|  Type /help for help.\n"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   630
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   631
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   632
    public void testEmptyClassPath() {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   633
        test(after -> assertCommand(after, "/classpath", "|  /classpath requires a path argument\n"));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   634
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   635
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   636
    public void testNoArgument() {
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   637
        String[] commands = {"/save", "/open", "/set start"};
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   638
        test(Stream.of(commands)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   639
                .map(cmd -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   640
                    String c = cmd;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   641
                    final String finalC = c;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   642
                    return (ReplTest) after -> assertCommand(after, cmd,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   643
                            "|  The " + finalC + " command requires a filename argument.\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   644
                })
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   645
                .toArray(ReplTest[]::new));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   646
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   647
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   648
    public void testStartSave() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   649
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   650
        Path startSave = compiler.getPath("startSave.txt");
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   651
        test(a -> assertCommand(a, "/save start " + startSave.toString(), null));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   652
        List<String> lines = Files.lines(startSave)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   653
                .filter(s -> !s.isEmpty())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   654
                .collect(Collectors.toList());
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   655
        assertEquals(lines, START_UP);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   656
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   657
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   658
    public void testConstrainedUpdates() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   659
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   660
                a -> assertClass(a, "class XYZZY { }", "class", "XYZZY"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   661
                a -> assertVariable(a, "XYZZY", "xyzzy"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   662
                a -> assertCommandCheckOutput(a, "import java.util.stream.*",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   663
                        (out) -> assertTrue(out.trim().isEmpty(), "Expected no output, got: " + out))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   664
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   665
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   666
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   667
    public void testRemoteExit() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   668
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   669
                a -> assertVariable(a, "int", "x"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   670
                a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   671
                a -> assertCommandOutputContains(a, "System.exit(5);", "terminated"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   672
                a -> assertCommandCheckOutput(a, "/vars", s ->
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   673
                        assertTrue(s.trim().isEmpty(), s)),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   674
                a -> assertMethod(a, "void f() { }", "()void", "f"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   675
                a -> assertCommandCheckOutput(a, "/methods", assertMethods())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   676
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   677
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   678
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   679
    // Check that each line of output contains the corresponding string from the list
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   680
    private void checkLineToList(String in, List<String> match) {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   681
        String[] res = in.trim().split("\n");
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   682
        assertEquals(res.length, match.size(), "Got: " + Arrays.asList(res));
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   683
        for (int i = 0; i < match.size(); ++i) {
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   684
            assertTrue(res[i].contains(match.get(i)));
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   685
        }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   686
    }
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   687
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   688
    public void testListArgs() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   689
        String arg = "qqqq";
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   690
        List<String> startVarList = new ArrayList<>(START_UP);
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   691
        startVarList.add("int aardvark");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   692
        test(
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   693
                a -> assertCommandCheckOutput(a, "/list all",
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   694
                        s -> checkLineToList(s, START_UP)),
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   695
                a -> assertCommandCheckOutput(a, "/list " + arg,
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   696
                        s -> assertEquals(s, "|  No definition or id named " + arg +
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   697
                                " found.  There are no active definitions.\n")),
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   698
                a -> assertVariable(a, "int", "aardvark"),
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   699
                a -> assertCommandOutputContains(a, "/list aardvark", "aardvark"),
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   700
                a -> assertCommandCheckOutput(a, "/list start",
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   701
                        s -> checkLineToList(s, START_UP)),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   702
                a -> assertCommandCheckOutput(a, "/list all",
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   703
                        s -> checkLineToList(s, startVarList)),
35812
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   704
                a -> assertCommandCheckOutput(a, "/list printf",
6a6ca0bd3c14 8146138: jshell tool: add /help <command>
rfield
parents: 35002
diff changeset
   705
                        s -> assertTrue(s.contains("void printf"))),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   706
                a -> assertCommandCheckOutput(a, "/list " + arg,
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   707
                        s -> assertEquals(s, "|  No definition or id named " + arg +
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   708
                                " found.  Try /list without arguments.\n"))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   709
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   710
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   711
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   712
    public void testFeedbackNegative() {
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   713
        test(a -> assertCommandCheckOutput(a, "/set feedback aaaa",
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   714
                assertStartsWith("|  Does not match any current feedback mode")));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   715
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   716
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   717
    public void testFeedbackOff() {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   718
        for (String off : new String[]{"o", "off"}) {
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   719
            test(
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   720
                    a -> assertCommand(a, "/set feedback " + off, ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   721
                    a -> assertCommand(a, "int a", ""),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   722
                    a -> assertCommand(a, "void f() {}", ""),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   723
                    a -> assertCommandCheckOutput(a, "aaaa", assertStartsWith("|  Error:")),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   724
                    a -> assertCommandCheckOutput(a, "public void f() {}", assertStartsWith("|  Warning:"))
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   725
            );
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   726
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   727
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   728
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   729
    public void testFeedbackNormal() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   730
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   731
        Path testNormalFile = compiler.getPath("testConciseNormal");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   732
        String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"};
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   733
        String[] sources2 = new String[] {"int a //again", "void f() {int y = 4;}", "class A {} //again", "a = 10"};
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   734
        String[] output = new String[] {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   735
                "|  Added variable a of type int\n",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   736
                "|  Added method f()\n",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   737
                "|  Added class A\n",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   738
                "|  Variable a has been assigned the value 10\n"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   739
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   740
        compiler.writeToFile(testNormalFile, sources2);
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   741
        for (String feedback : new String[]{"/set f", "/set feedback"}) {
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   742
            for (String feedbackState : new String[]{"n", "normal", "o", "off"}) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   743
                test(
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   744
                        a -> assertCommand(a, feedback + " " + feedbackState, "|  Feedback mode: normal\n"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   745
                        a -> assertCommand(a, sources[0], output[0]),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   746
                        a -> assertCommand(a, sources[1], output[1]),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   747
                        a -> assertCommand(a, sources[2], output[2]),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   748
                        a -> assertCommand(a, sources[3], output[3]),
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   749
                        a -> assertCommand(a, "/o " + testNormalFile.toString(), "")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   750
                );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   751
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   752
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   753
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   754
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   755
    public void testDrop() {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   756
        test(false, new String[]{"-nostartup"},
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   757
                a -> assertVariable(a, "int", "a"),
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36494
diff changeset
   758
                a -> dropVariable(a, "/drop 1", "int a = 0", "|  Dropped variable a\n"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   759
                a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36494
diff changeset
   760
                a -> dropMethod(a, "/drop 2", "b ()I", "|  Dropped method b()\n"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   761
                a -> assertClass(a, "class A {}", "class", "A"),
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36494
diff changeset
   762
                a -> dropClass(a, "/drop 3", "class A", "|  Dropped class A\n"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   763
                a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36494
diff changeset
   764
                a -> dropImport(a, "/drop 4", "import java.util.stream.*", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   765
                a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   766
                a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   767
                a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   768
                a -> assertCommandCheckOutput(a, "/imports", assertImports())
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   769
        );
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   770
        test(false, new String[]{"-nostartup"},
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   771
                a -> assertVariable(a, "int", "a"),
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36494
diff changeset
   772
                a -> dropVariable(a, "/drop a", "int a = 0", "|  Dropped variable a\n"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   773
                a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36494
diff changeset
   774
                a -> dropMethod(a, "/drop b", "b ()I", "|  Dropped method b()\n"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   775
                a -> assertClass(a, "class A {}", "class", "A"),
36718
bf40906bf49d 8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents: 36494
diff changeset
   776
                a -> dropClass(a, "/drop A", "class A", "|  Dropped class A\n"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   777
                a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   778
                a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   779
                a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   780
                a -> assertCommandCheckOutput(a, "/imports", assertImports())
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   781
        );
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   782
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   783
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   784
    public void testDropNegative() {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   785
        test(false, new String[]{"-nostartup"},
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   786
                a -> assertCommand(a, "/drop 0", "|  No definition or id named 0 found.  See /classes, /methods, /vars, or /list\n"),
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   787
                a -> assertCommand(a, "/drop a", "|  No definition or id named a found.  See /classes, /methods, /vars, or /list\n"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   788
                a -> assertCommandCheckOutput(a, "/drop",
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   789
                        assertStartsWith("|  In the /drop argument, please specify an import, variable, method, or class to drop.")),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   790
                a -> assertVariable(a, "int", "a"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   791
                a -> assertCommand(a, "a", "|  Variable a of type int has value 0\n"),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   792
                a -> assertCommand(a, "/drop 2", "|  The argument did not specify an active import, variable, method, or class to drop.\n")
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   793
        );
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   794
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   795
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   796
    public void testAmbiguousDrop() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   797
        Consumer<String> check = s -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   798
            assertTrue(s.startsWith("|  The argument references more than one import, variable, method, or class"), s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   799
            int lines = s.split("\n").length;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   800
            assertEquals(lines, 5, "Expected 3 ambiguous keys, but found: " + (lines - 2) + "\n" + s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   801
        };
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   802
        test(
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   803
                a -> assertVariable(a, "int", "a"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   804
                a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   805
                a -> assertClass(a, "class a {}", "class", "a"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   806
                a -> assertCommandCheckOutput(a, "/drop a", check),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   807
                a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   808
                a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   809
                a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   810
                a -> assertCommandCheckOutput(a, "/imports", assertImports())
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   811
        );
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   812
        test(
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   813
                a -> assertMethod(a, "int a() { return 0; }", "()int", "a"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   814
                a -> assertMethod(a, "double a(int a) { return 0; }", "(int)double", "a"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   815
                a -> assertMethod(a, "double a(double a) { return 0; }", "(double)double", "a"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   816
                a -> assertCommandCheckOutput(a, "/drop a", check),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   817
                a -> assertCommandCheckOutput(a, "/methods", assertMethods())
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   818
        );
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   819
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   820
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   821
    public void testHistoryReference() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   822
        test(false, new String[]{"-nostartup"},
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   823
                a -> assertCommand(a, "System.err.println(1)", "", "", null, "", "1\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   824
                a -> assertCommand(a, "System.err.println(2)", "", "", null, "", "2\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   825
                a -> assertCommand(a, "/-2", "System.err.println(1)\n", "", null, "", "1\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   826
                a -> assertCommand(a, "/history", "\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   827
                                                    "/debug 0\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   828
                                                    "System.err.println(1)\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   829
                                                    "System.err.println(2)\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   830
                                                    "System.err.println(1)\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   831
                                                    "/history\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   832
                a -> assertCommand(a, "/-2", "System.err.println(2)\n", "", null, "", "2\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   833
                a -> assertCommand(a, "/!", "System.err.println(2)\n", "", null, "", "2\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   834
                a -> assertCommand(a, "/2", "System.err.println(2)\n", "", null, "", "2\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   835
                a -> assertCommand(a, "/1", "System.err.println(1)\n", "", null, "", "1\n")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   836
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   837
    }
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   838
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   839
    private String unresolvableMessage(Path p) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   840
        try {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   841
            new FileInputStream(p.toFile());
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   842
            throw new AssertionError("Expected exception did not occur.");
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   843
        } catch (IOException ex) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   844
            return ex.getMessage();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   845
        }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   846
    }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
   847
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   848
    public void testCommandPrefix() {
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   849
        test(a -> assertCommandCheckOutput(a, "/s",
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   850
                      assertStartsWith("|  Command: /s is ambiguous: /save, /set")),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   851
             a -> assertCommand(a, "int var", "|  Added variable var of type int\n"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   852
             a -> assertCommandCheckOutput(a, "/va",
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   853
                      assertStartsWith("|    int var = 0")),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   854
             a -> assertCommandCheckOutput(a, "/save",
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   855
                      assertStartsWith("|  The /save command requires a filename argument.")));
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   856
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   857
}