junit 5: support implicit conversions, skip checks for explicit conversions

This commit is contained in:
Anna.Kozlova
2017-04-21 20:03:27 +02:00
parent 58fcabfd2b
commit 74f5ea3348
5 changed files with 45 additions and 8 deletions
@@ -33,4 +33,6 @@ public class JUnitCommonClassNames {
public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_ARGUMENTS = "org.junit.jupiter.params.provider.Arguments";
public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_SOURCE = "org.junit.jupiter.params.provider.CsvSource";
public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_FILE_SOURCE = "org.junit.jupiter.params.provider.CsvFileSource";
public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_ARGUMENTS_SOURCE = "org.junit.jupiter.params.provider.ArgumentsSource";
public static final String ORG_JUNIT_JUPITER_PARAMS_CONVERTER_CONVERT_WITH = "org.junit.jupiter.params.converter.ConvertWith";
}
@@ -15,6 +15,7 @@
*/
package com.intellij.execution.junit.codeInsight
import com.intellij.codeInsight.AnnotationUtil
import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil
import com.intellij.codeInsight.intention.QuickFixFactory
import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool
@@ -26,6 +27,7 @@ import com.intellij.openapi.projectRoots.JavaVersionService
import com.intellij.psi.*
import com.intellij.psi.util.InheritanceUtil
import com.intellij.psi.util.PsiUtil
import com.intellij.psi.util.TypeConversionUtil
import com.intellij.util.containers.ContainerUtil
import com.siyeh.InspectionGadgetsBundle
import com.siyeh.ig.junit.JUnitCommonClassNames
@@ -68,8 +70,9 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool(
val csvFileSource = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_FILE_SOURCE)
val csvSource = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_SOURCE)
val argSources = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PROVIDER_ARGUMENTS_SOURCE)
val noMultiArgsProvider = methodSource == null && csvFileSource == null && csvSource == null
val noMultiArgsProvider = methodSource == null && csvFileSource == null && csvSource == null && argSources == null
if (valuesSource == null && enumSource == null && noMultiArgsProvider) {
holder.registerProblem(parameterizedAnnotation, "No sources are provided, the suite would be empty")
@@ -163,9 +166,25 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool(
attributeValue: PsiAnnotationMemberValue,
componentType: PsiType) {
val parameters = method.parameterList.parameters
if (parameters.size == 1 && !parameters[0].type.isAssignableFrom(componentType) && !isArgumentsInheritor(componentType)) {
holder.registerProblem(attributeValue,
"No implicit conversion found to convert object of type " + componentType.presentableText + " to " + parameters[0].type.presentableText)
if (parameters.size == 1) {
val paramType = parameters[0].type
if (!paramType.isAssignableFrom(componentType) && !isArgumentsInheritor(componentType)) {
if (componentType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
//implicit conversion to primitive/wrapper
if (TypeConversionUtil.isPrimitiveAndNotNullOrWrapper(paramType)) return
val psiClass = PsiUtil.resolveClassInClassTypeOnly(paramType)
//implicit conversion to enum
if (psiClass != null) {
if (psiClass.isEnum && psiClass.findFieldByName((attributeValue as PsiLiteral).value as String?, false) != null) return
//implicit java time conversion
val qualifiedName = psiClass.qualifiedName
if (qualifiedName != null && qualifiedName.startsWith("java.time.")) return
}
}
if (AnnotationUtil.isAnnotated(parameters[0], JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_CONVERTER_CONVERT_WITH, false)) return
holder.registerProblem(attributeValue,
"No implicit conversion found to convert object of type " + componentType.presentableText + " to " + paramType.presentableText)
}
}
}
@@ -46,9 +46,8 @@ public class JUnit5MalformedParameterizedTest extends LightInspectionTestCase {
"}\n");
}
public void testMalformedSources() throws Exception {
doTest();
}
public void testMalformedSources() { doTest(); }
public void testMalformedSourcesImplicitConversion() { doTest(); }
@Override
protected String getBasePath() {
@@ -52,7 +52,7 @@ class ParameterizedTestsDemo {
@ParameterizedTest
<warning descr="Exactly one type of input must be provided">@ValueSource(ints = {1},
strings = <warning descr="No implicit conversion found to convert object of type String to int">"str"</warning>)</warning>
strings = "str")</warning>
void testWithMultipleValues(int i) { }
@ParameterizedTest
@@ -0,0 +1,17 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
class ParameterizedTestsDemo {
enum E {
A, B;
}
@ParameterizedTest
@ValueSource(strings = {"A"})
void testStrToEnum(E e) { }
@ParameterizedTest
@ValueSource(strings = {"1"})
void testStrToPrimitive(int i) { }
}