33362
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 |
* @summary Tests for EvaluationState.variables
|
|
27 |
* @build KullaTesting TestingInputStream ExpectedDiagnostic
|
|
28 |
* @run testng VariablesTest
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.List;
|
|
32 |
import javax.tools.Diagnostic;
|
|
33 |
|
|
34 |
import jdk.jshell.Snippet;
|
|
35 |
import jdk.jshell.TypeDeclSnippet;
|
|
36 |
import jdk.jshell.VarSnippet;
|
|
37 |
import jdk.jshell.Snippet.SubKind;
|
|
38 |
import jdk.jshell.SnippetEvent;
|
|
39 |
import org.testng.annotations.Test;
|
|
40 |
|
|
41 |
import static jdk.jshell.Snippet.Status.*;
|
|
42 |
import static jdk.jshell.Snippet.SubKind.VAR_DECLARATION_SUBKIND;
|
|
43 |
import static org.testng.Assert.assertEquals;
|
|
44 |
import static org.testng.Assert.fail;
|
|
45 |
|
|
46 |
@Test
|
|
47 |
public class VariablesTest extends KullaTesting {
|
|
48 |
|
|
49 |
public void noVariables() {
|
|
50 |
assertNumberOfActiveVariables(0);
|
|
51 |
}
|
|
52 |
|
|
53 |
private void badVarValue(VarSnippet key) {
|
|
54 |
try {
|
|
55 |
getState().varValue(key);
|
|
56 |
fail("Expected exception for: " + key.source());
|
|
57 |
} catch (IllegalArgumentException e) {
|
|
58 |
// ok
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
public void testVarValue1() {
|
|
63 |
VarSnippet v1 = varKey(assertEval("und1 a;", added(RECOVERABLE_NOT_DEFINED)));
|
|
64 |
badVarValue(v1);
|
|
65 |
VarSnippet v2 = varKey(assertEval("und2 a;",
|
|
66 |
ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, RECOVERABLE_NOT_DEFINED, false, null),
|
|
67 |
ste(v1, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET)));
|
|
68 |
badVarValue(v2);
|
|
69 |
TypeDeclSnippet und = classKey(assertEval("class und2 {}",
|
|
70 |
added(VALID),
|
|
71 |
ste(v2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET)));
|
|
72 |
assertVarValue(v2, "null");
|
|
73 |
assertDrop(und,
|
|
74 |
DiagCheck.DIAG_OK,
|
|
75 |
DiagCheck.DIAG_ERROR,
|
|
76 |
ste(und, VALID, DROPPED, true, null),
|
|
77 |
ste(v2, VALID, RECOVERABLE_NOT_DEFINED, true, und));
|
|
78 |
badVarValue(v1);
|
|
79 |
badVarValue(v2);
|
|
80 |
}
|
|
81 |
|
|
82 |
public void testVarValue2() {
|
|
83 |
VarSnippet v1 = (VarSnippet) assertDeclareFail("int a = 0.0;", "compiler.err.prob.found.req");
|
|
84 |
badVarValue(v1);
|
|
85 |
VarSnippet v2 = varKey(assertEval("int a = 0;", ste(v1, REJECTED, VALID, true, null)));
|
|
86 |
assertDrop(v2, ste(MAIN_SNIPPET, VALID, DROPPED, true, null));
|
|
87 |
badVarValue(v2);
|
|
88 |
}
|
|
89 |
|
|
90 |
public void testSignature1() {
|
|
91 |
VarSnippet v1 = varKey(assertEval("und1 a;", added(RECOVERABLE_NOT_DEFINED)));
|
|
92 |
assertVariableDeclSnippet(v1, "a", "und1", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
|
|
93 |
VarSnippet v2 = varKey(assertEval("und2 a;",
|
|
94 |
ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, RECOVERABLE_NOT_DEFINED, false, null),
|
|
95 |
ste(v1, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET)));
|
|
96 |
assertVariableDeclSnippet(v2, "a", "und2", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
|
|
97 |
TypeDeclSnippet und = classKey(assertEval("class und2 {}",
|
|
98 |
added(VALID),
|
|
99 |
ste(v2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET)));
|
|
100 |
assertVariableDeclSnippet(v2, "a", "und2", VALID, VAR_DECLARATION_SUBKIND, 0, 0);
|
|
101 |
assertDrop(und,
|
|
102 |
DiagCheck.DIAG_OK,
|
|
103 |
DiagCheck.DIAG_ERROR,
|
|
104 |
ste(und, VALID, DROPPED, true, null),
|
|
105 |
ste(v2, VALID, RECOVERABLE_NOT_DEFINED, true, und));
|
|
106 |
assertVariableDeclSnippet(v2, "a", "und2", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
|
|
107 |
}
|
|
108 |
|
|
109 |
public void testSignature2() {
|
|
110 |
VarSnippet v1 = (VarSnippet) assertDeclareFail("int a = 0.0;", "compiler.err.prob.found.req");
|
|
111 |
assertVariableDeclSnippet(v1, "a", "int", REJECTED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 1);
|
|
112 |
VarSnippet v2 = varKey(assertEval("int a = 0;",
|
|
113 |
ste(v1, REJECTED, VALID, true, null)));
|
|
114 |
assertVariableDeclSnippet(v2, "a", "int", VALID, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 0);
|
|
115 |
assertDrop(v2, ste(MAIN_SNIPPET, VALID, DROPPED, true, null));
|
|
116 |
assertVariableDeclSnippet(v2, "a", "int", DROPPED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 0);
|
|
117 |
}
|
|
118 |
|
|
119 |
public void variables() {
|
|
120 |
VarSnippet snx = varKey(assertEval("int x = 10;"));
|
|
121 |
VarSnippet sny = varKey(assertEval("String y = \"hi\";"));
|
|
122 |
VarSnippet snz = varKey(assertEval("long z;"));
|
|
123 |
assertVariables(variable("int", "x"), variable("String", "y"), variable("long", "z"));
|
|
124 |
assertVarValue(snx, "10");
|
|
125 |
assertVarValue(sny, "\"hi\"");
|
|
126 |
assertVarValue(snz, "0");
|
|
127 |
assertActiveKeys();
|
|
128 |
}
|
|
129 |
|
|
130 |
public void variablesArray() {
|
|
131 |
VarSnippet sn = varKey(assertEval("int[] a = new int[12];"));
|
|
132 |
assertEquals(sn.typeName(), "int[]");
|
|
133 |
assertEval("int len = a.length;", "12");
|
|
134 |
assertVariables(variable("int[]", "a"), variable("int", "len"));
|
|
135 |
assertActiveKeys();
|
|
136 |
}
|
|
137 |
|
|
138 |
public void variablesArrayOld() {
|
|
139 |
VarSnippet sn = varKey(assertEval("int a[] = new int[12];"));
|
|
140 |
assertEquals(sn.typeName(), "int[]");
|
|
141 |
assertEval("int len = a.length;", "12");
|
|
142 |
assertVariables(variable("int[]", "a"), variable("int", "len"));
|
|
143 |
assertActiveKeys();
|
|
144 |
}
|
|
145 |
|
|
146 |
public void variablesRedefinition() {
|
|
147 |
Snippet x = varKey(assertEval("int x = 10;"));
|
|
148 |
Snippet y = varKey(assertEval("String y = \"\";", added(VALID)));
|
|
149 |
assertVariables(variable("int", "x"), variable("String", "y"));
|
|
150 |
assertActiveKeys();
|
|
151 |
assertEval("long x;",
|
|
152 |
ste(MAIN_SNIPPET, VALID, VALID, true, null),
|
|
153 |
ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
154 |
assertVariables(variable("long", "x"), variable("String", "y"));
|
|
155 |
assertActiveKeys();
|
|
156 |
assertEval("String y;",
|
|
157 |
ste(MAIN_SNIPPET, VALID, VALID, false, null),
|
|
158 |
ste(y, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
159 |
assertVariables(variable("long", "x"), variable("String", "y"));
|
|
160 |
assertActiveKeys();
|
|
161 |
}
|
|
162 |
|
|
163 |
public void variablesTemporary() {
|
|
164 |
assertEval("int $1 = 10;", added(VALID));
|
|
165 |
assertEval("2 * $1;", added(VALID));
|
|
166 |
assertVariables(variable("int", "$1"), variable("int", "$2"));
|
|
167 |
assertActiveKeys();
|
|
168 |
assertEval("String y;", added(VALID));
|
|
169 |
assertVariables(variable("int", "$1"), variable("int", "$2"), variable("String", "y"));
|
|
170 |
assertActiveKeys();
|
|
171 |
}
|
|
172 |
|
|
173 |
public void variablesTemporaryNull() {
|
|
174 |
assertEval("null;", added(VALID));
|
|
175 |
assertVariables(variable("Object", "$1"));
|
|
176 |
assertEval("(String) null;", added(VALID));
|
|
177 |
assertVariables(variable("Object", "$1"), variable("String", "$2"));
|
|
178 |
assertActiveKeys();
|
|
179 |
assertEval("\"\";", added(VALID));
|
|
180 |
assertVariables(
|
|
181 |
variable("Object", "$1"),
|
|
182 |
variable("String", "$2"),
|
|
183 |
variable("String", "$3"));
|
|
184 |
assertActiveKeys();
|
|
185 |
}
|
|
186 |
|
|
187 |
public void variablesClassReplace() {
|
|
188 |
assertEval("import java.util.*;", added(VALID));
|
|
189 |
Snippet var = varKey(assertEval("List<Integer> list = new ArrayList<>();", "[]",
|
|
190 |
added(VALID)));
|
|
191 |
assertVariables(variable("List<Integer>", "list"));
|
|
192 |
assertEval("class List {}",
|
|
193 |
DiagCheck.DIAG_OK,
|
|
194 |
DiagCheck.DIAG_ERROR,
|
|
195 |
added(VALID),
|
|
196 |
ste(var, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET));
|
|
197 |
assertVariables();
|
|
198 |
assertEval("List list = new List();",
|
|
199 |
DiagCheck.DIAG_OK, DiagCheck.DIAG_IGNORE,
|
|
200 |
ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, VALID, true, null),
|
|
201 |
ste(var, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
202 |
assertVariables(variable("List", "list"));
|
|
203 |
assertActiveKeys();
|
|
204 |
}
|
|
205 |
|
|
206 |
public void variablesErrors() {
|
|
207 |
assertDeclareFail("String;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 6, 0, -1, -1, Diagnostic.Kind.ERROR));
|
|
208 |
assertNumberOfActiveVariables(0);
|
|
209 |
assertActiveKeys();
|
|
210 |
}
|
|
211 |
|
|
212 |
public void variablesUnresolvedActiveFailed() {
|
|
213 |
VarSnippet key = varKey(assertEval("und x;", added(RECOVERABLE_NOT_DEFINED)));
|
|
214 |
assertVariableDeclSnippet(key, "x", "und", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
|
|
215 |
assertUnresolvedDependencies1(key, RECOVERABLE_NOT_DEFINED, "class und");
|
|
216 |
assertNumberOfActiveVariables(1);
|
|
217 |
assertActiveKeys();
|
|
218 |
}
|
|
219 |
|
|
220 |
public void variablesUnresolvedError() {
|
|
221 |
assertDeclareFail("und y = null;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 3, 0, -1, -1, Diagnostic.Kind.ERROR));
|
|
222 |
assertNumberOfActiveVariables(0);
|
|
223 |
assertActiveKeys();
|
|
224 |
}
|
|
225 |
|
|
226 |
public void variablesMultiByteCharacterType() {
|
|
227 |
assertEval("class \u3042 {}");
|
|
228 |
assertEval("\u3042 \u3042 = null;", added(VALID));
|
|
229 |
assertVariables(variable("\u3042", "\u3042"));
|
|
230 |
assertEval("new \u3042()", added(VALID));
|
|
231 |
assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"));
|
|
232 |
|
|
233 |
assertEval("class \u3042\u3044\u3046\u3048\u304a {}");
|
|
234 |
assertEval("\u3042\u3044\u3046\u3048\u304a \u3042\u3044\u3046\u3048\u304a = null;", added(VALID));
|
|
235 |
assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"),
|
|
236 |
variable("\u3042\u3044\u3046\u3048\u304a", "\u3042\u3044\u3046\u3048\u304a"));
|
|
237 |
assertEval("new \u3042\u3044\u3046\u3048\u304a();");
|
|
238 |
assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"),
|
|
239 |
variable("\u3042\u3044\u3046\u3048\u304a", "\u3042\u3044\u3046\u3048\u304a"),
|
|
240 |
variable("\u3042\u3044\u3046\u3048\u304a", "$2"));
|
|
241 |
assertActiveKeys();
|
|
242 |
}
|
|
243 |
|
|
244 |
@Test(enabled = false) // TODO 8081689
|
|
245 |
public void methodVariablesAreNotVisible() {
|
|
246 |
Snippet foo = varKey(assertEval("int foo() {" +
|
|
247 |
"int x = 10;" +
|
|
248 |
"int y = 2 * x;" +
|
|
249 |
"return x * y;" +
|
|
250 |
"}", added(VALID)));
|
|
251 |
assertNumberOfActiveVariables(0);
|
|
252 |
assertActiveKeys();
|
|
253 |
assertEval("int x = 10;", "10");
|
|
254 |
assertEval("int foo() {" +
|
|
255 |
"int y = 2 * x;" +
|
|
256 |
"return x * y;" +
|
|
257 |
"}",
|
|
258 |
ste(foo, VALID, VALID, false, null));
|
|
259 |
assertVariables(variable("int", "x"));
|
|
260 |
assertActiveKeys();
|
|
261 |
assertEval("foo();", "200");
|
|
262 |
assertVariables(variable("int", "x"), variable("int", "$1"));
|
|
263 |
assertActiveKeys();
|
|
264 |
}
|
|
265 |
|
|
266 |
@Test(enabled = false) // TODO 8081689
|
|
267 |
public void classFieldsAreNotVisible() {
|
|
268 |
Snippet key = classKey(assertEval("class clazz {" +
|
|
269 |
"int x = 10;" +
|
|
270 |
"int y = 2 * x;" +
|
|
271 |
"}"));
|
|
272 |
assertNumberOfActiveVariables(0);
|
|
273 |
assertEval("int x = 10;", "10");
|
|
274 |
assertActiveKeys();
|
|
275 |
assertEval(
|
|
276 |
"class clazz {" +
|
|
277 |
"int y = 2 * x;" +
|
|
278 |
"}",
|
|
279 |
ste(key, VALID, VALID, true, null));
|
|
280 |
assertVariables(variable("int", "x"));
|
|
281 |
assertEval("new clazz().y;", "20");
|
|
282 |
assertVariables(variable("int", "x"), variable("int", "$1"));
|
|
283 |
assertActiveKeys();
|
|
284 |
}
|
|
285 |
|
|
286 |
public void multiVariables() {
|
|
287 |
List<SnippetEvent> abc = assertEval("int a, b, c = 10;",
|
|
288 |
DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
|
|
289 |
chain(added(VALID)),
|
|
290 |
chain(added(VALID)),
|
|
291 |
chain(added(VALID)));
|
|
292 |
Snippet a = abc.get(0).snippet();
|
|
293 |
Snippet b = abc.get(1).snippet();
|
|
294 |
Snippet c = abc.get(2).snippet();
|
|
295 |
assertVariables(variable("int", "a"), variable("int", "b"), variable("int", "c"));
|
|
296 |
assertEval("double a = 1.4, b = 8.8;", DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
|
|
297 |
chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)),
|
|
298 |
chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
|
|
299 |
assertVariables(variable("double", "a"), variable("double", "b"), variable("int", "c"));
|
|
300 |
assertEval("double c = a + b;",
|
|
301 |
ste(MAIN_SNIPPET, VALID, VALID, true, null),
|
|
302 |
ste(c, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
303 |
assertVariables(variable("double", "a"), variable("double", "b"), variable("double", "c"));
|
|
304 |
assertActiveKeys();
|
|
305 |
}
|
|
306 |
|
|
307 |
public void syntheticVariables() {
|
|
308 |
assertEval("assert false;");
|
|
309 |
assertNumberOfActiveVariables(0);
|
|
310 |
assertActiveKeys();
|
|
311 |
}
|
|
312 |
|
|
313 |
public void undefinedReplaceVariable() {
|
|
314 |
Snippet key = varKey(assertEval("int d = 234;", "234"));
|
|
315 |
assertVariables(variable("int", "d"));
|
|
316 |
String src = "undefined d;";
|
|
317 |
Snippet undefKey = varKey(assertEval(src,
|
|
318 |
ste(MAIN_SNIPPET, VALID, RECOVERABLE_NOT_DEFINED, true, null),
|
|
319 |
ste(key, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
|
|
320 |
//assertEquals(getState().source(snippet), src);
|
|
321 |
//assertEquals(snippet, undefKey);
|
|
322 |
assertEquals(getState().status(undefKey), RECOVERABLE_NOT_DEFINED);
|
|
323 |
List<String> unr = getState().unresolvedDependencies((VarSnippet) undefKey);
|
|
324 |
assertEquals(unr.size(), 1);
|
|
325 |
assertEquals(unr.get(0), "class undefined");
|
|
326 |
assertVariables(variable("undefined", "d"));
|
|
327 |
}
|
|
328 |
|
|
329 |
public void variableTypeName() {
|
|
330 |
assertEquals(varKey(assertEval("\"x\"")).typeName(), "String");
|
|
331 |
|
|
332 |
assertEquals(varKey(assertEval("java.util.regex.Pattern.compile(\"x\")")).typeName(), "java.util.regex.Pattern");
|
|
333 |
assertEval("import java.util.regex.*;", added(VALID));
|
|
334 |
assertEquals(varKey(assertEval("java.util.regex.Pattern.compile(\"x\")")).typeName(), "Pattern");
|
|
335 |
|
|
336 |
assertEquals(varKey(assertEval("new java.util.ArrayList()")).typeName(), "java.util.ArrayList");
|
|
337 |
assertEval("import java.util.ArrayList;", added(VALID));
|
|
338 |
assertEquals(varKey(assertEval("new java.util.ArrayList()")).typeName(), "ArrayList");
|
|
339 |
|
|
340 |
assertEquals(varKey(assertEval("java.util.Locale.Category.FORMAT")).typeName(), "java.util.Locale.Category");
|
|
341 |
assertEval("import static java.util.Locale.Category;", added(VALID));
|
|
342 |
assertEquals(varKey(assertEval("java.util.Locale.Category.FORMAT")).typeName(), "Category");
|
|
343 |
}
|
|
344 |
}
|