Commit a90c9f96 authored by Anna.Kozlova's avatar Anna.Kozlova
Browse files

constant assertion inspection for testng

parent dcd5dd41
Showing with 126 additions and 62 deletions
+126 -62
/*
* Copyright 2000-2016 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.psi.PsiExpression;
import com.intellij.psi.PsiExpressionList;
import com.intellij.psi.PsiMethodCallExpression;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
public abstract class ConstantAssertArgumentInspectionBase extends BaseInspection {
@NonNls
private static final Set<String> ASSERT_METHODS = new HashSet<>();
static {
ASSERT_METHODS.add("assertTrue");
ASSERT_METHODS.add("assertFalse");
ASSERT_METHODS.add("assertNull");
ASSERT_METHODS.add("assertNotNull");
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"constant.junit.assert.argument.problem.descriptor");
}
protected abstract boolean checkTestNG();
@Override
public BaseInspectionVisitor buildVisitor() {
return new ConstantAssertArgumentVisitor();
}
private class ConstantAssertArgumentVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
AssertHint assertHint = AssertHint.create(expression, methodName -> ASSERT_METHODS.contains(methodName) ? 1 : null, checkTestNG());
if (assertHint == null) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression argument = arguments[assertHint.getArgIndex()];
if (!PsiUtil.isConstantExpression(argument)) {
return;
}
registerError(argument);
}
}
}
......@@ -15,79 +15,21 @@
*/
package com.siyeh.ig.junit;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
public class ConstantJUnitAssertArgumentInspection extends BaseInspection {
@NonNls
private static final Set<String> ASSERT_METHODS = new HashSet<>();
static {
ASSERT_METHODS.add("assertTrue");
ASSERT_METHODS.add("assertFalse");
ASSERT_METHODS.add("assertNull");
ASSERT_METHODS.add("assertNotNull");
}
public class ConstantJUnitAssertArgumentInspection extends ConstantAssertArgumentInspectionBase {
@Override
@Nls
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"constant.junit.assert.argument.display.name");
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"constant.junit.assert.argument.problem.descriptor");
return InspectionGadgetsBundle.message("constant.junit.assert.argument.display.name");
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new ConstantJUnitAssertArgumentVisitor();
}
private static class ConstantJUnitAssertArgumentVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
@NonNls final String methodName = methodExpression.getReferenceName();
if (!ASSERT_METHODS.contains(methodName)) {
return;
}
final PsiMethod method = expression.resolveMethod();
if (method == null) {
return;
}
final boolean messageOnFirstPosition = AssertHint.isMessageOnFirstPosition(method, false);
final boolean messageOnLastPosition = AssertHint.isMessageOnLastPosition(method, false);
if (!messageOnFirstPosition && !messageOnLastPosition) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression argument = arguments[messageOnFirstPosition ? arguments.length - 1 : 0];
if (!PsiUtil.isConstantExpression(argument)) {
return;
}
registerError(argument);
}
protected boolean checkTestNG() {
return false;
}
}
......@@ -76,6 +76,8 @@
groupPath="Java" language="JAVA" shortName="SimplifiedTestNGAssertion" bundle="com.siyeh.InspectionGadgetsBundle"
key="simplifiable.testng.assertion.display.name" groupName="TestNG"
enabledByDefault="false" level="WARNING"/>
<localInspection groupPath="Java" language="JAVA" shortName="ConstantTestNGAssertArgument" displayName="Constant TestNG assert argument"
groupName="TestNG" enabledByDefault="false" level="WARNING" implementationClass="com.theoryinpractice.testng.inspection.ConstantTestNGAssertArgumentInspection"/>
</extensions>
<extensions defaultExtensionNs="com.theoryinpractice.testng">
<listener implementation="org.testng.TestNGTestDiscoveryListener"/>
......
/*
* Copyright 2000-2016 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.theoryinpractice.testng.inspection;
import com.siyeh.ig.junit.ConstantAssertArgumentInspectionBase;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class ConstantTestNGAssertArgumentInspection extends ConstantAssertArgumentInspectionBase {
@Override
protected boolean checkTestNG() {
return true;
}
@Nls
@NotNull
@Override
public String getDisplayName() {
return "Constant TestNG assert argument";
}
}
<html>
<body>
Reports constant arguments to TestNG assertTrue, assertFalse,
assertNull and assertNotNull method calls. Calls to these methods with such
constant arguments will either always fail or always succeed.
Such statements can easily be left over after refactoring and are probably not intended.
<small>New in 2017.1</small>
</body>
</html>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment