-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSymfonyLightCodeInsightFixtureTestCase.java
More file actions
316 lines (238 loc) · 12.5 KB
/
Copy pathSymfonyLightCodeInsightFixtureTestCase.java
File metadata and controls
316 lines (238 loc) · 12.5 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package de.espend.idea.php.toolbox.tests;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
import com.jetbrains.php.lang.psi.elements.Function;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpReference;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public abstract class SymfonyLightCodeInsightFixtureTestCase extends BasePlatformTestCase {
public void assertCompletionContains(LanguageFileType languageFileType, String configureByText, String... lookupStrings) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
checkContainsCompletion(lookupStrings);
}
public void assertAtTextCompletionContains(String findByText, String... lookupStrings) {
final PsiElement element = myFixture.findElementByText(findByText, PsiElement.class);
assert element != null : "No element found by text: " + findByText;
myFixture.getEditor().getCaretModel().moveToOffset(element.getTextOffset() + 1);
myFixture.completeBasic();
checkContainsCompletion(lookupStrings);
}
public void assertCompletionNotContains(String filename, String configureByText, String... lookupStrings) {
myFixture.configureByText(filename, configureByText);
myFixture.completeBasic();
assertCompletionNot(lookupStrings);
}
public void assertCompletionNotContains(LanguageFileType languageFileType, String configureByText, String... lookupStrings) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
assertCompletionNot(lookupStrings);
}
private void assertCompletionNot(String[] lookupStrings) {
List<String> lookupElements = myFixture.getLookupElementStrings();
for (String s : Arrays.asList(lookupStrings)) {
if(lookupElements.contains(s)) {
fail(String.format("failed that completion not contains %s in %s", s, lookupElements.toString()));
}
}
}
public void assertCompletionContains(String filename, String configureByText, String... lookupStrings) {
myFixture.configureByText(filename, configureByText);
myFixture.completeBasic();
completionContainsAssert(lookupStrings);
}
private void completionContainsAssert(String[] lookupStrings) {
List<String> lookupElements = myFixture.getLookupElementStrings();
if(lookupElements == null || lookupElements.size() == 0) {
fail(String.format("failed that completion contains '%s' in empty list", StringUtils.join(lookupStrings, ",")));
}
for (String s : Arrays.asList(lookupStrings)) {
if(!lookupElements.contains(s)) {
fail(String.format("failed that completion contains %s in %s", s, lookupElements.toString()));
}
}
}
public void assertNavigationContains(LanguageFileType languageFileType, String configureByText, String targetShortcut) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
assertNavigationContains(psiElement, targetShortcut);
}
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {
if(!targetShortcut.startsWith("\\")) {
targetShortcut = "\\" + targetShortcut;
}
Set<String> classTargets = new HashSet<>();
for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {
for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
if(gotoDeclarationTarget instanceof Method) {
String meName = ((Method) gotoDeclarationTarget).getName();
String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
if(!clName.startsWith("\\")) {
clName = "\\" + clName;
}
classTargets.add(clName + "::" + meName);
} else if(gotoDeclarationTarget instanceof Function) {
classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
}
}
}
}
if(!classTargets.contains(targetShortcut)) {
fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
}
}
public void assertNavigationMatchWithParent(LanguageFileType languageFileType, String configureByText, IElementType iElementType) {
assertNavigationMatch(languageFileType, configureByText, PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement(iElementType)));
}
public void assertNavigationMatch(LanguageFileType languageFileType, String configureByText, ElementPattern<?> pattern) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
Set<String> targetStrings = new HashSet<>();
for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
if(gotoDeclarationTargets == null || gotoDeclarationTargets.length == 0) {
continue;
}
for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
targetStrings.add(gotoDeclarationTarget.toString());
if(pattern.accepts(gotoDeclarationTarget)) {
return;
}
}
}
fail(String.format("failed that PsiElement (%s) navigate matches one of %s", psiElement.toString(), targetStrings.toString()));
}
public void assertNavigationContainsFile(LanguageFileType languageFileType, String configureByText, String targetShortcut) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
Set<String> targets = new HashSet<>();
for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
if (gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {
for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
if(gotoDeclarationTarget instanceof PsiFile) {
targets.add(((PsiFile) gotoDeclarationTarget).getVirtualFile().getUrl());
}
}
}
}
// its possible to have memory fields,
// so simple check for ending conditions
// temp:///src/interchange.en.xlf
for (String target : targets) {
if(target.endsWith(targetShortcut)) {
return;
}
}
fail(String.format("failed that PsiElement (%s) navigate to file %s", psiElement.toString(), targetShortcut));
}
public void assertCompletionLookupTailEquals(LanguageFileType languageFileType, String configureByText, String lookupString, String tailText) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
for (LookupElement lookupElement : myFixture.getLookupElements()) {
if(!lookupElement.getLookupString().equals(lookupString)) {
continue;
}
LookupElementPresentation presentation = new LookupElementPresentation();
lookupElement.renderElement(presentation);
if(presentation.getTailText() == null) {
fail(String.format("failed to check '%s'", lookupString));
}
if(!presentation.getTailText().equals(tailText)) {
fail(String.format("failed that on '%s' '%s' is equal '%s'", lookupString, tailText, presentation.getTailText()));
}
return;
}
fail(String.format("failed to check '%s' because it's unknown", lookupString));
}
public LookupElementPresentation getCompletionLookupElement(LanguageFileType languageFileType, String configureByText, String lookupString) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
for (LookupElement lookupElement : myFixture.getLookupElements()) {
if(!lookupElement.getLookupString().equals(lookupString)) {
continue;
}
LookupElementPresentation presentation = new LookupElementPresentation();
lookupElement.renderElement(presentation);
return presentation;
}
fail(String.format("failed to to find lookup element '%s'", lookupString));
return null;
}
public void assertPhpReferenceResolveTo(LanguageFileType languageFileType, String configureByText, ElementPattern<?> pattern) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
psiElement = PsiTreeUtil.getParentOfType(psiElement, PhpReference.class);
if (psiElement == null) {
fail("Element is not PhpReference.");
}
PsiElement resolve = ((PhpReference) psiElement).resolve();
if(!pattern.accepts(resolve)) {
fail(String.format("failed pattern matches element of '%s'", resolve == null ? "null" : resolve.toString()));
}
assertTrue(pattern.accepts(resolve));
}
public void assertCompletionResultEquals(String filename, String complete, String result) {
myFixture.configureByText(filename, complete);
myFixture.completeBasic();
myFixture.checkResult(result);
}
public void assertCompletionResultEquals(LanguageFileType languageFileType, String complete, String result) {
myFixture.configureByText(languageFileType, complete);
myFixture.completeBasic();
myFixture.checkResult(result);
}
public void assertCheckHighlighting(String filename, String result) {
myFixture.configureByText(filename, result);
myFixture.checkHighlighting();
}
protected void createDummyFiles(String... files) {
for (String file : files) {
String path = myFixture.getProject().getBaseDir().getPath() + "/" + file;
File f = new File(path);
try {
f.getParentFile().mkdirs();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
fail(String.format("failed to create file '%s'", file));
}
}
}
protected void deleteDummyFiles(String... files) {
for (String file : files) {
String path = myFixture.getProject().getBaseDir().getPath() + "/" + file;
File f = new File(path);
if(!f.exists()) {
continue;
}
try {
f.delete();
} catch (Exception e) {
e.printStackTrace();
fail(String.format("failed to delete file '%s'", file));
}
}
}
private void checkContainsCompletion(String[] lookupStrings) {
completionContainsAssert(lookupStrings);
}
}