33362
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @summary Test SourceCodeAnalysis
|
|
27 |
* @build KullaTesting TestingInputStream
|
|
28 |
* @run testng CompletenessTest
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.Map;
|
|
32 |
import java.util.HashMap;
|
|
33 |
|
|
34 |
import org.testng.annotations.Test;
|
|
35 |
import jdk.jshell.SourceCodeAnalysis.Completeness;
|
|
36 |
|
|
37 |
import static jdk.jshell.SourceCodeAnalysis.Completeness.*;
|
|
38 |
|
|
39 |
@Test
|
|
40 |
public class CompletenessTest extends KullaTesting {
|
|
41 |
|
|
42 |
// Add complete units that end with semicolon to complete_with_semi (without
|
|
43 |
// the semicolon). Both cases will be tested.
|
|
44 |
static final String[] complete = new String[] {
|
|
45 |
"{ x= 4; }",
|
|
46 |
"int mm(int x) {kll}",
|
|
47 |
"if (t) { ddd; }",
|
|
48 |
"for (int i = 0; i < lines.length(); ++i) { foo }",
|
|
49 |
"while (ct == null) { switch (current.kind) { case EOF: { } } }",
|
|
50 |
"if (match.kind == BRACES && (prevCT.kind == ARROW || prevCT.kind == NEW_MIDDLE)) { new CT(UNMATCHED, current, \"Unmatched \" + unmatched); }",
|
|
51 |
"enum TK { EOF(TokenKind.EOF, 0), NEW_MIDDLE(XEXPR1|XTERM); }",
|
|
52 |
"List<T> f() { return null; }",
|
|
53 |
"List<?> f() { return null; }",
|
|
54 |
"List<? extends Object> f() { return null; }",
|
|
55 |
"Map<? extends Object, ? super Object> f() { return null; }",
|
|
56 |
"class C { int z; }",
|
|
57 |
"synchronized (r) { f(); }",
|
|
58 |
"try { } catch (Exception ex) { }",
|
|
59 |
"try { } catch (Exception ex) { } finally { }",
|
|
60 |
"try { } finally { }",
|
|
61 |
"try (java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName)) { }",
|
|
62 |
"foo: while (true) { printf(\"Innn\"); break foo; }",
|
|
63 |
";",
|
|
64 |
};
|
|
65 |
|
|
66 |
static final String[] expression = new String[] {
|
|
67 |
"test",
|
|
68 |
"x + y",
|
|
69 |
"x + y ++",
|
|
70 |
"p = 9",
|
|
71 |
"match(BRACKETS, TokenKind.LBRACKET)",
|
|
72 |
"new C()",
|
|
73 |
"new C() { public String toString() { return \"Hi\"; } }",
|
|
74 |
"new int[]",
|
|
75 |
"new int[] {1, 2,3}",
|
|
76 |
"new Foo() {}",
|
|
77 |
"i >= 0 && Character.isWhitespace(s.charAt(i))",
|
|
78 |
};
|
|
79 |
|
|
80 |
static final String[] complete_with_semi = new String[] {
|
|
81 |
"int mm",
|
|
82 |
"if (t) ddd",
|
|
83 |
"int p = 9",
|
|
84 |
"int p",
|
|
85 |
"Deque<Token> stack = new ArrayDeque<>()",
|
|
86 |
"final Deque<Token> stack = new ArrayDeque<>()",
|
|
87 |
"java.util.Scanner input = new java.util.Scanner(System.in)",
|
|
88 |
"java.util.Scanner input = new java.util.Scanner(System.in) { }",
|
|
89 |
"int j = -i",
|
|
90 |
"String[] a = { \"AAA\" }",
|
|
91 |
"assert true",
|
|
92 |
"int path[]",
|
|
93 |
"int path[][]",
|
|
94 |
"int path[][] = new int[22][]",
|
|
95 |
"int path[] = new int[22]",
|
|
96 |
"int path[] = new int[] {1, 2, 3}",
|
|
97 |
"int[] path",
|
|
98 |
"int path[] = new int[22]",
|
|
99 |
"int path[][] = new int[22][]",
|
|
100 |
"for (Object o : a) System.out.println(\"Yep\")",
|
|
101 |
"while (os == null) System.out.println(\"Yep\")",
|
|
102 |
"do f(); while (t)",
|
|
103 |
"if (os == null) System.out.println(\"Yep\")",
|
|
104 |
"if (t) if (!t) System.out.println(123)",
|
|
105 |
"for (int i = 0; i < 10; ++i) if (i < 5) System.out.println(i); else break",
|
|
106 |
"for (int i = 0; i < 10; ++i) if (i < 5) System.out.println(i); else continue",
|
|
107 |
"for (int i = 0; i < 10; ++i) if (i < 5) System.out.println(i); else return",
|
|
108 |
"throw ex",
|
|
109 |
"C c = new C()",
|
|
110 |
"java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName)",
|
|
111 |
"BufferedReader br = new BufferedReader(new FileReader(path))",
|
|
112 |
"bar: g()",
|
|
113 |
"baz: while (true) if (t()) printf('-'); else break baz",
|
|
114 |
};
|
|
115 |
|
|
116 |
static final String[] considered_incomplete = new String[] {
|
|
117 |
"if (t)",
|
|
118 |
"if (t) { } else",
|
|
119 |
"if (t) if (!t)",
|
|
120 |
"if (match.kind == BRACES && (prevCT.kind == ARROW || prevCT.kind == NEW_MIDDLE))",
|
|
121 |
"for (int i = 0; i < 10; ++i)",
|
|
122 |
"while (os == null)",
|
|
123 |
};
|
|
124 |
|
|
125 |
static final String[] definitely_incomplete = new String[] {
|
|
126 |
"int mm(",
|
|
127 |
"int mm(int x",
|
|
128 |
"int mm(int x)",
|
|
129 |
"int mm(int x) {",
|
|
130 |
"int mm(int x) {kll",
|
|
131 |
"if",
|
|
132 |
"if (",
|
|
133 |
"if (t",
|
|
134 |
"if (t) {",
|
|
135 |
"if (t) { ddd",
|
|
136 |
"if (t) { ddd;",
|
|
137 |
"if (t) if (",
|
|
138 |
"if (stack.isEmpty()) {",
|
|
139 |
"if (match.kind == BRACES && (prevCT.kind == ARROW || prevCT.kind == NEW_MIDDLE)) {",
|
|
140 |
"if (match.kind == BRACES && (prevCT.kind == ARROW || prevCT.kind == NEW_MIDDLE)) { new CT(UNMATCHED, current, \"Unmatched \" + unmatched);",
|
|
141 |
"x +",
|
|
142 |
"int",
|
|
143 |
"for (int i = 0; i < lines.length(); ++i) {",
|
|
144 |
"new",
|
|
145 |
"new C(",
|
|
146 |
"new int[",
|
|
147 |
"new int[] {1, 2,3",
|
|
148 |
"new int[] {",
|
|
149 |
"while (ct == null) {",
|
|
150 |
"while (ct == null) { switch (current.kind) {",
|
|
151 |
"while (ct == null) { switch (current.kind) { case EOF: {",
|
|
152 |
"while (ct == null) { switch (current.kind) { case EOF: { } }",
|
|
153 |
"enum TK {",
|
|
154 |
"enum TK { EOF(TokenKind.EOF, 0),",
|
|
155 |
"enum TK { EOF(TokenKind.EOF, 0), NEW_MIDDLE(XEXPR1|XTERM)",
|
|
156 |
"enum TK { EOF(TokenKind.EOF, 0), NEW_MIDDLE(XEXPR1|XTERM); ",
|
|
157 |
};
|
|
158 |
|
|
159 |
static final String[] unknown = new String[] {
|
|
160 |
"new ;"
|
|
161 |
};
|
|
162 |
|
|
163 |
static final Map<Completeness, String[]> statusToCases = new HashMap<>();
|
|
164 |
static {
|
|
165 |
statusToCases.put(COMPLETE, complete);
|
|
166 |
statusToCases.put(COMPLETE_WITH_SEMI, complete_with_semi);
|
|
167 |
statusToCases.put(CONSIDERED_INCOMPLETE, considered_incomplete);
|
|
168 |
statusToCases.put(DEFINITELY_INCOMPLETE, definitely_incomplete);
|
|
169 |
}
|
|
170 |
|
|
171 |
private void assertStatus(String input, Completeness status, String source) {
|
|
172 |
String augSrc;
|
|
173 |
switch (status) {
|
|
174 |
case COMPLETE_WITH_SEMI:
|
|
175 |
augSrc = source + ";";
|
|
176 |
break;
|
|
177 |
|
|
178 |
case DEFINITELY_INCOMPLETE:
|
|
179 |
case CONSIDERED_INCOMPLETE:
|
|
180 |
augSrc = null;
|
|
181 |
break;
|
|
182 |
|
|
183 |
case EMPTY:
|
|
184 |
case COMPLETE:
|
|
185 |
case UNKNOWN:
|
|
186 |
augSrc = source;
|
|
187 |
break;
|
|
188 |
|
|
189 |
default:
|
|
190 |
throw new AssertionError();
|
|
191 |
}
|
|
192 |
assertAnalyze(input, status, augSrc);
|
|
193 |
}
|
|
194 |
|
|
195 |
private void assertStatus(String[] ins, Completeness status) {
|
|
196 |
for (String input : ins) {
|
|
197 |
assertStatus(input, status, input);
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
public void test_complete() {
|
|
202 |
assertStatus(complete, COMPLETE);
|
|
203 |
}
|
|
204 |
|
|
205 |
public void test_expression() {
|
|
206 |
assertStatus(expression, COMPLETE);
|
|
207 |
}
|
|
208 |
|
|
209 |
public void test_complete_with_semi() {
|
|
210 |
assertStatus(complete_with_semi, COMPLETE_WITH_SEMI);
|
|
211 |
}
|
|
212 |
|
|
213 |
public void test_considered_incomplete() {
|
|
214 |
assertStatus(considered_incomplete, CONSIDERED_INCOMPLETE);
|
|
215 |
}
|
|
216 |
|
|
217 |
public void test_definitely_incomplete() {
|
|
218 |
assertStatus(definitely_incomplete, DEFINITELY_INCOMPLETE);
|
|
219 |
}
|
|
220 |
|
|
221 |
public void test_unknown() {
|
|
222 |
assertStatus(definitely_incomplete, DEFINITELY_INCOMPLETE);
|
|
223 |
}
|
|
224 |
|
|
225 |
public void testCompleted_complete_with_semi() {
|
|
226 |
for (String in : complete_with_semi) {
|
|
227 |
String input = in + ";";
|
|
228 |
assertStatus(input, COMPLETE, input);
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
public void testCompleted_expression_with_semi() {
|
|
233 |
for (String in : expression) {
|
|
234 |
String input = in + ";";
|
|
235 |
assertStatus(input, COMPLETE, input);
|
|
236 |
}
|
|
237 |
}
|
|
238 |
|
|
239 |
public void testCompleted_considered_incomplete() {
|
|
240 |
for (String in : considered_incomplete) {
|
|
241 |
String input = in + ";";
|
|
242 |
assertStatus(input, COMPLETE, input);
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
private void assertSourceByStatus(String first) {
|
|
247 |
for (Map.Entry<Completeness, String[]> e : statusToCases.entrySet()) {
|
|
248 |
for (String in : e.getValue()) {
|
|
249 |
String input = first + in;
|
|
250 |
assertAnalyze(input, COMPLETE, first, in, true);
|
|
251 |
}
|
|
252 |
}
|
|
253 |
}
|
|
254 |
|
|
255 |
public void testCompleteSource_complete() {
|
|
256 |
for (String input : complete) {
|
|
257 |
assertSourceByStatus(input);
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
public void testCompleteSource_complete_with_semi() {
|
|
262 |
for (String in : complete_with_semi) {
|
|
263 |
String input = in + ";";
|
|
264 |
assertSourceByStatus(input);
|
|
265 |
}
|
|
266 |
}
|
|
267 |
|
|
268 |
public void testCompleteSource_expression() {
|
|
269 |
for (String in : expression) {
|
|
270 |
String input = in + ";";
|
|
271 |
assertSourceByStatus(input);
|
|
272 |
}
|
|
273 |
}
|
|
274 |
|
|
275 |
public void testCompleteSource_considered_incomplete() {
|
|
276 |
for (String in : considered_incomplete) {
|
|
277 |
String input = in + ";";
|
|
278 |
assertSourceByStatus(input);
|
|
279 |
}
|
|
280 |
}
|
|
281 |
|
|
282 |
public void testTrailingSlash() {
|
|
283 |
assertStatus("\"abc\\", UNKNOWN, "\"abc\\");
|
|
284 |
}
|
|
285 |
|
|
286 |
public void testMiscSource() {
|
|
287 |
assertStatus("if (t) if ", DEFINITELY_INCOMPLETE, "if (t) if"); //Bug
|
|
288 |
assertStatus("int m() {} dfd", COMPLETE, "int m() {}");
|
|
289 |
assertStatus("int p = ", DEFINITELY_INCOMPLETE, "int p ="); //Bug
|
|
290 |
}
|
|
291 |
}
|