mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
IDEA-88124 Groovy: Code | Generate on a JUnit test class could suggest to create Test, SetUp and TearDown methods
This commit is contained in:
@@ -480,11 +480,12 @@ public class OverrideImplementUtil {
|
||||
properties.setProperty(FileTemplate.ATTRIBUTE_CALL_SUPER, callSuper(originalMethod, result));
|
||||
JavaTemplateUtil.setClassAndMethodNameProperties(properties, targetClass, result);
|
||||
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(originalMethod.getProject()).getElementFactory();
|
||||
JVMElementFactory factory = JVMElementFactories.getFactory(targetClass.getLanguage(), originalMethod.getProject());
|
||||
if (factory == null) factory = JavaPsiFacade.getInstance(originalMethod.getProject()).getElementFactory();
|
||||
@NonNls String methodText;
|
||||
try {
|
||||
String bodyText = template.getText(properties);
|
||||
if (!"".equals(bodyText)) bodyText += "\n";
|
||||
if (bodyText != null && !bodyText.isEmpty()) bodyText += "\n";
|
||||
methodText = "void foo () {\n" + bodyText + "}";
|
||||
methodText = FileTemplateUtil.indent(methodText, result.getProject(), fileType);
|
||||
} catch (Exception e) {
|
||||
@@ -769,14 +770,6 @@ public class OverrideImplementUtil {
|
||||
return aClass == null || !allowInterface && aClass.isInterface() ? null : aClass;
|
||||
}
|
||||
|
||||
private static PsiSubstitutor getContextSubstitutor(PsiClass aClass) {
|
||||
if (aClass instanceof PsiAnonymousClass) {
|
||||
return ((PsiAnonymousClass)aClass).getBaseClassType().resolveGenerics().getSubstitutor();
|
||||
}
|
||||
|
||||
return PsiSubstitutor.EMPTY;
|
||||
}
|
||||
|
||||
public static void overrideOrImplementMethodsInRightPlace(Editor editor1, PsiClass aClass, Collection<PsiMethodMember> members, boolean copyJavadoc) {
|
||||
boolean insert = CodeStyleSettingsManager.getSettings(aClass.getProject()).INSERT_OVERRIDE_ANNOTATION;
|
||||
overrideOrImplementMethodsInRightPlace(editor1, aClass, members, copyJavadoc, insert);
|
||||
|
||||
+13
-11
@@ -25,7 +25,6 @@ import com.intellij.codeInsight.hint.HintManager;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.popup.PopupChooserBuilder;
|
||||
import com.intellij.psi.*;
|
||||
@@ -34,14 +33,12 @@ import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.ui.components.JBList;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.SmartList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
|
||||
@@ -74,6 +71,17 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isValidForFile(Project project, Editor editor, PsiFile file) {
|
||||
if (file instanceof PsiCompiledElement) return false;
|
||||
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
|
||||
PsiClass targetClass = getTargetClass(editor, file);
|
||||
return targetClass != null && isValidForClass(targetClass);
|
||||
}
|
||||
|
||||
|
||||
protected boolean isValidFor(PsiClass targetClass, TestFramework framework) {
|
||||
return true;
|
||||
}
|
||||
@@ -145,13 +153,7 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
|
||||
PsiMethod method = generateDummyMethod(editor, file);
|
||||
if (method == null) return;
|
||||
|
||||
TestIntegrationUtils.runTestMethodTemplate(myMethodKind,
|
||||
framework,
|
||||
editor,
|
||||
targetClass,
|
||||
method,
|
||||
"name",
|
||||
false);
|
||||
TestIntegrationUtils.runTestMethodTemplate(myMethodKind, framework, editor, targetClass, method, "name", false);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
HintManager.getInstance().showErrorHint(editor, "Cannot generate method: " + e.getMessage());
|
||||
@@ -165,7 +167,7 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
|
||||
private PsiMethod generateDummyMethod(Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
List<GenerationInfo> members = new ArrayList<GenerationInfo>();
|
||||
|
||||
final PsiMethod method = TestIntegrationUtils.createDummyMethod(file.getProject());
|
||||
final PsiMethod method = TestIntegrationUtils.createDummyMethod(file);
|
||||
final PsiMethod[] result = new PsiMethod[1];
|
||||
|
||||
members.add(new GenerationInfoBase() {
|
||||
|
||||
@@ -233,9 +233,10 @@ public class TestIntegrationUtils {
|
||||
return template;
|
||||
}
|
||||
|
||||
public static PsiMethod createDummyMethod(Project project) {
|
||||
PsiElementFactory f = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
return f.createMethod("dummy", PsiType.VOID);
|
||||
public static PsiMethod createDummyMethod(@NotNull PsiElement context) {
|
||||
JVMElementFactory factory = JVMElementFactories.getFactory(context.getLanguage(), context.getProject());
|
||||
if (factory == null) factory = JavaPsiFacade.getElementFactory(context.getProject());
|
||||
return factory.createMethod("dummy", PsiType.VOID);
|
||||
}
|
||||
|
||||
public static List<TestFramework> findSuitableFrameworks(PsiClass targetClass) {
|
||||
|
||||
@@ -122,7 +122,7 @@ public class JavaTestGenerator implements TestGenerator {
|
||||
PsiClass targetClass,
|
||||
Editor editor,
|
||||
@Nullable String name) {
|
||||
PsiMethod method = (PsiMethod)targetClass.add(TestIntegrationUtils.createDummyMethod(targetClass.getProject()));
|
||||
PsiMethod method = (PsiMethod)targetClass.add(TestIntegrationUtils.createDummyMethod(targetClass));
|
||||
PsiDocumentManager.getInstance(targetClass.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument());
|
||||
TestIntegrationUtils.runTestMethodTemplate(methodKind, descriptor, editor, targetClass, method, name, true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user