Skip to content

Commit

Permalink
Add isEquivalentAccordingToCompareTo(), in prep for removing compares…
Browse files Browse the repository at this point in the history
…EqualTo().

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=91308469
  • Loading branch information
kluever authored and cgruber committed Apr 25, 2015
1 parent 7209e07 commit de1ff4b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 8 deletions.
10 changes: 9 additions & 1 deletion core/src/main/java/com/google/common/truth/BooleanSubject.java
Expand Up @@ -28,12 +28,20 @@ public class BooleanSubject extends ComparableSubject<BooleanSubject, Boolean> {
super(failureStrategy, subject);
}

/**
* @deprecated Use {@link #isEqualTo} instead. Boolean comparison is consistent with equality.
*/
@Deprecated
public final void isEquivalentAccordingToCompareTo(Boolean other) {
super.isEquivalentAccordingToCompareTo(other);
}

/**
* @deprecated Use {@link #isEqualTo} instead. Boolean comparison is consistent with equality.
*/
@Deprecated
public final void comparesEqualTo(Boolean other) {
super.comparesEqualTo(other);
super.isEquivalentAccordingToCompareTo(other);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions core/src/main/java/com/google/common/truth/ComparableSubject.java
Expand Up @@ -56,12 +56,25 @@ public final void isNotIn(Range<T> range) {
* <p><b>Note:</b> Do not use this method for checking object equality. Instead, use
* {@link #isEqualTo(Object)}.
*/
public void comparesEqualTo(T other) {
public void isEquivalentAccordingToCompareTo(T other) {
if (getSubject().compareTo(other) != 0) {
failWithRawMessage("%s should have been equivalent to <%s>", getDisplaySubject(), other);
failWithRawMessage("%s should have been equivalent to <%s> according to compareTo()",
getDisplaySubject(), other);
}
}

/**
* Fails if the subject is not equivalent to the given value according to
* {@link Comparable#compareTo}, (i.e., fails if {@code a.comparesTo(b) != 0}).
*
* <p><b>Note:</b> Do not use this method for checking object equality. Instead, use
* {@link #isEqualTo(Object)}.
*/
// TODO(kak): @deprecated Use {@link #isEquivalentAccordingToCompareTo} instead.
public void comparesEqualTo(T other) {
isEquivalentAccordingToCompareTo(other);
}

/**
* Fails if the subject is not greater than the given value.
*/
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/com/google/common/truth/DoubleSubject.java
Expand Up @@ -134,12 +134,20 @@ public final void isNotEqualTo(@Nullable Double other) {
super.isNotEqualTo(other);
}

/**
* @deprecated Use {@link #isWithin} instead. Double comparison should always have a tolerance.
*/
@Deprecated
public final void isEquivalentAccordingToCompareTo(Double other) {
super.isEquivalentAccordingToCompareTo(other);
}

/**
* @deprecated Use {@link #isWithin} instead. Double comparison should always have a tolerance.
*/
@Deprecated
public final void comparesEqualTo(Double other) {
super.comparesEqualTo(other);
super.isEquivalentAccordingToCompareTo(other);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/com/google/common/truth/IntegerSubject.java
Expand Up @@ -33,12 +33,20 @@ public IntegerSubject(FailureStrategy failureStrategy, @Nullable Integer integer
super(failureStrategy, integer);
}

/**
* @deprecated Use {@link #isEqualTo} instead. Integer comparison is consistent with equality.
*/
@Deprecated
public final void isEquivalentAccordingToCompareTo(Integer other) {
super.isEquivalentAccordingToCompareTo(other);
}

/**
* @deprecated Use {@link #isEqualTo} instead. Integer comparison is consistent with equality.
*/
@Deprecated
public final void comparesEqualTo(Integer other) {
super.comparesEqualTo(other);
super.isEquivalentAccordingToCompareTo(other);
}

public void isEqualTo(Object other) {
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/com/google/common/truth/LongSubject.java
Expand Up @@ -31,12 +31,20 @@ public LongSubject(FailureStrategy failureStrategy, @Nullable Long subject) {
super(failureStrategy, subject);
}

/**
* @deprecated Use {@link #isEqualTo} instead. Long comparison is consistent with equality.
*/
@Deprecated
public final void isEquivalentAccordingToCompareTo(Long other) {
super.isEquivalentAccordingToCompareTo(other);
}

/**
* @deprecated Use {@link #isEqualTo} instead. Long comparison is consistent with equality.
*/
@Deprecated
public final void comparesEqualTo(Long other) {
super.comparesEqualTo(other);
super.isEquivalentAccordingToCompareTo(other);
}

public void isEqualTo(Object other) {
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/com/google/common/truth/StringSubject.java
Expand Up @@ -71,12 +71,20 @@ public StringSubject(FailureStrategy failureStrategy, @Nullable String string) {
}
}

/**
* @deprecated Use {@link #isEqualTo} instead. String comparison is consistent with equality.
*/
@Deprecated
public final void isEquivalentAccordingToCompareTo(String other) {
super.isEquivalentAccordingToCompareTo(other);
}

/**
* @deprecated Use {@link #isEqualTo} instead. String comparison is consistent with equality.
*/
@Deprecated
public final void comparesEqualTo(String other) {
super.comparesEqualTo(other);
super.isEquivalentAccordingToCompareTo(other);
}

/**
Expand Down
Expand Up @@ -98,7 +98,8 @@ public class ComparableSubjectTest {
assertThat(new BigDecimal("2.0")).comparesEqualTo(new BigDecimal("2.1"));
fail("should have thrown");
} catch (AssertionError e) {
assertThat(e.getMessage()).isEqualTo("<2.0> should have been equivalent to <2.1>");
assertThat(e)
.hasMessage("<2.0> should have been equivalent to <2.1> according to compareTo()");
}
}

Expand Down

0 comments on commit de1ff4b

Please sign in to comment.