author | rfield |
Tue, 09 Aug 2016 23:00:49 -0700 | |
changeset 40304 | 0318f4e75c6d |
parent 37644 | 33cf53901cac |
child 41514 | a75c2b869d8d |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
36499 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
/* |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
25 |
* @test 8080069 8152925 |
33362 | 26 |
* @summary Test of Snippet redefinition and replacement. |
27 |
* @build KullaTesting TestingInputStream |
|
28 |
* @run testng ReplaceTest |
|
29 |
*/ |
|
30 |
||
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
31 |
import java.util.Iterator; |
36499 | 32 |
import java.util.List; |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
33 |
import java.util.stream.Stream; |
33362 | 34 |
import jdk.jshell.Snippet; |
35 |
import jdk.jshell.MethodSnippet; |
|
36 |
import jdk.jshell.PersistentSnippet; |
|
37 |
import jdk.jshell.TypeDeclSnippet; |
|
38 |
import jdk.jshell.VarSnippet; |
|
39 |
import jdk.jshell.DeclarationSnippet; |
|
40 |
import org.testng.annotations.Test; |
|
41 |
||
36499 | 42 |
import jdk.jshell.SnippetEvent; |
43 |
import jdk.jshell.UnresolvedReferenceException; |
|
44 |
import static org.testng.Assert.assertEquals; |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
45 |
import static org.testng.Assert.assertFalse; |
33362 | 46 |
import static jdk.jshell.Snippet.Status.*; |
47 |
import static jdk.jshell.Snippet.SubKind.*; |
|
48 |
import static org.testng.Assert.assertTrue; |
|
49 |
||
50 |
@Test |
|
51 |
public class ReplaceTest extends KullaTesting { |
|
52 |
||
53 |
public void testRedefine() { |
|
54 |
Snippet vx = varKey(assertEval("int x;")); |
|
55 |
Snippet mu = methodKey(assertEval("int mu() { return x * 4; }")); |
|
56 |
Snippet c = classKey(assertEval("class C { String v() { return \"#\" + mu(); } }")); |
|
57 |
assertEval("C c0 = new C();"); |
|
58 |
assertEval("c0.v();", "\"#0\""); |
|
59 |
assertEval("int x = 10;", "10", |
|
60 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
61 |
ste(vx, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
62 |
assertEval("c0.v();", "\"#40\""); |
|
63 |
assertEval("C c = new C();"); |
|
64 |
assertEval("c.v();", "\"#40\""); |
|
65 |
assertEval("int mu() { return x * 3; }", |
|
66 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
67 |
ste(mu, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
68 |
assertEval("c.v();", "\"#30\""); |
|
69 |
assertEval("class C { String v() { return \"@\" + mu(); } }", |
|
70 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
71 |
ste(c, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
72 |
assertEval("c0.v();", "\"@30\""); |
|
73 |
assertEval("c = new C();"); |
|
74 |
assertEval("c.v();", "\"@30\""); |
|
75 |
assertActiveKeys(); |
|
76 |
} |
|
77 |
||
78 |
public void testReplaceClassToVar() { |
|
79 |
Snippet oldA = classKey(assertEval("class A { public String toString() { return \"old\"; } }")); |
|
80 |
Snippet v = varKey(assertEval("A a = new A();", "old")); |
|
81 |
assertEval("a;", "old"); |
|
82 |
Snippet midA = classKey(assertEval("class A { public String toString() { return \"middle\"; } }", |
|
83 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
84 |
ste(oldA, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
85 |
assertEval("a;", "middle"); |
|
86 |
assertEval("class A { int x; public String toString() { return \"new\"; } }", |
|
87 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
88 |
ste(midA, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
89 |
ste(v, VALID, VALID, true, MAIN_SNIPPET)); |
|
90 |
assertEval("a;", "null"); |
|
91 |
assertActiveKeys(); |
|
92 |
} |
|
93 |
||
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
94 |
private <T extends Snippet> void identityMatch(Stream<T> got, T expected) { |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
95 |
Iterator<T> it = got.iterator(); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
96 |
assertTrue(it.hasNext(), "expected exactly one"); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
97 |
assertTrue(expected == it.next(), "Identity must not change"); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
98 |
assertFalse(it.hasNext(), "expected exactly one"); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
99 |
} |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
100 |
|
33362 | 101 |
public void testReplaceVarToMethod() { |
102 |
Snippet x = varKey(assertEval("int x;")); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
103 |
MethodSnippet musn = methodKey(assertEval("double mu() { return x * 4; }")); |
33362 | 104 |
assertEval("x == 0;", "true"); |
105 |
assertEval("mu() == 0.0;", "true"); |
|
106 |
assertEval("double x = 2.5;", |
|
107 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36780
diff
changeset
|
108 |
ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
109 |
identityMatch(getState().methods(), musn); |
33362 | 110 |
assertEval("x == 2.5;", "true"); |
111 |
assertEval("mu() == 10.0;", "true"); // Auto redefine |
|
112 |
assertActiveKeys(); |
|
113 |
} |
|
114 |
||
115 |
public void testReplaceMethodToMethod() { |
|
116 |
Snippet a = methodKey(assertEval("double a() { return 2; }")); |
|
117 |
Snippet b = methodKey(assertEval("double b() { return a() * 10; }")); |
|
118 |
assertEval("double c() { return b() * 3; }"); |
|
119 |
assertEval("double d() { return c() + 1000; }"); |
|
120 |
assertEval("d();", "1060.0"); |
|
121 |
assertEval("int a() { return 5; }", |
|
122 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36780
diff
changeset
|
123 |
ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
33362 | 124 |
assertEval("d();", "1150.0"); |
125 |
assertActiveKeys(); |
|
126 |
} |
|
127 |
||
128 |
public void testReplaceClassToMethod() { |
|
129 |
Snippet c = classKey(assertEval("class C { int f() { return 7; } }")); |
|
130 |
Snippet m = methodKey(assertEval("int m() { return new C().f(); }")); |
|
131 |
assertEval("m();", "7"); |
|
132 |
assertEval("class C { int x = 99; int f() { return x; } }", |
|
133 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36780
diff
changeset
|
134 |
ste(c, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
33362 | 135 |
assertEval("m();", "99"); |
136 |
assertActiveKeys(); |
|
137 |
} |
|
138 |
||
139 |
public void testReplaceVarToClass() { |
|
140 |
Snippet x = varKey(assertEval("int x;")); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
141 |
TypeDeclSnippet c = classKey(assertEval("class A { double a = 4 * x; }")); |
33362 | 142 |
assertEval("x == 0;", "true"); |
143 |
assertEval("new A().a == 0.0;", "true"); |
|
144 |
assertEval("double x = 2.5;", |
|
145 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36780
diff
changeset
|
146 |
ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
147 |
identityMatch(getState().types(), c); |
33362 | 148 |
assertEval("x == 2.5;", "true"); |
149 |
assertEval("new A().a == 10.0;", "true"); |
|
150 |
assertActiveKeys(); |
|
151 |
} |
|
152 |
||
153 |
public void testReplaceMethodToClass() { |
|
154 |
Snippet x = methodKey(assertEval("int x() { return 0; }")); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
155 |
TypeDeclSnippet c = classKey(assertEval("class A { double a = 4 * x(); }")); |
33362 | 156 |
assertEval("x() == 0;", "true"); |
157 |
assertEval("new A().a == 0.0;", "true"); |
|
158 |
assertEval("double x() { return 2.5; }", |
|
159 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36780
diff
changeset
|
160 |
ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
33362 | 161 |
assertEval("x();", "2.5"); |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
162 |
identityMatch(getState().types(), c); |
33362 | 163 |
assertEval("x() == 2.5;", "true"); |
164 |
assertEval("new A().a == 10.0;", "true"); |
|
165 |
assertActiveKeys(); |
|
166 |
} |
|
167 |
||
168 |
public void testReplaceClassToClass() { |
|
169 |
TypeDeclSnippet a = classKey(assertEval("class A {}")); |
|
170 |
assertTypeDeclSnippet(a, "A", VALID, CLASS_SUBKIND, 0, 0); |
|
171 |
TypeDeclSnippet b = classKey(assertEval("class B extends A {}")); |
|
172 |
TypeDeclSnippet c = classKey(assertEval("class C extends B {}")); |
|
173 |
TypeDeclSnippet d = classKey(assertEval("class D extends C {}")); |
|
174 |
assertEval("class A { int x; public String toString() { return \"NEW\"; } }", |
|
175 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
176 |
ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
177 |
ste(b, VALID, VALID, true, MAIN_SNIPPET), |
|
178 |
ste(c, VALID, VALID, true, b), |
|
179 |
ste(d, VALID, VALID, true, c)); |
|
180 |
assertTypeDeclSnippet(b, "B", VALID, CLASS_SUBKIND, 0, 0); |
|
181 |
assertTypeDeclSnippet(c, "C", VALID, CLASS_SUBKIND, 0, 0); |
|
182 |
assertTypeDeclSnippet(d, "D", VALID, CLASS_SUBKIND, 0, 0); |
|
183 |
assertEval("new D();", "NEW"); |
|
184 |
assertActiveKeys(); |
|
185 |
} |
|
186 |
||
187 |
public void testOverwriteReplaceMethod() { |
|
188 |
MethodSnippet k1 = methodKey(assertEval("String m(Integer i) { return i.toString(); }")); |
|
189 |
MethodSnippet k2 = methodKey(assertEval("String m(java.lang.Integer i) { return \"java.lang.\" + i.toString(); }", |
|
190 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
191 |
ste(k1, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
192 |
assertMethodDeclSnippet(k1, "m", "(Integer)String", OVERWRITTEN, 0, 0); |
|
193 |
assertEval("m(6);", "\"java.lang.6\""); |
|
194 |
assertEval("String m(Integer i) { return i.toString(); }", |
|
195 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
196 |
ste(k2, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
197 |
assertMethodDeclSnippet(k2, "m", "(java.lang.Integer)String", OVERWRITTEN, 0, 0); |
|
198 |
assertEval("m(6);", "\"6\""); |
|
199 |
assertActiveKeys(); |
|
200 |
} |
|
201 |
||
202 |
public void testOverwriteMethodForwardReferenceClass() { |
|
203 |
Snippet k1 = methodKey(assertEval("int q(Boo b) { return b.x; }", |
|
204 |
added(RECOVERABLE_NOT_DEFINED))); |
|
205 |
assertUnresolvedDependencies1((MethodSnippet) k1, RECOVERABLE_NOT_DEFINED, "class Boo"); |
|
206 |
assertEval("class Boo { int x = 55; }", |
|
207 |
added(VALID), |
|
208 |
ste(k1, RECOVERABLE_NOT_DEFINED, VALID, true, null)); |
|
209 |
assertMethodDeclSnippet((MethodSnippet) k1, "q", "(Boo)int", VALID, 0, 0); |
|
210 |
assertEval("q(new Boo());", "55"); |
|
211 |
assertActiveKeys(); |
|
212 |
} |
|
213 |
||
214 |
public void testOverwriteMethodForwardReferenceClassImport() { |
|
215 |
MethodSnippet k1 = methodKey(assertEval("int ff(List lis) { return lis.size(); }", |
|
216 |
added(RECOVERABLE_NOT_DEFINED))); |
|
217 |
assertUnresolvedDependencies1(k1, RECOVERABLE_NOT_DEFINED, "class List"); |
|
218 |
assertEval("import java.util.*;", |
|
219 |
added(VALID), |
|
220 |
ste(k1, RECOVERABLE_NOT_DEFINED, VALID, true, null)); |
|
221 |
assertMethodDeclSnippet(k1, "ff", "(List)int", VALID, 0, 0); |
|
222 |
assertEval("ff(new ArrayList());", "0"); |
|
223 |
assertActiveKeys(); |
|
224 |
} |
|
225 |
||
226 |
public void testForwardVarToMethod() { |
|
227 |
DeclarationSnippet t = methodKey(assertEval("int t() { return x; }", added(RECOVERABLE_DEFINED))); |
|
228 |
assertUnresolvedDependencies1(t, RECOVERABLE_DEFINED, "variable x"); |
|
229 |
assertEvalUnresolvedException("t();", "t", 1, 0); |
|
230 |
Snippet x = varKey(assertEval("int x = 33;", "33", |
|
231 |
added(VALID), |
|
232 |
ste(t, RECOVERABLE_DEFINED, VALID, false, null))); |
|
233 |
assertEval("t();", "33"); |
|
234 |
assertEval("double x = 0.88;", |
|
235 |
"0.88", null, |
|
236 |
DiagCheck.DIAG_OK, |
|
237 |
DiagCheck.DIAG_ERROR, |
|
238 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
239 |
ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
240 |
ste(t, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
241 |
assertEvalUnresolvedException("t();", "t", 0, 1); |
|
242 |
assertActiveKeys(); |
|
243 |
} |
|
244 |
||
245 |
public void testForwardMethodToMethod() { |
|
246 |
Snippet t = methodKey(assertEval("int t() { return f(); }", added(RECOVERABLE_DEFINED))); |
|
247 |
Snippet f = methodKey(assertEval("int f() { return g(); }", |
|
248 |
added(RECOVERABLE_DEFINED), |
|
249 |
ste(t, RECOVERABLE_DEFINED, VALID, false, null))); |
|
250 |
assertUnresolvedDependencies1((DeclarationSnippet) f, RECOVERABLE_DEFINED, "method g()"); |
|
251 |
assertEvalUnresolvedException("t();", "f", 1, 0); |
|
252 |
Snippet g = methodKey(assertEval("int g() { return 55; }", |
|
253 |
added(VALID), |
|
254 |
ste(f, RECOVERABLE_DEFINED, VALID, false, null))); |
|
255 |
assertEval("t();", "55"); |
|
256 |
assertEval("double g() { return 3.14159; }", |
|
257 |
DiagCheck.DIAG_OK, |
|
258 |
DiagCheck.DIAG_ERROR, |
|
259 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
260 |
ste(g, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
261 |
ste(f, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
36499 | 262 |
DeclarationSnippet exsn = assertEvalUnresolvedException("t();", "f", 0, 1); |
33362 | 263 |
assertTrue(exsn == f, "Identity must not change"); |
264 |
assertActiveKeys(); |
|
265 |
} |
|
266 |
||
267 |
public void testForwardClassToMethod() { |
|
268 |
DeclarationSnippet t = methodKey(assertEval("int t() { return new A().f(); }", added(RECOVERABLE_DEFINED))); |
|
269 |
assertUnresolvedDependencies1(t, RECOVERABLE_DEFINED, "class A"); |
|
270 |
assertEvalUnresolvedException("t();", "t", 1, 0); |
|
271 |
Snippet a = classKey(assertEval( |
|
272 |
"class A {\n" + |
|
273 |
" int f() { return 10; }\n" + |
|
274 |
"}", |
|
275 |
added(VALID), |
|
276 |
ste(t, RECOVERABLE_DEFINED, VALID, false, null))); |
|
277 |
assertEval("t();", "10"); |
|
278 |
assertEval( |
|
279 |
"class A {\n" + |
|
280 |
" double f() { return 88.0; }\n" + |
|
281 |
"}", |
|
282 |
DiagCheck.DIAG_OK, |
|
283 |
DiagCheck.DIAG_ERROR, |
|
284 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
285 |
ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
286 |
ste(t, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
287 |
assertEvalUnresolvedException("t();", "t", 0, 1); |
|
288 |
assertActiveKeys(); |
|
289 |
} |
|
290 |
||
291 |
public void testForwardVarToClass() { |
|
36499 | 292 |
DeclarationSnippet a = classKey(assertEval("class A { int f() { return g; } }", added(RECOVERABLE_DEFINED))); |
293 |
assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable g"); |
|
294 |
Snippet g = varKey(assertEval("int g = 10;", "10", |
|
295 |
added(VALID), |
|
296 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null))); |
|
297 |
assertEval("new A().f();", "10"); |
|
298 |
assertEval("double g = 10;", "10.0", null, |
|
299 |
DiagCheck.DIAG_OK, |
|
300 |
DiagCheck.DIAG_ERROR, |
|
301 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
302 |
ste(g, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
303 |
ste(a, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
304 |
assertUnresolvedDependencies(a, 0); |
|
305 |
assertActiveKeys(); |
|
306 |
} |
|
307 |
||
308 |
public void testForwardVarToClassGeneric() { |
|
309 |
DeclarationSnippet a = classKey(assertEval("class A<T> { final T x; A(T v) { this.x = v; } ; T get() { return x; } int core() { return g; } }", added(RECOVERABLE_DEFINED))); |
|
310 |
assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable g"); |
|
311 |
||
312 |
List<SnippetEvent> events = assertEval("A<String> as = new A<>(\"hi\");", null, |
|
313 |
UnresolvedReferenceException.class, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null); |
|
314 |
SnippetEvent ste = events.get(0); |
|
315 |
Snippet assn = ste.snippet(); |
|
316 |
DeclarationSnippet unsn = ((UnresolvedReferenceException) ste.exception()).getSnippet(); |
|
317 |
assertEquals(unsn.name(), "A", "Wrong with unresolved"); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
318 |
assertEquals(getState().unresolvedDependencies(unsn).count(), 1, "Wrong size unresolved"); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
37644
diff
changeset
|
319 |
assertEquals(getState().diagnostics(unsn).count(), 0L, "Expected no diagnostics"); |
36499 | 320 |
|
33362 | 321 |
Snippet g = varKey(assertEval("int g = 10;", "10", |
322 |
added(VALID), |
|
36499 | 323 |
ste(a, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET))); |
324 |
assertEval("A<String> as = new A<>(\"low\");", |
|
325 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
326 |
ste(assn, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
327 |
assertEval("as.get();", "\"low\""); |
|
328 |
assertUnresolvedDependencies(a, 0); |
|
329 |
assertActiveKeys(); |
|
330 |
} |
|
331 |
||
332 |
public void testForwardVarToClassExtendsImplements() { |
|
333 |
DeclarationSnippet ik = classKey(assertEval("interface I { default int ii() { return 1; } }", added(VALID))); |
|
334 |
DeclarationSnippet jk = classKey(assertEval("interface J { default int jj() { return 2; } }", added(VALID))); |
|
335 |
DeclarationSnippet ck = classKey(assertEval("class C { int cc() { return 3; } }", added(VALID))); |
|
336 |
DeclarationSnippet dk = classKey(assertEval("class D extends C implements I,J { int dd() { return g; } }", added(RECOVERABLE_DEFINED))); |
|
337 |
DeclarationSnippet ek = classKey(assertEval("class E extends D { int ee() { return 5; } }", added(VALID))); |
|
338 |
assertUnresolvedDependencies1(dk, RECOVERABLE_DEFINED, "variable g"); |
|
339 |
assertEvalUnresolvedException("new D();", "D", 1, 0); |
|
340 |
assertEvalUnresolvedException("new E();", "D", 1, 0); |
|
341 |
VarSnippet g = varKey(assertEval("int g = 10;", "10", |
|
342 |
added(VALID), |
|
343 |
ste(dk, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET))); |
|
344 |
assertEval("E e = new E();"); |
|
345 |
assertDrop(g, |
|
346 |
ste(g, VALID, DROPPED, true, null), |
|
347 |
ste(dk, VALID, RECOVERABLE_DEFINED, false, g)); |
|
348 |
assertEvalUnresolvedException("new D();", "D", 1, 0); |
|
349 |
assertEvalUnresolvedException("new E();", "D", 1, 0); |
|
350 |
assertEval("e.ee();", "5"); |
|
351 |
assertEvalUnresolvedException("e.dd();", "D", 1, 0); |
|
352 |
assertEval("e.cc();", "3"); |
|
353 |
assertEval("e.jj();", "2"); |
|
354 |
assertEval("e.ii();", "1"); |
|
355 |
assertActiveKeys(); |
|
356 |
} |
|
357 |
||
358 |
public void testForwardVarToInterface() { |
|
359 |
DeclarationSnippet i = classKey(assertEval("interface I { default int f() { return x; } }", added(RECOVERABLE_DEFINED))); |
|
360 |
assertUnresolvedDependencies1(i, RECOVERABLE_DEFINED, "variable x"); |
|
361 |
DeclarationSnippet c = classKey(assertEval("class C implements I { int z() { return 2; } }", added(VALID))); |
|
362 |
assertEval("C c = new C();"); |
|
363 |
assertEval("c.z();", "2"); |
|
364 |
assertEvalUnresolvedException("c.f()", "I", 1, 0); |
|
365 |
Snippet g = varKey(assertEval("int x = 55;", "55", |
|
366 |
added(VALID), |
|
367 |
ste(i, RECOVERABLE_DEFINED, VALID, false, null))); |
|
368 |
assertEval("c.f();", "55"); |
|
369 |
assertUnresolvedDependencies(i, 0); |
|
370 |
assertActiveKeys(); |
|
371 |
} |
|
372 |
||
373 |
public void testForwardVarToEnum() { |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
374 |
DeclarationSnippet a = classKey(assertEval("enum E { Q, W, E; float ff() { return fff; } }", added(RECOVERABLE_DEFINED))); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
375 |
assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable fff"); |
36499 | 376 |
Snippet g = varKey(assertEval("float fff = 4.5f;", "4.5", |
377 |
added(VALID), |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
378 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null))); |
36499 | 379 |
assertEval("E.Q.ff();", "4.5"); |
380 |
assertEval("double fff = 3.3;", "3.3", null, |
|
33362 | 381 |
DiagCheck.DIAG_OK, |
382 |
DiagCheck.DIAG_ERROR, |
|
383 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
384 |
ste(g, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
385 |
ste(a, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
33362 | 386 |
assertUnresolvedDependencies(a, 0); |
387 |
assertActiveKeys(); |
|
388 |
} |
|
389 |
||
390 |
public void testForwardMethodToClass() { |
|
36499 | 391 |
DeclarationSnippet a = classKey(assertEval("class A { int f() { return g(); } }", added(RECOVERABLE_DEFINED))); |
392 |
assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "method g()"); |
|
393 |
assertEval("A foo() { return null; }"); |
|
394 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 395 |
Snippet g = methodKey(assertEval("int g() { return 10; }", |
396 |
added(VALID), |
|
36499 | 397 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null))); |
33362 | 398 |
assertEval("new A().f();", "10"); |
399 |
assertEval("double g() { return 10; }", |
|
400 |
DiagCheck.DIAG_OK, |
|
401 |
DiagCheck.DIAG_ERROR, |
|
402 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
403 |
ste(g, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
36499 | 404 |
ste(a, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
33362 | 405 |
assertUnresolvedDependencies(a, 0); |
406 |
assertActiveKeys(); |
|
407 |
} |
|
408 |
||
409 |
public void testForwardClassToClass1() { |
|
410 |
Snippet a = classKey(assertEval("class A { B b = new B(); }", added(RECOVERABLE_NOT_DEFINED))); |
|
411 |
assertDeclareFail("new A().b;", "compiler.err.cant.resolve.location"); |
|
412 |
||
413 |
Snippet b = classKey(assertEval("class B { public String toString() { return \"B\"; } }", |
|
414 |
added(VALID), |
|
415 |
ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, null))); |
|
416 |
assertEval("new A().b;", "B"); |
|
417 |
assertEval("interface B { }", |
|
418 |
DiagCheck.DIAG_OK, |
|
419 |
DiagCheck.DIAG_ERROR, |
|
420 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
421 |
ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
36499 | 422 |
ste(a, VALID, RECOVERABLE_DEFINED, true, MAIN_SNIPPET)); |
423 |
assertEvalUnresolvedException("new A().b;", "A", 0, 1); |
|
33362 | 424 |
assertActiveKeys(); |
425 |
} |
|
426 |
||
427 |
public void testForwardClassToClass2() { |
|
428 |
Snippet a = classKey(assertEval("class A extends B { }", added(RECOVERABLE_NOT_DEFINED))); |
|
429 |
assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
430 |
||
431 |
Snippet b = classKey(assertEval("class B { public String toString() { return \"B\"; } }", |
|
432 |
added(VALID), |
|
433 |
ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, null))); |
|
434 |
assertEval("new A();", "B"); |
|
435 |
assertEval("interface B { }", |
|
436 |
DiagCheck.DIAG_OK, |
|
437 |
DiagCheck.DIAG_ERROR, |
|
438 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
439 |
ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
440 |
ste(a, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET)); |
|
441 |
assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
442 |
assertActiveKeys(); |
|
443 |
} |
|
444 |
||
445 |
public void testForwardClassToClass3() { |
|
446 |
Snippet a = classKey(assertEval("interface A extends B { static int f() { return 10; } }", added(RECOVERABLE_NOT_DEFINED))); |
|
447 |
assertDeclareFail("A.f();", "compiler.err.cant.resolve.location"); |
|
448 |
||
449 |
Snippet b = classKey(assertEval("interface B { }", |
|
450 |
added(VALID), |
|
451 |
ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, null))); |
|
452 |
assertEval("A.f();", "10"); |
|
453 |
assertEval("class B { }", |
|
454 |
DiagCheck.DIAG_OK, |
|
455 |
DiagCheck.DIAG_ERROR, |
|
456 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
457 |
ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
458 |
ste(a, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET)); |
|
459 |
assertDeclareFail("A.f();", "compiler.err.cant.resolve.location"); |
|
460 |
assertActiveKeys(); |
|
461 |
} |
|
462 |
||
463 |
public void testImportDeclare() { |
|
464 |
Snippet singleImport = importKey(assertEval("import java.util.List;", added(VALID))); |
|
465 |
Snippet importOnDemand = importKey(assertEval("import java.util.*;", added(VALID))); |
|
466 |
Snippet singleStaticImport = importKey(assertEval("import static java.lang.Math.abs;", added(VALID))); |
|
467 |
Snippet staticImportOnDemand = importKey(assertEval("import static java.lang.Math.*;", added(VALID))); |
|
468 |
assertEval("import java.util.List; //again", |
|
469 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
470 |
ste(singleImport, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
471 |
assertEval("import java.util.*; //again", |
|
472 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
473 |
ste(importOnDemand, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
474 |
assertEval("import static java.lang.Math.abs; //again", |
|
475 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
476 |
ste(singleStaticImport, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
477 |
assertEval("import static java.lang.Math.*; //again", |
|
478 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
479 |
ste(staticImportOnDemand, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
480 |
assertActiveKeys(); |
|
481 |
} |
|
482 |
||
483 |
public void testForwardVariable() { |
|
484 |
assertEval("int f() { return x; }", added(RECOVERABLE_DEFINED)); |
|
485 |
assertEvalUnresolvedException("f();", "f", 1, 0); |
|
486 |
assertActiveKeys(); |
|
487 |
} |
|
488 |
||
489 |
public void testLocalClassInUnresolved() { |
|
490 |
Snippet f = methodKey(assertEval("void f() { class A {} g(); }", added(RECOVERABLE_DEFINED))); |
|
491 |
assertEval("void g() {}", |
|
492 |
added(VALID), |
|
493 |
ste(f, RECOVERABLE_DEFINED, VALID, false, null)); |
|
494 |
assertEval("f();", ""); |
|
495 |
} |
|
496 |
||
497 |
@Test(enabled = false) // TODO 8129420 |
|
498 |
public void testLocalClassEvolve() { |
|
499 |
Snippet j = methodKey(assertEval("Object j() { return null; }", added(VALID))); |
|
500 |
assertEval("Object j() { class B {}; return null; }", |
|
501 |
ste(MAIN_SNIPPET, VALID, VALID, false, null)); |
|
502 |
assertEval("Object j() { class B {}; return new B(); }", |
|
503 |
ste(MAIN_SNIPPET, VALID, VALID, false, null)); |
|
504 |
assertEval("j().getClass().getSimpleName();", "\"B\""); |
|
505 |
assertEval("Object j() { class B { int p; public String toString() { return \"Yep\";} }; return new B(); }", |
|
506 |
ste(MAIN_SNIPPET, VALID, VALID, false, null)); |
|
507 |
assertEval("j().getClass().getSimpleName();", "\"B\""); |
|
508 |
assertEval("j();", "Yep"); |
|
509 |
} |
|
510 |
||
511 |
public void testForwardSingleImportMethodToMethod() { |
|
512 |
DeclarationSnippet string = methodKey(assertEval("String string() { return format(\"string\"); }", |
|
513 |
added(RECOVERABLE_DEFINED))); |
|
514 |
assertUnresolvedDependencies1(string, RECOVERABLE_DEFINED, "method format(java.lang.String)"); |
|
515 |
assertEvalUnresolvedException("string();", "string", 1, 0); |
|
516 |
assertEval("import static java.lang.String.format;", |
|
517 |
added(VALID), |
|
518 |
ste(string, RECOVERABLE_DEFINED, VALID, false, null)); |
|
519 |
assertEval("string();", "\"string\""); |
|
520 |
||
521 |
assertEval("double format(String s) { return 0; }", |
|
522 |
DiagCheck.DIAG_OK, |
|
523 |
DiagCheck.DIAG_ERROR, |
|
524 |
added(VALID), |
|
525 |
ste(string, VALID, RECOVERABLE_DEFINED, false, null)); |
|
526 |
assertEvalUnresolvedException("string();", "string", 0, 1); |
|
527 |
assertActiveKeys(); |
|
528 |
} |
|
529 |
||
530 |
public void testForwardImportMethodOnDemandToMethod() { |
|
531 |
DeclarationSnippet string = methodKey(assertEval("String string() { return format(\"string\"); }", |
|
532 |
added(RECOVERABLE_DEFINED))); |
|
533 |
assertUnresolvedDependencies1(string, RECOVERABLE_DEFINED, "method format(java.lang.String)"); |
|
534 |
assertEvalUnresolvedException("string();", "string", 1, 0); |
|
535 |
assertEval("import static java.lang.String.*;", |
|
536 |
added(VALID), |
|
537 |
ste(string, RECOVERABLE_DEFINED, VALID, false, null)); |
|
538 |
assertEval("string();", "\"string\""); |
|
539 |
||
540 |
assertEval("double format(String s) { return 0; }", |
|
541 |
DiagCheck.DIAG_OK, |
|
542 |
DiagCheck.DIAG_ERROR, |
|
543 |
added(VALID), |
|
544 |
ste(string, VALID, RECOVERABLE_DEFINED, false, null)); |
|
545 |
assertEvalUnresolvedException("string();", "string", 0, 1); |
|
546 |
assertActiveKeys(); |
|
547 |
} |
|
548 |
||
549 |
public void testForwardSingleImportFieldToMethod() { |
|
550 |
DeclarationSnippet pi = methodKey(assertEval("double pi() { return PI; }", |
|
551 |
added(RECOVERABLE_DEFINED))); |
|
552 |
assertUnresolvedDependencies1(pi, RECOVERABLE_DEFINED, "variable PI"); |
|
553 |
assertEvalUnresolvedException("pi();", "pi", 1, 0); |
|
554 |
assertEval("import static java.lang.Math.PI;", |
|
555 |
added(VALID), |
|
556 |
ste(pi, RECOVERABLE_DEFINED, VALID, false, null)); |
|
557 |
assertEval("Math.abs(pi() - 3.1415) < 0.001;", "true"); |
|
558 |
||
559 |
assertEval("String PI;", |
|
560 |
DiagCheck.DIAG_OK, |
|
561 |
DiagCheck.DIAG_ERROR, |
|
562 |
added(VALID), |
|
563 |
ste(pi, VALID, RECOVERABLE_DEFINED, false, null)); |
|
564 |
assertEvalUnresolvedException("pi();", "pi", 0, 1); |
|
565 |
assertActiveKeys(); |
|
566 |
} |
|
567 |
||
568 |
public void testForwardImportFieldOnDemandToMethod() { |
|
569 |
DeclarationSnippet pi = methodKey(assertEval("double pi() { return PI; }", |
|
570 |
added(RECOVERABLE_DEFINED))); |
|
571 |
assertUnresolvedDependencies1(pi, RECOVERABLE_DEFINED, "variable PI"); |
|
572 |
assertEvalUnresolvedException("pi();", "pi", 1, 0); |
|
573 |
assertEval("import static java.lang.Math.*;", |
|
574 |
added(VALID), |
|
575 |
ste(pi, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET)); |
|
576 |
assertEval("Math.abs(pi() - 3.1415) < 0.001;", "true"); |
|
577 |
||
578 |
assertEval("String PI;", |
|
579 |
DiagCheck.DIAG_OK, |
|
580 |
DiagCheck.DIAG_ERROR, |
|
581 |
added(VALID), |
|
582 |
ste(pi, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
583 |
assertEvalUnresolvedException("pi();", "pi", 0, 1); |
|
584 |
assertActiveKeys(); |
|
585 |
} |
|
586 |
||
587 |
public void testForwardSingleImportMethodToClass1() { |
|
588 |
PersistentSnippet a = classKey(assertEval("class A { String s = format(\"%d\", 10); }", |
|
36499 | 589 |
added(RECOVERABLE_DEFINED))); |
590 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 591 |
assertEval("import static java.lang.String.format;", |
592 |
added(VALID), |
|
36499 | 593 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null)); |
33362 | 594 |
assertEval("new A().s;", "\"10\""); |
595 |
PersistentSnippet format = methodKey(assertEval("void format(String s, int d) { }", |
|
596 |
DiagCheck.DIAG_OK, |
|
597 |
DiagCheck.DIAG_ERROR, |
|
598 |
added(VALID), |
|
36499 | 599 |
ste(a, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET))); |
600 |
assertEvalUnresolvedException("new A();", "A", 0, 1); |
|
33362 | 601 |
assertActiveKeys(); |
602 |
assertDrop(format, |
|
603 |
ste(format, VALID, DROPPED, true, null), |
|
36499 | 604 |
ste(a, RECOVERABLE_DEFINED, VALID, false, format)); |
33362 | 605 |
} |
606 |
||
607 |
public void testForwardSingleImportMethodToClass2() { |
|
608 |
PersistentSnippet a = classKey(assertEval("class A { String s() { return format(\"%d\", 10); } }", |
|
36499 | 609 |
added(RECOVERABLE_DEFINED))); |
610 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 611 |
assertEval("import static java.lang.String.format;", |
612 |
added(VALID), |
|
36499 | 613 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null)); |
33362 | 614 |
assertEval("new A().s();", "\"10\""); |
615 |
PersistentSnippet format = methodKey(assertEval("void format(String s, int d) { }", |
|
616 |
DiagCheck.DIAG_OK, |
|
617 |
DiagCheck.DIAG_ERROR, |
|
618 |
added(VALID), |
|
36499 | 619 |
ste(a, VALID, RECOVERABLE_DEFINED, false, null))); |
620 |
assertEvalUnresolvedException("new A();", "A", 0, 1); |
|
33362 | 621 |
assertActiveKeys(); |
622 |
assertDrop(format, |
|
623 |
ste(format, VALID, DROPPED, true, null), |
|
36499 | 624 |
ste(a, RECOVERABLE_DEFINED, VALID, false, format)); |
33362 | 625 |
} |
626 |
||
627 |
public void testForwardSingleImportClassToClass1() { |
|
628 |
PersistentSnippet a = classKey(assertEval("class A { static List<Integer> list; }", |
|
629 |
added(RECOVERABLE_NOT_DEFINED))); |
|
630 |
assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
631 |
assertEval("import java.util.List;", |
|
632 |
added(VALID), |
|
633 |
ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, null)); |
|
634 |
assertEval("import java.util.Arrays;", added(VALID)); |
|
635 |
assertEval("A.list = Arrays.asList(1, 2, 3);", "[1, 2, 3]"); |
|
636 |
||
637 |
PersistentSnippet list = classKey(assertEval("class List {}", |
|
638 |
DiagCheck.DIAG_OK, |
|
639 |
DiagCheck.DIAG_ERROR, |
|
640 |
added(VALID), |
|
641 |
ste(a, VALID, RECOVERABLE_NOT_DEFINED, true, null))); |
|
642 |
assertDeclareFail("A.list = Arrays.asList(1, 2, 3);", "compiler.err.already.defined.static.single.import"); |
|
643 |
assertActiveKeys(); |
|
644 |
assertDrop(list, |
|
645 |
ste(list, VALID, DROPPED, true, null), |
|
646 |
ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, list)); |
|
647 |
} |
|
648 |
||
649 |
public void testForwardSingleImportClassToClass2() { |
|
650 |
PersistentSnippet clsA = classKey(assertEval("class A extends ArrayList<Integer> { }", |
|
651 |
added(RECOVERABLE_NOT_DEFINED))); |
|
652 |
assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
653 |
assertEval("import java.util.ArrayList;", |
|
654 |
added(VALID), |
|
655 |
ste(clsA, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET)); |
|
656 |
Snippet vara = varKey(assertEval("A a = new A();", "[]")); |
|
657 |
||
658 |
PersistentSnippet arraylist = classKey(assertEval("class ArrayList {}", |
|
659 |
DiagCheck.DIAG_OK, |
|
660 |
DiagCheck.DIAG_ERROR, |
|
661 |
added(VALID), |
|
662 |
ste(clsA, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET), |
|
663 |
ste(vara, VALID, RECOVERABLE_NOT_DEFINED, true, clsA))); |
|
664 |
assertDeclareFail("A a = new A();", "compiler.err.cant.resolve.location", |
|
665 |
ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, REJECTED, false, null), |
|
666 |
ste(vara, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
667 |
assertActiveKeys(); |
|
668 |
assertDrop(arraylist, |
|
669 |
ste(arraylist, VALID, DROPPED, true, null), |
|
670 |
ste(clsA, RECOVERABLE_NOT_DEFINED, VALID, true, arraylist)); |
|
671 |
} |
|
672 |
||
673 |
public void testForwardImportOnDemandMethodToClass1() { |
|
674 |
PersistentSnippet a = classKey(assertEval("class A { String s = format(\"%d\", 10); }", |
|
36499 | 675 |
added(RECOVERABLE_DEFINED))); |
676 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 677 |
assertEval("import static java.lang.String.*;", |
678 |
added(VALID), |
|
36499 | 679 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null)); |
680 |
assertEval("A x = new A();"); |
|
681 |
assertEval("x.s;", "\"10\""); |
|
33362 | 682 |
PersistentSnippet format = methodKey(assertEval("void format(String s, int d) { }", |
683 |
DiagCheck.DIAG_OK, |
|
684 |
DiagCheck.DIAG_ERROR, |
|
685 |
added(VALID), |
|
36499 | 686 |
ste(a, VALID, RECOVERABLE_DEFINED, false, null))); |
687 |
assertEvalUnresolvedException("new A();", "A", 0, 1); |
|
33362 | 688 |
assertActiveKeys(); |
689 |
assertDrop(format, |
|
690 |
ste(format, VALID, DROPPED, true, null), |
|
36499 | 691 |
ste(a, RECOVERABLE_DEFINED, VALID, false, format)); |
692 |
assertEval("x.s;", "\"10\""); |
|
33362 | 693 |
} |
694 |
||
695 |
public void testForwardImportOnDemandMethodToClass2() { |
|
696 |
PersistentSnippet a = classKey(assertEval("class A { String s() { return format(\"%d\", 10); } }", |
|
36499 | 697 |
added(RECOVERABLE_DEFINED))); |
698 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 699 |
assertEval("import static java.lang.String.*;", |
700 |
added(VALID), |
|
36499 | 701 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null)); |
33362 | 702 |
assertEval("new A().s();", "\"10\""); |
703 |
PersistentSnippet format = methodKey(assertEval("void format(String s, int d) { }", |
|
704 |
DiagCheck.DIAG_OK, |
|
705 |
DiagCheck.DIAG_ERROR, |
|
706 |
added(VALID), |
|
36499 | 707 |
ste(a, VALID, RECOVERABLE_DEFINED, false, null))); |
708 |
assertEvalUnresolvedException("new A();", "A", 0, 1); |
|
33362 | 709 |
assertActiveKeys(); |
710 |
assertDrop(format, |
|
711 |
ste(format, VALID, DROPPED, true, null), |
|
36499 | 712 |
ste(a, RECOVERABLE_DEFINED, VALID, false, format)); |
33362 | 713 |
} |
714 |
||
715 |
public void testForwardImportOnDemandClassToClass1() { |
|
716 |
PersistentSnippet a = classKey(assertEval("class A { static List<Integer> list; }", |
|
717 |
added(RECOVERABLE_NOT_DEFINED))); |
|
718 |
assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
719 |
assertEval("import java.util.*;", |
|
720 |
added(VALID), |
|
721 |
ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, null)); |
|
722 |
assertEval("A.list = Arrays.asList(1, 2, 3);", "[1, 2, 3]"); |
|
723 |
||
724 |
PersistentSnippet list = classKey(assertEval("class List {}", |
|
725 |
DiagCheck.DIAG_OK, |
|
726 |
DiagCheck.DIAG_ERROR, |
|
727 |
added(VALID), |
|
728 |
ste(a, VALID, RECOVERABLE_NOT_DEFINED, true, null))); |
|
729 |
assertDeclareFail("A.list = Arrays.asList(1, 2, 3);", "compiler.err.cant.resolve.location"); |
|
730 |
assertActiveKeys(); |
|
731 |
assertDrop(list, |
|
732 |
ste(list, VALID, DROPPED, true, null), |
|
733 |
ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, list)); |
|
734 |
} |
|
735 |
||
736 |
public void testForwardImportOnDemandClassToClass2() { |
|
737 |
PersistentSnippet clsA = classKey(assertEval("class A extends ArrayList<Integer> { }", |
|
738 |
added(RECOVERABLE_NOT_DEFINED))); |
|
739 |
assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
740 |
assertEval("import java.util.*;", |
|
741 |
added(VALID), |
|
742 |
ste(clsA, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET)); |
|
743 |
Snippet vara = varKey(assertEval("A a = new A();", "[]")); |
|
744 |
||
745 |
PersistentSnippet arraylist = classKey(assertEval("class ArrayList {}", |
|
746 |
DiagCheck.DIAG_OK, |
|
747 |
DiagCheck.DIAG_ERROR, |
|
748 |
added(VALID), |
|
749 |
ste(clsA, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET), |
|
750 |
ste(vara, VALID, RECOVERABLE_NOT_DEFINED, true, clsA))); |
|
751 |
assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
752 |
assertActiveKeys(); |
|
753 |
assertDrop(arraylist, |
|
754 |
ste(arraylist, VALID, DROPPED, true, null), |
|
755 |
ste(clsA, RECOVERABLE_NOT_DEFINED, VALID, true, arraylist), |
|
756 |
ste(vara, RECOVERABLE_NOT_DEFINED, VALID, true, clsA)); |
|
757 |
} |
|
758 |
||
759 |
public void testForwardSingleImportFieldToClass1() { |
|
760 |
PersistentSnippet a = classKey(assertEval("class A { static double pi() { return PI; } }", |
|
36499 | 761 |
added(RECOVERABLE_DEFINED))); |
762 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 763 |
assertEval("import static java.lang.Math.PI;", |
764 |
added(VALID), |
|
36499 | 765 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null)); |
33362 | 766 |
assertEval("Math.abs(A.pi() - 3.1415) < 0.001;", "true"); |
767 |
||
768 |
PersistentSnippet list = varKey(assertEval("String PI;", |
|
769 |
DiagCheck.DIAG_OK, |
|
770 |
DiagCheck.DIAG_ERROR, |
|
771 |
added(VALID), |
|
36499 | 772 |
ste(a, VALID, RECOVERABLE_DEFINED, false, null))); |
773 |
assertEvalUnresolvedException("new A();", "A", 0, 1); |
|
33362 | 774 |
assertActiveKeys(); |
775 |
assertDrop(list, |
|
776 |
ste(list, VALID, DROPPED, true, null), |
|
36499 | 777 |
ste(a, RECOVERABLE_DEFINED, VALID, false, list)); |
33362 | 778 |
} |
779 |
||
780 |
public void testForwardSingleImportFieldToClass2() { |
|
781 |
PersistentSnippet a = classKey(assertEval("class A { static double pi = PI; }", |
|
36499 | 782 |
added(RECOVERABLE_DEFINED))); |
783 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 784 |
assertEval("import static java.lang.Math.PI;", |
785 |
added(VALID), |
|
36499 | 786 |
ste(a, RECOVERABLE_DEFINED, VALID, true, null)); |
33362 | 787 |
assertEval("Math.abs(A.pi - 3.1415) < 0.001;", "true"); |
788 |
||
789 |
PersistentSnippet list = varKey(assertEval("String PI;", |
|
790 |
DiagCheck.DIAG_OK, |
|
791 |
DiagCheck.DIAG_ERROR, |
|
792 |
added(VALID), |
|
36499 | 793 |
ste(a, VALID, RECOVERABLE_DEFINED, true, null))); |
794 |
assertEvalUnresolvedException("new A();", "A", 0, 1); |
|
33362 | 795 |
assertActiveKeys(); |
796 |
assertDrop(list, |
|
797 |
ste(list, VALID, DROPPED, true, null), |
|
36499 | 798 |
ste(a, RECOVERABLE_DEFINED, VALID, true, list)); |
33362 | 799 |
} |
800 |
||
801 |
public void testForwardImportOnDemandFieldToClass1() { |
|
802 |
PersistentSnippet a = classKey(assertEval("class A { static double pi() { return PI; } }", |
|
36499 | 803 |
added(RECOVERABLE_DEFINED))); |
804 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 805 |
assertEval("import static java.lang.Math.*;", |
806 |
added(VALID), |
|
36499 | 807 |
ste(a, RECOVERABLE_DEFINED, VALID, false, null)); |
33362 | 808 |
assertEval("Math.abs(A.pi() - 3.1415) < 0.001;", "true"); |
809 |
||
810 |
PersistentSnippet list = varKey(assertEval("String PI;", |
|
811 |
DiagCheck.DIAG_OK, |
|
812 |
DiagCheck.DIAG_ERROR, |
|
813 |
added(VALID), |
|
36499 | 814 |
ste(a, VALID, RECOVERABLE_DEFINED, false, null))); |
815 |
assertEvalUnresolvedException("new A();", "A", 0, 1); |
|
33362 | 816 |
assertActiveKeys(); |
817 |
assertDrop(list, |
|
818 |
ste(list, VALID, DROPPED, true, null), |
|
36499 | 819 |
ste(a, RECOVERABLE_DEFINED, VALID, false, list)); |
33362 | 820 |
} |
821 |
||
822 |
public void testForwardImportOnDemandFieldToClass2() { |
|
823 |
PersistentSnippet a = classKey(assertEval("class A { static double pi = PI; }", |
|
36499 | 824 |
added(RECOVERABLE_DEFINED))); |
825 |
assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
33362 | 826 |
assertEval("import static java.lang.Math.*;", |
827 |
added(VALID), |
|
36499 | 828 |
ste(a, RECOVERABLE_DEFINED, VALID, true, null)); |
33362 | 829 |
assertEval("Math.abs(A.pi - 3.1415) < 0.001;", "true"); |
830 |
||
831 |
PersistentSnippet list = varKey(assertEval("String PI;", |
|
832 |
DiagCheck.DIAG_OK, |
|
833 |
DiagCheck.DIAG_ERROR, |
|
834 |
added(VALID), |
|
36499 | 835 |
ste(a, VALID, RECOVERABLE_DEFINED, true, null))); |
836 |
assertEvalUnresolvedException("new A();", "A", 0, 1); |
|
33362 | 837 |
assertActiveKeys(); |
838 |
assertDrop(list, |
|
839 |
ste(list, VALID, DROPPED, true, null), |
|
36499 | 840 |
ste(a, RECOVERABLE_DEFINED, VALID, true, list)); |
841 |
assertEval("Math.abs(A.pi - 3.1415) < 0.001;", "true"); |
|
33362 | 842 |
} |
843 |
||
844 |
public void testReplaceCausesMethodReferenceError() { |
|
845 |
Snippet l = classKey(assertEval("interface Logger { public void log(String message); }", added(VALID))); |
|
846 |
Snippet v = varKey(assertEval("Logger l = System.out::println;", added(VALID))); |
|
847 |
assertEval("interface Logger { public boolean accept(String message); }", |
|
848 |
DiagCheck.DIAG_OK, |
|
849 |
DiagCheck.DIAG_ERROR, |
|
850 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
851 |
ste(l, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
852 |
ste(v, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET)); |
|
853 |
} |
|
854 |
||
855 |
public void testReplaceCausesClassCompilationError() { |
|
856 |
Snippet l = classKey(assertEval("interface L { }", added(VALID))); |
|
857 |
Snippet c = classKey(assertEval("class C implements L { }", added(VALID))); |
|
858 |
assertEval("interface L { void m(); }", |
|
859 |
DiagCheck.DIAG_OK, |
|
860 |
DiagCheck.DIAG_ERROR, |
|
861 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
862 |
ste(l, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
863 |
ste(c, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET)); |
|
864 |
} |
|
865 |
||
866 |
public void testOverwriteNoUpdate() { |
|
867 |
String xsi = "int x = 5;"; |
|
868 |
String xsd = "double x = 3.14159;"; |
|
869 |
VarSnippet xi = varKey(assertEval(xsi, added(VALID))); |
|
870 |
String ms1 = "double m(Integer i) { return i + x; }"; |
|
871 |
String ms2 = "double m(java.lang.Integer i) { return i + x; }"; |
|
872 |
MethodSnippet k1 = methodKey(assertEval(ms1, added(VALID))); |
|
873 |
VarSnippet xd = varKey(assertEval(xsd, |
|
874 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36780
diff
changeset
|
875 |
ste(xi, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
33362 | 876 |
MethodSnippet k2 = methodKey(assertEval(ms2, |
877 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), //TODO: technically, should be false |
|
878 |
ste(k1, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
879 |
VarSnippet xi2 = varKey(assertEval(xsi, |
|
880 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36780
diff
changeset
|
881 |
ste(xd, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
33362 | 882 |
varKey(assertEval(xsd, |
883 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36780
diff
changeset
|
884 |
ste(xi2, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
33362 | 885 |
} |
886 |
} |