fixed PY-4539 Completion: elif keyword shouldn't be available in completion list...
authorEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Tue, 29 Oct 2013 13:01:40 +0000 (17:01 +0400)
committerEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Tue, 29 Oct 2013 13:01:40 +0000 (17:01 +0400)
extracted keyword completion tests

40 files changed:
python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java
python/testData/keywordCompletion/continue.after.py [moved from python/testData/completion/continue.after.py with 100% similarity]
python/testData/keywordCompletion/continue.py [moved from python/testData/completion/continue.py with 100% similarity]
python/testData/keywordCompletion/elif.after.py [moved from python/testData/completion/elif.after.py with 100% similarity]
python/testData/keywordCompletion/elif.py [moved from python/testData/completion/elif.py with 100% similarity]
python/testData/keywordCompletion/elifNotIndented.after.py [moved from python/testData/completion/elifNotIndented.after.py with 100% similarity]
python/testData/keywordCompletion/elifNotIndented.py [moved from python/testData/completion/elifNotIndented.py with 100% similarity]
python/testData/keywordCompletion/else.after.py [moved from python/testData/completion/else.after.py with 100% similarity]
python/testData/keywordCompletion/else.py [moved from python/testData/completion/else.py with 100% similarity]
python/testData/keywordCompletion/elseInCondExpr.after.py [moved from python/testData/completion/elseInCondExpr.after.py with 100% similarity]
python/testData/keywordCompletion/elseInCondExpr.py [moved from python/testData/completion/elseInCondExpr.py with 100% similarity]
python/testData/keywordCompletion/elseInTryNotIndented.after.py [moved from python/testData/completion/elseInTryNotIndented.after.py with 100% similarity]
python/testData/keywordCompletion/elseInTryNotIndented.py [moved from python/testData/completion/elseInTryNotIndented.py with 100% similarity]
python/testData/keywordCompletion/elseNotIndented.after.py [moved from python/testData/completion/elseNotIndented.after.py with 100% similarity]
python/testData/keywordCompletion/elseNotIndented.py [moved from python/testData/completion/elseNotIndented.py with 100% similarity]
python/testData/keywordCompletion/except.after.py [moved from python/testData/completion/except.after.py with 100% similarity]
python/testData/keywordCompletion/except.py [moved from python/testData/completion/except.py with 100% similarity]
python/testData/keywordCompletion/exceptNotIndented.after.py [moved from python/testData/completion/exceptNotIndented.after.py with 100% similarity]
python/testData/keywordCompletion/exceptNotIndented.py [moved from python/testData/completion/exceptNotIndented.py with 100% similarity]
python/testData/keywordCompletion/finallyInExcept.after.py [moved from python/testData/completion/finallyInExcept.after.py with 100% similarity]
python/testData/keywordCompletion/finallyInExcept.py [moved from python/testData/completion/finallyInExcept.py with 100% similarity]
python/testData/keywordCompletion/fromDotImport.after.py [moved from python/testData/completion/fromDotImport.after.py with 100% similarity]
python/testData/keywordCompletion/fromDotImport.py [moved from python/testData/completion/fromDotImport.py with 100% similarity]
python/testData/keywordCompletion/importKeyword.after.py [moved from python/testData/completion/importKeyword.after.py with 100% similarity]
python/testData/keywordCompletion/importKeyword.py [moved from python/testData/completion/importKeyword.py with 100% similarity]
python/testData/keywordCompletion/keywordAfterComment.after.py [moved from python/testData/completion/keywordAfterComment.after.py with 100% similarity]
python/testData/keywordCompletion/keywordAfterComment.py [moved from python/testData/completion/keywordAfterComment.py with 100% similarity]
python/testData/keywordCompletion/lambdaInExpression.after.py [moved from python/testData/completion/lambdaInExpression.after.py with 100% similarity]
python/testData/keywordCompletion/lambdaInExpression.py [moved from python/testData/completion/lambdaInExpression.py with 100% similarity]
python/testData/keywordCompletion/noContinueInFinally.py [moved from python/testData/completion/noContinueInFinally.py with 100% similarity]
python/testData/keywordCompletion/noElifBeforeElse.py [new file with mode: 0644]
python/testData/keywordCompletion/noneInArgList.after.py [moved from python/testData/completion/noneInArgList.after.py with 100% similarity]
python/testData/keywordCompletion/noneInArgList.py [moved from python/testData/completion/noneInArgList.py with 100% similarity]
python/testData/keywordCompletion/nonlocal.after.py [moved from python/testData/completion/nonlocal.after.py with 100% similarity]
python/testData/keywordCompletion/nonlocal.py [moved from python/testData/completion/nonlocal.py with 100% similarity]
python/testData/keywordCompletion/yield.after.py [moved from python/testData/completion/yield.after.py with 100% similarity]
python/testData/keywordCompletion/yield.py [moved from python/testData/completion/yield.py with 100% similarity]
python/testSrc/com/jetbrains/python/PythonCompletionTest.java
python/testSrc/com/jetbrains/python/PythonKeywordCompletionTest.java [new file with mode: 0644]
python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java

index 9ebf493537001bf8de62cc514f3a37d032a9f1ad..bb18d89d230272d93f7a01d25c7dd77f6ed32f63 100644 (file)
@@ -310,7 +310,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor {
   private static final PsiElementPattern.Capture<PsiElement> IN_EXCEPT_BODY =
     psiElement().inside(psiElement(PyStatementList.class).inside(psiElement(PyExceptPart.class)));
 
-  private static final PsiElementPattern.Capture<PsiElement> AFTER_IF = afterStatement(psiElement(PyIfStatement.class));
+  private static final PsiElementPattern.Capture<PsiElement> AFTER_IF = afterStatement(psiElement(PyIfStatement.class).withLastChild(psiElement(PyIfPart.class)));
   private static final PsiElementPattern.Capture<PsiElement> AFTER_TRY = afterStatement(psiElement(PyTryExceptStatement.class));
 
   private static final PsiElementPattern.Capture<PsiElement> AFTER_LOOP_NO_ELSE =
@@ -355,14 +355,11 @@ public class PyKeywordCompletionContributor extends CompletionContributor {
     }
   }
 
-  private static void putKeyword(
-    @NotNull @NonNls String keyword,
-    InsertHandler<PythonLookupElement> handler,
-    TailType tail,
-    CompletionResultSet result) {
-    final PythonLookupElement lookup_elt = new PythonLookupElement(keyword, true, null);
-    lookup_elt.setHandler(handler);
-    result.addElement(TailTypeDecorator.withTail(lookup_elt, tail));
+  private static void putKeyword(@NotNull @NonNls String keyword, InsertHandler<PythonLookupElement> handler, TailType tail,
+                                 CompletionResultSet result) {
+    final PythonLookupElement lookupElement = new PythonLookupElement(keyword, true, null);
+    lookupElement.setHandler(handler);
+    result.addElement(TailTypeDecorator.withTail(lookupElement, tail));
   }
 
   private void addPreColonStatements() {
@@ -472,8 +469,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor {
       CompletionType.BASIC, psiElement()
       .withLanguage(PythonLanguage.getInstance())
       .and(FIRST_ON_LINE)
-      .andOr(IN_IF_BODY, AFTER_IF)  // NOTE: does allow 'elif' after 'else', may be useful for easier reordering of branches
-        //.andNot(RIGHT_AFTER_COLON)
+      .andOr(IN_IF_BODY, AFTER_IF)
       .andNot(AFTER_QUALIFIER).andNot(IN_STRING_LITERAL)
       ,
       new PyKeywordCompletionProvider(TailType.NONE, UnindentingInsertHandler.INSTANCE, "elif"));
diff --git a/python/testData/keywordCompletion/noElifBeforeElse.py b/python/testData/keywordCompletion/noElifBeforeElse.py
new file mode 100644 (file)
index 0000000..b5b02b3
--- /dev/null
@@ -0,0 +1,5 @@
+if True:
+    a = 1
+else:
+    pass
+<caret>
\ No newline at end of file
index 88d6779540b07383db26e3a72c6fa468350268cc..c00e632a21eb43d2a39be211cc9f9bba9c8fc2a9 100644 (file)
@@ -83,10 +83,6 @@ public class PythonCompletionTest extends PyTestCase {
     doTest();
   }
 
-  public void testKeywordAfterComment() {  // PY-697
-    doTest();
-  }
-
   public void testClassPrivate() {
     doTest();
   }
@@ -228,14 +224,6 @@ public class PythonCompletionTest extends PyTestCase {
     doTest();
   }
 
-  public void testEmptyFile() {  // PY-1845
-    myFixture.configureByText(PythonFileType.INSTANCE, "");
-    myFixture.completeBasic();
-    final List<String> elements = myFixture.getLookupElementStrings();
-    assertNotNull(elements);
-    assertTrue(elements.contains("import"));
-  }
-
   public void testImportItself() {  // PY-1895
     myFixture.copyDirectoryToProject("completion/importItself/package1", "package1");
     myFixture.configureFromTempProjectFile("package1/submodule1.py");
@@ -275,14 +263,6 @@ public class PythonCompletionTest extends PyTestCase {
     doTest();
   }
 
-  public void testNonlocal() {  // PY-2289
-    doTest3K();
-  }
-
-  public void testYield() {
-    doTest();
-  }
-
   private void doTest3K() {
     PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON30);
     try {
@@ -297,55 +277,6 @@ public class PythonCompletionTest extends PyTestCase {
     doTest();
   }
 
-  public void testElse() {
-    doTest();
-  }
-
-  public void testElseNotIndented() {
-    doTest();
-  }
-
-  public void testElseInTryNotIndented() {
-    doTest();
-  }
-
-  public void testElif() {
-    doTest();
-  }
-
-  public void testElifNotIndented() {
-    doTest();
-  }
-
-  public void testExcept() {
-    doTest();
-  }
-
-  public void testExceptNotIndented() {
-    doTest();
-  }
-
-  public void testFinallyInExcept() {
-    doTest();
-  }
-
-  public void testContinue() {
-    doTest();
-  }
-
-  public void testNoContinueInFinally() {
-    final String testName = "completion/" + getTestName(true);
-    myFixture.configureByFile(testName + ".py");
-    myFixture.completeBasic();
-    final List<String> lookupElementStrings = myFixture.getLookupElementStrings();
-    assertNotNull(lookupElementStrings);
-    assertFalse(lookupElementStrings.contains("continue"));
-  }
-
-  public void testElseInCondExpr() {  // PY-2397
-    doTest();
-  }
-
   public void testLocalVarInDictKey() {  // PY-2558
     doTest();
   }
@@ -358,10 +289,6 @@ public class PythonCompletionTest extends PyTestCase {
     doTest();
   }
 
-  public void testFromDotImport() {  // PY-2772
-    doTest3K();
-  }
-
   public void testNoIdentifiersInImport() {
     doTest();
   }
@@ -400,10 +327,6 @@ public class PythonCompletionTest extends PyTestCase {
     doMultiFileTest();
   }
 
-  public void testLambdaInExpression() {  // PY-3150
-    doTest();
-  }
-
   public void testVeryPrivate() {  // PY-3246
     doTest();
   }
@@ -467,10 +390,6 @@ public class PythonCompletionTest extends PyTestCase {
     myFixture.checkResultByFile("completion/identifiersInPlainDocstring.after.py");
   }
 
-  public void testNoneInArgList() {  // PY-3464
-    doTest3K();
-  }
-
   public void testPep328Completion() {  // PY-3409
     myFixture.copyDirectoryToProject("completion/pep328", "pep328");
     myFixture.configureByFile("pep328/package/subpackage1/moduleX.py");
@@ -528,11 +447,6 @@ public class PythonCompletionTest extends PyTestCase {
     doTest();
   }
 
-  // PY-5144
-  public void testImportKeyword() {
-    doTest();
-  }
-
   public void testAssignedNearby() {
     doTest();
   }
@@ -624,24 +538,6 @@ public class PythonCompletionTest extends PyTestCase {
     doTest();
   }
 
-  public void testAsInWith() {  // PY-3701
-    setLanguageLevel(LanguageLevel.PYTHON27);
-    assertTrue(doTestByText("with open(foo) <caret>").contains("as"));
-  }
-
-  public void testAsInExcept() {  // PY-1846
-    setLanguageLevel(LanguageLevel.PYTHON27);
-    assertTrue(doTestByText("try:\n" +
-                            "    pass\n" +
-                            "except IOError <caret>").contains("as"));
-  }
-
-  public void testElseInFor() {  // PY-6755
-    assertTrue(doTestByText("for item in range(10):\n" +
-                            "    pass\n" +
-                            "el<caret>").contains("else"));
-  }
-
   public void testArgs() {  // PY-7208
     doTestByText("def foo(*<caret>)");
     myFixture.checkResult("def foo(*args)");
diff --git a/python/testSrc/com/jetbrains/python/PythonKeywordCompletionTest.java b/python/testSrc/com/jetbrains/python/PythonKeywordCompletionTest.java
new file mode 100644 (file)
index 0000000..6e9f454
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2000-2013 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python;
+
+import com.intellij.codeInsight.completion.impl.CamelHumpMatcher;
+import com.jetbrains.python.fixtures.PyTestCase;
+import com.jetbrains.python.psi.LanguageLevel;
+import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
+
+import java.util.List;
+
+public class PythonKeywordCompletionTest extends PyTestCase {
+
+  private void doTest3K() {
+    PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON30);
+    try {
+      doTest();
+    }
+    finally {
+      PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), null);
+    }
+  }
+
+  private void doTest() {
+    CamelHumpMatcher.forceStartMatching(getTestRootDisposable());
+    final String testName = "keywordCompletion/" + getTestName(true);
+    myFixture.configureByFile(testName + ".py");
+    myFixture.completeBasic();
+    myFixture.checkResultByFile(testName + ".after.py");
+  }
+
+  private List<String> doTestByText(String text) {
+    myFixture.configureByText(PythonFileType.INSTANCE, text);
+    myFixture.completeBasic();
+    return myFixture.getLookupElementStrings();
+  }
+
+  public void testKeywordAfterComment() {  // PY-697
+    doTest();
+  }
+
+  public void testEmptyFile() {  // PY-1845
+    myFixture.configureByText(PythonFileType.INSTANCE, "");
+    myFixture.completeBasic();
+    final List<String> elements = myFixture.getLookupElementStrings();
+    assertNotNull(elements);
+    assertTrue(elements.contains("import"));
+  }
+
+  public void testNonlocal() {  // PY-2289
+    doTest3K();
+  }
+
+  public void testYield() {
+    doTest();
+  }
+
+  public void testElse() {
+    doTest();
+  }
+
+  public void testElseNotIndented() {
+    doTest();
+  }
+
+  public void testElseInTryNotIndented() {
+    doTest();
+  }
+
+  public void testElif() {
+    doTest();
+  }
+
+  public void testElifNotIndented() {
+    doTest();
+  }
+
+  public void testExcept() {
+    doTest();
+  }
+
+  public void testExceptNotIndented() {
+    doTest();
+  }
+
+  public void testFinallyInExcept() {
+    doTest();
+  }
+
+  public void testContinue() {
+    doTest();
+  }
+
+  public void testNoContinueInFinally() {
+    final String testName = "keywordCompletion/" + getTestName(true);
+    myFixture.configureByFile(testName + ".py");
+    myFixture.completeBasic();
+    final List<String> lookupElementStrings = myFixture.getLookupElementStrings();
+    assertNotNull(lookupElementStrings);
+    assertDoesntContain(lookupElementStrings, "continue");
+  }
+
+  public void testNoElifBeforeElse() {
+    final String testName = "keywordCompletion/" + getTestName(true);
+    myFixture.configureByFile(testName + ".py");
+    myFixture.completeBasic();
+    final List<String> lookupElementStrings = myFixture.getLookupElementStrings();
+    assertNotNull(lookupElementStrings);
+    assertDoesntContain(lookupElementStrings, "elif");
+  }
+
+  public void testElseInCondExpr() {  // PY-2397
+    doTest();
+  }
+
+  public void testFromDotImport() {  // PY-2772
+    doTest3K();
+  }
+
+  public void testLambdaInExpression() {  // PY-3150
+    doTest();
+  }
+
+  public void testNoneInArgList() {  // PY-3464
+    doTest3K();
+  }
+
+  // PY-5144
+  public void testImportKeyword() {
+    doTest();
+  }
+
+  public void testAsInWith() {  // PY-3701
+    setLanguageLevel(LanguageLevel.PYTHON27);
+    assertTrue(doTestByText("with open(foo) <caret>").contains("as"));
+  }
+
+  public void testAsInExcept() {  // PY-1846
+    setLanguageLevel(LanguageLevel.PYTHON27);
+    assertTrue(doTestByText("try:\n" +
+                            "    pass\n" +
+                            "except IOError <caret>").contains("as"));
+  }
+
+  public void testElseInFor() {  // PY-6755
+    assertTrue(doTestByText("for item in range(10):\n" +
+                            "    pass\n" +
+                            "el<caret>").contains("else"));
+  }
+}
index 4d227f6dbcfe7b7cce2825039243ab8602d08e2d..09c74b76c4d477a5ef9c0280465058728dd277cd 100644 (file)
@@ -52,7 +52,7 @@ import java.io.File;
 /**
  * @author yole
  */
-@TestDataPath("$CONTENT_ROOT/../../testData/")
+@TestDataPath("$CONTENT_ROOT/../testData/")
 public abstract class PyTestCase extends UsefulTestCase {
   public static String PYTHON_2_MOCK_SDK = "2.7";
   public static String PYTHON_3_MOCK_SDK = "3.2";