Skip to content

Commit 7c65fc6

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committedJan 30, 2024
Make it possible to write expect.that(optionalInt).isPresent(), assertWithMessage(...).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
1 parent 87b371d commit 7c65fc6

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
 

‎core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java

+69
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@
2222
import com.google.common.collect.Multimap;
2323
import com.google.common.collect.Multiset;
2424
import com.google.common.collect.Table;
25+
import com.google.j2objc.annotations.J2ObjCIncompatible;
2526
import java.math.BigDecimal;
27+
import java.nio.file.Path;
2628
import java.util.Map;
2729
import java.util.Optional;
30+
import java.util.OptionalDouble;
31+
import java.util.OptionalInt;
32+
import java.util.OptionalLong;
33+
import java.util.stream.IntStream;
34+
import java.util.stream.LongStream;
2835
import java.util.stream.Stream;
2936
import org.checkerframework.checker.nullness.qual.Nullable;
3037

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

192+
/**
193+
* @since 1.4.0 (with access to {@link OptionalIntSubject} previously part of {@code
194+
* truth-java8-extension})
195+
*/
196+
@SuppressWarnings(
197+
"Java7ApiChecker") // no more dangerous that wherever the user got the OptionalInt
198+
public final OptionalIntSubject that(@Nullable OptionalInt actual) {
199+
return new OptionalIntSubject(metadata(), actual, "optionalInt");
200+
}
201+
202+
/**
203+
* @since 1.4.0 (with access to {@link OptionalLongSubject} previously part of {@code
204+
* truth-java8-extension})
205+
*/
206+
@SuppressWarnings(
207+
"Java7ApiChecker") // no more dangerous that wherever the user got the OptionalLong
208+
public final OptionalLongSubject that(@Nullable OptionalLong actual) {
209+
return new OptionalLongSubject(metadata(), actual, "optionalLong");
210+
}
211+
212+
/**
213+
* @since 1.4.0 (with access to {@link OptionalDoubleSubject} previously part of {@code
214+
* truth-java8-extension})
215+
*/
216+
@SuppressWarnings(
217+
"Java7ApiChecker") // no more dangerous that wherever the user got the OptionalDouble
218+
public final OptionalDoubleSubject that(@Nullable OptionalDouble actual) {
219+
return new OptionalDoubleSubject(metadata(), actual, "optionalDouble");
220+
}
221+
185222
/**
186223
* @since 1.3.0 (with access to {@link StreamSubject} previously part of {@code
187224
* truth-java8-extension})
@@ -191,6 +228,38 @@ public final StreamSubject that(@Nullable Stream<?> actual) {
191228
return new StreamSubject(metadata(), actual);
192229
}
193230

231+
/**
232+
* @since 1.4.0 (with access to {@link IntStreamSubject} previously part of {@code
233+
* truth-java8-extension})
234+
*/
235+
@SuppressWarnings("Java7ApiChecker") // no more dangerous that wherever the user got the IntStream
236+
public final IntStreamSubject that(@Nullable IntStream actual) {
237+
return new IntStreamSubject(metadata(), actual);
238+
}
239+
240+
/**
241+
* @since 1.4.0 (with access to {@link LongStreamSubject} previously part of {@code
242+
* truth-java8-extension})
243+
*/
244+
@SuppressWarnings(
245+
"Java7ApiChecker") // no more dangerous that wherever the user got the LongStream
246+
public final LongStreamSubject that(@Nullable LongStream actual) {
247+
return new LongStreamSubject(metadata(), actual);
248+
}
249+
250+
// TODO(b/64757353): Add support for DoubleStream?
251+
252+
/**
253+
* @since 1.4.0 (with access to {@link PathSubject} previously part of {@code
254+
* truth-java8-extension})
255+
*/
256+
@GwtIncompatible
257+
@J2ObjCIncompatible
258+
@J2ktIncompatible
259+
public final PathSubject that(@Nullable Path actual) {
260+
return new PathSubject(metadata(), actual);
261+
}
262+
194263
/**
195264
* Returns a new instance that will output the given message before the main failure message. If
196265
* this method is called multiple times, the messages will appear in the order that they were

‎core/src/test/java/com/google/common/truth/TruthAssertThatTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
import com.google.common.reflect.TypeToken;
2626
import java.lang.reflect.Method;
2727
import java.lang.reflect.Modifier;
28+
import java.nio.file.Path;
29+
import java.util.OptionalDouble;
30+
import java.util.OptionalInt;
31+
import java.util.OptionalLong;
32+
import java.util.stream.IntStream;
33+
import java.util.stream.LongStream;
2834
import org.junit.Test;
2935
import org.junit.runner.RunWith;
3036
import org.junit.runners.JUnit4;
@@ -45,6 +51,13 @@ public void staticAssertThatMethodsMatchStandardSubjectBuilderInstanceMethods()
4551
ImmutableSortedSet<TypeToken<?>> verbTypes =
4652
FluentIterable.from(asList(StandardSubjectBuilder.class.getMethods()))
4753
.filter(input -> input.getName().equals("that"))
54+
// TODO: b/166630734 - Remove this when we add the assertThat overloads.
55+
.filter(input -> input.getParameterTypes()[0] != IntStream.class)
56+
.filter(input -> input.getParameterTypes()[0] != LongStream.class)
57+
.filter(input -> input.getParameterTypes()[0] != OptionalDouble.class)
58+
.filter(input -> input.getParameterTypes()[0] != OptionalInt.class)
59+
.filter(input -> input.getParameterTypes()[0] != OptionalLong.class)
60+
.filter(input -> input.getParameterTypes()[0] != Path.class)
4861
.transform(TruthAssertThatTest::methodToReturnTypeToken)
4962
.toSortedSet(Ordering.usingToString());
5063
ImmutableSortedSet<TypeToken<?>> truthTypes =

0 commit comments

Comments
 (0)