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

Add a test confirming that ArgumentSelectionDefectChecker works on record construction, but _not_ record deconstruction. #4652

Merged
merged 1 commit into from
Oct 30, 2024
Merged
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 @@ -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
Loading