1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
package org.javacs;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class JavaCompilerServiceTest {
static {
Main.setRootFormat();
}
private JavaCompilerService compiler = new JavaCompilerService(Collections.emptySet(), Collections.emptySet());
static Path simpleProjectSrc() {
return Paths.get("src/test/test-project/simple").normalize();
}
@Before
public void setWorkspaceRoot() {
FileStore.setWorkspaceRoots(Set.of(simpleProjectSrc()));
}
static String contents(String resourceFile) {
var root = JavaCompilerServiceTest.simpleProjectSrc();
var file = root.resolve(resourceFile);
List<String> lines;
try {
lines = Files.readAllLines(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
var join = new StringJoiner("\n");
for (var l : lines) join.add(l);
return join.toString();
}
static URI resourceUri(String resourceFile) {
var root = JavaCompilerServiceTest.simpleProjectSrc();
var file = root.resolve(resourceFile);
return file.toUri();
}
@Test
public void element() {
var uri = resourceUri("HelloWorld.java");
var found = compiler.compileFocus(uri, 3, 24).element();
assertThat(found.getSimpleName(), hasToString(containsString("println")));
}
@Test
public void elementWithError() {
var uri = resourceUri("CompleteMembers.java");
var found = compiler.compileFocus(uri, 3, 12).element();
assertThat(found, notNullValue());
}
private List<String> completionNames(List<Completion> found) {
var result = new ArrayList<String>();
for (var c : found) {
if (c.element != null) result.add(c.element.getSimpleName().toString());
else if (c.packagePart != null) result.add(c.packagePart.name);
else if (c.keyword != null) result.add(c.keyword);
else if (c.className != null) result.add(Parser.lastName(c.className.name));
else if (c.snippet != null) result.add(c.snippet.snippet);
}
return result;
}
private List<String> elementNames(List<Element> found) {
return found.stream().map(e -> e.getSimpleName().toString()).collect(Collectors.toList());
}
@Test
public void identifiers() {
var uri = resourceUri("CompleteIdentifiers.java");
var focus = compiler.compileFocus(uri, 13, 21);
var found = focus.scopeMembers("complete");
var names = elementNames(found);
assertThat(names, hasItem("completeLocal"));
assertThat(names, hasItem("completeParam"));
// assertThat(names, hasItem("super"));
// assertThat(names, hasItem("this"));
assertThat(names, hasItem("completeOtherMethod"));
assertThat(names, hasItem("completeInnerField"));
assertThat(names, hasItem("completeOuterField"));
assertThat(names, hasItem("completeOuterStatic"));
// assertThat(names, hasItem("CompleteIdentifiers"));
}
@Test
public void identifiersInMiddle() {
var uri = resourceUri("CompleteInMiddle.java");
var focus = compiler.compileFocus(uri, 13, 21);
var found = focus.scopeMembers("complete");
var names = elementNames(found);
assertThat(names, hasItem("completeLocal"));
assertThat(names, hasItem("completeParam"));
// assertThat(names, hasItem("super"));
// assertThat(names, hasItem("this"));
assertThat(names, hasItem("completeOtherMethod"));
assertThat(names, hasItem("completeInnerField"));
assertThat(names, hasItem("completeOuterField"));
assertThat(names, hasItem("completeOuterStatic"));
// assertThat(names, hasItem("CompleteInMiddle"));
}
@Test
public void completeIdentifiers() {
var uri = resourceUri("CompleteIdentifiers.java");
var ctx = compiler.parseFile(uri).completionContext(13, 21).get();
var focus = compiler.compileFocus(uri, ctx.line, ctx.character);
var found = focus.completeIdentifiers(ctx.inClass, ctx.inMethod, ctx.partialName);
var names = completionNames(found);
assertThat(names, hasItem("completeLocal"));
assertThat(names, hasItem("completeParam"));
// assertThat(names, hasItem("super"));
// assertThat(names, hasItem("this"));
assertThat(names, hasItem("completeOtherMethod"));
assertThat(names, hasItem("completeInnerField"));
assertThat(names, hasItem("completeOuterField"));
assertThat(names, hasItem("completeOuterStatic"));
// assertThat(names, hasItem("CompleteIdentifiers"));
}
@Test
public void members() {
var uri = resourceUri("CompleteMembers.java");
var focus = compiler.compileFocus(uri, 3, 14);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItem("subMethod"));
assertThat(names, hasItem("superMethod"));
assertThat(names, hasItem("equals"));
}
@Test
public void completeMembers() {
var uri = resourceUri("CompleteMembers.java");
var ctx = compiler.parseFile(uri).completionContext(3, 15).get();
var focus = compiler.compileFocus(uri, ctx.line, ctx.character);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItem("subMethod"));
assertThat(names, hasItem("superMethod"));
assertThat(names, hasItem("equals"));
}
@Test
public void completeExpression() {
var uri = resourceUri("CompleteExpression.java");
var ctx = compiler.parseFile(uri).completionContext(3, 37).get();
var focus = compiler.compileFocus(uri, ctx.line, ctx.character);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItem("instanceMethod"));
assertThat(names, not(hasItem("create")));
assertThat(names, hasItem("equals"));
}
@Test
public void completeClass() {
var uri = resourceUri("CompleteClass.java");
var ctx = compiler.parseFile(uri).completionContext(3, 23).get();
var focus = compiler.compileFocus(uri, ctx.line, ctx.character);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItems("staticMethod", "staticField"));
assertThat(names, hasItems("class"));
assertThat(names, not(hasItem("instanceMethod")));
assertThat(names, not(hasItem("instanceField")));
}
@Test
public void completeImports() {
var uri = resourceUri("CompleteImports.java");
var ctx = compiler.parseFile(uri).completionContext(1, 18).get();
var focus = compiler.compileFocus(uri, ctx.line, ctx.character);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItem("List"));
assertThat(names, hasItem("concurrent"));
}
@Test
public void overloads() {
var uri = resourceUri("Overloads.java");
var found = compiler.compileFocus(uri, 3, 15).methodInvocation().get();
var strings = found.overloads.stream().map(Object::toString).collect(Collectors.toList());
assertThat(strings, hasItem(containsString("print(int)")));
assertThat(strings, hasItem(containsString("print(java.lang.String)")));
}
@Test
public void reportErrors() {
var uri = resourceUri("HasError.java");
var files = Collections.singleton(uri);
var diags = compiler.reportErrors(files);
assertThat(diags, not(empty()));
}
private static List<String> errorStrings(List<Diagnostic<? extends JavaFileObject>> diags) {
var strings = new ArrayList<String>();
for (var d : diags) {
var file = Parser.fileName(d.getSource().toUri());
var line = d.getLineNumber();
var msg = d.getMessage(null);
var string = String.format("%s(%d): %s", file, line, msg);
strings.add(string);
}
return strings;
}
// TODO get these back somehow
@Test
@Ignore
public void errorProne() {
var uri = resourceUri("ErrorProne.java");
var files = Collections.singleton(uri);
var diags = compiler.reportErrors(files);
var strings = errorStrings(diags);
assertThat(strings, hasItem(containsString("ErrorProne.java(7): [CollectionIncompatibleType]")));
}
// TODO get these back somehow
@Test
@Ignore
public void unusedVar() {
var uri = resourceUri("UnusedVar.java");
var files = Collections.singleton(uri);
var diags = compiler.reportErrors(files);
var strings = errorStrings(diags);
assertThat(strings, hasItem(containsString("UnusedVar.java(3): [Unused]")));
}
@Test
public void localDoc() {
var uri = resourceUri("LocalMethodDoc.java");
var invocation = compiler.compileFocus(uri, 3, 21).methodInvocation().get();
var method = invocation.activeMethod.get();
var ptr = new Ptr(method);
var file = compiler.docs().find(ptr).get();
var parse = compiler.docs().parse(file);
var path = parse.fuzzyFind(ptr).get();
var doc = parse.doc(path);
assertThat(doc.toString(), containsString("A great method"));
}
@Test
public void fixImports() {
var uri = resourceUri("MissingImport.java");
var qualifiedNames = compiler.compileFile(uri).fixImports();
assertThat(qualifiedNames, hasItem("java.util.List"));
}
@Test
public void matchesPartialName() {
assertTrue(CompileFocus.matchesPartialName("foobar", "foo"));
assertFalse(CompileFocus.matchesPartialName("foo", "foobar"));
}
}
|