Skip to content

Commit

Permalink
Make "value of" lines work with StreamSubject.
Browse files Browse the repository at this point in the history
...by migrating off the deprecated no-arg `check()` overload.

Usages of that method [cause problems for "value of" lines](https://github.com/google/truth/blob/37fd8bea90c0ab4528c4c922c88fa176eb45f65b/core/src/main/java/com/google/common/truth/FailureMetadata.java#L230-L232).

Also, fill in a `typeDescriptionOverride` value for `MultisetSubject`. This is largely a no-op, but it helps under GWT/J2CL (where [we can't infer the type name](https://github.com/google/truth/blob/37fd8bea90c0ab4528c4c922c88fa176eb45f65b/core/src/main/java/com/google/common/truth/super/com/google/common/truth/Platform.java#L72)).

I left `IterableSubject` itself without a `typeDescriptionOverride`, since it has subclasses outside the package, some of which may prefer the more specific names (which are normally automatically derived from the `Subject` class's name—e.g., `AttributeListSubject` would result in `value of: attributeList.size()`). In hindsight, I guess that I could have given _direct_ usages of `IterableSubject` a `typeDescriptionOverride` (by making `StandardSubjectBuilder` pass one), but that seems like more effort than is worthwhile now that I've already tested this CL as it stands.

RELNOTES=n/a
PiperOrigin-RevId: 598964798
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jan 16, 2024
1 parent 37fd8be commit 16db780
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
10 changes: 9 additions & 1 deletion core/src/main/java/com/google/common/truth/IterableSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ public class IterableSubject extends Subject {
* {@link Subject#check(String, Object...) check(...)}{@code .that(actual)}.
*/
protected IterableSubject(FailureMetadata metadata, @Nullable Iterable<?> iterable) {
super(metadata, iterable);
this(metadata, iterable, null);
}

/** Constructor for use by package-private callers. */
IterableSubject(
FailureMetadata metadata,
@Nullable Iterable<?> iterable,
@Nullable String typeDescriptionOverride) {
super(metadata, iterable, typeDescriptionOverride);
this.actual = iterable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class MultisetSubject extends IterableSubject {
private final @Nullable Multiset<?> actual;

MultisetSubject(FailureMetadata metadata, @Nullable Multiset<?> multiset) {
super(metadata, multiset);
super(metadata, multiset, /* typeDescriptionOverride= */ "multiset");
this.actual = multiset;
}

Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/com/google/common/truth/StreamSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,16 @@ public void isNotEqualTo(@Nullable Object unexpected) {

// TODO(user): Do we want to support comparingElementsUsing() on StreamSubject?

// TODO: b/134064106 - Migrate off no-arg check (to a direct IterableSubject constructor call?)
@SuppressWarnings("deprecation")
private IterableSubject checkThatContentsList() {
return check().that(listSupplier.get());
/*
* Calling Subject constructors directly is usually not advisable: It does not update the
* metadata, so the resultant failure message might say (for example) "value of: foo" when it
* should say "value of: foo.size()." However, in this specific case, that's exactly what we
* want: We're testing the contents of the stream, so we want a "value of" line for the stream,
* even though we happen to implement the contents check by delegating to IterableSubject.
*/
return new IterableSubject(
metadata, listSupplier.get(), /* typeDescriptionOverride= */ "stream");
}

private static Supplier<@Nullable List<?>> listCollector(@Nullable Stream<?> actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ public void testHasSize() throws Exception {

@Test
public void testHasSize_fails() throws Exception {
AssertionError unused =
AssertionError failure =
expectFailure(whenTesting -> whenTesting.that(Stream.of("hello")).hasSize(2));
assertThat(failure).factValue("value of").isEqualTo("stream.size()");
}

@Test
Expand Down

0 comments on commit 16db780

Please sign in to comment.