Skip to content

Commit

Permalink
Refactor helpers in UseCorrectAssertInTestsTest to allow an arbitrary…
Browse files Browse the repository at this point in the history
… number of LOC

This is really a *prefactoring* step; see child unknown commit to see how this enables better handling around primitive types.

PiperOrigin-RevId: 518852774
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed Mar 23, 2023
1 parent c262ba7 commit e5cff75
Showing 1 changed file with 72 additions and 58 deletions.
Expand Up @@ -16,6 +16,9 @@

package com.google.errorprone.bugpatterns;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import org.junit.Test;
Expand All @@ -29,9 +32,9 @@
public final class UseCorrectAssertInTestsTest {

private static final String ASSERT_THAT_IMPORT =
"static com.google.common.truth.Truth.assertThat;";
"import static com.google.common.truth.Truth.assertThat;";
private static final String ASSERT_WITH_MESSAGE_IMPORT =
"static com.google.common.truth.Truth.assertWithMessage;";
"import static com.google.common.truth.Truth.assertWithMessage;";
private static final String INPUT = "in/FooTest.java";
private static final String OUTPUT = "out/FooTest.java";

Expand All @@ -46,15 +49,18 @@ public final class UseCorrectAssertInTestsTest {
public void correctAssertInTest() {
refactoringHelper
.addInputLines(
INPUT, inputWithExpressionAndImport("assertThat(true).isTrue();", ASSERT_THAT_IMPORT))
INPUT,
inputWithExpressionsAndImport(
ASSERT_THAT_IMPORT, //
"assertThat(true).isTrue();"))
.expectUnchanged()
.doTest();
}

@Test
public void noAssertInTestsFound() {
refactoringHelper
.addInputLines(INPUT, inputWithExpression("int a = 1;"))
.addInputLines(INPUT, inputWithExpressions("int a = 1;"))
.expectUnchanged()
.doTest();
}
Expand Down Expand Up @@ -93,7 +99,7 @@ public void assertInNonTestMethod() {
"}")
.addOutputLines(
OUTPUT,
"import static com.google.common.truth.Truth.assertThat;",
ASSERT_THAT_IMPORT,
"import org.junit.runner.RunWith;",
"import org.junit.runners.JUnit4;",
"@RunWith(JUnit4.class)",
Expand All @@ -117,7 +123,7 @@ public void assertInTestOnlyCode() {
"}")
.addOutputLines(
OUTPUT,
"import static com.google.common.truth.Truth.assertThat;",
ASSERT_THAT_IMPORT,
"public class FooTest {",
" void foo() {",
" assertThat(true).isTrue();",
Expand All @@ -144,57 +150,70 @@ public void assertInNonTestCode() {
@Test
public void wrongAssertInTestWithParentheses() {
refactoringHelper
.addInputLines(INPUT, inputWithExpression("assert (true);"))
.addInputLines(INPUT, inputWithExpressions("assert (true);"))
.addOutputLines(
OUTPUT, inputWithExpressionAndImport("assertThat(true).isTrue();", ASSERT_THAT_IMPORT))
OUTPUT,
inputWithExpressionsAndImport(
ASSERT_THAT_IMPORT, //
"assertThat(true).isTrue();"))
.doTest();
}

@Test
public void wrongAssertInTestWithoutParentheses() {
refactoringHelper
.addInputLines(INPUT, inputWithExpression("assert true;"))
.addInputLines(INPUT, inputWithExpressions("assert true;"))
.addOutputLines(
OUTPUT, inputWithExpressionAndImport("assertThat(true).isTrue();", ASSERT_THAT_IMPORT))
OUTPUT,
inputWithExpressionsAndImport(
ASSERT_THAT_IMPORT, //
"assertThat(true).isTrue();"))
.doTest();
}

@Test
public void wrongAssertInTestWithDetailString() {
refactoringHelper
.addInputLines(INPUT, inputWithExpression("assert (true) : \"description\";"))
.addInputLines(INPUT, inputWithExpressions("assert (true) : \"description\";"))
.addOutputLines(
OUTPUT,
inputWithExpressionAndImport(
"assertWithMessage(\"description\").that(true).isTrue();",
ASSERT_WITH_MESSAGE_IMPORT))
inputWithExpressionsAndImport(
ASSERT_WITH_MESSAGE_IMPORT, //
"assertWithMessage(\"description\").that(true).isTrue();"))
.doTest();
}

@Test
public void wrongAssertInTestWithDetailStringVariable() {
refactoringHelper
.addInputLines(
INPUT, inputWithExpressions("String desc = \"description\";", "assert (true) : desc;"))
INPUT,
inputWithExpressions(
"String desc = \"description\";", //
"assert (true) : desc;"))
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
ASSERT_WITH_MESSAGE_IMPORT, //
"String desc = \"description\";",
"assertWithMessage(desc).that(true).isTrue();",
ASSERT_WITH_MESSAGE_IMPORT))
"assertWithMessage(desc).that(true).isTrue();"))
.doTest();
}

@Test
public void wrongAssertInTestWithDetailNonStringVariable() {
refactoringHelper
.addInputLines(INPUT, inputWithExpressions("Integer desc = 1;", "assert (true) : desc;"))
.addInputLines(
INPUT,
inputWithExpressions(
"Integer desc = 1;", //
"assert (true) : desc;"))
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
ASSERT_WITH_MESSAGE_IMPORT, //
"Integer desc = 1;",
"assertWithMessage(desc.toString()).that(true).isTrue();",
ASSERT_WITH_MESSAGE_IMPORT))
"assertWithMessage(desc.toString()).that(true).isTrue();"))
.doTest();
}

Expand All @@ -209,7 +228,9 @@ public void wrongAssertFalseCase() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
"boolean a = false;", "assertThat(a).isFalse();", ASSERT_THAT_IMPORT))
ASSERT_THAT_IMPORT, //
"boolean a = false;",
"assertThat(a).isFalse();"))
.doTest();
}

Expand All @@ -224,7 +245,9 @@ public void wrongAssertEqualsCase() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
"String a = \"test\";", "assertThat(a).isEqualTo(\"test\");", ASSERT_THAT_IMPORT))
ASSERT_THAT_IMPORT, //
"String a = \"test\";",
"assertThat(a).isEqualTo(\"test\");"))
.doTest();
}

Expand All @@ -239,9 +262,9 @@ public void wrongAssertEqualsNullCase() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
"Integer a = null;", //
"assertThat(a).isNull();",
ASSERT_THAT_IMPORT))
ASSERT_THAT_IMPORT, //
"Integer a = null;",
"assertThat(a).isNull();"))
.doTest();
}

Expand All @@ -256,9 +279,9 @@ public void wrongAssertEqualsNullCaseLeftSide() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
"Integer a = null;", //
"assertThat(a).isNull();",
ASSERT_THAT_IMPORT))
ASSERT_THAT_IMPORT, //
"Integer a = null;",
"assertThat(a).isNull();"))
.doTest();
}

Expand All @@ -273,9 +296,9 @@ public void wrongAssertEqualsNullCaseWithDetail() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
ASSERT_WITH_MESSAGE_IMPORT, //
"Integer a = null;",
"assertWithMessage(\"detail\").that(a).isNull();",
ASSERT_WITH_MESSAGE_IMPORT))
"assertWithMessage(\"detail\").that(a).isNull();"))
.doTest();
}

Expand All @@ -290,9 +313,9 @@ public void wrongAssertNotEqualsNullCase() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
"Integer a = 1;", //
"assertThat(a).isNotNull();",
ASSERT_THAT_IMPORT))
ASSERT_THAT_IMPORT, //
"Integer a = 1;",
"assertThat(a).isNotNull();"))
.doTest();
}

Expand All @@ -307,9 +330,9 @@ public void wrongAssertReferenceSameCase() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
"Integer a = 1;", //
"assertThat(a).isSameInstanceAs(1);",
ASSERT_THAT_IMPORT))
ASSERT_THAT_IMPORT, //
"Integer a = 1;",
"assertThat(a).isSameInstanceAs(1);"))
.doTest();
}

Expand All @@ -324,9 +347,9 @@ public void wrongAssertReferenceWithParensCase() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
"Integer a = 1;", //
"assertThat(a).isSameInstanceAs(1);",
ASSERT_THAT_IMPORT))
ASSERT_THAT_IMPORT, //
"Integer a = 1;",
"assertThat(a).isSameInstanceAs(1);"))
.doTest();
}

Expand All @@ -341,9 +364,9 @@ public void wrongAssertReferenceNotSameCase() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
"Integer a = 1;", //
"assertThat(a).isNotSameInstanceAs(1);",
ASSERT_THAT_IMPORT))
ASSERT_THAT_IMPORT, //
"Integer a = 1;",
"assertThat(a).isNotSameInstanceAs(1);"))
.doTest();
}

Expand All @@ -358,39 +381,30 @@ public void wrongAssertReferenceSameCaseWithDetailCase() {
.addOutputLines(
OUTPUT,
inputWithExpressionsAndImport(
ASSERT_WITH_MESSAGE_IMPORT, //
"int a = 1;",
"assertWithMessage(\"detail\").that(a).isSameInstanceAs(1);",
ASSERT_WITH_MESSAGE_IMPORT))
"assertWithMessage(\"detail\").that(a).isSameInstanceAs(1);"))
.doTest();
}

private static String[] inputWithExpressionAndImport(String expr, String importPath) {
return inputWithExpressionsAndImport(expr, "", importPath);
}
private static String[] inputWithExpressionsAndImport(String importStatement, String... body) {

private static String[] inputWithExpressionsAndImport(
String expr1, String expr2, String importPath) {
return new String[] {
importPath.isEmpty() ? "" : String.format("import %s", importPath),
importStatement,
"import org.junit.Test;",
"import org.junit.runner.RunWith;",
"import org.junit.runners.JUnit4;",
"@RunWith(JUnit4.class)",
"public class FooTest {",
" @Test",
" void foo() {",
String.format(" %s", expr1),
expr2.isEmpty() ? "" : String.format(" %s", expr2),
stream(body).map(line -> String.format(" %s", line)).collect(joining("\n")),
" }",
"}"
};
}

private static String[] inputWithExpression(String expr) {
return inputWithExpressionAndImport(expr, "");
}

private static String[] inputWithExpressions(String expr1, String expr2) {
return inputWithExpressionsAndImport(expr1, expr2, "");
private static String[] inputWithExpressions(String... expr) {
return inputWithExpressionsAndImport("", expr);
}
}

0 comments on commit e5cff75

Please sign in to comment.