Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-Comparable subjects to be compared using an explicit Comparator #191

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import javax.annotation.Nullable;

public class DefaultSubject extends Subject<DefaultSubject, Object> {
public DefaultSubject(FailureStrategy failureStrategy, @Nullable Object o) {
public class DefaultSubject<T> extends Subject<DefaultSubject<T>, T> {
public DefaultSubject(FailureStrategy failureStrategy, @Nullable T o) {
super(failureStrategy, o);
}
}
106 changes: 106 additions & 0 deletions core/src/main/java/com/google/common/truth/FauxComparable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2015 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.truth;

import java.util.Comparator;

import javax.annotation.Nullable;

/**
* Propositions which use an explicit {@link Comparator}.
*
* @author Ben Blank
*/
public class FauxComparable<S extends Subject<S, T>, T> {
private final Comparator<? super T> comparator;
private final Subject<S, T> subject;

public FauxComparable(Comparator<? super T> comparator, @Nullable Subject<S, T> subject) {
this.comparator = comparator;
this.subject = subject;
}

/**
* Fails if the subject is not equivalent to the given value according to
* the supplied {@link Comparator}, (i.e., fails if
* {@code comparator.compare(a, b) != 0}).
*
* <p>
* <b>Note:</b> Do not use this method for checking object equality.
* Instead, use {@link #isEqualTo(Object)}.
*/
public void isEquivalentAccordingToComparator(T other) {
if (comparator.compare(subject.getSubject(), other) != 0) {
subject.failWithRawMessage(
"%s should have been equivalent to <%s> according to comparator <%s>",
subject.getDisplaySubject(),
other,
comparator);
}
}

/**
* Fails if the subject is not greater than the given value.
*/
public final void isGreaterThan(T other) {
if (comparator.compare(subject.getSubject(), other) <= 0) {
subject.failWithRawMessage(
"Not true that %s is greater than <%s> according to comparator <%s>",
subject.getDisplaySubject(),
other,
comparator);
}
}

/**
* Fails if the subject is not less than the given value.
*/
public final void isLessThan(T other) {
if (comparator.compare(subject.getSubject(), other) >= 0) {
subject.failWithRawMessage(
"Not true that %s is less than <%s> according to comparator <%s>",
subject.getDisplaySubject(),
other,
comparator);
}
}

/**
* Fails if the subject is greater than the given value.
*/
public final void isAtMost(T other) {
if (comparator.compare(subject.getSubject(), other) > 0) {
subject.failWithRawMessage(
"Not true that %s is at most <%s> according to comparator <%s>",
subject.getDisplaySubject(),
other,
comparator);
}
}

/**
* Fails if the subject is less than the given value.
*/
public final void isAtLeast(T other) {
if (comparator.compare(subject.getSubject(), other) < 0) {
subject.failWithRawMessage(
"Not true that %s is at least <%s> according to comparator <%s>",
subject.getDisplaySubject(),
other,
comparator);
}
}
}
5 changes: 5 additions & 0 deletions core/src/main/java/com/google/common/truth/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;

import java.util.Comparator;
import java.util.List;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -191,6 +192,10 @@ public void isNoneOf(@Nullable Object first, @Nullable Object second, @Nullable
isNotIn(accumulate(first, second, rest));
}

public FauxComparable<S, T> whenComparedUsing(Comparator<? super T> comparator) {
return new FauxComparable<S, T>(checkNotNull(comparator), this);
}

protected T getSubject() {
return subject;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/google/common/truth/TestVerb.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public BigDecimalSubject that(@Nullable BigDecimal target) {
return new BigDecimalSubject(getFailureStrategy(), target);
}

public Subject<DefaultSubject, Object> that(@Nullable Object target) {
public <T> Subject<DefaultSubject<T>, T> that(@Nullable T target) {
return new DefaultSubject(getFailureStrategy(), target);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/google/common/truth/Truth.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static BigDecimalSubject assertThat(@Nullable BigDecimal target) {
return assert_().that(target);
}

public static Subject<DefaultSubject, Object> assertThat(@Nullable Object target) {
public static <T> Subject<DefaultSubject<T>, T> assertThat(@Nullable T target) {
return assert_().that(target);
}

Expand Down
Loading