Skip to content

Commit

Permalink
Make it possible to write expect.that(optionalInt).isPresent(), `as…
Browse files Browse the repository at this point in the history
…sertWithMessage(...).that(optionalInt).isPresent()`, etc., including for other types besides `OptionalInt`.

That is, you no longer need to use `about(optionalsInts())`, etc.

This CL does _not_ make it possible to write `Truth.assertThat(optionalInt).isPresent()`: For that, you still need to use `Truth8`. A future CL will eliminate the need to use `Truth8`.

This continues our work on #746.

(Compare previous cl/598637400.)

RELNOTES=Added more `that` overloads to make it possible to write type-specific assertions when using `expect.that(optionalInt)`, etc.
PiperOrigin-RevId: 602694153
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jan 30, 2024
1 parent 87b371d commit 7c65fc6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.Table;
import com.google.j2objc.annotations.J2ObjCIncompatible;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -182,6 +189,36 @@ public final OptionalSubject that(@Nullable Optional<?> actual) {
return new OptionalSubject(metadata(), actual, "optional");
}

/**
* @since 1.4.0 (with access to {@link OptionalIntSubject} previously part of {@code
* truth-java8-extension})
*/
@SuppressWarnings(
"Java7ApiChecker") // no more dangerous that wherever the user got the OptionalInt
public final OptionalIntSubject that(@Nullable OptionalInt actual) {
return new OptionalIntSubject(metadata(), actual, "optionalInt");
}

/**
* @since 1.4.0 (with access to {@link OptionalLongSubject} previously part of {@code
* truth-java8-extension})
*/
@SuppressWarnings(
"Java7ApiChecker") // no more dangerous that wherever the user got the OptionalLong
public final OptionalLongSubject that(@Nullable OptionalLong actual) {
return new OptionalLongSubject(metadata(), actual, "optionalLong");
}

/**
* @since 1.4.0 (with access to {@link OptionalDoubleSubject} previously part of {@code
* truth-java8-extension})
*/
@SuppressWarnings(
"Java7ApiChecker") // no more dangerous that wherever the user got the OptionalDouble
public final OptionalDoubleSubject that(@Nullable OptionalDouble actual) {
return new OptionalDoubleSubject(metadata(), actual, "optionalDouble");
}

/**
* @since 1.3.0 (with access to {@link StreamSubject} previously part of {@code
* truth-java8-extension})
Expand All @@ -191,6 +228,38 @@ public final StreamSubject that(@Nullable Stream<?> actual) {
return new StreamSubject(metadata(), actual);
}

/**
* @since 1.4.0 (with access to {@link IntStreamSubject} previously part of {@code
* truth-java8-extension})
*/
@SuppressWarnings("Java7ApiChecker") // no more dangerous that wherever the user got the IntStream
public final IntStreamSubject that(@Nullable IntStream actual) {
return new IntStreamSubject(metadata(), actual);
}

/**
* @since 1.4.0 (with access to {@link LongStreamSubject} previously part of {@code
* truth-java8-extension})
*/
@SuppressWarnings(
"Java7ApiChecker") // no more dangerous that wherever the user got the LongStream
public final LongStreamSubject that(@Nullable LongStream actual) {
return new LongStreamSubject(metadata(), actual);
}

// TODO(b/64757353): Add support for DoubleStream?

/**
* @since 1.4.0 (with access to {@link PathSubject} previously part of {@code
* truth-java8-extension})
*/
@GwtIncompatible
@J2ObjCIncompatible
@J2ktIncompatible
public final PathSubject that(@Nullable Path actual) {
return new PathSubject(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
import com.google.common.reflect.TypeToken;
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;
Expand All @@ -45,6 +51,13 @@ public void staticAssertThatMethodsMatchStandardSubjectBuilderInstanceMethods()
ImmutableSortedSet<TypeToken<?>> 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] != 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)
.toSortedSet(Ordering.usingToString());
ImmutableSortedSet<TypeToken<?>> truthTypes =
Expand Down

0 comments on commit 7c65fc6

Please sign in to comment.