Skip to content

Commit

Permalink
Confirm the exhaustiveness of ProtoTruth methods.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 570411077
  • Loading branch information
graememorgan authored and Error Prone Team committed Oct 3, 2023
1 parent a4053d0 commit 8b00f66
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.google.errorprone.bugpatterns.collectionincompatibletype;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
import static java.lang.String.format;
import static java.util.Arrays.stream;

import com.google.common.collect.ImmutableList;
import com.google.common.truth.IterableSubject;
import com.google.common.truth.Subject;
import com.google.common.truth.extensions.proto.IterableOfProtosSubject;
import com.google.common.truth.extensions.proto.ProtoSubject;
import com.google.errorprone.CompilationTestHelper;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameter.TestParameterValuesProvider;
Expand Down Expand Up @@ -623,6 +626,31 @@ public void iterableSubjectExhaustiveness(
.doTest();
}

@Test
public void protoTruthSubject_exhaustive(
@TestParameter(valuesProvider = ProtoTruthSubjectMethods.class) Method method) {
compilationHelper
.addSourceLines(
"Test.java",
"import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;",
"import com.google.errorprone.bugpatterns.proto.ProtoTest.TestFieldProtoMessage;",
"import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;",
"public class Test {",
" public void f(TestProtoMessage a, TestFieldProtoMessage b) {",
" // BUG: Diagnostic contains:",
getOffensiveLine(method),
" }",
"}")
.doTest();
}

@Test
public void protoTruthIterableSubjectExhaustiveness() {
// There are no additional methods on IterableOfProtosSubject, otherwise we'd want to test that.
// This test just makes sure that remains true.
assertThat(getAssertionMethods(IterableOfProtosSubject.class)).isEmpty();
}

private static String getOffensiveLine(Method method) {
if (stream(method.getParameterTypes()).allMatch(p -> p.equals(Iterable.class))) {
return format(" assertThat(a).%s(ImmutableList.of(b));", method.getName());
Expand Down Expand Up @@ -654,13 +682,21 @@ public ImmutableList<Method> provideValues() {
}
}

private static final class ProtoTruthSubjectMethods implements TestParameterValuesProvider {
@Override
public ImmutableList<Method> provideValues() {
return getAssertionMethods(ProtoSubject.class);
}
}

private static ImmutableList<Method> getAssertionMethods(Class<?> clazz) {
return stream(clazz.getDeclaredMethods())
.filter(
m ->
Modifier.isPublic(m.getModifiers())
&& !m.getName().equals("equals")
&& m.getParameterCount() > 0
&& !m.getName().startsWith("ignoring")
&& (stream(m.getParameterTypes()).allMatch(p -> p.equals(Iterable.class))
|| stream(m.getParameterTypes())
.allMatch(p -> p.equals(Object.class) || p.isArray())
Expand Down

0 comments on commit 8b00f66

Please sign in to comment.