c5d5bf4ab833a380866849114fc3876256cc3bdc
[idea/community.git] / plugins / groovy / test / org / jetbrains / plugins / groovy / lang / completion / CompletionTestBase.java
1 package org.jetbrains.plugins.groovy.lang.completion;
2
3 import com.intellij.codeInsight.CodeInsightSettings;
4 import com.intellij.codeInsight.lookup.LookupElement;
5 import com.intellij.codeInsight.lookup.LookupManager;
6 import com.intellij.codeInsight.lookup.impl.LookupImpl;
7 import com.intellij.openapi.fileTypes.FileType;
8 import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
9 import org.jetbrains.plugins.groovy.CompositeCompletionData;
10 import org.jetbrains.plugins.groovy.GroovyFileType;
11 import org.jetbrains.plugins.groovy.util.TestUtils;
12
13 import java.util.Collections;
14 import java.util.Comparator;
15 import java.util.List;
16
17 /**
18  * author ven
19  */
20 public abstract class CompletionTestBase extends JavaCodeInsightFixtureTestCase {
21
22   protected void doTest() throws Throwable {
23     final List<String> stringList = TestUtils.readInput(getTestDataPath() + "/" + getTestName(true) + ".test");
24     final String fileName = getTestName(true) + "." + getExtension();
25     myFixture.addFileToProject(fileName, stringList.get(0));
26     myFixture.configureByFile(fileName);
27
28     CompositeCompletionData.restrictCompletion(addReferenceVariants(), addKeywords());
29
30     boolean old = CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX;
31     CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX = false;
32     CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = false;
33
34
35     String result = "";
36     try {
37       myFixture.completeBasic();
38
39       final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(myFixture.getEditor());
40       if (lookup != null) {
41         List<LookupElement> items = lookup.getItems();
42         Collections.sort(items, new Comparator<LookupElement>() {
43           public int compare(LookupElement o1, LookupElement o2) {
44             return o1.getLookupString().compareTo(o2.getLookupString());
45           }
46         });
47         result = "";
48         for (LookupElement item : items) {
49           result = result + "\n" + item.getLookupString();
50         }
51         result = result.trim();
52         LookupManager.getInstance(myFixture.getProject()).hideActiveLookup();
53       }
54
55     }
56     finally {
57       CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = true;
58       CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX = old;
59       CompositeCompletionData.restrictCompletion(true, true);
60     }
61     assertEquals(stringList.get(1), result);
62   }
63
64   protected String getExtension() {
65     return "groovy";
66   }
67
68   protected FileType getFileType() {
69     return GroovyFileType.GROOVY_FILE_TYPE;
70   }
71
72   protected boolean addKeywords() {
73     return true;
74   }
75
76   protected boolean addReferenceVariants() {
77     return true;
78   }
79 }