-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathCodeActionTest.java
More file actions
132 lines (110 loc) · 4.34 KB
/
CodeActionTest.java
File metadata and controls
132 lines (110 loc) · 4.34 KB
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
package org.javacs;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.javacs.lsp.*;
import org.junit.Before;
import org.junit.Test;
public class CodeActionTest {
private static final List<Diagnostic> errors = new ArrayList<>();
private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer(errors::add);
@Before
public void clearErrors() {
errors.clear();
}
@Test
public void testCantConvertToStatement() {
assertThat(quickFix("org/javacs/action/TestCantConvertToStatement.java"), empty());
}
@Test
public void testConvertToStatement() {
assertThat(quickFix("org/javacs/action/TestConvertToStatement.java"), contains("Convert to statement"));
}
@Test
public void testConvertToBlock() {
assertThat(quickFix("org/javacs/action/TestConvertToBlock.java"), contains("Convert to block"));
}
@Test
public void testRemoveDeclaration() {
assertThat(quickFix("org/javacs/action/TestRemoveDeclaration.java"), contains("Remove method"));
}
@Test
public void testUnusedException() {
assertThat(quickFix("org/javacs/action/TestUnusedException.java"), empty());
}
@Test
public void testSuppressWarning() {
assertThat(quickFix("org/javacs/action/TestSuppressWarning.java"), contains("Suppress 'unchecked' warning"));
}
@Test
public void testAddThrows() {
assertThat(quickFix("org/javacs/action/TestAddThrows.java"), contains("Add 'throws'"));
}
@Test
public void testAddImport() {
String[] expect = {
"Import 'java.util.List'", "Import 'com.google.gson.Gson'", "Import 'com.sun.source.util.TreePathScanner'"
};
assertThat(quickFix("org/javacs/action/TestAddImport.java"), hasItems(expect));
}
@Test
public void testAddImportInAnonymousClass() {
String[] expect = {"Import 'java.util.List'"};
assertThat(quickFix("org/javacs/action/TestAddImportAnonymousClass.java"), hasItems(expect));
}
@Test
public void testRemoveNotThrown() {
assertThat(quickFix("org/javacs/action/TestRemoveNotThrown.java"), contains("Remove 'IOException'"));
}
@Test
public void testGenerateConstructor() {
assertThat(quickFix("org/javacs/action/TestGenerateConstructor.java"), contains("Generate constructor"));
}
@Test
public void testDontGenerateConstructor() {
assertThat(
quickFix("org/javacs/action/TestDontGenerateConstructor.java"), not(hasItem("Generate constructor")));
}
@Test
public void testImplementAbstractMethods() {
assertThat(
quickFix("org/javacs/action/TestImplementAbstractMethods.java"), hasItem("Implement abstract methods"));
}
@Test
public void testOverrideInheritedMethod() {
assertThat(
forCursor("org/javacs/action/TestOverrideInheritedMethod.java", 6, 1),
hasItem("Override 'andThen' from java.util.function.Function"));
}
@Test
public void testCreateMissingMethod() {
assertThat(quickFix("org/javacs/action/TestCreateMissingMethod.java"), hasItem("Create missing method"));
}
private List<String> quickFix(String testFile) {
var file = FindResource.path(testFile);
server.lint(List.of(file));
var params = new CodeActionParams();
params.textDocument = new TextDocumentIdentifier(file.toUri());
params.context.diagnostics = errors;
var actions = server.codeAction(params);
return extractTitles(actions);
}
private List<String> forCursor(String testFile, int line, int column) {
var file = FindResource.path(testFile);
var cursor = new Position(line - 1, column - 1);
server.lint(List.of(file));
var params = new CodeActionParams();
params.textDocument = new TextDocumentIdentifier(file.toUri());
params.range = new Range(cursor, cursor);
var actions = server.codeAction(params);
return extractTitles(actions);
}
private List<String> extractTitles(List<CodeAction> actions) {
var quickFix = new ArrayList<String>();
for (var a : actions) {
quickFix.add(a.title);
}
return quickFix;
}
}