From ca7e8f4c51742ec2e89d2e49e85f9cea3e3048cf Mon Sep 17 00:00:00 2001 From: cpovirk Date: Mon, 15 Jan 2024 10:52:21 -0800 Subject: [PATCH] Make it possible to write `expect.that(optional).isPresent()`, `assertWithMessage(...).that(optional).isPresent()`, etc., including for `Stream` as well as `Optional`. That is, you no longer need to use `about(optionals())` (or `about(streams())`). (Later, we'll do the same thing for lesser-used APIs like `optionalInts().) This CL does _not_ make it possible to write `Truth.assertThat(optional).isPresent()`: For that, you still need to use `Truth8`. A future CL will eliminate the need to use `Truth8`. This continues our work on https://github.com/google/truth/issues/746. RELNOTES=Added `that` overloads to make it possible to write type-specific assertions when using `expect.that(optional)` and `expect.that(stream)`. PiperOrigin-RevId: 598637400 --- .../common/truth/StandardSubjectBuilder.java | 15 +++++++++++++++ .../google/common/truth/TruthAssertThatTest.java | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java b/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java index 0d90808b7..dae053d2b 100644 --- a/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java +++ b/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java @@ -24,6 +24,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; /** @@ -168,6 +170,19 @@ public final TableSubject that(@Nullable Table actual) { return new TableSubject(metadata(), actual); } + @SuppressWarnings({ + "Java7ApiChecker", // no more dangerous that wherever the user got the Optional + "NullableOptional", // Truth always accepts nulls, no matter the type + }) + public final OptionalSubject that(@Nullable Optional actual) { + return new OptionalSubject(metadata(), actual, "optional"); + } + + @SuppressWarnings("Java7ApiChecker") // no more dangerous that wherever the user got the Stream + public final StreamSubject that(@Nullable Stream actual) { + return new StreamSubject(metadata(), actual); + } + /** * Returns a new instance that will output the given message before the main failure message. If * this method is called multiple times, the messages will appear in the order that they were 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 cb75b934e..1d1205c9a 100644 --- a/core/src/test/java/com/google/common/truth/TruthAssertThatTest.java +++ b/core/src/test/java/com/google/common/truth/TruthAssertThatTest.java @@ -25,6 +25,8 @@ 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; @@ -45,6 +47,9 @@ 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 =