Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply assorted test cleanup #562

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
// `String.valueOf(null)` may not. That is because the latter matches `String#valueOf(char[])`. We
// could special-case `null` arguments, but that doesn't seem worth the trouble.
final class RedundantStringConversionTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(RedundantStringConversion.class, getClass());
Comment on lines -13 to -14
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that initially we decided against this, but it is more consistent this way (and it may help simplify the generalizations we're discussing in #494).


@Test
void identificationOfIdentityTransformation() {
compilationTestHelper
CompilationTestHelper.newInstance(RedundantStringConversion.class, getClass())
.addSourceLines(
"A.java",
"class A {",
Expand All @@ -38,7 +35,7 @@ void identificationOfIdentityTransformation() {

@Test
void identificationWithinMutatingAssignment() {
compilationTestHelper
CompilationTestHelper.newInstance(RedundantStringConversion.class, getClass())
.addSourceLines(
"A.java",
"import java.math.BigInteger;",
Expand Down Expand Up @@ -99,7 +96,7 @@ void identificationWithinMutatingAssignment() {

@Test
void identificationWithinBinaryOperation() {
compilationTestHelper
CompilationTestHelper.newInstance(RedundantStringConversion.class, getClass())
.addSourceLines(
"A.java",
"import java.math.BigInteger;",
Expand Down Expand Up @@ -194,7 +191,7 @@ void identificationWithinBinaryOperation() {

@Test
void identificationWithinStringBuilderMethod() {
compilationTestHelper
CompilationTestHelper.newInstance(RedundantStringConversion.class, getClass())
.addSourceLines(
"A.java",
"import java.math.BigInteger;",
Expand Down Expand Up @@ -249,7 +246,7 @@ void identificationWithinStringBuilderMethod() {
// XXX: Also test the other formatter methods.
@Test
void identificationWithinFormatterMethod() {
compilationTestHelper
CompilationTestHelper.newInstance(RedundantStringConversion.class, getClass())
.addSourceLines(
"A.java",
"import java.util.Formattable;",
Expand Down Expand Up @@ -294,7 +291,7 @@ void identificationWithinFormatterMethod() {

@Test
void identificationWithinGuavaGuardMethod() {
compilationTestHelper
CompilationTestHelper.newInstance(RedundantStringConversion.class, getClass())
.addSourceLines(
"A.java",
"import static com.google.common.base.Preconditions.checkArgument;",
Expand Down Expand Up @@ -354,7 +351,7 @@ void identificationWithinGuavaGuardMethod() {

@Test
void identificationWithinSlf4jLoggerMethod() {
compilationTestHelper
CompilationTestHelper.newInstance(RedundantStringConversion.class, getClass())
.addSourceLines(
"A.java",
"import java.util.Formattable;",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class JavaKeywordsTest {
private static Stream<Arguments> isValidIdentifierTestCases() {
/* { str, expected } */
/* { string, expected } */
return Stream.of(
arguments("", false),
arguments("public", false),
Expand All @@ -28,7 +28,7 @@ private static Stream<Arguments> isValidIdentifierTestCases() {

@MethodSource("isValidIdentifierTestCases")
@ParameterizedTest
void isValidIdentifier(String str, boolean expected) {
assertThat(JavaKeywords.isValidIdentifier(str)).isEqualTo(expected);
void isValidIdentifier(String string, boolean expected) {
assertThat(JavaKeywords.isValidIdentifier(string)).isEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@
import org.junit.jupiter.api.Test;

final class MethodMatcherFactoryTest {
/** A {@link BugChecker} that flags method invocations matched by {@link #TEST_MATCHER}. */
@BugPattern(severity = SUGGESTION, summary = "Flags methods matched by the test matcher.")
public static final class MatchedMethodsFlagger extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (TEST_MATCHER.matches(tree, state)) {
return buildDescription(tree).build();
}

return Description.NO_MATCH;
}
}

private static final Matcher<ExpressionTree> TEST_MATCHER =
new MethodMatcherFactory()
.create(
Expand Down Expand Up @@ -207,4 +191,20 @@ void matcher() {
"}")
.doTest();
}

/** A {@link BugChecker} that flags method invocations matched by {@link #TEST_MATCHER}. */
@BugPattern(severity = SUGGESTION, summary = "Flags methods matched by the test matcher.")
public static final class MatchedMethodsFlagger extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (TEST_MATCHER.matches(tree, state)) {
return buildDescription(tree).build();
}

return Description.NO_MATCH;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
import reactor.core.publisher.Flux;

final class ThirdPartyLibraryTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(TestChecker.class, getClass());
Comment on lines -21 to -22
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also a BugCheckerRefactoringTestHelper field in SourceCodeTest, but that's part of #561.


@Test
void isIntroductionAllowed() {
compilationTestHelper
CompilationTestHelper.newInstance(TestChecker.class, getClass())
.addSourceLines(
"A.java",
"// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true, REACTOR: true",
Expand All @@ -33,7 +30,7 @@ void isIntroductionAllowed() {

@Test
void isIntroductionAllowedWitnessClassesInSymtab() {
compilationTestHelper
CompilationTestHelper.newInstance(TestChecker.class, getClass())
.addSourceLines(
"A.java",
"import com.google.common.collect.ImmutableList;",
Expand All @@ -55,7 +52,7 @@ void isIntroductionAllowedWitnessClassesInSymtab() {

@Test
void isIntroductionAllowedWitnessClassesPartiallyOnClassPath() {
compilationTestHelper
CompilationTestHelper.newInstance(TestChecker.class, getClass())
.withClasspath(ImmutableList.class, Flux.class)
.addSourceLines(
"A.java",
Expand All @@ -66,7 +63,7 @@ void isIntroductionAllowedWitnessClassesPartiallyOnClassPath() {

@Test
void isIntroductionAllowedWitnessClassesNotOnClassPath() {
compilationTestHelper
CompilationTestHelper.newInstance(TestChecker.class, getClass())
.withClasspath()
.addSourceLines(
"A.java",
Expand All @@ -79,7 +76,7 @@ void isIntroductionAllowedWitnessClassesNotOnClassPath() {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void isIntroductionAllowedIgnoreClasspathCompat(boolean ignoreClassPath) {
compilationTestHelper
CompilationTestHelper.newInstance(TestChecker.class, getClass())
.setArgs("-XepOpt:ErrorProneSupport:IgnoreClasspathCompat=" + ignoreClassPath)
.withClasspath(ImmutableList.class, Flux.class)
.addSourceLines(
Expand Down