From 37fd8bea90c0ab4528c4c922c88fa176eb45f65b Mon Sep 17 00:00:00 2001 From: cpovirk Date: Mon, 15 Jan 2024 14:31:04 -0800 Subject: [PATCH] Copy `Truth8.assertThat` overloads for `Optional` and `Stream` to the main `Truth` class. (and prepare AutoValue tests for the new overloads, since the Eclipse compiler considers the (temporary!) signatures of `assertThat(Optional)` to be ambiguous) We'll post some migration suggestions as part of the release notes. This is the biggest part of https://github.com/google/truth/issues/746. RELNOTES=Added `assertThat` overloads for `Optional` and `Stream` to the main `Truth` class. PiperOrigin-RevId: 598664192 --- .../main/java/com/google/common/truth/Truth.java | 14 ++++++++++++++ .../google/common/truth/TruthAssertThatTest.java | 5 ----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/common/truth/Truth.java b/core/src/main/java/com/google/common/truth/Truth.java index 6cf4ca7e8..7ca25d6fc 100644 --- a/core/src/main/java/com/google/common/truth/Truth.java +++ b/core/src/main/java/com/google/common/truth/Truth.java @@ -23,6 +23,8 @@ import com.google.common.collect.Table; import java.math.BigDecimal; import java.util.Map; +import java.util.Optional; +import java.util.stream.Stream; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -249,6 +251,18 @@ public static TableSubject assertThat(@Nullable Table actual) { return assert_().that(actual); } + @SuppressWarnings("Java7ApiChecker") // no more dangerous that wherever the user got the Optional + @GwtIncompatible // creates ambiguities (Eclipse bug 577808 or similar?) + public static OptionalSubject assertThat(@Nullable Optional actual) { + return assert_().that(actual); + } + + @SuppressWarnings("Java7ApiChecker") // no more dangerous that wherever the user got the Stream + @GwtIncompatible // creates ambiguities (Eclipse bug 577808 or similar?) + public static StreamSubject assertThat(@Nullable Stream actual) { + return assert_().that(actual); + } + /** * An {@code AssertionError} that (a) always supports a cause, even under old versions of Android * and (b) omits "java.lang.AssertionError:" from the beginning of its toString() representation. diff --git a/core/src/test/java/com/google/common/truth/TruthAssertThatTest.java b/core/src/test/java/com/google/common/truth/TruthAssertThatTest.java index 1d1205c9a..cb75b934e 100644 --- a/core/src/test/java/com/google/common/truth/TruthAssertThatTest.java +++ b/core/src/test/java/com/google/common/truth/TruthAssertThatTest.java @@ -25,8 +25,6 @@ import com.google.common.reflect.TypeToken; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.Optional; -import java.util.stream.Stream; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -47,9 +45,6 @@ public void staticAssertThatMethodsMatchStandardSubjectBuilderInstanceMethods() ImmutableSortedSet> verbTypes = FluentIterable.from(asList(StandardSubjectBuilder.class.getMethods())) .filter(input -> input.getName().equals("that")) - // TODO: b/166630734 - Remove this when we add the assertThat overloads. - .filter(input -> input.getParameterTypes()[0] != Optional.class) - .filter(input -> input.getParameterTypes()[0] != Stream.class) .transform(TruthAssertThatTest::methodToReturnTypeToken) .toSortedSet(Ordering.usingToString()); ImmutableSortedSet> truthTypes =