Skip to content

Commit

Permalink
Add a test confirming that ArgumentSelectionDefectChecker works on re…
Browse files Browse the repository at this point in the history
…cord construction, but _not_ record deconstruction.

PiperOrigin-RevId: 691455946
  • Loading branch information
graememorgan authored and Error Prone Team committed Oct 30, 2024
1 parent 866c49b commit f0c3c1e
Showing 1 changed file with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.errorprone.bugpatterns.argumentselectiondefects;

import static com.google.common.truth.TruthJUnit.assume;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.SeverityLevel;
Expand All @@ -38,6 +40,10 @@
@RunWith(JUnit4.class)
public class ArgumentSelectionDefectCheckerTest {

private final CompilationTestHelper testHelper =
CompilationTestHelper.newInstance(
ArgumentSelectionDefectWithStringEquality.class, getClass());

/**
* A {@link BugChecker} which runs the ArgumentSelectionDefectChecker checker using string
* equality for edit distance
Expand All @@ -56,7 +62,7 @@ public ArgumentSelectionDefectWithStringEquality() {

@Test
public void argumentSelectionDefectChecker_findsSwap_withSwappedMatchingPair() {
CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass())
testHelper
.addSourceLines(
"Test.java",
"""
Expand All @@ -75,7 +81,7 @@ void test(Object first, Object second) {

@Test
public void argumentSelectionDefectChecker_findsSwap_withSwappedMatchingPairWithMethod() {
CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass())
testHelper
.addSourceLines(
"Test.java",
"""
Expand All @@ -96,7 +102,7 @@ void test(Object first) {

@Test
public void argumentSelectionDefectChecker_findsSwap_withOneNullArgument() {
CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass())
testHelper
.addSourceLines(
"Test.java",
"""
Expand All @@ -115,7 +121,7 @@ void test(Object second) {

@Test
public void argumentSelectionDefectChecker_rejectsSwap_withNoAssignableAlternatives() {
CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass())
testHelper
.addSourceLines(
"Test.java",
"""
Expand All @@ -132,7 +138,7 @@ void test(Integer first, String second) {

@Test
public void argumentSelectionDefectChecker_commentsOnlyOnSwappedPair_withThreeArguments() {
CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass())
testHelper
.addSourceLines(
"Test.java",
"""
Expand Down Expand Up @@ -360,7 +366,7 @@ public void parameterNamesAvailable_returnsTree_onMethodNotInCompilationUnit() {

@Test
public void description() {
CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass())
testHelper
.addSourceLines(
"Test.java",
"""
Expand All @@ -374,6 +380,48 @@ void test(Object first, Object second) {
target(second, first);
}
}
""")
.doTest();
}

@Test
public void records() {
assume().that(Runtime.version().feature()).isAtLeast(16);

testHelper
.addSourceLines(
"Test.java",
"""
class Test {
Foo test(String first, String second) {
// BUG: Diagnostic contains: may have been swapped
return new Foo(second, first);
}
}
record Foo(String first, String second) {}
""")
.doTest();
}

@Test
public void recordDeconstruction() {
assume().that(Runtime.version().feature()).isAtLeast(21);

testHelper
.addSourceLines(
"Test.java",
"""
class Test {
void test(Foo foo) {
switch (foo) {
// TODO(user): We should report a finding here!
case Foo(String second, String first) -> {}
}
}
}
record Foo(String first, String second) {}
""")
.doTest();
}
Expand Down

0 comments on commit f0c3c1e

Please sign in to comment.