From 06166d1382a02ec7ff18d55f9deceb30e16ef87c Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Wed, 30 May 2018 01:36:27 +0300 Subject: [PATCH] PY-30032: Provide fixture names as arguments to tests def test_sample(Fixture must be provided here): pass --- python/src/META-INF/python-core-common.xml | 2 + .../PyTestFixtureCompletionContributor.kt | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 python/src/com/jetbrains/python/testing/pyTestFixtures/PyTestFixtureCompletionContributor.kt diff --git a/python/src/META-INF/python-core-common.xml b/python/src/META-INF/python-core-common.xml index f682b4445c94..cb711e9b09fe 100644 --- a/python/src/META-INF/python-core-common.xml +++ b/python/src/META-INF/python-core-common.xml @@ -67,6 +67,8 @@ + () { + override fun addCompletions(parameters: CompletionParameters, context: ProcessingContext?, result: CompletionResultSet) { + val pyFunction = PsiTreeUtil.getParentOfType(parameters.position, PyParameterList::class.java)?.containingFunction ?: return + val module = ModuleUtilCore.findModuleForPsiElement(pyFunction) ?: return + val typeEvalContext = TypeEvalContext.userInitiated(pyFunction.project, pyFunction.containingFile) + + if (TestRunnerService.getInstance(module).projectConfiguration != pyTestName) { + return + } + val usedParams = pyFunction.getParameters(typeEvalContext).mapNotNull { it.name }.toSet() + if (isTestElement(pyFunction, ThreeState.UNSURE, typeEvalContext)) { + getFixtures(module).map { it.name }.filter { !usedParams.contains(it) }.forEach { + result.addElement(PythonLookupElement(it, false, PythonIcons.Python.Function)) + } + } + } +} + +/** + * Provide pytest fixtures are possible arguments for tests + */ +class PyTestFixtureCompletionContributor : CompletionContributor() { + init { + extend(CompletionType.BASIC, PlatformPatterns.psiElement().inside(PyParameterList::class.java), PyTestFixtureCompletionProvider) + } +}