From acf4053f9c1315a56877cdbfffd33dab2ca004f2 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 14 Aug 2015 23:07:27 +0200 Subject: [PATCH] IG: fix lastStatementIsCallToMethodWithAssertion() logic --- ...tMethodWithoutAssertionInspectionBase.java | 14 +--- .../TestMethodWithNoAssertionsInspection.java | 45 ------------- .../TestMethodWithoutAssertion.java | 60 +++++++++++++++++ ...tMethodWithoutAssertionInspectionTest.java | 67 +++++++++++++++++++ 4 files changed, 130 insertions(+), 56 deletions(-) delete mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/junit/TestMethodWithNoAssertionsInspection.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_without_assertion/TestMethodWithoutAssertion.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionBase.java index c8bfd68dd073..d7b8a4cc1337 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionBase.java @@ -19,6 +19,7 @@ import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.WriteExternalException; import com.intellij.psi.*; import com.intellij.psi.util.InheritanceUtil; +import com.intellij.psi.util.PsiTreeUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -31,7 +32,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -112,11 +112,7 @@ public class TestMethodWithoutAssertionInspectionBase extends BaseInspection { return false; } } - if (pattern == null) { - return false; - } - final Matcher matcher = pattern.matcher(methodName); - return matcher.matches(); + return pattern.matcher(methodName).matches(); } private class TestMethodWithoutAssertionVisitor @@ -145,11 +141,7 @@ public class TestMethodWithoutAssertionInspectionBase extends BaseInspection { if (body == null) { return false; } - final PsiStatement[] statements = body.getStatements(); - if (statements.length <= 0) { - return false; - } - final PsiStatement lastStatement = statements[0]; + final PsiStatement lastStatement = PsiTreeUtil.getPrevSiblingOfType(body.getLastChild(), PsiStatement.class); if (!(lastStatement instanceof PsiExpressionStatement)) { return false; } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/TestMethodWithNoAssertionsInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/TestMethodWithNoAssertionsInspection.java deleted file mode 100644 index 74d3208d0533..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/TestMethodWithNoAssertionsInspection.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.siyeh.igtest.junit; - -import junit.framework.TestCase; -import org.junit.Test; -import org.junit.Assert; - -public class TestMethodWithNoAssertionsInspection extends TestCase -{ - public TestMethodWithNoAssertionsInspection() - { - } - - public void test() - { - - } - - @Test - public void fourOhTest() - { - - } - - @Test(expected = Exception.class) - public void fourOhTestWithExpected() - { - - } - - @Test - public void fourOhTest2() - { - Assert.assertTrue(true); - } - - public void test2() - { - assertTrue(true); - } - - public void test3() - { - fail(); - } -} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_without_assertion/TestMethodWithoutAssertion.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_without_assertion/TestMethodWithoutAssertion.java new file mode 100644 index 000000000000..7660fa6d4045 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_without_assertion/TestMethodWithoutAssertion.java @@ -0,0 +1,60 @@ +package com.siyeh.igtest.junit; + +import junit.framework.TestCase; +import org.junit.Test; +import org.junit.Assert; + +public class TestMethodWithoutAssertion extends TestCase +{ + public TestMethodWithoutAssertion() + { + } + + public void test() + { + + } + + @Test + public void fourOhTest() + { + + } + + @Test(expected = Exception.class) + public void fourOhTestWithExpected() + { + + } + + @Test + public void fourOhTest2() + { + Assert.assertTrue(true); + } + + public void test2() + { + assertTrue(true); + } + + public void test3() + { + fail(); + } + + @Test + public void delegateOnly() { + check(); + } + + @Test + public void delegateAdditionally() { + final int i = 9; + check(); + } + + private void check() { + Assert.assertTrue(true); + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionTest.java new file mode 100644 index 000000000000..4178db05223e --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodWithoutAssertionInspectionTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.siyeh.ig.junit; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class TestMethodWithoutAssertionInspectionTest extends LightInspectionTestCase { + + public void testTestMethodWithoutAssertion() { + doTest(); + } + + @Override + protected String[] getEnvironmentClasses() { + return new String[] { + "package org.junit;" + + "import java.lang.annotation.ElementType;" + + "import java.lang.annotation.Retention;" + + "import java.lang.annotation.RetentionPolicy;" + + "import java.lang.annotation.Target;" + + "@Retention(RetentionPolicy.RUNTIME)" + + "@Target({ElementType.METHOD})" + + "public @interface Test {" + + " Class expected() default org.junit.Test.None.class;" + + "}", + + "package org.junit;" + + "public class Assert {" + + " static public void assertTrue(boolean condition) {}" + + "}", + + "package junit.framework;" + + "public class Assert {" + + " static public void assertTrue(boolean condition) {}" + + " static public void fail() {}" + + "}", + + "package junit.framework;" + + "public abstract class TestCase extends Assert {}" + }; + + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new TestMethodWithoutAssertionInspection(); + } +} \ No newline at end of file