Search in sources :

Example 6 with PyClass

use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.

the class PyExtractSuperclassPresenterTest method testInvalidSuperClassNameLeadsToError.

/**
   * Checks that presenter displays error if user enters invalid name for new class
   */
public void testInvalidSuperClassNameLeadsToError() {
    final String className = "Child";
    final PyClass aClass = getClassByName(className);
    final List<PyMemberInfo<PyElement>> classMemberInfos = new PyMemberInfoStorage(aClass).getClassMemberInfos(aClass);
    assert !classMemberInfos.isEmpty() : "No member infos for " + className;
    final PyMemberInfo<PyElement> pyMemberInfo = classMemberInfos.get(0);
    EasyMock.expect(myView.getSelectedMemberInfos()).andReturn(Collections.singletonList(pyMemberInfo)).anyTimes();
    EasyMock.expect(myView.getSuperClassName()).andReturn("INVALID CLASS NAME").anyTimes();
    final Capture<String> errorMessageCapture = configureViewToCaptureError();
    final PyExtractSuperclassPresenterImpl sut = configureByClass(className);
    myMocksControl.replay();
    sut.launch();
    sut.okClicked();
    Assert.assertTrue("No error displayed for invalid class name", errorMessageCapture.hasCaptured());
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyMemberInfoStorage(com.jetbrains.python.refactoring.classes.PyMemberInfoStorage) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement)

Example 7 with PyClass

use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.

the class PyExtractSuperclassTest method doSimpleTest.

private void doSimpleTest(final String className, final String superclassName, final String expectedError, final boolean sameFile, boolean asAbstract, final String... membersName) throws Exception {
    try {
        String baseName = "/refactoring/extractsuperclass/" + getTestName(true);
        myFixture.configureByFile(baseName + ".before.py");
        final PyClass clazz = findClass(className);
        final List<PyMemberInfo<PyElement>> members = new ArrayList<>();
        for (String memberName : membersName) {
            final PyElement member = findMember(className, memberName);
            final PyMemberInfo<PyElement> memberInfo = MembersManager.findMember(clazz, member);
            memberInfo.setToAbstract(asAbstract);
            members.add(memberInfo);
        }
        new WriteCommandAction.Simple(myFixture.getProject()) {

            @Override
            protected void run() throws Throwable {
                //noinspection ConstantConditions
                final String url = sameFile ? myFixture.getFile().getVirtualFile().getUrl() : myFixture.getFile().getVirtualFile().getParent().getUrl();
                PyExtractSuperclassHelper.extractSuperclass(clazz, members, superclassName, url);
            }
        }.execute();
        myFixture.checkResultByFile(baseName + ".after.py");
    } catch (Exception e) {
        if (expectedError == null)
            throw e;
        assertEquals(expectedError, e.getMessage());
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ArrayList(java.util.ArrayList) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement)

Example 8 with PyClass

use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.

the class PyExtractSuperclassTest method testMultifileAppend.

public void testMultifileAppend() {
    // this is half-copy-paste of testMultifileNew. generalization won't make either easier to follow.
    String baseName = "/refactoring/extractsuperclass/multifile/";
    myFixture.configureByFiles(baseName + "source.py", baseName + "a/__init__.py", baseName + "a/b/__init__.py", baseName + "a/b/foo.py");
    final String className = "Foo";
    final String superclassName = "Suppa";
    final PyClass clazz = findClass(className);
    final List<PyMemberInfo<PyElement>> members = new ArrayList<>();
    final PyElement member = findMember(className, ".foo");
    members.add(MembersManager.findMember(clazz, member));
    final VirtualFile base_dir = myFixture.getFile().getVirtualFile().getParent();
    new WriteCommandAction.Simple(myFixture.getProject()) {

        @Override
        protected void run() throws Throwable {
            //TODO: Test via presenter
            //noinspection ConstantConditions
            final String path = base_dir.getPath() + "/a/b";
            PyExtractSuperclassHelper.extractSuperclass(clazz, members, superclassName, path + "/foo.py");
        }
    }.execute();
    final PsiManager psi_mgr = PsiManager.getInstance(myFixture.getProject());
    VirtualFile vfile = base_dir.findChild("a");
    assertTrue(vfile.isDirectory());
    vfile = vfile.findChild(PyNames.INIT_DOT_PY);
    assertNotNull(vfile);
    vfile = base_dir.findChild("a").findChild("b");
    assertTrue(vfile.isDirectory());
    assertNotNull(vfile.findChild(PyNames.INIT_DOT_PY));
    vfile = vfile.findChild("foo.py");
    assertNotNull(vfile);
    PsiFile psi_file = psi_mgr.findFile(vfile);
    String result = psi_file.getText().trim();
    File expected_file = new File(getTestDataPath() + baseName, "target.append.py");
    String expected = psi_mgr.findFile(LocalFileSystem.getInstance().findFileByIoFile(expected_file)).getText().trim();
    assertEquals(expected, result);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ArrayList(java.util.ArrayList) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) PyElement(com.jetbrains.python.psi.PyElement)

Example 9 with PyClass

use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.

the class PyPullUpTest method doPullUp.

private void doPullUp(final String className, final String superClassName, final boolean toAbstract, final String... memberNames) {
    final PyClass clazz = findClass(className);
    final PyClass superClass = findClass(superClassName);
    final Collection<PyMemberInfo<PyElement>> membersToMove = new ArrayList<>(memberNames.length);
    for (final String memberName : memberNames) {
        final PyElement member = findMember(className, memberName);
        final PyMemberInfo<PyElement> memberInfo = MembersManager.findMember(clazz, member);
        memberInfo.setToAbstract(toAbstract);
        membersToMove.add(memberInfo);
    }
    moveViaProcessor(clazz.getProject(), new PyPullUpProcessor(clazz, superClass, membersToMove));
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) ArrayList(java.util.ArrayList) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement)

Example 10 with PyClass

use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.

the class PyPushDownTest method doProcessorTest.

private void doProcessorTest(final String className, final String expectedError, final String... memberNames) throws Exception {
    try {
        String baseName = "/refactoring/pushdown/" + getTestName(true);
        myFixture.configureByFile(baseName + ".before.py");
        final PyClass clazz = findClass(className);
        final List<PyMemberInfo<PyElement>> members = new ArrayList<>();
        for (String memberName : memberNames) {
            final PyElement member = findMember(className, memberName);
            members.add(MembersManager.findMember(clazz, member));
        }
        final PyPushDownProcessor processor = new PyPushDownProcessor(myFixture.getProject(), members, clazz);
        moveViaProcessor(myFixture.getProject(), processor);
        myFixture.checkResultByFile(baseName + ".after.py");
    } catch (Exception e) {
        if (expectedError == null)
            throw e;
        assertTrue(e.getMessage(), e.getMessage().contains(expectedError));
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) ArrayList(java.util.ArrayList) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement)

Aggregations

PyClass (com.jetbrains.python.psi.PyClass)88 PyFunction (com.jetbrains.python.psi.PyFunction)32 PsiElement (com.intellij.psi.PsiElement)22 NotNull (org.jetbrains.annotations.NotNull)17 ArrayList (java.util.ArrayList)15 Nullable (org.jetbrains.annotations.Nullable)15 PyElement (com.jetbrains.python.psi.PyElement)12 PyMemberInfo (com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo)9 PyFile (com.jetbrains.python.psi.PyFile)8 PsiFile (com.intellij.psi.PsiFile)7 Project (com.intellij.openapi.project.Project)6 Editor (com.intellij.openapi.editor.Editor)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 MultiMap (com.intellij.util.containers.MultiMap)5 PyMethodMember (com.jetbrains.python.codeInsight.override.PyMethodMember)4 PyMemberInfoStorage (com.jetbrains.python.refactoring.classes.PyMemberInfoStorage)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 PsiNamedElement (com.intellij.psi.PsiNamedElement)3 PyHierarchyNodeDescriptor (com.jetbrains.python.hierarchy.PyHierarchyNodeDescriptor)3 ControlFlow (com.intellij.codeInsight.controlflow.ControlFlow)2