From c3c54b065b7e81a523aaed92bc6e735bc6528a36 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 30 Dec 2010 17:50:33 +0300 Subject: [PATCH] python doctests refactored --- .../PythonTestConfigurationProducer.java | 31 ++- .../PythonDocTestConfigurationProducer.java | 198 ++-------------- .../PythonDocTestRunConfiguration.java | 164 ++------------ .../PythonDocTestRunConfigurationForm.form | 116 +--------- .../PythonDocTestRunConfigurationForm.java | 211 +----------------- .../PythonDocTestRunConfigurationParams.java | 20 +- 6 files changed, 73 insertions(+), 667 deletions(-) diff --git a/python/src/com/jetbrains/python/testing/PythonTestConfigurationProducer.java b/python/src/com/jetbrains/python/testing/PythonTestConfigurationProducer.java index 2ffc232cde0a..52140459013b 100644 --- a/python/src/com/jetbrains/python/testing/PythonTestConfigurationProducer.java +++ b/python/src/com/jetbrains/python/testing/PythonTestConfigurationProducer.java @@ -27,7 +27,7 @@ import org.jetbrains.annotations.Nullable; import java.util.List; abstract public class PythonTestConfigurationProducer extends RuntimeConfigurationProducer { - private PsiElement myPsiElement; + protected PsiElement myPsiElement; public PythonTestConfigurationProducer (final Class configurationTypeClass) { super(ConfigurationTypeUtil.findConfigurationType(configurationTypeClass)); @@ -70,10 +70,11 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati } @Nullable - private RunnerAndConfigurationSettings createConfigurationFromFunction(Location location, PyElement element) { + protected RunnerAndConfigurationSettings createConfigurationFromFunction(Location location, PyElement element) { PyFunction pyFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class, false); - if (pyFunction == null || !PythonUnitTestUtil.isTestCaseFunction(pyFunction)) return null; + if (! isTestFunction(pyFunction)) return null; final PyClass containingClass = pyFunction.getContainingClass(); + final RunnerAndConfigurationSettings settings = makeConfigurationSettings(location, "tests from function"); final AbstractPythonTestRunConfiguration configuration = (AbstractPythonTestRunConfiguration)settings.getConfiguration(); configuration.setMethodName(pyFunction.getName()); @@ -91,9 +92,9 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati } @Nullable - private RunnerAndConfigurationSettings createConfigurationFromClass(Location location, PyElement element) { + protected RunnerAndConfigurationSettings createConfigurationFromClass(Location location, PyElement element) { PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class, false); - if (pyClass == null || !PythonUnitTestUtil.isTestCaseClass(pyClass)) return null; + if (!isTestClass(pyClass)) return null; final RunnerAndConfigurationSettings settings = makeConfigurationSettings(location, "tests from class"); final AbstractPythonTestRunConfiguration configuration = (AbstractPythonTestRunConfiguration)settings.getConfiguration(); @@ -108,8 +109,18 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati return settings; } + protected boolean isTestClass(PyClass pyClass) { + if (pyClass == null || !PythonUnitTestUtil.isTestCaseClass(pyClass)) return false; + return true; + } + + protected boolean isTestFunction(PyFunction pyFunction) { + if (pyFunction == null || !PythonUnitTestUtil.isTestCaseFunction(pyFunction)) return false; + return true; + } + @Nullable - private RunnerAndConfigurationSettings createConfigurationFromFolder(Location location) { + protected RunnerAndConfigurationSettings createConfigurationFromFolder(Location location) { final PsiElement element = location.getPsiElement(); if (!(element instanceof PsiDirectory)) return null; @@ -134,7 +145,7 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati } - private static boolean isPythonModule(Module module) { + protected static boolean isPythonModule(Module module) { if (module == null) { return false; } @@ -151,7 +162,7 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati } @Nullable - private RunnerAndConfigurationSettings createConfigurationFromFile(Location location, PsiElement element) { + protected RunnerAndConfigurationSettings createConfigurationFromFile(Location location, PsiElement element) { PsiElement file = element.getContainingFile(); if (file == null || !(file instanceof PyFile)) return null; @@ -170,7 +181,7 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati return settings; } - private RunnerAndConfigurationSettings makeConfigurationSettings(Location location, String name) { + protected RunnerAndConfigurationSettings makeConfigurationSettings(Location location, String name) { final RunnerAndConfigurationSettings result = RunManager.getInstance(location.getProject()).createRunConfiguration(name, getConfigurationFactory()); AbstractPythonTestRunConfiguration configuration = (AbstractPythonTestRunConfiguration)result.getConfiguration(); @@ -179,7 +190,7 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati return result; } - private static boolean setupConfigurationScript(AbstractPythonTestRunConfiguration cfg, PyElement element) { + protected static boolean setupConfigurationScript(AbstractPythonTestRunConfiguration cfg, PyElement element) { final PyFile containingFile = PyUtil.getContainingPyFile(element); if (containingFile == null) return false; final VirtualFile vFile = containingFile.getVirtualFile(); diff --git a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestConfigurationProducer.java b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestConfigurationProducer.java index 5e92e29df616..1f20a2a68389 100644 --- a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestConfigurationProducer.java +++ b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestConfigurationProducer.java @@ -4,157 +4,35 @@ package com.jetbrains.python.testing.doctest; import com.intellij.execution.Location; -import com.intellij.execution.RunManager; import com.intellij.execution.RunnerAndConfigurationSettings; -import com.intellij.execution.actions.ConfigurationContext; -import com.intellij.execution.configurations.ConfigurationTypeUtil; -import com.intellij.execution.junit.RuntimeConfigurationProducer; -import com.intellij.facet.Facet; -import com.intellij.facet.FacetManager; -import com.intellij.openapi.extensions.Extensions; -import com.intellij.openapi.module.Module; -import com.intellij.openapi.module.ModuleUtil; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.util.PsiTreeUtil; -import com.jetbrains.python.PythonModuleTypeBase; -import com.jetbrains.python.facet.PythonFacetSettings; -import com.jetbrains.python.psi.*; -import com.jetbrains.python.testing.*; -import org.jetbrains.annotations.NotNull; +import com.jetbrains.python.psi.PyClass; +import com.jetbrains.python.psi.PyElement; +import com.jetbrains.python.psi.PyFile; +import com.jetbrains.python.psi.PyFunction; +import com.jetbrains.python.testing.PythonTestConfigurationProducer; import org.jetbrains.annotations.Nullable; import java.util.List; -public class PythonDocTestConfigurationProducer extends RuntimeConfigurationProducer { - private PsiElement myPsiElement; +public class PythonDocTestConfigurationProducer extends PythonTestConfigurationProducer { public PythonDocTestConfigurationProducer() { - super(ConfigurationTypeUtil.findConfigurationType(PythonDocTestRunConfigurationType.class)); + super(PythonDocTestRunConfigurationType.class); } - @Override - public PsiElement getSourceElement() { - return myPsiElement; + protected boolean isTestFunction(PyFunction pyFunction) { + if (pyFunction == null || !PythonDocTestUtil.isDocTestFunction(pyFunction)) return false; + return true; } - @Override - protected RunnerAndConfigurationSettings createConfigurationByElement(Location location, ConfigurationContext context) { - RunnerAndConfigurationSettings settings; - - Module module = location.getModule(); - - if (module != null) { - for (RunnableUnitTestFilter f : Extensions.getExtensions(RunnableUnitTestFilter.EP_NAME)) { - if (f.isRunnableUnitTest(location.getPsiElement().getContainingFile(), module)) { - return null; - } - } - } - if (PythonUnitTestRunnableScriptFilter.isIfNameMain(location)) { - return null; - } - - settings = createConfigurationFromFolder(location); - if (settings != null) return settings; - final PyElement pyElement = PsiTreeUtil.getParentOfType(location.getPsiElement(), PyElement.class); - if (pyElement != null) { - settings = createConfigurationFromFunction(location, pyElement); - if (settings != null) return settings; - settings = createConfigurationFromClass(location, pyElement); - if (settings != null) return settings; - } - settings = createConfigurationFromFile(location, location.getPsiElement()); - if (settings != null) return settings; - - return null; + protected boolean isTestClass(PyClass pyClass) { + if (pyClass == null || !PythonDocTestUtil.isDocTestClass(pyClass)) return false; + return true; } @Nullable - private RunnerAndConfigurationSettings createConfigurationFromFunction(Location location, PyElement element) { - PyFunction pyFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class, false); - if (pyFunction == null || !PythonDocTestUtil.isDocTestFunction(pyFunction)) return null; - final PyClass containingClass = pyFunction.getContainingClass(); - final RunnerAndConfigurationSettings settings = makeConfigurationSettings(location, "doc tests from function"); - - final PythonDocTestRunConfiguration configuration = (PythonDocTestRunConfiguration)settings.getConfiguration(); - configuration.setMethodName(pyFunction.getName()); - if (containingClass != null) { - configuration.setClassName(containingClass.getName()); - configuration.setTestType(PythonDocTestRunConfiguration.TestType.TEST_METHOD); - } - else { - configuration.setTestType(PythonDocTestRunConfiguration.TestType.TEST_FUNCTION); - } - if (!setupConfigurationScript(configuration, pyFunction)) return null; - configuration.setName(configuration.suggestedName()); - myPsiElement = pyFunction; - return settings; - } - - @Nullable - private RunnerAndConfigurationSettings createConfigurationFromClass(Location location, PyElement element) { - PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class, false); - if (pyClass == null || !PythonDocTestUtil.isDocTestClass(pyClass)) return null; - final RunnerAndConfigurationSettings settings = makeConfigurationSettings(location, "doc tests from class"); - final PythonDocTestRunConfiguration configuration = (PythonDocTestRunConfiguration)settings.getConfiguration(); - - configuration.setTestType( - PythonDocTestRunConfiguration.TestType.TEST_CLASS); - configuration.setClassName(pyClass.getName()); - if (!setupConfigurationScript(configuration, pyClass)) return null; - configuration.setName(configuration.suggestedName()); - - myPsiElement = pyClass; - return settings; - } - - @Nullable - private RunnerAndConfigurationSettings createConfigurationFromFolder(Location location) { - final PsiElement element = location.getPsiElement(); - - if (!(element instanceof PsiDirectory)) return null; - - final Module module = location.getModule(); - if (!isPythonModule(module)) return null; - - PsiDirectory dir = (PsiDirectory)element; - final VirtualFile file = dir.getVirtualFile(); - final String path = file.getPath(); - - final RunnerAndConfigurationSettings settings = makeConfigurationSettings(location, "doc tests from class"); - final PythonDocTestRunConfiguration configuration = (PythonDocTestRunConfiguration)settings.getConfiguration(); - - configuration.setTestType(PythonDocTestRunConfiguration.TestType.TEST_FOLDER); - configuration.setFolderName(path); - configuration.setWorkingDirectory(path); - - configuration.setName(configuration.suggestedName()); - myPsiElement = dir; - return settings; - } - - - private static boolean isPythonModule(Module module) { - if (module == null) { - return false; - } - if (module.getModuleType() instanceof PythonModuleTypeBase) { - return true; - } - final Facet[] allFacets = FacetManager.getInstance(module).getAllFacets(); - for (Facet facet : allFacets) { - if (facet.getConfiguration() instanceof PythonFacetSettings) { - return true; - } - } - return false; - } - - @Nullable - private RunnerAndConfigurationSettings createConfigurationFromFile(Location location, PsiElement element) { + protected RunnerAndConfigurationSettings createConfigurationFromFile(Location location, PsiElement element) { PsiElement file = element.getContainingFile(); if (file == null || !(file instanceof PyFile)) return null; @@ -166,54 +44,12 @@ public class PythonDocTestConfigurationProducer extends RuntimeConfigurationProd final PythonDocTestRunConfiguration configuration = (PythonDocTestRunConfiguration)settings.getConfiguration(); configuration.setTestType(PythonDocTestRunConfiguration.TestType.TEST_SCRIPT); - if (!setupConfigurationScript(configuration, file)) return null; + if (!setupConfigurationScript(configuration, pyFile)) return null; configuration.setName(configuration.suggestedName()); - myPsiElement = file; + myPsiElement = pyFile; return settings; } - - private RunnerAndConfigurationSettings makeConfigurationSettings(Location location, String name) { - final RunnerAndConfigurationSettings result = - RunManager.getInstance(location.getProject()).createRunConfiguration(name, getConfigurationFactory()); - PythonDocTestRunConfiguration configuration = (PythonDocTestRunConfiguration)result.getConfiguration(); - configuration.setUseModuleSdk(true); - configuration.setModule(ModuleUtil.findModuleForPsiElement(location.getPsiElement())); - return result; - } - - private static boolean setupConfigurationScript(PythonDocTestRunConfiguration cfg, PsiElement element) { - PsiFile containingFile; - if (element instanceof PyElement) containingFile = PyUtil.getContainingPyFile((PyElement)element); - else containingFile = element.getContainingFile(); - if (containingFile == null) return false; - final VirtualFile vFile = containingFile.getVirtualFile(); - if (vFile == null) return false; - final VirtualFile parent = vFile.getParent(); - if (parent == null) return false; - - cfg.setScriptName(containingFile.getName()); - cfg.setWorkingDirectory(parent.getPath()); - + protected boolean isAvailable(Location location) { return true; } - - @Override - protected RunnerAndConfigurationSettings findExistingByElement(Location location, - @NotNull RunnerAndConfigurationSettings[] existingConfigurations, - ConfigurationContext context) { - final RunnerAndConfigurationSettings settings = createConfigurationByElement(location, null); - if (settings != null) { - final PythonDocTestRunConfiguration configuration = (PythonDocTestRunConfiguration)settings.getConfiguration(); - for (RunnerAndConfigurationSettings existingConfiguration : existingConfigurations) { - if (configuration.compareSettings((PythonDocTestRunConfiguration)existingConfiguration.getConfiguration())) { - return existingConfiguration; - } - } - } - return null; - } - - public int compareTo(Object o) { - return PREFERED; - } } \ No newline at end of file diff --git a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfiguration.java b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfiguration.java index 38451becb5e2..cb4d07e9e0ce 100644 --- a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfiguration.java +++ b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfiguration.java @@ -9,76 +9,51 @@ import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.JDOMExternalizerUtil; import com.intellij.openapi.util.WriteExternalException; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.util.text.StringUtil; -import com.jetbrains.python.PyBundle; -import com.jetbrains.python.run.AbstractPythonRunConfiguration; -import com.jetbrains.python.run.AbstractPythonRunConfigurationParams; +import com.jetbrains.python.testing.AbstractPythonTestRunConfiguration; import org.jdom.Element; import org.jetbrains.annotations.NotNull; /** * User: catherine */ -public class PythonDocTestRunConfiguration extends AbstractPythonRunConfiguration - implements AbstractPythonRunConfigurationParams, PythonDocTestRunConfigurationParams { - private String myClassName = ""; - private String myScriptName = ""; - private String myMethodName = ""; - private String myFolderName = ""; +public class PythonDocTestRunConfiguration extends AbstractPythonTestRunConfiguration + implements PythonDocTestRunConfigurationParams { private String myPattern = ""; // pattern for modules in folder to match against - private TestType myTestType = TestType.TEST_SCRIPT; protected PythonDocTestRunConfiguration(RunConfigurationModule module, ConfigurationFactory configurationFactory, String name) { - super(name, module, configurationFactory); + super(module, configurationFactory, name); } + @Override protected ModuleBasedConfiguration createInstance() { return new PythonDocTestRunConfiguration(getConfigurationModule(), getFactory(), getName()); } + @Override + public SettingsEditor getConfigurationEditor() { + return new PythonDocTestRunConfigurationEditor(getProject(), this); + } + + @Override + public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment env) throws ExecutionException { + return new PythonDocTestCommandLineState(this, env); + } + @Override public void readExternal(Element element) throws InvalidDataException { super.readExternal(element); - myScriptName = JDOMExternalizerUtil.readField(element, "SCRIPT_NAME"); - myClassName = JDOMExternalizerUtil.readField(element, "CLASS_NAME"); - myMethodName = JDOMExternalizerUtil.readField(element, "METHOD_NAME"); - myFolderName = JDOMExternalizerUtil.readField(element, "FOLDER_NAME"); myPattern = JDOMExternalizerUtil.readField(element, "PATTERN"); - - try { - myTestType = TestType.valueOf(JDOMExternalizerUtil.readField(element, "TEST_TYPE")); - } - catch (IllegalArgumentException e) { - myTestType = TestType.TEST_SCRIPT; // safe default - } } @Override public void writeExternal(Element element) throws WriteExternalException { super.writeExternal(element); - - JDOMExternalizerUtil.writeField(element, "SCRIPT_NAME", myScriptName); - JDOMExternalizerUtil.writeField(element, "CLASS_NAME", myClassName); - JDOMExternalizerUtil.writeField(element, "METHOD_NAME", myMethodName); - JDOMExternalizerUtil.writeField(element, "FOLDER_NAME", myFolderName); JDOMExternalizerUtil.writeField(element, "PATTERN", myPattern); - JDOMExternalizerUtil.writeField(element, "TEST_TYPE", myTestType.toString()); } - public SettingsEditor getConfigurationEditor() { - return new PythonDocTestRunConfigurationEditor(getProject(), this); - } - - public AbstractPythonRunConfigurationParams getBaseParams() { - return this; - } - - public String getClassName() { - return myClassName; - } - - public void setClassName(String className) { - myClassName = className; + public static void copyParams(PythonDocTestRunConfigurationParams source, PythonDocTestRunConfigurationParams target) { + copyParams(source.getTestRunConfigurationParams(), target.getTestRunConfigurationParams()); + target.setPattern(source.getPattern()); } public String getPattern() { @@ -89,70 +64,6 @@ public class PythonDocTestRunConfiguration extends AbstractPythonRunConfiguratio myPattern = pattern; } - public String getFolderName() { - return myFolderName; - } - - public void setFolderName(String folderName) { - myFolderName = folderName; - } - - public String getScriptName() { - return myScriptName; - } - - public void setScriptName(String scriptName) { - myScriptName = scriptName; - } - - public String getMethodName() { - return myMethodName; - } - - public void setMethodName(String methodName) { - myMethodName = methodName; - } - - public TestType getTestType() { - return myTestType; - } - - public void setTestType(TestType testType) { - myTestType = testType; - } - - public enum TestType { - TEST_FOLDER, - TEST_SCRIPT, - TEST_CLASS, - TEST_METHOD, - TEST_FUNCTION,} - - @Override - public void checkConfiguration() throws RuntimeConfigurationException { - super.checkConfiguration(); - - if (StringUtil.isEmptyOrSpaces(myFolderName) && myTestType == TestType.TEST_FOLDER) { - throw new RuntimeConfigurationError(PyBundle.message("runcfg.unittest.no_folder_name")); - } - - if (StringUtil.isEmptyOrSpaces(getScriptName()) && myTestType != TestType.TEST_FOLDER) { - throw new RuntimeConfigurationError(PyBundle.message("runcfg.unittest.no_script_name")); - } - - if (StringUtil.isEmptyOrSpaces(myClassName) && (myTestType == TestType.TEST_METHOD || myTestType == TestType.TEST_CLASS)) { - throw new RuntimeConfigurationError(PyBundle.message("runcfg.unittest.no_class_name")); - } - - if (StringUtil.isEmptyOrSpaces(myMethodName) && (myTestType == TestType.TEST_METHOD || myTestType == TestType.TEST_FUNCTION)) { - throw new RuntimeConfigurationError(PyBundle.message("runcfg.unittest.no_method_name")); - } - } - - public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment env) throws ExecutionException { - return new PythonDocTestCommandLineState(this, env); - } - @Override public String suggestedName() { switch (myTestType) { @@ -170,43 +81,4 @@ public class PythonDocTestRunConfiguration extends AbstractPythonRunConfiguratio throw new IllegalStateException("Unknown test type: " + myTestType); } } - - public boolean compareSettings(PythonDocTestRunConfiguration cfg) { - if (cfg == null) return false; - - if (getTestType() != cfg.getTestType()) return false; - - switch (getTestType()) { - case TEST_FOLDER: - return getFolderName().equals(cfg.getFolderName()); - case TEST_SCRIPT: - return getScriptName().equals(cfg.getScriptName()) && - getWorkingDirectory().equals(cfg.getWorkingDirectory()); - case TEST_CLASS: - return getScriptName().equals(cfg.getScriptName()) && - getWorkingDirectory().equals(cfg.getWorkingDirectory()) && - getClassName().equals(cfg.getClassName()); - case TEST_METHOD: - return getScriptName().equals(cfg.getScriptName()) && - getWorkingDirectory().equals(cfg.getWorkingDirectory()) && - getClassName().equals(cfg.getClassName()) && - getMethodName().equals(cfg.getMethodName()); - case TEST_FUNCTION: - return getScriptName().equals(cfg.getScriptName()) && - getWorkingDirectory().equals(cfg.getWorkingDirectory()) && - getMethodName().equals(cfg.getMethodName()); - default: - throw new IllegalStateException("Unknown test type: " + getTestType()); - } - } - - public static void copyParams(PythonDocTestRunConfigurationParams source, PythonDocTestRunConfigurationParams target) { - AbstractPythonRunConfiguration.copyParams(source.getBaseParams(), target.getBaseParams()); - target.setScriptName(source.getScriptName()); - target.setClassName(source.getClassName()); - target.setFolderName(source.getFolderName()); - target.setMethodName(source.getMethodName()); - target.setTestType(source.getTestType()); - target.setPattern(source.getPattern()); - } } diff --git a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationForm.form b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationForm.form index 443458e91422..8376d4abf5ef 100644 --- a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationForm.form +++ b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationForm.form @@ -1,128 +1,20 @@
- + - + - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationForm.java b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationForm.java index c4cc54ee484e..7bfe534a94bd 100644 --- a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationForm.java +++ b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationForm.java @@ -1,231 +1,42 @@ package com.jetbrains.python.testing.doctest; import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.LabeledComponent; -import com.intellij.openapi.ui.TextFieldWithBrowseButton; -import com.intellij.openapi.util.Ref; -import com.intellij.openapi.util.io.FileUtil; -import com.jetbrains.python.PyBundle; -import com.jetbrains.python.run.AbstractPyCommonOptionsForm; -import com.jetbrains.python.run.AbstractPythonRunConfigurationParams; -import com.jetbrains.python.run.PyCommonOptionsFormFactory; -import com.jetbrains.python.run.PythonRunConfigurationFormUtil; +import com.jetbrains.python.testing.AbstractPythonTestRunConfigurationParams; +import com.jetbrains.python.testing.PythonTestRunConfigurationForm; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import static com.intellij.openapi.util.io.FileUtil.toSystemIndependentName; -import static com.jetbrains.python.testing.doctest.PythonDocTestRunConfiguration.TestType; /** * User: catherine */ public class PythonDocTestRunConfigurationForm implements PythonDocTestRunConfigurationParams { + private JPanel myTestsPlaceHolder; private JPanel myRootPanel; - private LabeledComponent myTestClassComponent; - private LabeledComponent myTestMethodComponent; - private LabeledComponent myTestFolderComponent; - private LabeledComponent myTestScriptComponent; - private JRadioButton myAllInFolderRB; - private JRadioButton myTestScriptRB; - private JRadioButton myTestClassRB; - private JRadioButton myTestMethodRB; - private JPanel myCommonOptionsPlaceholder; - private LabeledComponent myPatternComponent; - private JRadioButton myTestFunctionRB; - private TextFieldWithBrowseButton myTestFolderTextField; - private TextFieldWithBrowseButton myTestScriptTextField; - private JTextField myTestMethodTextField; - private JTextField myTestClassTextField; - private JTextField myPatternTextField; + private final PythonTestRunConfigurationForm myTestRunConfigurationForm; - private final Project myProject; - private final AbstractPyCommonOptionsForm myCommonOptionsForm; public PythonDocTestRunConfigurationForm(final Project project, final PythonDocTestRunConfiguration configuration) { - myProject = project; - - myCommonOptionsForm = PyCommonOptionsFormFactory.getInstance().createForm(configuration); - myCommonOptionsPlaceholder.add(myCommonOptionsForm.getMainPanel(), BorderLayout.CENTER); - - initComponents(); + myTestRunConfigurationForm = new PythonTestRunConfigurationForm(project, configuration); + myTestsPlaceHolder.add(myTestRunConfigurationForm.getPanel(), BorderLayout.CENTER); } - - public AbstractPythonRunConfigurationParams getBaseParams() { - return myCommonOptionsForm; - } - - private void initComponents() { - - final ActionListener testTypeListener = new ActionListener() { - public void actionPerformed(final ActionEvent e) { - setTestType(getTestType()); - } - }; - - myAllInFolderRB.addActionListener(testTypeListener); - myTestScriptRB.addActionListener(testTypeListener); - myTestClassRB.addActionListener(testTypeListener); - myTestMethodRB.addActionListener(testTypeListener); - myTestFunctionRB.addActionListener(testTypeListener); - } - - public String getClassName() { - return myTestClassTextField.getText().trim(); - } - - public void setClassName(String className) { - myTestClassTextField.setText(className); - } - public String getPattern() { - return myPatternTextField.getText().trim(); + return myTestRunConfigurationForm.getPattern(); } public void setPattern(String pattern) { - myPatternTextField.setText(pattern); + myTestRunConfigurationForm.setPattern(pattern); } - public String getFolderName() { - return toSystemIndependentName(myTestFolderTextField.getText().trim()); - } - - public void setFolderName(String folderName) { - myTestFolderTextField.setText(FileUtil.toSystemDependentName(folderName)); - } - - public String getScriptName() { - return toSystemIndependentName(myTestScriptTextField.getText().trim()); - } - - public void setScriptName(String scriptName) { - myTestScriptTextField.setText(FileUtil.toSystemDependentName(scriptName)); - } - - public String getMethodName() { - return myTestMethodTextField.getText().trim(); - } - - public void setMethodName(String methodName) { - myTestMethodTextField.setText(methodName); - } - - public TestType getTestType() { - if (myAllInFolderRB.isSelected()) { - return TestType.TEST_FOLDER; - } - else if (myTestScriptRB.isSelected()) { - return TestType.TEST_SCRIPT; - } - else if (myTestClassRB.isSelected()) { - return TestType.TEST_CLASS; - } - else if (myTestMethodRB.isSelected()) { - return TestType.TEST_METHOD; - } - else { - return TestType.TEST_FUNCTION; - } - } - - private static void setSelectedIfNeeded(boolean condition, JRadioButton rb) { - if (condition) { - rb.setSelected(true); - } - } - - public void setTestType(TestType testType) { - setSelectedIfNeeded(testType == TestType.TEST_FOLDER, myAllInFolderRB); - setSelectedIfNeeded(testType == TestType.TEST_SCRIPT, myTestScriptRB); - setSelectedIfNeeded(testType == TestType.TEST_CLASS, myTestClassRB); - setSelectedIfNeeded(testType == TestType.TEST_METHOD, myTestMethodRB); - setSelectedIfNeeded(testType == TestType.TEST_FUNCTION, myTestFunctionRB); - - myTestFolderComponent.setEnabled(testType == TestType.TEST_FOLDER); - myTestScriptComponent.setEnabled(testType != TestType.TEST_FOLDER); - myTestClassComponent.setEnabled(testType == TestType.TEST_CLASS || testType == TestType.TEST_METHOD); - myTestMethodComponent.setEnabled(testType == TestType.TEST_METHOD || testType == TestType.TEST_FUNCTION); - myPatternComponent.setEnabled(testType == TestType.TEST_FOLDER); + @Override + public AbstractPythonTestRunConfigurationParams getTestRunConfigurationParams() { + return myTestRunConfigurationForm; } public JComponent getPanel() { return myRootPanel; } - - private static LabeledComponent createTestFolderComponent(final Ref testsFolderTextFieldWrapper) { - final TextFieldWithBrowseButton testsFolderTextField = new TextFieldWithBrowseButton(); - testsFolderTextFieldWrapper.set(testsFolderTextField); - - LabeledComponent myComponent = new LabeledComponent(); - myComponent.setComponent(testsFolderTextField); - myComponent.setText(PyBundle.message("runcfg.unittest.dlg.folder_path")); - - return myComponent; - } - - public static LabeledComponent createScriptPathComponent(final Ref testScriptTextFieldWrapper, - final String text) { - final TextFieldWithBrowseButton testScriptTextField = new TextFieldWithBrowseButton(); - testScriptTextFieldWrapper.set(testScriptTextField); - - LabeledComponent myComponent = new LabeledComponent(); - myComponent.setComponent(testScriptTextField); - myComponent.setText(text); - - return myComponent; - } - - private LabeledComponent createTestClassComponent() { - myTestClassTextField = new JTextField(); - - LabeledComponent myComponent = new LabeledComponent(); - myComponent.setComponent(myTestClassTextField); - myComponent.setText(PyBundle.message("runcfg.unittest.dlg.class_label")); - - return myComponent; - } - - private LabeledComponent createTestMethodComponent() { - myTestMethodTextField = new JTextField(); - - LabeledComponent myComponent = new LabeledComponent(); - - myComponent.setComponent(myTestMethodTextField); - myComponent.setText(PyBundle.message("runcfg.unittest.dlg.method_label")); - - return myComponent; - } - - private void createUIComponents() { - myTestClassComponent = createTestClassComponent(); - myTestMethodComponent = createTestMethodComponent(); - myPatternComponent = createPatternComponent(); - - final Ref testsFolderTextFieldWrapper = new Ref(); - myTestFolderComponent = createTestFolderComponent(testsFolderTextFieldWrapper); - myTestFolderTextField = testsFolderTextFieldWrapper.get(); - String title = PyBundle.message("runcfg.unittest.dlg.select.folder.path"); - PythonRunConfigurationFormUtil.addFolderChooser(title, myTestFolderTextField, myProject); - - final Ref testScriptTextFieldWrapper = new Ref(); - myTestScriptComponent = createScriptPathComponent(testScriptTextFieldWrapper, PyBundle.message("runcfg.unittest.dlg.folder_path")); - myTestScriptTextField = testScriptTextFieldWrapper.get(); - title = PyBundle.message("runcfg.unittest.dlg.select.script.path"); - PythonRunConfigurationFormUtil.addFileChooser(title, myTestScriptTextField, myProject); - } - - private LabeledComponent createPatternComponent() { - myPatternTextField = new JTextField(); - - LabeledComponent myComponent = new LabeledComponent(); - myComponent.setComponent(myPatternTextField); - myComponent.setText(PyBundle.message("runcfg.unittest.dlg.pattern")); - - return myComponent; - } } diff --git a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationParams.java b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationParams.java index ac482f1e9c0b..5309236cec9e 100644 --- a/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationParams.java +++ b/python/src/com/jetbrains/python/testing/doctest/PythonDocTestRunConfigurationParams.java @@ -1,29 +1,13 @@ package com.jetbrains.python.testing.doctest; -import com.jetbrains.python.run.AbstractPythonRunConfigurationParams; +import com.jetbrains.python.testing.AbstractPythonTestRunConfigurationParams; /** * User: catherine */ public interface PythonDocTestRunConfigurationParams { - AbstractPythonRunConfigurationParams getBaseParams(); - - String getClassName(); - void setClassName(String className); - - String getFolderName(); - void setFolderName(String folderName); - - String getScriptName(); - void setScriptName(String scriptName); - - String getMethodName(); - void setMethodName(String methodName); - - PythonDocTestRunConfiguration.TestType getTestType(); - void setTestType(PythonDocTestRunConfiguration.TestType testType); - String getPattern(); void setPattern(String pattern); + AbstractPythonTestRunConfigurationParams getTestRunConfigurationParams(); }