|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.tests; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 4 | +import com.intellij.codeInsight.lookup.LookupElementPresentation; |
| 5 | +import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler; |
| 6 | +import com.intellij.openapi.extensions.Extensions; |
| 7 | +import com.intellij.openapi.fileTypes.LanguageFileType; |
| 8 | +import com.intellij.psi.PsiElement; |
| 9 | +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; |
| 10 | +import com.jetbrains.php.lang.psi.elements.Function; |
| 11 | +import com.jetbrains.php.lang.psi.elements.Method; |
| 12 | +import com.jetbrains.twig.TwigFileType; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.Settings; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +public abstract class SymfonyLightCodeInsightFixtureTestCase extends LightCodeInsightFixtureTestCase { |
| 20 | + |
| 21 | + @Override |
| 22 | + public void setUp() throws Exception { |
| 23 | + super.setUp(); |
| 24 | + Settings.getInstance(myFixture.getProject()).pluginEnabled = true; |
| 25 | + } |
| 26 | + |
| 27 | + public void assertCompletionContains(LanguageFileType languageFileType, String configureByText, String... lookupStrings) { |
| 28 | + |
| 29 | + myFixture.configureByText(languageFileType, configureByText); |
| 30 | + myFixture.completeBasic(); |
| 31 | + |
| 32 | + assertTrue(myFixture.getLookupElementStrings().containsAll(Arrays.asList(lookupStrings))); |
| 33 | + } |
| 34 | + |
| 35 | + public void assertCompletionContains(String filename, String configureByText, String... lookupStrings) { |
| 36 | + |
| 37 | + myFixture.configureByText(filename, configureByText); |
| 38 | + myFixture.completeBasic(); |
| 39 | + |
| 40 | + assertTrue(myFixture.getLookupElementStrings().containsAll(Arrays.asList(lookupStrings))); |
| 41 | + } |
| 42 | + |
| 43 | + public void assertNavigationContains(LanguageFileType languageFileType, String configureByText, String targetShortcut) { |
| 44 | + myFixture.configureByText(languageFileType, configureByText); |
| 45 | + PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); |
| 46 | + assertNavigationContains(psiElement, targetShortcut); |
| 47 | + } |
| 48 | + |
| 49 | + public void assertNavigationContains(PsiElement psiElement, String targetShortcut) { |
| 50 | + |
| 51 | + if(!targetShortcut.startsWith("\\")) { |
| 52 | + targetShortcut = "\\" + targetShortcut; |
| 53 | + } |
| 54 | + |
| 55 | + Set<String> classTargets = new HashSet<String>(); |
| 56 | + |
| 57 | + for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) { |
| 58 | + PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor()); |
| 59 | + if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) { |
| 60 | + |
| 61 | + for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) { |
| 62 | + if(gotoDeclarationTarget instanceof Method) { |
| 63 | + |
| 64 | + String meName = ((Method) gotoDeclarationTarget).getName(); |
| 65 | + |
| 66 | + String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN(); |
| 67 | + if(!clName.startsWith("\\")) { |
| 68 | + clName = "\\" + clName; |
| 69 | + } |
| 70 | + |
| 71 | + classTargets.add(clName + "::" + meName); |
| 72 | + } else if(gotoDeclarationTarget instanceof Function) { |
| 73 | + classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if(!classTargets.contains(targetShortcut)) { |
| 81 | + fail(String.format("failed that PsiElement (%s) navigate to %s", psiElement.toString(), targetShortcut)); |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + public void assertCompletionLookupTailIsEqual(LanguageFileType languageFileType, String configureByText, String lookupString, String tailText) { |
| 87 | + |
| 88 | + myFixture.configureByText(languageFileType, configureByText); |
| 89 | + myFixture.completeBasic(); |
| 90 | + |
| 91 | + for (LookupElement lookupElement : myFixture.getLookupElements()) { |
| 92 | + |
| 93 | + if(!lookupElement.getLookupString().equals(lookupString)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + LookupElementPresentation presentation = new LookupElementPresentation(); |
| 98 | + lookupElement.renderElement(presentation); |
| 99 | + |
| 100 | + if(presentation.getTailText() == null) { |
| 101 | + fail(String.format("failed to check '%s'", lookupString)); |
| 102 | + } |
| 103 | + |
| 104 | + if(!presentation.getTailText().equals(tailText)) { |
| 105 | + fail(String.format("failed that on '%s' '%s' is equal '%s'", lookupString, tailText, presentation.getTailText())); |
| 106 | + } |
| 107 | + |
| 108 | + return; |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + fail(String.format("failed to check '%s' because it's unknown", lookupString)); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments