use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyChangeSignatureTest method doValidationTest.
public void doValidationTest(@Nullable String newName, @Nullable List<PyParameterInfo> parameters, @Nullable String expected) {
myFixture.configureByFile("refactoring/changeSignature/" + getTestName(true) + ".py");
final PyChangeSignatureHandler changeSignatureHandler = new PyChangeSignatureHandler();
final PyFunction function = (PyFunction) changeSignatureHandler.findTargetMember(myFixture.getFile(), myFixture.getEditor());
assertNotNull(function);
final PyMethodDescriptor method = new PyMethodDescriptor(function);
final TestPyChangeSignatureDialog dialog = new TestPyChangeSignatureDialog(function.getProject(), method);
try {
if (newName != null) {
dialog.setNewName(newName);
}
if (parameters != null) {
dialog.setParameterInfos(parameters);
}
final String validationError = dialog.validateAndCommitData();
assertEquals(expected, validationError);
} finally {
Disposer.dispose(dialog.getDisposable());
}
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyMakeFunctionTopLevelTest method runRefactoring.
private void runRefactoring(@Nullable String destination, @Nullable String errorMessage) {
final PyFunction function = assertInstanceOf(myFixture.getElementAtCaret(), PyFunction.class);
if (destination == null) {
destination = PyPsiUtils.getContainingFilePath(function);
} else {
final VirtualFile srcRoot = ModuleRootManager.getInstance(myFixture.getModule()).getSourceRoots()[0];
destination = FileUtil.join(srcRoot.getPath(), destination);
}
assertNotNull(destination);
final String finalDestination = destination;
try {
WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> {
if (function.getContainingClass() != null) {
new PyMakeMethodTopLevelProcessor(function, finalDestination).run();
} else {
new PyMakeLocalFunctionTopLevelProcessor(function, finalDestination).run();
}
});
} catch (IncorrectOperationException e) {
if (errorMessage == null) {
fail("Refactoring failed unexpectedly with message: " + e.getMessage());
}
assertEquals(errorMessage, e.getMessage());
}
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class CreateTestAction method invoke.
@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final PyFunction srcFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
final PyClass srcClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
if (srcClass == null && srcFunction == null)
return;
final PsiDirectory dir = element.getContainingFile().getContainingDirectory();
final CreateTestDialog d = new CreateTestDialog(project);
if (srcClass != null) {
d.setClassName("Test" + StringUtil.capitalize(srcClass.getName()));
d.setFileName("test_" + StringUtil.decapitalize(srcClass.getName()) + ".py");
if (dir != null)
d.setTargetDir(dir.getVirtualFile().getPath());
if (srcFunction != null) {
d.methodsSize(1);
d.addMethod("test_" + srcFunction.getName(), 0);
} else {
final List<PyFunction> methods = Lists.newArrayList();
srcClass.visitMethods(pyFunction -> {
if (pyFunction.getName() != null && !pyFunction.getName().startsWith("__"))
methods.add(pyFunction);
return true;
}, false, null);
d.methodsSize(methods.size());
int i = 0;
for (PyFunction f : methods) {
d.addMethod("test_" + f.getName(), i);
++i;
}
}
} else {
d.setClassName("Test" + StringUtil.capitalize(srcFunction.getName()));
d.setFileName("test_" + StringUtil.decapitalize(srcFunction.getName()) + ".py");
if (dir != null)
d.setTargetDir(dir.getVirtualFile().getPath());
d.methodsSize(1);
d.addMethod("test_" + srcFunction.getName(), 0);
}
if (!d.showAndGet()) {
return;
}
CommandProcessor.getInstance().executeCommand(project, () -> {
PsiFile e = PyTestCreator.generateTestAndNavigate(project, d);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitAllDocuments();
}, CodeInsightBundle.message("intention.create.test"), this);
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyFunctionNameMacro method calculateResult.
@Nullable
@Override
public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
PsiElement place = context.getPsiElementAtStartOffset();
PyFunction pyFunction = PsiTreeUtil.getParentOfType(place, PyFunction.class);
if (pyFunction == null) {
return null;
}
String name = pyFunction.getName();
return name == null ? null : new TextResult(name);
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyTestUtil method isPyTestClass.
public static boolean isPyTestClass(final PyClass pyClass, @Nullable final TypeEvalContext context) {
final TypeEvalContext contextToUse = (context != null ? context : TypeEvalContext.codeInsightFallback(pyClass.getProject()));
for (PyClassLikeType type : pyClass.getAncestorTypes(contextToUse)) {
if (type != null && PYTHON_TEST_QUALIFIED_CLASSES.contains(type.getClassQName())) {
return true;
}
}
final String className = pyClass.getName();
if (className == null)
return false;
final String name = className.toLowerCase();
if (name.startsWith("test")) {
for (PyFunction cls : pyClass.getMethods()) {
if (isPyTestFunction(cls)) {
return true;
}
}
}
return false;
}
Aggregations