PY-17265 Return the test that checks that refactoring is available
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Tue, 18 Oct 2016 15:50:58 +0000 (18:50 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Mon, 24 Oct 2016 21:03:49 +0000 (00:03 +0300)
python/src/com/jetbrains/python/refactoring/move/PyMoveSymbolDelegate.java
python/testSrc/com/jetbrains/python/refactoring/PyMakeFunctionTopLevelTest.java

index 39275aac3bbd7d67cb6d31961ead85990e4e3d26..13e78f63a9c41255ccf46ac48bb75e4fb42e7ac2 100644 (file)
@@ -15,6 +15,7 @@
  */
 package com.jetbrains.python.refactoring.move;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Lists;
 import com.intellij.openapi.actionSystem.DataContext;
 import com.intellij.openapi.application.ApplicationManager;
@@ -63,7 +64,7 @@ public class PyMoveSymbolDelegate extends MoveHandlerDelegate {
       return false;
     }
     // Local function or method
-    if (findTargetFunction(elements[0]) != null) {
+    if (findMovableLocalFunctionOrMethod(elements[0]) != null) {
       return true;
     }
     
@@ -87,7 +88,7 @@ public class PyMoveSymbolDelegate extends MoveHandlerDelegate {
     }
     
     final BaseRefactoringProcessor processor;
-    final PyFunction function = findTargetFunction(elements[0]);
+    final PyFunction function = findMovableLocalFunctionOrMethod(elements[0]);
     if (function != null) {
       final PyMakeFunctionTopLevelDialog dialog = new PyMakeFunctionTopLevelDialog(project, function, initialPath, initialPath);
       if (!dialog.showAndGet()) {
@@ -155,7 +156,7 @@ public class PyMoveSymbolDelegate extends MoveHandlerDelegate {
     // Fallback to the old way to select single element to move
     final PsiNamedElement e = PyMoveModuleMembersHelper.extractNamedElement(element);
     if (e != null && PyMoveModuleMembersHelper.hasMovableElementType(e)) {
-      if (PyMoveModuleMembersHelper.isMovableModuleMember(e) || findTargetFunction(e) != null) {
+      if (PyMoveModuleMembersHelper.isMovableModuleMember(e) || findMovableLocalFunctionOrMethod(e) != null) {
         doMove(project, new PsiElement[]{e}, targetContainer, null);
       }
       else {
@@ -189,8 +190,9 @@ public class PyMoveSymbolDelegate extends MoveHandlerDelegate {
     });
   }
 
+  @VisibleForTesting
   @Nullable
-  public static PyFunction findTargetFunction(@NotNull PsiElement element) {
+  public static PyFunction findMovableLocalFunctionOrMethod(@NotNull PsiElement element) {
     if (isLocalFunction(element) || isSuitableInstanceMethod(element)) {
       return (PyFunction)element;
     }
index 9101c80b77b660ca41aac93af943274d12c02013..753dc5230257274fc300d9ec224e7e25653243f8 100644 (file)
@@ -19,13 +19,17 @@ import com.intellij.openapi.command.WriteCommandAction;
 import com.intellij.openapi.roots.ModuleRootManager;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
 import com.intellij.testFramework.PlatformTestUtil;
 import com.intellij.util.IncorrectOperationException;
 import com.jetbrains.python.PyBundle;
+import com.jetbrains.python.PyTokenTypes;
 import com.jetbrains.python.fixtures.PyTestCase;
 import com.jetbrains.python.psi.LanguageLevel;
 import com.jetbrains.python.psi.PyFunction;
 import com.jetbrains.python.psi.impl.PyPsiUtils;
+import com.jetbrains.python.refactoring.move.PyMoveSymbolDelegate;
 import com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeLocalFunctionTopLevelProcessor;
 import com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeMethodTopLevelProcessor;
 import org.jetbrains.annotations.NotNull;
@@ -94,12 +98,10 @@ public class PyMakeFunctionTopLevelTest extends PyTestCase {
     }
   }
 
-  //private static boolean isActionEnabled() {
-  //  final PyMakeFunctionTopLevelRefactoring action = new PyMakeFunctionTopLevelRefactoring();
-  //  final TestActionEvent event = new TestActionEvent(action);
-  //  action.beforeActionPerformedUpdate(event);
-  //  return event.getPresentation().isEnabled();
-  //}
+  private boolean isActionEnabled() {
+    final PsiElement elementUnderCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
+    return PyMoveSymbolDelegate.findMovableLocalFunctionOrMethod(elementUnderCaret) != null; 
+  }
 
   // PY-6637
   public void testLocalFunctionSimple() {
@@ -107,43 +109,43 @@ public class PyMakeFunctionTopLevelTest extends PyTestCase {
   }
 
   // PY-6637
-  //public void testRefactoringAvailability() {
-  //  myFixture.configureByFile(getTestName(true) + ".py");
-  //
-  //  final PsiFile file = myFixture.getFile();
-  //  moveByText("func");
-  //  assertFalse(isActionEnabled());
-  //  moveByText("local");
-  //  assertTrue(isActionEnabled());
-  //
-  //  // move to "def" keyword
-  //  myFixture.getEditor().getCaretModel().moveCaretRelatively(-3, 0, false, false, false);
-  //  final PsiElement tokenAtCaret = file.findElementAt(myFixture.getCaretOffset());
-  //  assertNotNull(tokenAtCaret);
-  //  assertEquals(tokenAtCaret.getNode().getElementType(), PyTokenTypes.DEF_KEYWORD);
-  //  assertTrue(isActionEnabled());
-  //
-  //  moveByText("method");
-  //  assertTrue(isActionEnabled());
-  //
-  //  moveByText("static_method");
-  //  assertFalse(isActionEnabled());
-  //  moveByText("class_method");
-  //  assertFalse(isActionEnabled());
-  //
-  //  // Overridden method
-  //  moveByText("overridden_method");
-  //  assertFalse(isActionEnabled());
-  //
-  //  // Overriding method
-  //  moveByText("upper");
-  //  assertFalse(isActionEnabled());
-  //
-  //  moveByText("property");
-  //  assertFalse(isActionEnabled());
-  //  moveByText("__magic__");
-  //  assertFalse(isActionEnabled());
-  //}
+  public void testRefactoringAvailability() {
+    myFixture.configureByFile(getTestName(true) + ".py");
+
+    final PsiFile file = myFixture.getFile();
+    moveByText("func");
+    assertFalse(isActionEnabled());
+    moveByText("local");
+    assertTrue(isActionEnabled());
+
+    // move to "def" keyword
+    myFixture.getEditor().getCaretModel().moveCaretRelatively(-3, 0, false, false, false);
+    final PsiElement tokenAtCaret = file.findElementAt(myFixture.getCaretOffset());
+    assertNotNull(tokenAtCaret);
+    assertEquals(tokenAtCaret.getNode().getElementType(), PyTokenTypes.DEF_KEYWORD);
+    assertTrue(isActionEnabled());
+
+    moveByText("method");
+    assertTrue(isActionEnabled());
+
+    moveByText("static_method");
+    assertFalse(isActionEnabled());
+    moveByText("class_method");
+    assertFalse(isActionEnabled());
+
+    // Overridden method
+    moveByText("overridden_method");
+    assertFalse(isActionEnabled());
+
+    // Overriding method
+    moveByText("upper");
+    assertFalse(isActionEnabled());
+
+    moveByText("property");
+    assertFalse(isActionEnabled());
+    moveByText("__magic__");
+    assertFalse(isActionEnabled());
+  }
 
   // PY-6637
   public void testLocalFunctionNonlocalReferenceToOuterScope() {