Search in sources :

Example 1 with PyMemberInfo

use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo 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 2 with PyMemberInfo

use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo 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 3 with PyMemberInfo

use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo 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 4 with PyMemberInfo

use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo 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 5 with PyMemberInfo

use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo 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

PyMemberInfo (com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo)10 PyClass (com.jetbrains.python.psi.PyClass)9 PyElement (com.jetbrains.python.psi.PyElement)8 ArrayList (java.util.ArrayList)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2 PsiManager (com.intellij.psi.PsiManager)2 MultiMap (com.intellij.util.containers.MultiMap)2 File (java.io.File)2 Collection (java.util.Collection)2 NotNull (org.jetbrains.annotations.NotNull)2 Predicate (com.google.common.base.Predicate)1 ApplicationManagerEx (com.intellij.openapi.application.ex.ApplicationManagerEx)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Document (com.intellij.openapi.editor.Document)1 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)1 Comparing (com.intellij.openapi.util.Comparing)1 FileUtil (com.intellij.openapi.util.io.FileUtil)1