author | tschatzl |
Fri, 04 Aug 2017 14:15:42 +0200 | |
changeset 46750 | bcab0128a16f |
parent 45045 | 834233132ab1 |
permissions | -rw-r--r-- |
39370
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
1 |
/* |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
4 |
* |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
8 |
* |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
13 |
* accompanied this code). |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
14 |
* |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
45045
834233132ab1
8179889: Fix typographic errors in copyright headers
ihse
parents:
39370
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
39370
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
18 |
* |
45045
834233132ab1
8179889: Fix typographic errors in copyright headers
ihse
parents:
39370
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
39370
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
21 |
* questions. |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
22 |
*/ |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
23 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
24 |
/* |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
25 |
* @test |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
26 |
* @bug 8159111 |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
27 |
* @summary test wrappers and dependencies |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
28 |
* @modules jdk.jshell/jdk.jshell |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
29 |
* @build KullaTesting |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
30 |
* @run testng WrapperTest |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
31 |
*/ |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
32 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
33 |
import java.util.Collection; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
34 |
import java.util.List; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
35 |
import org.testng.annotations.Test; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
36 |
import jdk.jshell.ErroneousSnippet; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
37 |
import jdk.jshell.Snippet; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
38 |
import jdk.jshell.Snippet.Kind; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
39 |
import jdk.jshell.SourceCodeAnalysis.SnippetWrapper; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
40 |
import static org.testng.Assert.assertEquals; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
41 |
import static org.testng.Assert.assertTrue; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
42 |
import static jdk.jshell.Snippet.Status.RECOVERABLE_DEFINED; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
43 |
import static jdk.jshell.Snippet.Status.VALID; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
44 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
45 |
@Test |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
46 |
public class WrapperTest extends KullaTesting { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
47 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
48 |
public void testMethod() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
49 |
String src = "void glib() { System.out.println(\"hello\"); }"; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
50 |
List<SnippetWrapper> swl = getState().sourceCodeAnalysis().wrappers(src); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
51 |
assertEquals(swl.size(), 1, "unexpected list length"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
52 |
assertWrapperHas(swl.get(0), src, Kind.METHOD, "void", "glib", "println"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
53 |
assertPosition(swl.get(0), src, 0, 4); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
54 |
assertPosition(swl.get(0), src, 5, 4); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
55 |
assertPosition(swl.get(0), src, 15, 6); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
56 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
57 |
Snippet g = methodKey(assertEval(src, added(VALID))); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
58 |
SnippetWrapper swg = getState().sourceCodeAnalysis().wrapper(g); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
59 |
assertWrapperHas(swg, src, Kind.METHOD, "void", "glib", "println"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
60 |
assertPosition(swg, src, 0, 4); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
61 |
assertPosition(swg, src, 5, 4); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
62 |
assertPosition(swg, src, 15, 6); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
63 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
64 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
65 |
@Test(enabled = false) // TODO 8159740 |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
66 |
public void testMethodCorralled() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
67 |
String src = "void glib() { f(); }"; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
68 |
Snippet g = methodKey(assertEval(src, added(RECOVERABLE_DEFINED))); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
69 |
SnippetWrapper swg = getState().sourceCodeAnalysis().wrapper(g); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
70 |
assertWrapperHas(swg, src, Kind.METHOD, "void", "glib"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
71 |
assertPosition(swg, src, 5, 4); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
72 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
73 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
74 |
public void testMethodBad() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
75 |
String src = "void flob() { ?????; }"; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
76 |
List<SnippetWrapper> swl = getState().sourceCodeAnalysis().wrappers(src); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
77 |
assertEquals(swl.size(), 1, "unexpected list length"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
78 |
assertWrapperHas(swl.get(0), src, Kind.METHOD, "void", "flob", "?????"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
79 |
assertPosition(swl.get(0), src, 9, 2); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
80 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
81 |
Snippet f = key(assertEvalFail(src)); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
82 |
assertEquals(f.kind(), Kind.ERRONEOUS); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
83 |
assertEquals(((ErroneousSnippet)f).probableKind(), Kind.METHOD); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
84 |
SnippetWrapper sw = getState().sourceCodeAnalysis().wrapper(f); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
85 |
assertWrapperHas(sw, src, Kind.METHOD, "void", "flob", "?????"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
86 |
assertPosition(swl.get(0), src, 14, 5); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
87 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
88 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
89 |
public void testVar() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
90 |
String src = "int gx = 1234;"; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
91 |
List<SnippetWrapper> swl = getState().sourceCodeAnalysis().wrappers(src); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
92 |
assertEquals(swl.size(), 1, "unexpected list length"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
93 |
assertWrapperHas(swl.get(0), src, Kind.VAR, "int", "gx", "1234"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
94 |
assertPosition(swl.get(0), src, 4, 2); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
95 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
96 |
Snippet g = varKey(assertEval(src, added(VALID))); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
97 |
SnippetWrapper swg = getState().sourceCodeAnalysis().wrapper(g); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
98 |
assertWrapperHas(swg, src, Kind.VAR, "int", "gx", "1234"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
99 |
assertPosition(swg, src, 0, 3); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
100 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
101 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
102 |
public void testVarBad() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
103 |
String src = "double dd = ?????;"; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
104 |
List<SnippetWrapper> swl = getState().sourceCodeAnalysis().wrappers(src); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
105 |
assertEquals(swl.size(), 1, "unexpected list length"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
106 |
assertWrapperHas(swl.get(0), src, Kind.VAR, "double", "dd", "?????"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
107 |
assertPosition(swl.get(0), src, 9, 2); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
108 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
109 |
Snippet f = key(assertEvalFail(src)); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
110 |
assertEquals(f.kind(), Kind.ERRONEOUS); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
111 |
assertEquals(((ErroneousSnippet)f).probableKind(), Kind.VAR); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
112 |
SnippetWrapper sw = getState().sourceCodeAnalysis().wrapper(f); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
113 |
assertWrapperHas(sw, src, Kind.VAR, "double", "dd", "?????"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
114 |
assertPosition(swl.get(0), src, 12, 5); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
115 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
116 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
117 |
public void testImport() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
118 |
String src = "import java.lang.*;"; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
119 |
List<SnippetWrapper> swl = getState().sourceCodeAnalysis().wrappers(src); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
120 |
assertEquals(swl.size(), 1, "unexpected list length"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
121 |
assertWrapperHas(swl.get(0), src, Kind.IMPORT, "import", "java.lang"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
122 |
assertPosition(swl.get(0), src, 7, 4); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
123 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
124 |
Snippet g = key(assertEval(src, added(VALID))); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
125 |
SnippetWrapper swg = getState().sourceCodeAnalysis().wrapper(g); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
126 |
assertWrapperHas(swg, src, Kind.IMPORT, "import", "java.lang"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
127 |
assertPosition(swg, src, 0, 6); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
128 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
129 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
130 |
public void testImportBad() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
131 |
String src = "import java.?????;"; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
132 |
List<SnippetWrapper> swl = getState().sourceCodeAnalysis().wrappers(src); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
133 |
assertEquals(swl.size(), 1, "unexpected list length"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
134 |
assertWrapperHas(swl.get(0), src, Kind.IMPORT, "import", "?????"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
135 |
assertPosition(swl.get(0), src, 7, 4); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
136 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
137 |
Snippet f = key(assertEvalFail(src)); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
138 |
assertEquals(f.kind(), Kind.ERRONEOUS); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
139 |
assertEquals(((ErroneousSnippet)f).probableKind(), Kind.IMPORT); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
140 |
SnippetWrapper sw = getState().sourceCodeAnalysis().wrapper(f); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
141 |
assertWrapperHas(sw, src, Kind.IMPORT, "import", "?????"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
142 |
assertPosition(swl.get(0), src, 0, 6); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
143 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
144 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
145 |
public void testErroneous() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
146 |
String src = "@@@@@@@@@@"; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
147 |
List<SnippetWrapper> swl = getState().sourceCodeAnalysis().wrappers(src); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
148 |
assertEquals(swl.size(), 1, "unexpected list length"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
149 |
assertWrapperHas(swl.get(0), src, Kind.ERRONEOUS, "@@@@@@@@@@"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
150 |
assertPosition(swl.get(0), src, 0, 10); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
151 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
152 |
Snippet f = key(assertEvalFail(src)); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
153 |
assertEquals(f.kind(), Kind.ERRONEOUS); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
154 |
assertEquals(((ErroneousSnippet)f).probableKind(), Kind.ERRONEOUS); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
155 |
SnippetWrapper sw = getState().sourceCodeAnalysis().wrapper(f); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
156 |
assertWrapperHas(sw, src, Kind.ERRONEOUS, "@@@@@@@@@@"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
157 |
assertPosition(swl.get(0), src, 0, 10); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
158 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
159 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
160 |
public void testEmpty() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
161 |
String src = ""; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
162 |
List<SnippetWrapper> swl = getState().sourceCodeAnalysis().wrappers(src); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
163 |
assertEquals(swl.size(), 0, "expected empty list"); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
164 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
165 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
166 |
public void testDependencies() { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
167 |
Snippet a = key(assertEval("int aaa = 6;", added(VALID))); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
168 |
Snippet b = key(assertEval("class B { B(int x) { aaa = x; } }", added(VALID))); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
169 |
Snippet c = key(assertEval("B ccc() { return new B(aaa); }", added(VALID))); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
170 |
Collection<Snippet> dep; |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
171 |
dep = getState().sourceCodeAnalysis().dependents(c); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
172 |
assertEquals(dep.size(), 0); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
173 |
dep = getState().sourceCodeAnalysis().dependents(b); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
174 |
assertEquals(dep.size(), 1); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
175 |
assertTrue(dep.contains(c)); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
176 |
dep = getState().sourceCodeAnalysis().dependents(a); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
177 |
assertEquals(dep.size(), 2); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
178 |
assertTrue(dep.contains(c)); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
179 |
assertTrue(dep.contains(b)); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
180 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
181 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
182 |
private void assertWrapperHas(SnippetWrapper sw, String source, Kind kind, String... has) { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
183 |
assertEquals(sw.source(), source); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
184 |
assertEquals(sw.kind(), kind); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
185 |
if (kind == Kind.IMPORT) { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
186 |
assertTrue(sw.wrapped().contains("import")); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
187 |
} else { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
188 |
String cn = sw.fullClassName(); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
189 |
int idx = cn.lastIndexOf("."); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
190 |
assertTrue(sw.wrapped().contains(cn.substring(idx+1))); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
191 |
assertTrue(sw.wrapped().contains("class")); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
192 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
193 |
for (String s : has) { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
194 |
assertTrue(sw.wrapped().contains(s)); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
195 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
196 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
197 |
|
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
198 |
private void assertPosition(SnippetWrapper sw, String source, int start, int length) { |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
199 |
int wpg = sw.sourceToWrappedPosition(start); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
200 |
assertEquals(sw.wrapped().substring(wpg, wpg+length), |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
201 |
source.substring(start, start+length), |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
202 |
"position " + wpg + " in " + sw.wrapped()); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
203 |
assertEquals(sw.wrappedToSourcePosition(wpg), start); |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
204 |
} |
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
diff
changeset
|
205 |
} |