From 9be8e774ca41a2a85c49d203bc47dac78861aeaa Mon Sep 17 00:00:00 2001 From: cpovirk Date: Thu, 1 Feb 2024 14:36:02 -0800 Subject: [PATCH] =?UTF-8?q?Copy=20remaining=20`Truth8.assertThat`=20overlo?= =?UTF-8?q?ads=20to=20the=20main=20`Truth`=20class=E2=80=94except=20`Path`?= =?UTF-8?q?=20and=20`OptionalLong`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We'll post some migration suggestions as part of the release notes. This is the biggest remaining part of https://github.com/google/truth/issues/746, but some loose ends remain. RELNOTES=Added most remaining `Truth8.assertThat` overloads to the main `Truth` class. PiperOrigin-RevId: 603485177 --- .../java/com/google/common/truth/Truth.java | 47 +++++++++++++++++-- .../common/truth/TruthAssertThatTest.java | 8 ---- 2 files changed, 43 insertions(+), 12 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 f49b84695..f2e1a1d32 100644 --- a/core/src/main/java/com/google/common/truth/Truth.java +++ b/core/src/main/java/com/google/common/truth/Truth.java @@ -24,6 +24,10 @@ import java.math.BigDecimal; import java.util.Map; import java.util.Optional; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.stream.IntStream; +import java.util.stream.LongStream; import java.util.stream.Stream; import org.checkerframework.checker.nullness.qual.Nullable; @@ -251,24 +255,59 @@ 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?) /** * @since 1.3.0 (present in {@link Truth8} since before 1.0) */ + @SuppressWarnings({ + "Java7ApiChecker", // no more dangerous than wherever the user got the Optional + "NullableOptional", // Truth always accepts nulls, no matter the type + }) 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?) /** * @since 1.3.0 (present in {@link Truth8} since before 1.0) */ + @SuppressWarnings("Java7ApiChecker") // no more dangerous than wherever the user got the Stream + public static OptionalIntSubject assertThat(@Nullable OptionalInt actual) { + return assert_().that(actual); + } + + /** + * @since 1.4.0 (present in {@link Truth8} since before 1.0) + */ + @SuppressWarnings("Java7ApiChecker") // no more dangerous than wherever the user got the Stream + public static OptionalDoubleSubject assertThat(@Nullable OptionalDouble actual) { + return assert_().that(actual); + } + + /** + * @since 1.4.0 (present in {@link Truth8} since before 1.0) + */ + @SuppressWarnings("Java7ApiChecker") // no more dangerous than wherever the user got the Stream public static StreamSubject assertThat(@Nullable Stream actual) { return assert_().that(actual); } + /** + * @since 1.4.0 (present in {@link Truth8} since before 1.0) + */ + @SuppressWarnings("Java7ApiChecker") // no more dangerous than wherever the user got the Stream + public static IntStreamSubject assertThat(@Nullable IntStream actual) { + return assert_().that(actual); + } + + /** + * @since 1.4.0 (present in {@link Truth8} since before 1.0) + */ + @SuppressWarnings("Java7ApiChecker") // no more dangerous than wherever the user got the Stream + public static LongStreamSubject assertThat(@Nullable LongStream actual) { + return assert_().that(actual); + } + + // TODO(b/64757353): Add support for DoubleStream? + /** * 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 97b1c110c..e5577f3ec 100644 --- a/core/src/test/java/com/google/common/truth/TruthAssertThatTest.java +++ b/core/src/test/java/com/google/common/truth/TruthAssertThatTest.java @@ -26,11 +26,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.file.Path; -import java.util.OptionalDouble; -import java.util.OptionalInt; import java.util.OptionalLong; -import java.util.stream.IntStream; -import java.util.stream.LongStream; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -52,10 +48,6 @@ public void staticAssertThatMethodsMatchStandardSubjectBuilderInstanceMethods() 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] != IntStream.class) - .filter(input -> input.getParameterTypes()[0] != LongStream.class) - .filter(input -> input.getParameterTypes()[0] != OptionalDouble.class) - .filter(input -> input.getParameterTypes()[0] != OptionalInt.class) .filter(input -> input.getParameterTypes()[0] != OptionalLong.class) .filter(input -> input.getParameterTypes()[0] != Path.class) .transform(TruthAssertThatTest::methodToReturnTypeToken)