mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-163195 - added a quick fix for message creation
GitOrigin-RevId: aad3d98ca868875a6e27e2631ef41bb7df90584e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
1d5a54d369
commit
112d905b23
+2
-1
@@ -1073,7 +1073,8 @@ manual.array.copy.problem.descriptor=Manual array copy #loc
|
||||
manual.array.to.collection.copy.problem.descriptor=Manual array to collection copy #loc
|
||||
call.to.simple.getter.in.class.problem.descriptor=Call to simple getter <code>#ref()</code> from within class #loc
|
||||
call.to.simple.setter.in.class.problem.descriptor=Call to simple setter <code>#ref()</code> from within class #loc
|
||||
asserts.without.messages.problem.descriptor=<code>#ref()</code> without message #loc
|
||||
assert.without.message.problem.descriptor=<code>#ref()</code> without message #loc
|
||||
assert.without.message.quick.fix.family.name=Add error message
|
||||
test.case.with.constructor.problem.descriptor=Initialization logic in constructor <code>#ref()</code> instead of 'setUp()' #loc
|
||||
test.case.with.constructor.problem.descriptor.initializer=Initialization logic in initializer instead of 'setUp()'
|
||||
misordered.assert.equals.arguments.problem.descriptor=Arguments to <code>#ref()</code> in wrong order #loc
|
||||
|
||||
+36
-3
@@ -15,12 +15,19 @@
|
||||
*/
|
||||
package com.siyeh.ig.testFrameworks;
|
||||
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiMethodCallExpression;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.util.IntentionFamilyName;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AssertWithoutMessageInspection extends BaseInspection {
|
||||
|
||||
@@ -32,7 +39,7 @@ public class AssertWithoutMessageInspection extends BaseInspection {
|
||||
@Override
|
||||
@NotNull
|
||||
protected String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message("asserts.without.messages.problem.descriptor");
|
||||
return InspectionGadgetsBundle.message("assert.without.message.problem.descriptor");
|
||||
}
|
||||
|
||||
private static class AssertionsWithoutMessagesVisitor extends BaseInspectionVisitor {
|
||||
@@ -50,4 +57,30 @@ public class AssertWithoutMessageInspection extends BaseInspection {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new InspectionGadgetsFix() {
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
PsiMethodCallExpression methodCallExpr = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethodCallExpression.class);
|
||||
if (methodCallExpr == null) return;
|
||||
|
||||
PsiExpressionList methodArgs = methodCallExpr.getArgumentList();
|
||||
PsiExpression[] methodArgExprs = methodArgs.getExpressions();
|
||||
PsiExpression firstMethodArgExpr = methodArgExprs.length > 0 ? methodArgExprs[0] : null;
|
||||
PsiExpression newMessageExpr = JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText("\"\"", methodCallExpr);
|
||||
PsiElement createdMessageExpr = methodArgs.addBefore(newMessageExpr, firstMethodArgExpr);
|
||||
|
||||
final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
|
||||
if (editor == null) return;
|
||||
editor.getCaretModel().moveToOffset(createdMessageExpr.getTextOffset() + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @IntentionFamilyName @NotNull String getFamilyName() {
|
||||
return InspectionGadgetsBundle.message("assert.without.message.quick.fix.family.name");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+47
@@ -28,6 +28,48 @@ public class JUnitAssertsWithoutMessagesInspectionTest extends LightJavaInspecti
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testQuickFixAssertEquals() {
|
||||
doTest("import org.junit.Test;\n" +
|
||||
"import static org.junit.Assert.*;\n" +
|
||||
|
||||
"class TestCase {\n" +
|
||||
" @Test\n" +
|
||||
" public void test() {\n" +
|
||||
" <warning descr=\"'assertEquals()' without message\"><caret>assertEquals</warning>(1, 1);\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
checkQuickFix("Add error message", "import org.junit.Test;\n" +
|
||||
"import static org.junit.Assert.*;\n" +
|
||||
|
||||
"class TestCase {\n" +
|
||||
" @Test\n" +
|
||||
" public void test() {\n" +
|
||||
" assertEquals(\"<caret>\", 1, 1);\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
}
|
||||
|
||||
public void testQuickFixFail() {
|
||||
doTest("import org.junit.Test;\n" +
|
||||
"import static org.junit.Assert.*;\n" +
|
||||
|
||||
"class TestCase {\n" +
|
||||
" @Test\n" +
|
||||
" public void test() {\n" +
|
||||
" <warning descr=\"'fail()' without message\"><caret>fail</warning>();\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
checkQuickFix("Add error message", "import org.junit.Test;\n" +
|
||||
"import static org.junit.Assert.*;\n" +
|
||||
|
||||
"class TestCase {\n" +
|
||||
" @Test\n" +
|
||||
" public void test() {\n" +
|
||||
" fail(\"<caret>\");\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit_asserts_without_messages";
|
||||
@@ -48,6 +90,11 @@ public class JUnitAssertsWithoutMessagesInspectionTest extends LightJavaInspecti
|
||||
"public class Assert {" +
|
||||
" static public void assertEquals(double expected, double actual, double delta) {}" +
|
||||
" static public void assertEquals(Object expected, Object actual){}" +
|
||||
" static public void assertEquals(String message, Object expected, Object actual){}" +
|
||||
" static public void assertSame(Object expected, Object actual){}" +
|
||||
" static public void assertSame(String message, Object expected, Object actual){}" +
|
||||
" static public void fail(){}" +
|
||||
" static public void fail(String message){}" +
|
||||
"}",
|
||||
|
||||
"package org.junit.jupiter.api;\n" +
|
||||
|
||||
Reference in New Issue
Block a user