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