Search in sources :

Example 1 with TestFramework

use of com.intellij.testIntegration.TestFramework in project kotlin by JetBrains.

the class TestFrameworkListCellRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    Component result = super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus);
    if (value == null)
        return result;
    TestFramework framework = (TestFramework) value;
    setIcon(framework.getIcon());
    setText(framework.getName());
    return result;
}
Also used : TestFramework(com.intellij.testIntegration.TestFramework)

Example 2 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class GenerateMissedTestsAction method generateMissedTests.

private static void generateMissedTests(final PsiClass testClass, final PsiClass srcClass, Editor srcEditor) {
    if (testClass != null) {
        final TestFramework framework = TestFrameworks.detectFramework(testClass);
        if (framework != null) {
            final Project project = testClass.getProject();
            final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, testClass.getContainingFile(), testClass);
            if (!FileModificationService.getInstance().preparePsiElementsForWrite(testClass))
                return;
            final MissedTestsDialog dialog = new MissedTestsDialog(project, srcClass, testClass, framework);
            if (dialog.showAndGet()) {
                WriteCommandAction.runWriteCommandAction(project, () -> JavaTestGenerator.addTestMethods(editor, testClass, srcClass, framework, dialog.getSelectedMethods(), false, false));
            }
        } else {
            HintManager.getInstance().showErrorHint(srcEditor, "Failed to detect test framework for " + testClass.getQualifiedName());
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) TestFramework(com.intellij.testIntegration.TestFramework) Editor(com.intellij.openapi.editor.Editor)

Example 3 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class JavaTestGenerator method addTestMethods.

public static void addTestMethods(Editor editor, PsiClass targetClass, @Nullable PsiClass sourceClass, final TestFramework descriptor, Collection<MemberInfo> methods, boolean generateBefore, boolean generateAfter) throws IncorrectOperationException {
    final Set<String> existingNames = new HashSet<>();
    PsiMethod anchor = null;
    if (generateBefore && descriptor.findSetUpMethod(targetClass) == null) {
        anchor = generateMethod(TestIntegrationUtils.MethodKind.SET_UP, descriptor, targetClass, sourceClass, editor, null, existingNames, null);
    }
    if (generateAfter && descriptor.findTearDownMethod(targetClass) == null) {
        anchor = generateMethod(TestIntegrationUtils.MethodKind.TEAR_DOWN, descriptor, targetClass, sourceClass, editor, null, existingNames, anchor);
    }
    final Template template = TestIntegrationUtils.createTestMethodTemplate(TestIntegrationUtils.MethodKind.TEST, descriptor, targetClass, sourceClass, null, true, existingNames);
    JVMElementFactory elementFactory = JVMElementFactories.getFactory(targetClass.getLanguage(), targetClass.getProject());
    final String prefix = elementFactory != null ? elementFactory.createMethodFromText(template.getTemplateText(), targetClass).getName() : "";
    existingNames.addAll(ContainerUtil.map(targetClass.getMethods(), method -> StringUtil.decapitalize(StringUtil.trimStart(method.getName(), prefix))));
    for (MemberInfo m : methods) {
        anchor = generateMethod(TestIntegrationUtils.MethodKind.TEST, descriptor, targetClass, sourceClass, editor, m.getMember().getName(), existingNames, anchor);
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) TestFramework(com.intellij.testIntegration.TestFramework) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) CodeInsightUtil(com.intellij.codeInsight.CodeInsightUtil) HashSet(java.util.HashSet) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) Messages(com.intellij.openapi.ui.Messages) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) FileModificationService(com.intellij.codeInsight.FileModificationService) Properties(java.util.Properties) IncorrectOperationException(com.intellij.util.IncorrectOperationException) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) Template(com.intellij.codeInsight.template.Template) IdeDocumentHistory(com.intellij.openapi.fileEditor.ex.IdeDocumentHistory) Editor(com.intellij.openapi.editor.Editor) TestIntegrationUtils(com.intellij.testIntegration.TestIntegrationUtils) Nullable(org.jetbrains.annotations.Nullable) GlobalSearchScopesCore(com.intellij.psi.search.GlobalSearchScopesCore) FileTemplateDescriptor(com.intellij.ide.fileTemplates.FileTemplateDescriptor) FileTemplateUtil(com.intellij.ide.fileTemplates.FileTemplateUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) PostprocessReformattingAspect(com.intellij.psi.impl.source.PostprocessReformattingAspect) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) HashSet(java.util.HashSet) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Template(com.intellij.codeInsight.template.Template)

Example 4 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class JavaTestGenerator method createTestClass.

@Nullable
private static PsiClass createTestClass(CreateTestDialog d) {
    final TestFramework testFrameworkDescriptor = d.getSelectedTestFrameworkDescriptor();
    final FileTemplateDescriptor fileTemplateDescriptor = TestIntegrationUtils.MethodKind.TEST_CLASS.getFileTemplateDescriptor(testFrameworkDescriptor);
    final PsiDirectory targetDirectory = d.getTargetDirectory();
    final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(targetDirectory);
    if (aPackage != null) {
        final GlobalSearchScope scope = GlobalSearchScopesCore.directoryScope(targetDirectory, false);
        final PsiClass[] classes = aPackage.findClassByShortName(d.getClassName(), scope);
        if (classes.length > 0) {
            if (!FileModificationService.getInstance().preparePsiElementForWrite(classes[0])) {
                return null;
            }
            return classes[0];
        }
    }
    if (fileTemplateDescriptor != null) {
        final PsiClass classFromTemplate = createTestClassFromCodeTemplate(d, fileTemplateDescriptor, targetDirectory);
        if (classFromTemplate != null) {
            return classFromTemplate;
        }
    }
    return JavaDirectoryService.getInstance().createClass(targetDirectory, d.getClassName());
}
Also used : TestFramework(com.intellij.testIntegration.TestFramework) FileTemplateDescriptor(com.intellij.ide.fileTemplates.FileTemplateDescriptor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class JavaTestGenerator method generateTest.

public PsiElement generateTest(final Project project, final CreateTestDialog d) {
    return PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> ApplicationManager.getApplication().runWriteAction(new Computable<PsiElement>() {

        public PsiElement compute() {
            try {
                IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
                PsiClass targetClass = createTestClass(d);
                if (targetClass == null) {
                    return null;
                }
                final TestFramework frameworkDescriptor = d.getSelectedTestFrameworkDescriptor();
                final String defaultSuperClass = frameworkDescriptor.getDefaultSuperClass();
                final String superClassName = d.getSuperClassName();
                if (!Comparing.strEqual(superClassName, defaultSuperClass)) {
                    addSuperClass(targetClass, project, superClassName);
                }
                Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.getContainingFile(), targetClass);
                addTestMethods(editor, targetClass, d.getTargetClass(), frameworkDescriptor, d.getSelectedMethods(), d.shouldGeneratedBefore(), d.shouldGeneratedAfter());
                return targetClass;
            } catch (IncorrectOperationException e) {
                showErrorLater(project, d.getClassName());
                return null;
            }
        }
    }));
}
Also used : TestFramework(com.intellij.testIntegration.TestFramework) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) Computable(com.intellij.openapi.util.Computable)

Aggregations

TestFramework (com.intellij.testIntegration.TestFramework)15 Editor (com.intellij.openapi.editor.Editor)3 PsiClass (com.intellij.psi.PsiClass)3 Nullable (org.jetbrains.annotations.Nullable)3 FileTemplateDescriptor (com.intellij.ide.fileTemplates.FileTemplateDescriptor)2 Project (com.intellij.openapi.project.Project)2 Computable (com.intellij.openapi.util.Computable)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 JavaTestFramework (com.intellij.testIntegration.JavaTestFramework)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)1 CodeInsightUtil (com.intellij.codeInsight.CodeInsightUtil)1 FileModificationService (com.intellij.codeInsight.FileModificationService)1 Template (com.intellij.codeInsight.template.Template)1 JUnit5Framework (com.intellij.execution.junit.JUnit5Framework)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)1 FileTemplateUtil (com.intellij.ide.fileTemplates.FileTemplateUtil)1 PropertiesComponent (com.intellij.ide.util.PropertiesComponent)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1