python doctests refactored

This commit is contained in:
Ekaterina Tuzova
2010-12-30 17:50:33 +03:00
parent 62aaa77b2b
commit c3c54b065b
6 changed files with 73 additions and 667 deletions
@@ -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();
@@ -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;
}
}
@@ -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<? extends RunConfiguration> 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<? extends RunConfiguration> 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());
}
}
@@ -1,128 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.jetbrains.python.testing.doctest.PythonDocTestRunConfigurationForm">
<grid id="27dc6" binding="myRootPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="myRootPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="139" y="101" width="754" height="400"/>
<xy x="139" y="101" width="24" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="6d5f3" binding="myCommonOptionsPlaceholder" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<grid id="f8bdd" layout-manager="GridLayoutManager" row-count="1" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<grid id="6d5f3" binding="myTestsPlaceHolder" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="11d0e" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.test_type_title"/>
</properties>
</component>
<component id="814d9" class="javax.swing.JRadioButton" binding="myAllInFolderRB">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.all_in_folder_title"/>
</properties>
</component>
<component id="ed9cd" class="javax.swing.JRadioButton" binding="myTestScriptRB">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.all_in_script_title"/>
</properties>
</component>
<component id="784c3" class="javax.swing.JRadioButton" binding="myTestClassRB">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.test_class_title"/>
</properties>
</component>
<component id="de052" class="javax.swing.JRadioButton" binding="myTestMethodRB">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.test_method_title"/>
</properties>
</component>
<component id="5b2fd" class="javax.swing.JRadioButton" binding="myTestFunctionRB">
<constraints>
<grid row="0" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.test_function_title"/>
</properties>
</component>
</children>
</grid>
<grid id="16300" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="5" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Tests:"/>
<children>
<component id="fff6c" class="com.intellij.openapi.ui.LabeledComponent" binding="myPatternComponent" custom-create="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.pattern"/>
</properties>
</component>
<component id="54301" class="com.intellij.openapi.ui.LabeledComponent" binding="myTestScriptComponent" custom-create="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.test_script_label"/>
</properties>
</component>
<component id="9ac22" class="com.intellij.openapi.ui.LabeledComponent" binding="myTestMethodComponent" custom-create="true">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.method_label"/>
</properties>
</component>
<component id="1aebe" class="com.intellij.openapi.ui.LabeledComponent" binding="myTestClassComponent" custom-create="true">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.class_label"/>
</properties>
</component>
<component id="2c797" class="com.intellij.openapi.ui.LabeledComponent" binding="myTestFolderComponent" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="runcfg.unittest.dlg.folder_path"/>
</properties>
</component>
</children>
<children/>
</grid>
</children>
</grid>
@@ -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<TextFieldWithBrowseButton> testsFolderTextFieldWrapper) {
final TextFieldWithBrowseButton testsFolderTextField = new TextFieldWithBrowseButton();
testsFolderTextFieldWrapper.set(testsFolderTextField);
LabeledComponent<TextFieldWithBrowseButton> myComponent = new LabeledComponent<TextFieldWithBrowseButton>();
myComponent.setComponent(testsFolderTextField);
myComponent.setText(PyBundle.message("runcfg.unittest.dlg.folder_path"));
return myComponent;
}
public static LabeledComponent<TextFieldWithBrowseButton> createScriptPathComponent(final Ref<TextFieldWithBrowseButton> testScriptTextFieldWrapper,
final String text) {
final TextFieldWithBrowseButton testScriptTextField = new TextFieldWithBrowseButton();
testScriptTextFieldWrapper.set(testScriptTextField);
LabeledComponent<TextFieldWithBrowseButton> myComponent = new LabeledComponent<TextFieldWithBrowseButton>();
myComponent.setComponent(testScriptTextField);
myComponent.setText(text);
return myComponent;
}
private LabeledComponent createTestClassComponent() {
myTestClassTextField = new JTextField();
LabeledComponent<JTextField> myComponent = new LabeledComponent<JTextField>();
myComponent.setComponent(myTestClassTextField);
myComponent.setText(PyBundle.message("runcfg.unittest.dlg.class_label"));
return myComponent;
}
private LabeledComponent createTestMethodComponent() {
myTestMethodTextField = new JTextField();
LabeledComponent<JTextField> myComponent = new LabeledComponent<JTextField>();
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<TextFieldWithBrowseButton> testsFolderTextFieldWrapper = new Ref<TextFieldWithBrowseButton>();
myTestFolderComponent = createTestFolderComponent(testsFolderTextFieldWrapper);
myTestFolderTextField = testsFolderTextFieldWrapper.get();
String title = PyBundle.message("runcfg.unittest.dlg.select.folder.path");
PythonRunConfigurationFormUtil.addFolderChooser(title, myTestFolderTextField, myProject);
final Ref<TextFieldWithBrowseButton> testScriptTextFieldWrapper = new Ref<TextFieldWithBrowseButton>();
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<JTextField> myComponent = new LabeledComponent<JTextField>();
myComponent.setComponent(myPatternTextField);
myComponent.setText(PyBundle.message("runcfg.unittest.dlg.pattern"));
return myComponent;
}
}
@@ -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();
}