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

Proposed major change: delete And interface #6

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Truth: We've made failure a strategy.
Truth: We've made failure a strategy.
4 changes: 2 additions & 2 deletions src/main/java/org/junit/contrib/truth/AbstractVerb.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ protected FailureStrategy getFailureStrategy() {
* @param factory a SubjectFactory<S, T> implementation
* @returns A custom verb for the type returned by the SubjectFactory
*/
public <S extends Subject<S,T>, T, SF extends SubjectFactory<S, T>>
public <S extends Subject<T>, T, SF extends SubjectFactory<S, T>>
DelegatedVerb<S, T> about(SF factory) {
return new DelegatedVerb<S, T>(getFailureStrategy(), factory);
}

/**
* A special Verb implementation which wraps a SubjectFactory
*/
public static class DelegatedVerb<S extends Subject<S,T>, T>
public static class DelegatedVerb<S extends Subject<T>, T>
extends AbstractVerb {

private final SubjectFactory<S, T> factory;
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/junit/contrib/truth/Expect.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

@SuppressWarnings("deprecation")
@SuppressWarnings("deprecation")
public class Expect extends TestVerb implements MethodRule {
protected static class ExpectationGatherer implements FailureStrategy {
List<String> messages = new ArrayList<String>();
Expand All @@ -45,14 +45,23 @@ public static Expect create() {
this.gatherer = gatherer;
}

@Override
protected FailureStrategy getFailureStrategy() {
if (!inRuleContext) {
String message = "assertion made on Expect instance, but it's not enabled as a @Rule.";
throw new IllegalStateException(message);
}
return super.getFailureStrategy();
}

// TODO(cgruber): Make this override TestRule when 4.9 is released.
@Override public Statement apply(final Statement base,
FrameworkMethod method, Object target) {
inRuleContext = true;
return new Statement() {

@Override public void evaluate() throws Throwable {
inRuleContext = true;
base.evaluate();
inRuleContext = false;
if (!gatherer.messages.isEmpty()) {
String message = "All failed expectations:\n";
for (int i = 0; i < gatherer.messages.size(); i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/junit/contrib/truth/TestVerb.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public StringSubject that(String target) {
return new StringSubject(getFailureStrategy(), target);
}

public <T, C extends Collection<T>> CollectionSubject<? extends CollectionSubject<?, T, C>, T, C> that(Collection<T> target) {
public <T, C extends Collection<T>> CollectionSubject<T, C> that(C target) {
return CollectionSubject.create(getFailureStrategy(), target);
}

public <T, C extends List<T>> ListSubject<? extends ListSubject<?, T, C>, T, C> that(List<T> target) {
public <T> ListSubject<T, List<T>> that(List<T> target) {
return ListSubject.create(getFailureStrategy(), target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @author Christian Gruber (cgruber@israfil.net)
*/
public class BooleanSubject extends Subject<BooleanSubject, Boolean> {
public class BooleanSubject extends Subject<Boolean> {

public BooleanSubject(FailureStrategy failureStrategy, Boolean subject) {
super(failureStrategy, subject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
import java.util.List;
import java.util.Set;

public class CollectionSubject<S extends CollectionSubject<S, T, C>, T, C extends Collection<T>> extends Subject<S, C> {
public class CollectionSubject<T, C extends Collection<T>> extends Subject<C> {

@SuppressWarnings("unchecked")
public static <T, C extends Collection<T>> CollectionSubject<? extends CollectionSubject<?, T, C>, T, C> create(
FailureStrategy failureStrategy, Collection<T> list) {
return new CollectionSubject(failureStrategy, list);
public static <T, C extends Collection<T>> CollectionSubject<T, C> create(
FailureStrategy failureStrategy, C list) {
return new CollectionSubject<T, C>(failureStrategy, list);
}

CollectionSubject(FailureStrategy failureStrategy, C list) {
Expand All @@ -40,33 +39,33 @@ public static <T, C extends Collection<T>> CollectionSubject<? extends Collectio
/**
* Attests that a Collection contains the provided object or fails.
*/
public And<S> contains(T item) {
public CollectionSubject<T,C> contains(T item) {
if (!getSubject().contains(item)) {
fail("contains", item);
}
return nextChain();
return this;
}

/**
* Attests that a Collection contains at least one of the provided
* objects or fails.
*/
public And<S> containsAnyOf(T ... items) {
public CollectionSubject<T,C> containsAnyOf(T ... items) {
Collection<T> collection = getSubject();
for (T item : items) {
if (collection.contains(item)) {
return nextChain();
return this;
}
}
fail("contains", (Object[])items);
return nextChain();
return this;
}

/**
* Attests that a Collection contains all of the provided objects or fails.
* This copes with duplicates in both the Collection and the parameters.
*/
public And<S> containsAllOf(T ... items) {
public CollectionSubject<T,C> containsAllOf(T ... items) {
Collection<T> collection = getSubject();
// Arrays.asList() does not support remove() so we need a mutable copy.
List<T> required = new ArrayList<T>(Arrays.asList(items));
Expand All @@ -84,7 +83,7 @@ public And<S> containsAllOf(T ... items) {
}
fail("contains", params);
}
return nextChain();
return this;
}

private static <T> int countOf(T t, T... items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.junit.contrib.truth.FailureStrategy;

public class DefaultSubject extends Subject<DefaultSubject, Object> {
public class DefaultSubject extends Subject<Object> {
public DefaultSubject(FailureStrategy failureStrategy, Object o) {
super(failureStrategy, o);
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/org/junit/contrib/truth/subjects/IntegerSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @author David Saff
* @author Christian Gruber (cgruber@israfil.net)
*/
public class IntegerSubject extends Subject<IntegerSubject, Long> {
public class IntegerSubject extends Subject<Long> {

private static final String RANGE_BOUNDS_OUT_OF_ORDER_MSG = "Range inclusion parameter lower (%d) should not be greater than upper (%d)";

Expand All @@ -43,12 +43,12 @@ public IntegerSubject(FailureStrategy failureStrategy, Integer i) {
* @throws IllegalArgumentException
* if the lower bound is greater than the upper.
*/
public And<IntegerSubject> isInclusivelyInRange(long lower, long upper) {
public IntegerSubject isInclusivelyInRange(long lower, long upper) {
ensureOrderedBoundaries(lower, upper);
if (!(lower <= getSubject() && getSubject() <= upper)) {
fail("is inclusively in range", lower, upper);
}
return nextChain();
return this;
}

/**
Expand All @@ -58,12 +58,12 @@ public And<IntegerSubject> isInclusivelyInRange(long lower, long upper) {
* @throws IllegalArgumentException
* if the lower bound is greater than the upper.
*/
public And<IntegerSubject> isBetween(long lower, long upper) {
public IntegerSubject isBetween(long lower, long upper) {
ensureOrderedBoundaries(lower, upper);
if (!(lower < getSubject() && getSubject() < upper)) {
fail("is in between", lower, upper);
}
return nextChain();
return this;
}

/**
Expand All @@ -77,11 +77,11 @@ private void ensureOrderedBoundaries(long lower, long upper) {
}
}

public And<IntegerSubject> isEqualTo(Integer other) {
public IntegerSubject isEqualTo(Integer other) {
return isEqualTo((other == null) ? null : new Long(other.longValue()));
}

public And<IntegerSubject> isEqualTo(Long other) {
public IntegerSubject isEqualTo(Long other) {
if (getSubject() == null) {
if(other != null) {
fail("is equal to", other);
Expand All @@ -92,14 +92,14 @@ public And<IntegerSubject> isEqualTo(Long other) {
fail("is equal to", other);
}
}
return nextChain();
return this;
}

public And<IntegerSubject> isNotEqualTo(Integer other) {
public IntegerSubject isNotEqualTo(Integer other) {
return isNotEqualTo((other == null) ? null : new Long(other.longValue()));
}

public And<IntegerSubject> isNotEqualTo(Long other) {
public IntegerSubject isNotEqualTo(Long other) {
if (getSubject() == null) {
if(other == null) {
fail("is not equal to", other);
Expand All @@ -110,7 +110,7 @@ public And<IntegerSubject> isNotEqualTo(Long other) {
fail("is not equal to", other);
}
}
return nextChain();
return this;
}


Expand Down
29 changes: 14 additions & 15 deletions src/main/java/org/junit/contrib/truth/subjects/ListSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import java.util.Comparator;
import java.util.List;

public class ListSubject<S extends ListSubject<S, T, C>, T, C extends List<T>>
extends CollectionSubject<S, T, C> {
public class ListSubject<T, C extends List<T>>
extends CollectionSubject<T, C> {

@SuppressWarnings("unchecked")
public static <T, C extends List<T>> ListSubject<? extends ListSubject<?, T, C>, T, C> create(
public static <T> ListSubject<T, List<T>> create(
FailureStrategy failureStrategy, List<T> list) {
return new ListSubject(failureStrategy, list);
return new ListSubject<T, List<T>>(failureStrategy, list);
}

protected ListSubject(FailureStrategy failureStrategy, C list) {
Expand All @@ -36,9 +35,9 @@ protected ListSubject(FailureStrategy failureStrategy, C list) {
/**
* Attests that a List contains the specified sequence.
*/
public And<S> containsSequence(List<T> sequence) {
public ListSubject<T,C> containsSequence(List<T> sequence) {
if (sequence.isEmpty()) {
return nextChain();
return this;
}
List<T> list = getSubject();
while (true) {
Expand All @@ -51,12 +50,12 @@ public And<S> containsSequence(List<T> sequence) {
break; // Not enough room left
}
if (sequence.equals(list.subList(first, last))) {
return nextChain();
return this;
}
list = list.subList(first + 1, list.size());
}
fail("contains sequence", sequence);
return nextChain();
return this;
}

/**
Expand All @@ -66,7 +65,7 @@ public And<S> containsSequence(List<T> sequence) {
* @throws ClassCastException if any pair of elements is not mutually Comparable.
* @throws NullPointerException if any element is null.
*/
public And<S> isOrdered() {
public ListSubject<T,C> isOrdered() {
return pairwiseCheck(new PairwiseChecker<T>() {
@SuppressWarnings("unchecked")
@Override public void check(T prev, T next) {
Expand All @@ -84,7 +83,7 @@ public And<S> isOrdered() {
* @throws ClassCastException if any pair of elements is not mutually Comparable.
* @throws NullPointerException if any element is null.
*/
public And<S> isPartiallyOrdered() {
public ListSubject<T,C> isPartiallyOrdered() {
return pairwiseCheck(new PairwiseChecker<T>() {
@SuppressWarnings("unchecked")
@Override public void check(T prev, T next) {
Expand All @@ -102,7 +101,7 @@ public And<S> isPartiallyOrdered() {
* @throws ClassCastException if any pair of elements is not mutually Comparable.
* @throws NullPointerException if any element is null.
*/
public And<S> isOrdered(final Comparator<T> comparator) {
public ListSubject<T,C> isOrdered(final Comparator<T> comparator) {
return pairwiseCheck(new PairwiseChecker<T>() {
@Override public void check(T prev, T next) {
if (comparator.compare(prev, next) >= 0) {
Expand All @@ -119,7 +118,7 @@ public And<S> isOrdered(final Comparator<T> comparator) {
* @throws ClassCastException if any pair of elements is not mutually Comparable.
* @throws NullPointerException if any element is null.
*/
public And<S> isPartiallyOrdered(final Comparator<T> comparator) {
public ListSubject<T,C> isPartiallyOrdered(final Comparator<T> comparator) {
return pairwiseCheck(new PairwiseChecker<T>() {
@Override public void check(T prev, T next) {
if (comparator.compare(prev, next) > 0) {
Expand All @@ -129,7 +128,7 @@ public And<S> isPartiallyOrdered(final Comparator<T> comparator) {
});
}

private And<S> pairwiseCheck(PairwiseChecker<T> checker) {
private ListSubject<T,C> pairwiseCheck(PairwiseChecker<T> checker) {
List<T> list = getSubject();
if (list.size() > 1) {
T prev = list.get(0);
Expand All @@ -139,7 +138,7 @@ private And<S> pairwiseCheck(PairwiseChecker<T> checker) {
prev = next;
}
}
return nextChain();
return this;
}

private interface PairwiseChecker<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,42 @@
* @author David Saff
* @author Christian Gruber (cgruber@israfil.net)
*/
public class StringSubject extends Subject<StringSubject, String> {
public class StringSubject extends Subject<String> {
public StringSubject(FailureStrategy failureStrategy, String string) {
super(failureStrategy, string);
}

public And<StringSubject> contains(String string) {
public StringSubject contains(String string) {
if (getSubject() == null) {
if (string != null) {
fail("contains", string);
}
} else if (!getSubject().contains(string)) {
fail("contains", string);
}
return nextChain();
return this;
}

public And<StringSubject> startsWith(String string) {
public StringSubject startsWith(String string) {
if (getSubject() == null) {
if (string != null) {
fail("starts with", string);
}
} else if (!getSubject().startsWith(string)) {
fail("starts with", string);
}
return nextChain();
return this;
}

public And<StringSubject> endsWith(String string) {
public StringSubject endsWith(String string) {
if (getSubject() == null) {
if (string != null) {
fail("ends with", string);
}
} else if (!getSubject().endsWith(string)) {
fail("ends with", string);
}
return nextChain();
return this;
}

}
Loading