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 =