fix contracts for junit5 assertions

This commit is contained in:
peter
2017-06-08 17:12:36 +02:00
parent 31ddfe6122
commit 0e938165ea
3 changed files with 34 additions and 2 deletions
@@ -199,6 +199,10 @@ public class HardcodedContracts {
return className.startsWith("junit.framework.") || className.startsWith("org.junit.");
}
private static boolean isJunit5(String className) {
return className.startsWith("org.junit.jupiter.");
}
private static boolean isTestng(String className) {
return className.startsWith("org.testng.");
}
@@ -245,7 +249,7 @@ public class HardcodedContracts {
if (paramCount == 0) return Collections.emptyList();
int checkedParam = testng ? 0 : paramCount - 1;
int checkedParam = testng || isJunit5(className) ? 0 : paramCount - 1;
MethodContract.ValueConstraint[] constraints = createConstraintArray(paramCount);
if ("assertTrue".equals(methodName) || "assumeTrue".equals(methodName)) {
constraints[checkedParam] = FALSE_VALUE;
@@ -8,4 +8,20 @@ class Contracts {
String s = o.toString();
}
void foo2(@Nullable Object o) {
Assertions.assertNotNull(o, "message");
String s = o.toString();
}
void foo3(java.util.function.BooleanSupplier o) {
Assertions.assertTrue(o, "message");
}
void foo3(boolean b) {
Assertions.assertTrue(b, "message");
if (<warning descr="Condition '!b' is always 'false'">!<warning descr="Value 'b' is always 'true'">b</warning></warning>) {
System.out.println("how?");
}
}
}
@@ -17,11 +17,18 @@ package com.intellij.java.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
/**
* @author peter
*/
public class HardcodedContractsTest extends DataFlowInspectionTestCase {
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_8;
}
@Override
protected String getTestDataPath() {
@@ -110,8 +117,13 @@ public class HardcodedContractsTest extends DataFlowInspectionTestCase {
}
public void testJunit5Assert() {
myFixture.addClass("package org.junit.jupiter.api; public class Assertions {\n" +
myFixture.addClass("package org.junit.jupiter.api;" +
"import java.util.function.BooleanSupplier;" +
"public class Assertions {\n" +
" public static void assertNotNull(Object actual){}" +
" public static void assertNotNull(Object actual, String message){}" +
" public static void assertTrue(boolean b, String message){}" +
" public static void assertTrue(BooleanSupplier b, String message){}" +
"}");
checkHighlighting();
}