Make `IterableSubject.<A, E>comparingElementsUsing` accept `Correspondence<? super A, ? super E>` instead of `Correspondence<A, E>`. And similarly for MapSubject and MultimapSubject.
This accurately reflects the correct constraint. It also allows things which aren't possible at the moment, e.g. making assertions about an iterable of a subtype using a correspondence which accepts the supertype and then doing displayingDiffsPairedBy using a key function which accepts the subtype. (The caller will need to specify the generics rather than letting them be inferred, but at least it's possible --- right now, doing this would require an unchecked cast.)
While we're at it, replace `? extends Object` with the simpler `?` in a few places.
RELNOTES=n/a
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=239564279
diff --git a/core/src/main/java/com/google/common/truth/IterableSubject.java b/core/src/main/java/com/google/common/truth/IterableSubject.java
index 265cc11..8255777 100644
--- a/core/src/main/java/com/google/common/truth/IterableSubject.java
+++ b/core/src/main/java/com/google/common/truth/IterableSubject.java
@@ -869,7 +869,7 @@
* encounter an actual element that is not of type {@code A}.
*/
public <A, E> UsingCorrespondence<A, E> comparingElementsUsing(
- Correspondence<A, E> correspondence) {
+ Correspondence<? super A, ? super E> correspondence) {
return new UsingCorrespondence<>(this, correspondence);
}
@@ -913,10 +913,10 @@
* }</pre>
*
* <p><b>Important</b>: The {code keyFunction} function must be able to accept both the actual
- * and the unexpected elements, i.e. it must satisfy {@code Function<? super A, ? extends
- * Object>} as well as {@code Function<? super E, ? extends Object>}. If that constraint is not
- * met then a subsequent method may throw {@link ClassCastException}. Use the two-parameter
- * overload if you need to specify different key functions for the actual and expected elements.
+ * and the unexpected elements, i.e. it must satisfy {@code Function<? super A, ?>} as well as
+ * {@code Function<? super E, ?>}. If that constraint is not met then a subsequent method may
+ * throw {@link ClassCastException}. Use the two-parameter overload if you need to specify
+ * different key functions for the actual and expected elements.
*
* <p>On assertions where it makes sense to do so, the elements are paired as follows: they are
* keyed by {@code keyFunction}, and if an unexpected element and a missing element have the
@@ -942,11 +942,9 @@
* <p>Note that calling this method makes no difference to whether a test passes or fails, it
* just improves the message if it fails.
*/
- public UsingCorrespondence<A, E> displayingDiffsPairedBy(
- Function<? super E, ? extends Object> keyFunction) {
+ public UsingCorrespondence<A, E> displayingDiffsPairedBy(Function<? super E, ?> keyFunction) {
@SuppressWarnings("unchecked") // throwing ClassCastException is the correct behaviour
- Function<? super A, ? extends Object> actualKeyFunction =
- (Function<? super A, ? extends Object>) keyFunction;
+ Function<? super A, ?> actualKeyFunction = (Function<? super A, ?>) keyFunction;
return displayingDiffsPairedBy(actualKeyFunction, keyFunction);
}
@@ -987,8 +985,7 @@
* just improves the message if it fails.
*/
public UsingCorrespondence<A, E> displayingDiffsPairedBy(
- Function<? super A, ? extends Object> actualKeyFunction,
- Function<? super E, ? extends Object> expectedKeyFunction) {
+ Function<? super A, ?> actualKeyFunction, Function<? super E, ?> expectedKeyFunction) {
return new UsingCorrespondence<>(
subject, correspondence, new Pairer(actualKeyFunction, expectedKeyFunction));
}
diff --git a/core/src/main/java/com/google/common/truth/MapSubject.java b/core/src/main/java/com/google/common/truth/MapSubject.java
index 74a2e3b..11d880e 100644
--- a/core/src/main/java/com/google/common/truth/MapSubject.java
+++ b/core/src/main/java/com/google/common/truth/MapSubject.java
@@ -505,7 +505,7 @@
* type {@code E}.
*/
public <A, E> UsingCorrespondence<A, E> comparingValuesUsing(
- Correspondence<A, E> correspondence) {
+ Correspondence<? super A, ? super E> correspondence) {
return new UsingCorrespondence<>(correspondence);
}
@@ -518,9 +518,9 @@
*/
public final class UsingCorrespondence<A, E> {
- private final Correspondence<A, E> correspondence;
+ private final Correspondence<? super A, ? super E> correspondence;
- private UsingCorrespondence(Correspondence<A, E> correspondence) {
+ private UsingCorrespondence(Correspondence<? super A, ? super E> correspondence) {
this.correspondence = checkNotNull(correspondence);
}
diff --git a/core/src/main/java/com/google/common/truth/MultimapSubject.java b/core/src/main/java/com/google/common/truth/MultimapSubject.java
index 2424483..54cc332 100644
--- a/core/src/main/java/com/google/common/truth/MultimapSubject.java
+++ b/core/src/main/java/com/google/common/truth/MultimapSubject.java
@@ -517,7 +517,7 @@
* encounter an actual value that is not of type {@code A}.
*/
public <A, E> UsingCorrespondence<A, E> comparingValuesUsing(
- Correspondence<A, E> correspondence) {
+ Correspondence<? super A, ? super E> correspondence) {
return new UsingCorrespondence<>(correspondence);
}
@@ -530,9 +530,9 @@
*/
public final class UsingCorrespondence<A, E> {
- private final Correspondence<A, E> correspondence;
+ private final Correspondence<? super A, ? super E> correspondence;
- private UsingCorrespondence(Correspondence<A, E> correspondence) {
+ private UsingCorrespondence(Correspondence<? super A, ? super E> correspondence) {
this.correspondence = checkNotNull(correspondence);
}
@@ -753,9 +753,9 @@
private static final class EntryCorrespondence<K, A, E>
extends Correspondence<Entry<K, A>, Entry<K, E>> {
- private final Correspondence<A, ? super E> valueCorrespondence;
+ private final Correspondence<? super A, ? super E> valueCorrespondence;
- EntryCorrespondence(Correspondence<A, ? super E> valueCorrespondence) {
+ EntryCorrespondence(Correspondence<? super A, ? super E> valueCorrespondence) {
this.valueCorrespondence = valueCorrespondence;
}