use of com.jetbrains.python.psi.PyElement in project intellij-community by JetBrains.
the class PyClassRefactoringTest method findMember.
/**
* @param className class where member should be found
* @param memberName member that starts with dot (<code>.</code>) is treated as method.
* member that starts with dash (<code>#</code>) is treated as attribute.
* It is treated parent class otherwise
* @return member or null if not found
*/
@NotNull
protected PyElement findMember(@NotNull final String className, @NotNull String memberName) {
final PyElement result;
//TODO: Get rid of this chain of copy pastes
if (memberName.contains(".")) {
result = findMethod(className, memberName.substring(1));
} else if (memberName.contains("#")) {
result = findField(className, memberName.substring(1));
} else {
result = findClass(memberName);
}
Assert.assertNotNull(String.format("No member %s found in class %s", memberName, className), result);
return result;
}
use of com.jetbrains.python.psi.PyElement 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());
}
use of com.jetbrains.python.psi.PyElement 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());
}
}
use of com.jetbrains.python.psi.PyElement 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);
}
use of com.jetbrains.python.psi.PyElement 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));
}
Aggregations