IG: fix lastStatementIsCallToMethodWithAssertion() logic

This commit is contained in:
Bas Leijdekkers
2015-08-14 23:07:27 +02:00
parent aedbc84de1
commit acf4053f9c
4 changed files with 130 additions and 56 deletions
@@ -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;
}
@@ -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();
}
}
@@ -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 <warning descr="JUnit test method 'test()' contains no assertions">test</warning>()
{
}
@Test
public void <warning descr="JUnit test method 'fourOhTest()' contains no assertions">fourOhTest</warning>()
{
}
@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);
}
}
@@ -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<? extends java.lang.Throwable> 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();
}
}