ToolFormatTest.java revision 3318:2f6ca5367226
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8148316 8148317 8151755 8152246
27 * @summary Tests for output customization
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.main
31 *          jdk.jdeps/com.sun.tools.javap
32 *          jdk.jshell/jdk.internal.jshell.tool
33 * @build KullaTesting TestingInputStream toolbox.ToolBox Compiler
34 * @run testng ToolFormatTest
35 */
36import java.util.ArrayList;
37import java.util.List;
38import org.testng.annotations.Test;
39
40@Test
41public class ToolFormatTest extends ReplToolTesting {
42
43    public void testSetFormat() {
44        try {
45            test(
46                    (a) -> assertCommandOutputStartsWith(a, "/set newmode test command", "|  Created new feedback mode: test"),
47                    (a) -> assertCommand(a, "/set format test pre '$ '", ""),
48                    (a) -> assertCommand(a, "/set format test post ''", ""),
49                    (a) -> assertCommand(a, "/set format test act 'ADD' added", ""),
50                    (a) -> assertCommand(a, "/set format test act 'MOD' modified", ""),
51                    (a) -> assertCommand(a, "/set format test act 'REP' replaced", ""),
52                    (a) -> assertCommand(a, "/set format test act 'OVR' overwrote", ""),
53                    (a) -> assertCommand(a, "/set format test act 'USE' used", ""),
54                    (a) -> assertCommand(a, "/set format test act 'DRP' dropped", ""),
55                    (a) -> assertCommand(a, "/set format test up 'UP-' update", ""),
56                    (a) -> assertCommand(a, "/set format test action '{up}{act} '", ""),
57                    (a) -> assertCommand(a, "/set format test resolve 'OK' ok", ""),
58                    (a) -> assertCommand(a, "/set format test resolve 'DEF' defined", ""),
59                    (a) -> assertCommand(a, "/set format test resolve 'NODEF' notdefined", ""),
60                    (a) -> assertCommand(a, "/set format test fname ':{name} ' ", ""),
61                    (a) -> assertCommand(a, "/set format test ftype '[{type}]' method,expression", ""),
62                    (a) -> assertCommand(a, "/set format test result '={value} ' expression", ""),
63                    (a) -> assertCommand(a, "/set format test display '{pre}{action}{ftype}{fname}{result}{resolve}'", ""),
64                    (a) -> assertCommand(a, "/set format test display '{pre}HI this is enum' enum", ""),
65                    (a) -> assertCommandOutputStartsWith(a, "/set feedback test", "$ Feedback mode: test"),
66                    (a) -> assertCommand(a, "class D {}", "$ ADD :D OK"),
67                    (a) -> assertCommand(a, "void m() {}", "$ ADD []:m OK"),
68                    (a) -> assertCommand(a, "interface EX extends EEX {}", "$ ADD :EX NODEF"),
69                    (a) -> assertCommand(a, "56", "$ ADD [int]:$4 =56 OK"),
70                    (a) -> assertCommand(a, "class D { int hh; }", "$ REP :D OK$ UP-OVR :D OK"),
71                    (a) -> assertCommand(a, "enum E {A,B}", "$ HI this is enum"),
72                    (a) -> assertCommand(a, "int z() { return f(); }", "$ ADD []:z DEF"),
73                    (a) -> assertCommand(a, "z()", "$ UP-USE []:z DEF"),
74                    (a) -> assertCommand(a, "/drop z", "$ DRP []:z OK"),
75                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal")
76            );
77        } finally {
78            assertCommandCheckOutput(false, "/set feedback normal", s -> {
79            });
80        }
81    }
82
83    public void testSetFormatSelector() {
84        List<ReplTest> tests = new ArrayList<>();
85        tests.add((a) -> assertCommandOutputStartsWith(a, "/set newmode ate quiet",
86                            "|  Created new feedback mode: ate"));
87        tests.add((a) -> assertCommand(a, "/set feedback ate", ""));
88        StringBuilder sb = new StringBuilder();
89        class KindList {
90            final String[] values;
91            final int matchIndex;
92            int current;
93            boolean match;
94            KindList(String[] values, int matchIndex) {
95                this.values = values;
96                this.matchIndex = matchIndex;
97                this.current = 1 << values.length;
98            }
99            boolean next() {
100                if (current <= 0) {
101                    return false;
102                }
103                --current;
104                return true;
105            }
106            boolean append(boolean ahead) {
107                boolean any = false;
108                match = false;
109                for (int i = values.length - 1; i >= 0 ; --i) {
110                    if ((current & (1 << i)) != 0) {
111                        match |= i == matchIndex;
112                        if (any) {
113                            sb.append(",");
114                        } else {
115                            if (ahead) {
116                                sb.append("-");
117                            }
118                        }
119                        sb.append(values[i]);
120                        any = true;
121                    }
122                }
123                match |= !any;
124                return ahead || any;
125            }
126        }
127        KindList klcase = new KindList(new String[] {"class", "method", "expression", "vardecl"}, 2);
128        while (klcase.next()) {
129            KindList klact  = new KindList(new String[] {"added", "modified", "replaced"}, 0);
130            while (klact.next()) {
131                KindList klwhen = new KindList(new String[] {"update", "primary"}, 1);
132                while (klwhen.next()) {
133                    sb.setLength(0);
134                    klwhen.append(
135                        klact.append(
136                            klcase.append(false)));
137                    boolean match = klcase.match && klact.match && klwhen.match;
138                    String select = sb.toString();
139                    String yes = "+++" + select + "+++";
140                    String no  = "---" + select + "---";
141                    String expect = match? yes : no;
142                    tests.add((a) -> assertCommand(a, "/set format ate display '" + no  + "'", ""));
143                    tests.add((a) -> assertCommand(a, "/set format ate display '" + yes + "' " + select, ""));
144                    tests.add((a) -> assertCommand(a, "\"" + select + "\"", expect));
145                }
146            }
147        }
148        tests.add((a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal"));
149
150        try {
151            test(tests.toArray(new ReplTest[tests.size()]));
152        } finally {
153            assertCommandCheckOutput(false, "/set feedback normal", s -> {
154            });
155        }
156    }
157
158    public void testSetNewModeQuiet() {
159        try {
160            test(
161                    (a) -> assertCommandOutputStartsWith(a, "/set newmode nmq quiet normal", "|  Created new feedback mode: nmq"),
162                    (a) -> assertCommand(a, "/set feedback nmq", ""),
163                    (a) -> assertCommand(a, "/se ne nmq2 q nor", ""),
164                    (a) -> assertCommand(a, "/se fee nmq2", ""),
165                    (a) -> assertCommand(a, "/set newmode nmc command normal", ""),
166                    (a) -> assertCommandOutputStartsWith(a, "/set feedback nmc", "|  Feedback mode: nmc"),
167                    (a) -> assertCommandOutputStartsWith(a, "/set newmode nm", "|  Created new feedback mode: nm"),
168                    (a) -> assertCommandOutputStartsWith(a, "/set feedback nm", "|  Feedback mode: nm"),
169                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", "|  Feedback mode: normal")
170            );
171        } finally {
172            assertCommandCheckOutput(false, "/set feedback normal", s -> {
173            });
174        }
175    }
176
177    public void testSetError() {
178        try {
179            test(
180                    (a) -> assertCommandOutputStartsWith(a, "/set newmode tee command foo",
181                            "|  Does not match any current feedback mode: foo"),
182                    (a) -> assertCommandOutputStartsWith(a, "/set newmode tee flurb",
183                            "|  Specify either 'command' or 'quiet'"),
184                    (a) -> assertCommandOutputStartsWith(a, "/set newmode te2",
185                            "|  Created new feedback mode: te2"),
186                    (a) -> assertCommandOutputStartsWith(a, "/set newmode te2 command",
187                            "|  Expected a new feedback mode name. 'te2' is a known feedback mode"),
188                    (a) -> assertCommandOutputStartsWith(a, "/set newmode te command normal",
189                            "|  Created new feedback mode: te"),
190                    (a) -> assertCommand(a, "/set format te errorpre 'ERROR: '", ""),
191                    (a) -> assertCommandOutputStartsWith(a, "/set feedback te",
192                            ""),
193                    (a) -> assertCommandOutputStartsWith(a, "/set ",
194                            "ERROR: The '/set' command requires a sub-command and arguments"),
195                    (a) -> assertCommandOutputStartsWith(a, "/set xyz",
196                            "ERROR: Invalid '/set' argument: xyz"),
197                    (a) -> assertCommandOutputStartsWith(a, "/set f",
198                            "ERROR: Ambiguous sub-command argument to '/set': f"),
199                    (a) -> assertCommandOutputStartsWith(a, "/set feedback",
200                            "ERROR: Expected a feedback mode"),
201                    (a) -> assertCommandOutputStartsWith(a, "/set feedback xyz",
202                            "ERROR: Does not match any current feedback mode"),
203                    (a) -> assertCommandOutputStartsWith(a, "/set format",
204                            "ERROR: Expected a feedback mode"),
205                    (a) -> assertCommandOutputStartsWith(a, "/set format xyz",
206                            "ERROR: Does not match any current feedback mode"),
207                    (a) -> assertCommandOutputStartsWith(a, "/set format t",
208                            "ERROR: Matches more then one current feedback mode: t"),
209                    (a) -> assertCommandOutputStartsWith(a, "/set format te",
210                            "ERROR: Expected field name missing"),
211                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld",
212                            "ERROR: Expected format missing"),
213                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld aaa",
214                            "ERROR: Format 'aaa' must be quoted"),
215                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' frog",
216                            "ERROR: Not a valid selector"),
217                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import-frog",
218                            "ERROR: Not a valid selector"),
219                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import-import",
220                            "ERROR: Selector kind in multiple sections of"),
221                    (a) -> assertCommandOutputStartsWith(a, "/set format te fld 'aaa' import,added",
222                            "ERROR: Different selector kinds in same sections of"),
223                    (a) -> assertCommandOutputStartsWith(a, "/set newmode",
224                            "ERROR: Expected new feedback mode"),
225                    (a) -> assertCommandOutputStartsWith(a, "/set newmode te",
226                            "ERROR: Expected a new feedback mode name"),
227                    (a) -> assertCommandOutputStartsWith(a, "/set newmode x xyz",
228                            "ERROR: Specify either 'command' or 'quiet'"),
229                    (a) -> assertCommandOutputStartsWith(a, "/set newmode x quiet y",
230                            "ERROR: Does not match any current feedback mode"),
231                    (a) -> assertCommandOutputStartsWith(a, "/set prompt",
232                            "ERROR: Expected a feedback mode"),
233                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te",
234                            "ERROR: Expected format missing"),
235                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te aaa xyz",
236                            "ERROR: Format 'aaa' must be quoted"),
237                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te 'aaa' xyz",
238                            "ERROR: Format 'xyz' must be quoted"),
239                    (a) -> assertCommandOutputStartsWith(a, "/set prompt",
240                            "ERROR: Expected a feedback mode"),
241                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te",
242                            "ERROR: Expected format missing"),
243                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te aaa",
244                            "ERROR: Format 'aaa' must be quoted"),
245                    (a) -> assertCommandOutputStartsWith(a, "/set prompt te 'aaa'",
246                            "ERROR: Expected format missing"),
247                    (a) -> assertCommandOutputStartsWith(a, "/set feedback normal",
248                            "|  Feedback mode: normal")
249            );
250        } finally {
251            assertCommandCheckOutput(false, "/set feedback normal", s -> {
252            });
253        }
254    }
255
256    public void testSetHelp() {
257        try {
258            test(
259                    (a) -> assertCommandOutputContains(a, "/help /set", "command to launch"),
260                    (a) -> assertCommandOutputContains(a, "/help /set format", "display"),
261                    (a) -> assertCommandOutputContains(a, "/hel /se for", "vardecl"),
262                    (a) -> assertCommandOutputContains(a, "/help /set editor", "temporary file")
263            );
264        } finally {
265            assertCommandCheckOutput(false, "/set feedback normal", s -> {
266            });
267        }
268    }
269}
270