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;
}
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());
}
}
}
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);
}
}
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());
}
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;
}
}
}));
}
Aggregations