use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyTestFinder method findTestsForClass.
@NotNull
@Override
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
PyDocStringOwner source = findSourceElement(element);
if (source == null)
return Collections.emptySet();
String sourceName = source.getName();
if (sourceName == null)
return Collections.emptySet();
List<Pair<? extends PsiNamedElement, Integer>> classesWithProximities = new ArrayList<>();
if (source instanceof PyClass) {
Collection<String> names = PyClassNameIndex.allKeys(element.getProject());
for (String eachName : names) {
if (eachName.contains(sourceName)) {
for (PyClass eachClass : PyClassNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
if (PythonUnitTestUtil.isTestCaseClass(eachClass, null) || PythonDocTestUtil.isDocTestClass(eachClass)) {
classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachClass, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
}
}
}
}
} else {
Collection<String> names = PyFunctionNameIndex.allKeys(element.getProject());
for (String eachName : names) {
if (eachName.contains(sourceName)) {
for (PyFunction eachFunction : PyFunctionNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
if (PythonUnitTestUtil.isTestCaseFunction(eachFunction) || PythonDocTestUtil.isDocTestFunction(eachFunction)) {
classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachFunction, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
}
}
}
}
}
return TestFinderHelper.getSortedElements(classesWithProximities, true);
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyTestFinder method findClassesForTest.
@NotNull
@Override
public Collection<PsiElement> findClassesForTest(@NotNull PsiElement element) {
final PyFunction sourceFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
final PyClass source = PsiTreeUtil.getParentOfType(element, PyClass.class);
if (sourceFunction == null && source == null)
return Collections.emptySet();
List<Pair<? extends PsiNamedElement, Integer>> classesWithWeights = new ArrayList<>();
final List<Pair<String, Integer>> possibleNames = new ArrayList<>();
if (source != null)
possibleNames.addAll(TestFinderHelper.collectPossibleClassNamesWithWeights(source.getName()));
if (sourceFunction != null)
possibleNames.addAll(TestFinderHelper.collectPossibleClassNamesWithWeights(sourceFunction.getName()));
for (Pair<String, Integer> eachNameWithWeight : possibleNames) {
for (PyClass eachClass : PyClassNameIndex.find(eachNameWithWeight.first, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
if (!PyTestUtil.isPyTestClass(eachClass, null))
classesWithWeights.add(new Pair<PsiNamedElement, Integer>(eachClass, eachNameWithWeight.second));
}
for (PyFunction function : PyFunctionNameIndex.find(eachNameWithWeight.first, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
if (!PyTestUtil.isPyTestFunction(function))
classesWithWeights.add(new Pair<PsiNamedElement, Integer>(function, eachNameWithWeight.second));
}
}
return TestFinderHelper.getSortedElements(classesWithWeights, false);
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyDecoratorTest method testDecoCall.
public void testDecoCall() throws Exception {
PsiElement targetElement = find().getParent();
assertTrue(targetElement instanceof PyDecorator);
PyDecorator deco = (PyDecorator) targetElement;
PyFunction decofun = deco.getTarget();
assertNotNull(decofun);
assertEquals("foo", decofun.getName());
assertFalse(deco.isBuiltin());
assertFalse(deco.hasArgumentList());
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyDecoratorTest method testDecoParamCall.
public void testDecoParamCall() throws Exception {
PsiElement targetElement = find().getParent();
assertTrue(targetElement instanceof PyDecorator);
PyDecorator deco = (PyDecorator) targetElement;
PyFunction decofun = deco.getTarget();
assertNotNull(decofun);
assertEquals("foo", decofun.getName());
assertFalse(deco.isBuiltin());
assertTrue(deco.hasArgumentList());
PyArgumentList arglist = deco.getArgumentList();
assertNotNull(arglist);
PyExpression[] args = arglist.getArguments();
assertEquals("argument count", 1, args.length);
assertEquals("argument value", "1", args[0].getText());
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyStatementListTest method testOneLineList.
public void testOneLineList() {
PyElementGenerator generator = PyElementGenerator.getInstance(myFixture.getProject());
PyFunction function = generator.createPhysicalFromText(LanguageLevel.PYTHON27, PyFunction.class, "def foo(): print 1");
PyFunction function2 = generator.createPhysicalFromText(LanguageLevel.PYTHON27, PyFunction.class, "def foo(): print 2");
final PyStatementList list1 = function.getStatementList();
final PyStatementList list2 = function2.getStatementList();
new WriteCommandAction.Simple(myFixture.getProject()) {
@Override
protected void run() throws Throwable {
list1.add(list2.getStatements()[0]);
}
}.execute();
assertEquals("def foo():\n print 1\n print 2", function.getText());
}
Aggregations