CompletionStyleTest -> community
[fedora-idea.git] / java / java-tests / testSrc / com / intellij / codeInsight / completion / CompletionStyleTest.java
blobbfff24fee7d2a1c27e189df6bdad54c3efbcbd17
1 package com.intellij.codeInsight.completion;
3 import com.intellij.JavaTestUtil;
4 import com.intellij.codeInsight.lookup.Lookup;
5 import com.intellij.codeInsight.lookup.LookupElement;
6 import com.intellij.codeInsight.lookup.LookupManager;
7 import com.intellij.codeInsight.lookup.impl.TestLookupManager;
8 import com.intellij.psi.codeStyle.CodeStyleSettings;
9 import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
10 import com.intellij.psi.statistics.StatisticsManager;
11 import com.intellij.psi.statistics.impl.StatisticsManagerImpl;
12 import com.intellij.testFramework.LightCodeInsightTestCase;
14 /**
15 * Created by IntelliJ IDEA.
16 * User: ik
17 * Date: 20.08.2003
18 * Time: 15:44:18
19 * To change this template use Options | File Templates.
21 public class CompletionStyleTest extends LightCodeInsightTestCase{
22 private static final String BASE_PATH = "/codeInsight/completion/style";
24 protected void setUp() throws Exception {
25 super.setUp();
26 ((StatisticsManagerImpl)StatisticsManager.getInstance()).clearStatistics();
29 @Override
30 protected String getTestDataPath() {
31 return JavaTestUtil.getJavaTestDataPath();
34 public void testGenericParametersReplace() throws Exception{
35 final String path = BASE_PATH;
37 configureByFile(path + "/before1.java");
38 performSmartCompletion();
39 select(Lookup.REPLACE_SELECT_CHAR, 2);
40 checkResultByFile(path + "/after1.java");
43 public void testIDEADEV4566() throws Exception {
44 configureFromFileText(
45 "A.java",
46 "public class A {\n" +
47 " public static Object createA(String s) {}\n" +
48 " public static void foo() {\n" +
49 " createA(<caret>);\n" +
50 " }\n" +
51 "}");
52 performSmartCompletion();
53 select(Lookup.NORMAL_SELECT_CHAR, 0);
54 checkResultByText(
55 "public class A {\n" +
56 " public static Object createA(String s) {}\n" +
57 " public static void foo() {\n" +
58 " createA(String.copyValueOf(<caret>));\n" +
59 " }\n" +
60 "}");
63 public void testIDEADEV5935() throws Exception {
64 configureFromFileText("A.java",
65 "public class A {\n" +
66 " public static void foo(String param1, String param2) {\n" +
67 " }\n" +
68 "\n" +
69 " public static void main(String[] args) {\n" +
70 " String param1 = args[0];\n" +
71 " String param2 = args[1];\n" +
72 " foo(<caret>);\n" +
73 " }\n" +
74 "}"
76 performSmartCompletion();
77 select(Lookup.NORMAL_SELECT_CHAR, getSelected());
78 checkResultByText(
79 "public class A {\n" +
80 " public static void foo(String param1, String param2) {\n" +
81 " }\n" +
82 "\n" +
83 " public static void main(String[] args) {\n" +
84 " String param1 = args[0];\n" +
85 " String param2 = args[1];\n" +
86 " foo(param1, <caret>);\n" +
87 " }\n" +
88 "}"
92 public void testIDEADEV2878() throws Exception {
93 configureFromFileText(
94 "A.java",
95 "class Bar<T> {}\n" +
96 "class Foo {\n" +
97 "public void createFoo(Bar<?> <caret>)\n" + "}");
98 performNormalCompletion();
99 checkResultByText(
100 "class Bar<T> {}\n" +
101 "class Foo {\n" +
102 "public void createFoo(Bar<?> bar<caret>)\n" + "}");
105 public void testMethodsParametersStyle1() throws Exception{
106 final String path = BASE_PATH;
108 configureByFile(path + "/before3.java");
109 performSmartCompletion();
110 checkResultByFile(path + "/after3.java");
113 public void testMethodsParametersStyle2() throws Exception{
114 final String path = BASE_PATH;
116 configureByFile(path + "/before4.java");
117 performSmartCompletion();
118 checkResultByFile(path + "/after4.java");
121 public void testKeywordsReplace() throws Exception{
122 final String path = BASE_PATH;
124 configureByFile(path + "/before6.java");
125 performNormalCompletion();
126 select(Lookup.REPLACE_SELECT_CHAR, 3); //equals(), true & false go first
127 checkResultByFile(path + "/after6.java");
130 public void testLocalVariablePreselect() throws Exception{
131 final String path = BASE_PATH;
133 configureByFile(path + "/before5.java");
134 performSmartCompletion();
135 assertEquals("xxxx", getSelected().getLookupString());
138 public void testMethodCompletionInsideInlineTags() throws Exception{
139 final String path = BASE_PATH;
141 configureByFile(path + "/before7.java");
142 performNormalCompletion();
143 select(Lookup.REPLACE_SELECT_CHAR, 1);
144 checkResultByFile(path + "/after7.java");
147 public void testConstantsCompletion1() throws Exception{
148 final String path = BASE_PATH;
150 configureByFile(path + "/before8.java");
151 performSmartCompletion();
152 select(Lookup.REPLACE_SELECT_CHAR, 0);
153 checkResultByFile(path + "/after8.java");
156 public void testConstantsCompletion2() throws Exception{
157 final String path = BASE_PATH;
159 configureByFile(path + "/before9.java");
160 performSmartCompletion();
161 select(Lookup.REPLACE_SELECT_CHAR, 0);
162 checkResultByFile(path + "/after9.java");
165 public void testConstantsCompletion3() throws Exception{
166 final String path = BASE_PATH;
168 configureByFile(path + "/before10.java");
169 performSmartCompletion();
170 select(Lookup.REPLACE_SELECT_CHAR, 0);
171 checkResultByFile(path + "/after10.java");
174 public void testCaretPositionAfterCompletion1() throws Exception{
175 final String path = BASE_PATH;
177 configureByFile(path + "/before11.java");
178 performNormalCompletion();
179 select(Lookup.NORMAL_SELECT_CHAR, 0);
180 checkResultByFile(path + "/after11.java");
183 public void testCaretPositionAfterCompletion2() throws Exception{
184 final String path = BASE_PATH;
186 configureByFile(path + "/before12.java");
187 performSmartCompletion();
188 select(Lookup.NORMAL_SELECT_CHAR, 0);
189 checkResultByFile(path + "/after12.java");
192 public void testParensReuse() throws Exception{
193 final String path = BASE_PATH;
195 configureByFile(path + "/before13.java");
196 performNormalCompletion();
197 checkResultByFile(path + "/after13.java");
200 public void testParensInSynchronized() throws Exception{
201 CodeStyleSettingsManager.getSettings(getProject()).SPACE_BEFORE_SYNCHRONIZED_PARENTHESES = false;
202 CodeStyleSettingsManager.getSettings(getProject()).SPACE_WITHIN_SYNCHRONIZED_PARENTHESES = true;
203 final String path = BASE_PATH;
205 configureByFile(path + "/before22.java");
206 performNormalCompletion();
207 checkResultByFile(path + "/after22.java");
210 public void testMethodReplacementReuseParens1() throws Exception{
211 final String path = BASE_PATH;
213 configureByFile(path + "/before15.java");
214 performNormalCompletion();
215 select(Lookup.REPLACE_SELECT_CHAR, getSelected());
216 checkResultByFile(path + "/after15.java");
219 public void testMethodReplacementReuseParens2() throws Exception{
220 final String path = BASE_PATH;
222 configureByFile(path + "/before16.java");
223 performNormalCompletion();
224 select(Lookup.REPLACE_SELECT_CHAR, getSelected());
225 checkResultByFile(path + "/after16.java");
228 public void testMethodReplacementReuseParens3() throws Exception{
229 final String path = BASE_PATH;
231 configureByFile(path + "/before17.java");
232 performNormalCompletion();
233 select(Lookup.REPLACE_SELECT_CHAR, getSelected());
234 checkResultByFile(path + "/after17.java");
237 public void testCastAfterNew1() throws Exception{
238 final String path = BASE_PATH;
240 configureByFile(path + "/before21.java");
241 performSmartCompletion();
242 checkResultByFile(path + "/after21.java");
245 public void testCastParensStyle1() throws Exception{
246 final String path = BASE_PATH;
247 CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(getProject());
248 final boolean space_within_cast_parentheses = styleSettings.SPACE_WITHIN_CAST_PARENTHESES;
249 final boolean space_after_type_cast = styleSettings.SPACE_AFTER_TYPE_CAST;
251 styleSettings.SPACE_WITHIN_CAST_PARENTHESES = true;
252 styleSettings.SPACE_AFTER_TYPE_CAST = false;
253 configureByFile(path + "/before31.java");
254 performSmartCompletion();
255 checkResultByFile(path + "/after31.java");
256 styleSettings.SPACE_WITHIN_CAST_PARENTHESES = space_within_cast_parentheses;
257 styleSettings.SPACE_AFTER_TYPE_CAST = space_after_type_cast;
260 public void testMethodParensStyle2() throws Exception{
261 final String path = BASE_PATH;
262 CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(getProject());
263 final boolean space_before_method_call_parentheses = styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES;
264 styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES = true;
265 configureByFile(path + "/before32.java");
266 performNormalCompletion();
267 checkResultByFile(path + "/after32.java");
268 styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES = space_before_method_call_parentheses;
271 public void testMethodParensStyle3() throws Exception{
272 final String path = BASE_PATH;
273 CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(getProject());
274 final boolean space_before_method_call_parentheses = styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES;
275 final boolean space_within_method_call_parentheses = styleSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES;
277 styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES = true;
278 styleSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES = true;
279 configureByFile(path + "/before32.java");
280 performNormalCompletion();
281 checkResultByFile(path + "/after32-a.java");
282 styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES = space_before_method_call_parentheses;
283 styleSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES = space_within_method_call_parentheses;
286 /*public void testClassNameCompletion1() throws Exception{
287 final String path = BASE_PATH;
288 configureByFile(path + "/before34.java");
289 performClassNameCompletion();
290 select('<', getSelected());
291 checkResultByFile(path + "/after34.java");
292 } */
294 public void testStaticsCompletion1() throws Exception{
295 final String path = BASE_PATH;
296 configureByFile(path + "/before33.java");
297 performSmartCompletion();
298 select(Lookup.NORMAL_SELECT_CHAR, getSelected());
299 checkResultByFile(path + "/after33.java");
302 public void testStaticsCompletion2() throws Exception{
303 final String path = BASE_PATH;
304 configureByFile(path + "/before39.java");
305 performSmartCompletion();
306 select(Lookup.NORMAL_SELECT_CHAR, getSelected());
307 checkResultByFile(path + "/after39.java");
310 public void testJavaDocLinkCompletion1() throws Exception{
311 final String path = BASE_PATH;
312 configureByFile(path + "/before36.java");
313 performNormalCompletion();
314 select(Lookup.REPLACE_SELECT_CHAR, getSelected());
315 checkResultByFile(path + "/after36.java");
319 public void testGetterNameInInterface() throws Exception{
320 final String path = BASE_PATH;
321 configureByFile(path + "/before38.java");
322 performNormalCompletion();
323 checkResultByFile(path + "/after38.java");
326 private void performSmartCompletion(){
327 new CodeCompletionHandlerBase(CompletionType.SMART).invoke(getProject(), getEditor(), getFile());
330 private void performNormalCompletion(){
331 new CodeCompletionHandlerBase(CompletionType.BASIC).invoke(getProject(), getEditor(), getFile());
334 private void select(char completionChar, int index){
335 ((TestLookupManager)LookupManager.getInstance(getProject())).forceSelection(completionChar, index);
338 private void select(char completionChar, LookupElement item){
339 ((TestLookupManager)LookupManager.getInstance(getProject())).forceSelection(completionChar, item);
342 private LookupElement getSelected(){
343 return LookupManager.getInstance(getProject()).getActiveLookup().getCurrentItem();
346 protected void tearDown() throws Exception {
347 LookupManager.getInstance(getProject()).hideActiveLookup();
348 super.tearDown(); //To change body of overriden methods use Options | File Templates.