Skip to content

Commit

Permalink
Various last-second API tweaks identified by looking through the Java…
Browse files Browse the repository at this point in the history
…doc.

For details, see release notes.

These all seem minor enough to squeeze in before 1.0, since we've still got a week (albeit a short week in the U.S.).

In addition to those changes, I:

- Updated Fact.toString() Javadoc.
- Removed type parameters from some internal MultimapSubject methods.
- Moved MultimapSubject.UsingCorrespondence containsExactly overloads together.

(The DoubleSubject/FloatSubject non-override appears to be an oversight, judging from CL 90636937.)

(Technically speaking, we could make all these changes after 1.0 -- with the notable exception of the `DoubleSubject`/`FloatSubject` changes -- because we promise only to be binary-compatible, not source-compatible. But it seems nice to fix all the problems we know about now :))

Relnotes:
  - Changed `DoubleSubject` and `FloatSubject` to override `isEqualTo` and `isNotEqualTo` instead of declaring an overload.
  - Changed `MultimapSubject.UsingCorrespondence` methods `containsExactly` and `containsAtLeast` to require an `E` for their one non-varargs value argument, and removed their type parameters.
  - Eliminated type parameters on `MapSubject.UsingCorrespondence` and `MultimapSubject.UsingCorrespondence` methods `containsExactlyEntriesIn` and `containsAtLeastEntriesIn`.
  - Changed `IntStreamSubject` and `LongStreamSubject` methods `isInOrder` and `isInStrictOrder` to require a compatible `Comparator`.

[]

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=255946872
  • Loading branch information
cpovirk committed Jul 1, 2019
1 parent 98f5df4 commit 4743c14
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 31 deletions.
6 changes: 4 additions & 2 deletions core/src/main/java/com/google/common/truth/DoubleSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public void of(double expected) {
* vice versa. For an assertion that passes for either {@code 0.0} or {@code -0.0}, use {@link
* #isZero}.
*/
public final void isEqualTo(@NullableDecl Double other) {
@Override
public final void isEqualTo(@NullableDecl Object other) {
super.isEqualTo(other);
}

Expand All @@ -190,7 +191,8 @@ public final void isEqualTo(@NullableDecl Double other) {
* versa. For an assertion that fails for either {@code 0.0} or {@code -0.0}, use {@link
* #isNonZero}.
*/
public final void isNotEqualTo(@NullableDecl Double other) {
@Override
public final void isNotEqualTo(@NullableDecl Object other) {
super.isNotEqualTo(other);
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/com/google/common/truth/Fact.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ private Fact(String key, @NullableDecl String value) {
}

/**
* Returns a simple string representation for the fact. While this is used by the old-style
* messages and {@code TruthFailureSubject} output, we're moving away from the old-style messages
* and onto {@link #makeMessage}, which aligns facts horizontally and indents multiline values.
* Returns a simple string representation for the fact. While this is used in the output of {@code
* TruthFailureSubject}, it's not used in normal failure messages, which automatically align facts
* horizontally and indent multiline values.
*/
@Override
public String toString() {
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/com/google/common/truth/FloatSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public void of(float expected) {
* vice versa. For an assertion that passes for either {@code 0.0f} or {@code -0.0f}, use {@link
* #isZero}.
*/
public final void isEqualTo(@NullableDecl Float other) {
@Override
public final void isEqualTo(@NullableDecl Object other) {
super.isEqualTo(other);
}

Expand All @@ -198,7 +199,8 @@ public final void isEqualTo(@NullableDecl Float other) {
* versa. For an assertion that fails for either {@code 0.0f} or {@code -0.0f}, use {@link
* #isNonZero}.
*/
public final void isNotEqualTo(@NullableDecl Float other) {
@Override
public final void isNotEqualTo(@NullableDecl Object other) {
super.isNotEqualTo(other);
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/google/common/truth/MapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ public Ordered containsAtLeast(@NullableDecl Object k0, @NullableDecl E v0, Obje
* correspond to the values of the given map.
*/
@CanIgnoreReturnValue
public <K, V extends E> Ordered containsExactlyEntriesIn(Map<K, V> expectedMap) {
public Ordered containsExactlyEntriesIn(Map<?, ? extends E> expectedMap) {
if (expectedMap.isEmpty()) {
if (actual.isEmpty()) {
return IN_ORDER;
Expand All @@ -720,7 +720,7 @@ public <K, V extends E> Ordered containsExactlyEntriesIn(Map<K, V> expectedMap)
* correspond to the values of the given map.
*/
@CanIgnoreReturnValue
public <K, V extends E> Ordered containsAtLeastEntriesIn(Map<K, V> expectedMap) {
public Ordered containsAtLeastEntriesIn(Map<?, ? extends E> expectedMap) {
if (expectedMap.isEmpty()) {
return IN_ORDER;
}
Expand Down
54 changes: 36 additions & 18 deletions core/src/main/java/com/google/common/truth/MultimapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ private static boolean advanceToFind(Iterator<?> iterator, Object value) {
return false;
}

private static <K, V> Collection<V> get(Multimap<K, V> multimap, @NullableDecl Object key) {
private static <V> Collection<V> get(Multimap<?, V> multimap, @NullableDecl Object key) {
if (multimap.containsKey(key)) {
return multimap.asMap().get(key);
} else {
Expand Down Expand Up @@ -495,10 +495,10 @@ private static List<?> difference(List<?> minuend, List<?> subtrahend) {
return difference;
}

private static <K, V> String countDuplicatesMultimap(Multimap<K, V> multimap) {
private static String countDuplicatesMultimap(Multimap<?, ?> multimap) {
List<String> entries = new ArrayList<>();
for (K key : multimap.keySet()) {
entries.add(key + "=" + SubjectUtils.countDuplicates(multimap.get(key)));
for (Object key : multimap.keySet()) {
entries.add(key + "=" + SubjectUtils.countDuplicates(get(multimap, key)));
}

StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -713,7 +713,17 @@ public void doesNotContainEntry(
* order.
*/
@CanIgnoreReturnValue
public <K, V extends E> Ordered containsExactlyEntriesIn(Multimap<K, V> expectedMultimap) {
public Ordered containsExactlyEntriesIn(Multimap<?, ? extends E> expectedMultimap) {
return internalContainsExactlyEntriesIn(expectedMultimap);
}

/*
* This helper exists so that we can declare the simpler, type-parameter-free signature for the
* public containsExactlyEntriesIn method. This is recommended by Effective Java item 31 (3rd
* edition).
*/
private <K, V extends E> Ordered internalContainsExactlyEntriesIn(
Multimap<K, V> expectedMultimap) {
// Note: The non-fuzzy MultimapSubject.containsExactlyEntriesIn has a custom implementation
// and produces somewhat better failure messages simply asserting about the iterables of
// entries would: it formats the expected values as k=[v1, v2] rather than k=v1, k=v2; and in
Expand All @@ -737,7 +747,17 @@ public <K, V extends E> Ordered containsExactlyEntriesIn(Multimap<K, V> expected
* order.
*/
@CanIgnoreReturnValue
public <K, V extends E> Ordered containsAtLeastEntriesIn(Multimap<K, V> expectedMultimap) {
public Ordered containsAtLeastEntriesIn(Multimap<?, ? extends E> expectedMultimap) {
return internalContainsAtLeastEntriesIn(expectedMultimap);
}

/*
* This helper exists so that we can declare the simpler, type-parameter-free signature for the
* public containsAtLeastEntriesIn method. This is recommended by Effective Java item 31 (3rd
* edition).
*/
private <K, V extends E> Ordered internalContainsAtLeastEntriesIn(
Multimap<K, V> expectedMultimap) {
// Note: The non-fuzzy MultimapSubject.containsAtLeastEntriesIn has a custom implementation
// and produces somewhat better failure messages simply asserting about the iterables of
// entries would: it formats the expected values as k=[v1, v2] rather than k=v1, k=v2; and in
Expand All @@ -758,33 +778,31 @@ public <K, V extends E> Ordered containsAtLeastEntriesIn(Multimap<K, V> expected
* key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
*/
@CanIgnoreReturnValue
public <K, V extends E> Ordered containsExactly(
@NullableDecl Object k0, @NullableDecl Object v0, Object... rest) {
public Ordered containsExactly(@NullableDecl Object k0, @NullableDecl E v0, Object... rest) {
@SuppressWarnings("unchecked")
Multimap<K, V> expectedMultimap = (Multimap<K, V>) accumulateMultimap(k0, v0, rest);
Multimap<?, E> expectedMultimap = (Multimap<?, E>) accumulateMultimap(k0, v0, rest);
return containsExactlyEntriesIn(expectedMultimap);
}

/** Fails if the multimap is not empty. */
@CanIgnoreReturnValue
public Ordered containsExactly() {
return MultimapSubject.this.containsExactly();
}

/**
* Fails if the multimap does not contain at least the given key/value pairs.
*
* <p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
* key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
*/
@CanIgnoreReturnValue
public <K, V extends E> Ordered containsAtLeast(
@NullableDecl Object k0, @NullableDecl Object v0, Object... rest) {
public Ordered containsAtLeast(@NullableDecl Object k0, @NullableDecl E v0, Object... rest) {
@SuppressWarnings("unchecked")
Multimap<K, V> expectedMultimap = (Multimap<K, V>) accumulateMultimap(k0, v0, rest);
Multimap<?, E> expectedMultimap = (Multimap<?, E>) accumulateMultimap(k0, v0, rest);
return containsAtLeastEntriesIn(expectedMultimap);
}

/** Fails if the multimap is not empty. */
@CanIgnoreReturnValue
public <K, V extends E> Ordered containsExactly() {
return MultimapSubject.this.containsExactly();
}

@SuppressWarnings("unchecked") // throwing ClassCastException is the correct behaviour
private Multimap<?, A> getCastActual() {
return (Multimap<?, A>) actual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ public void isNotWithinNonFinite() {
assertThatIsNotWithinFails(+1.0, 0.00001, Double.NEGATIVE_INFINITY);
}

@SuppressWarnings("TruthSelfEquals")
@Test
public void isEqualTo() {
assertThat(1.23).isEqualTo(1.23);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ public void isNotWithinNonFinite() {
assertThatIsNotWithinFails(+1.0f, 0.00001f, Float.NEGATIVE_INFINITY);
}

@SuppressWarnings("TruthSelfEquals")
@Test
public void isEqualTo() {
assertThat(GOLDEN).isEqualTo(GOLDEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void isInStrictOrder() {
*
* @throws ClassCastException if any pair of elements is not mutually Comparable
*/
public void isInStrictOrder(Comparator<?> comparator) {
public void isInStrictOrder(Comparator<? super Integer> comparator) {
check().that(actualList).isInStrictOrder(comparator);
}

Expand All @@ -219,7 +219,7 @@ public void isInOrder() {
*
* @throws ClassCastException if any pair of elements is not mutually Comparable
*/
public void isInOrder(Comparator<?> comparator) {
public void isInOrder(Comparator<? super Integer> comparator) {
check().that(actualList).isInOrder(comparator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void isInStrictOrder() {
*
* @throws ClassCastException if any pair of elements is not mutually Comparable
*/
public void isInStrictOrder(Comparator<?> comparator) {
public void isInStrictOrder(Comparator<? super Long> comparator) {
check().that(actualList).isInStrictOrder(comparator);
}

Expand All @@ -219,7 +219,7 @@ public void isInOrder() {
*
* @throws ClassCastException if any pair of elements is not mutually Comparable
*/
public void isInOrder(Comparator<?> comparator) {
public void isInOrder(Comparator<? super Long> comparator) {
check().that(actualList).isInOrder(comparator);
}

Expand Down

0 comments on commit 4743c14

Please sign in to comment.