Skip to content
Browse files

Killed And<>, thinking about going to one-arg CollectionSubject

  • Loading branch information...
1 parent af8a76c commit ff5bc6c15704a6f701dacb119239e4cf49fb8d55 David Saff committed Aug 22, 2011
View
2 README
@@ -1 +1,3 @@
Truth: We've made failure a strategy.
+
+# SAFF: spur
View
4 src/main/java/org/junit/contrib/truth/AbstractVerb.java
@@ -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;
View
4 src/main/java/org/junit/contrib/truth/TestVerb.java
@@ -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);
}
}
View
2 src/main/java/org/junit/contrib/truth/subjects/BooleanSubject.java
@@ -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);
View
23 src/main/java/org/junit/contrib/truth/subjects/CollectionSubject.java
@@ -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) {
@@ -40,33 +39,33 @@
/**
* 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));
@@ -84,7 +83,7 @@
}
fail("contains", params);
}
- return nextChain();
+ return this;
}
private static <T> int countOf(T t, T... items) {
View
2 src/main/java/org/junit/contrib/truth/subjects/DefaultSubject.java
@@ -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);
}
View
22 src/main/java/org/junit/contrib/truth/subjects/IntegerSubject.java
@@ -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)";
@@ -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;
}
/**
@@ -58,12 +58,12 @@ public IntegerSubject(FailureStrategy failureStrategy, Integer i) {
* @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;
}
/**
@@ -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);
@@ -92,14 +92,14 @@ private void ensureOrderedBoundaries(long lower, long upper) {
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);
@@ -110,7 +110,7 @@ private void ensureOrderedBoundaries(long lower, long upper) {
fail("is not equal to", other);
}
}
- return nextChain();
+ return this;
}
View
31 src/main/java/org/junit/contrib/truth/subjects/ListSubject.java
@@ -20,13 +20,14 @@
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);
+ // TODO: SAFF: is this right typing?
+ // TODO: SAFF: also Collection?
+ return new ListSubject<T, List<T>>(failureStrategy, list);
}
protected ListSubject(FailureStrategy failureStrategy, C list) {
@@ -36,9 +37,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) {
@@ -51,12 +52,12 @@ protected ListSubject(FailureStrategy failureStrategy, C list) {
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;
}
/**
@@ -66,7 +67,7 @@ protected ListSubject(FailureStrategy failureStrategy, C list) {
* @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) {
@@ -84,7 +85,7 @@ protected ListSubject(FailureStrategy failureStrategy, C list) {
* @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) {
@@ -102,7 +103,7 @@ protected ListSubject(FailureStrategy failureStrategy, C list) {
* @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) {
@@ -119,7 +120,7 @@ protected ListSubject(FailureStrategy failureStrategy, C list) {
* @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) {
@@ -129,7 +130,7 @@ protected ListSubject(FailureStrategy failureStrategy, C list) {
});
}
- 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);
@@ -139,7 +140,7 @@ protected ListSubject(FailureStrategy failureStrategy, C list) {
prev = next;
}
}
- return nextChain();
+ return this;
}
private interface PairwiseChecker<T> {
View
14 src/main/java/org/junit/contrib/truth/subjects/StringSubject.java
@@ -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;
}
}
View
41 src/main/java/org/junit/contrib/truth/subjects/Subject.java
@@ -25,32 +25,15 @@
* @author David Saff
* @author Christian Gruber (cgruber@israfil.net)
*/
-public class Subject<S extends Subject<S,T>,T> {
+public class Subject<T> {
private final FailureStrategy failureStrategy;
private final T subject;
- private final And<S> chain;
-
public Subject(FailureStrategy failureStrategy, T subject) {
this.failureStrategy = failureStrategy;
this.subject = subject;
-
- this.chain = new And<S>(){
- @SuppressWarnings("unchecked")
- @Override public S and() {
- return (S)Subject.this;
- }
- };
- }
-
- /**
- * A method which wraps the current Subject concrete
- * subtype in a chaining "And" object.
- */
- protected final And<S> nextChain() {
- return chain;
}
- public And<S> is(T other) {
+ public Subject<T> is(T other) {
if (getSubject() == null) {
if(other != null) {
@@ -61,24 +44,28 @@ public Subject(FailureStrategy failureStrategy, T subject) {
fail("is", other);
}
}
- return nextChain();
+ return this;
}
- public And<S> isNull() {
+ public Subject<T> isNull() {
if (getSubject() != null) {
failWithoutSubject("is null");
}
- return nextChain();
+ return this;
}
- public And<S> isNotNull() {
+ public Subject<T> isNotNull() {
if (getSubject() == null) {
failWithoutSubject("is not null");
}
return nextChain();
}
- public And<S> isEqualTo(Object other) {
+ private Subject<T> nextChain() {
+ return this;
+ }
+
+ public Subject<T> isEqualTo(Object other) {
if (getSubject() == null) {
if(other != null) {
fail("is equal to", other);
@@ -91,7 +78,7 @@ public Subject(FailureStrategy failureStrategy, T subject) {
return nextChain();
}
- public And<S> isNotEqualTo(Object other) {
+ public Subject<T> isNotEqualTo(Object other) {
if (getSubject() == null) {
if(other == null) {
fail("is not equal to", other);
@@ -104,14 +91,14 @@ public Subject(FailureStrategy failureStrategy, T subject) {
return nextChain();
}
- public And<S> isA(Class<?> clazz) {
+ public Subject<T> isA(Class<?> clazz) {
if (!clazz.isInstance(getSubject())) {
fail("is a", clazz.getName());
}
return nextChain();
}
- public And<S> isNotA(Class<?> clazz) {
+ public Subject<T> isNotA(Class<?> clazz) {
if (clazz.isInstance(getSubject())) {
fail("is not a", clazz.getName());
}
View
2 src/main/java/org/junit/contrib/truth/subjects/SubjectFactory.java
@@ -24,7 +24,7 @@
*
* @author Christian Gruber (cgruber@israfil.net)
*/
-public interface SubjectFactory<S extends Subject<S,T>, T> {
+public interface SubjectFactory<S extends Subject<T>, T> {
S getSubject(FailureStrategy fs, T that);
}
View
18 src/test/java/org/junit/contrib/truth/CollectionTest.java
@@ -40,7 +40,7 @@
}
@Test public void listContainsWithChaining() {
- ASSERT.that(collection(1, 2, 3)).contains(1).and().contains(2);
+ ASSERT.that(collection(1, 2, 3)).contains(1).contains(2);
}
@Test public void listContainsWithNull() {
@@ -50,12 +50,14 @@
@Test public void listContainsWith2KindsOfChaining() {
Collection<Integer> foo = collection(1, 2, 3);
Collection<Integer> bar = foo;
- ASSERT.that(foo).is(bar).and().contains(1).and().contains(2);
+ // This no longer compiles: ASSERT.that(foo).is(bar).contains(1).contains(2);
+ ASSERT.that(foo).is(bar);
+ ASSERT.that(foo).contains(1).contains(2);
}
@Test public void listContainsFailureWithChaining() {
try {
- ASSERT.that(collection(1, 2, 3)).contains(1).and().contains(5);
+ ASSERT.that(collection(1, 2, 3)).contains(1).contains(5);
fail("Should have thrown.");
} catch (AssertionError e) {}
}
@@ -103,7 +105,7 @@
ASSERT.that(collection(1, 2, 3)).containsAllOf(1, 2, 4);
fail("Should have thrown.");
} catch (AssertionError e) {
- ASSERT.that(e.getMessage()).contains("Not true that").and().contains("<4>");
+ ASSERT.that(e.getMessage()).contains("Not true that").contains("<4>");
}
}
@@ -113,8 +115,8 @@
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that")
- .and().contains("<3 copies of 2>")
- .and().contains("<4>");
+ .contains("<3 copies of 2>")
+ .contains("<4>");
}
}
@@ -128,7 +130,7 @@
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that")
- .and().endsWith("contains <3 copies of 4>");
+ .endsWith("contains <3 copies of 4>");
}
}
@@ -138,7 +140,7 @@
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that")
- .and().contains("<2 copies of null>");
+ .contains("<2 copies of null>");
}
}
View
2 src/test/java/org/junit/contrib/truth/ExampleTest.java
@@ -44,7 +44,7 @@
}
@Test public void listContainsWithChaining() {
- ASSERT.that(Arrays.asList(1, 2, 3)).contains(1).and().contains(2);
+ ASSERT.that(Arrays.asList(1, 2, 3)).contains(1).contains(2);
}
@Test public void equalityFail() {
View
2 src/test/java/org/junit/contrib/truth/ExpectFailureTest.java
@@ -43,7 +43,7 @@
}
@Test public void expectFailStringContains() {
- EXPECT.that("abc").contains("x").and().contains("y").and().contains("z");
+ EXPECT.that("abc").contains("x").contains("y").contains("z");
}
@Test public void expectFailContainsAllOf() {
View
2 src/test/java/org/junit/contrib/truth/ExpectTest.java
@@ -58,7 +58,7 @@ public void expectFail() {
thrown.expectMessage("1. Not true that <abc> contains <x>");
thrown.expectMessage("2. Not true that <abc> contains <y>");
thrown.expectMessage("3. Not true that <abc> contains <z>");
- EXPECT.that("abc").contains("x").and().contains("y").and().contains("z");
+ EXPECT.that("abc").contains("x").contains("y").contains("z");
}
@Test
View
2 src/test/java/org/junit/contrib/truth/IntegerTest.java
@@ -38,7 +38,7 @@
@Rule public Expect EXPECT = Expect.create();
@Test public void simpleEquality() {
- ASSERT.that(2 + 2).isEqualTo(4).and().isBetween(3, 5);
+ ASSERT.that(2 + 2).isEqualTo(4).isBetween(3, 5);
}
@Test public void simpleInequality() {
View
12 src/test/java/org/junit/contrib/truth/ListTest.java
@@ -58,7 +58,7 @@
ASSERT.that(Arrays.asList(1, 2, 3)).containsSequence(Arrays.asList(1, 2, 3, 4));
fail("Should have thrown.");
} catch (AssertionError e) {
- ASSERT.that(e.getMessage()).contains("contains sequence").and().contains("[1, 2, 3, 4]");
+ ASSERT.that(e.getMessage()).contains("contains sequence").contains("[1, 2, 3, 4]");
}
}
@@ -67,7 +67,7 @@
ASSERT.that(Arrays.asList(1, 2, 2, 3)).containsSequence(Arrays.asList(1, 2, 3));
fail("Should have thrown.");
} catch (AssertionError e) {
- ASSERT.that(e.getMessage()).contains("contains sequence").and().contains("[1, 2, 3]");
+ ASSERT.that(e.getMessage()).contains("contains sequence").contains("[1, 2, 3]");
}
}
@@ -82,7 +82,7 @@
ASSERT.that(Arrays.asList(1, 2, 2, 4)).isOrdered();
fail("Should have thrown.");
} catch (AssertionError e) {
- ASSERT.that(e.getMessage()).contains("is strictly ordered").and().contains("<2> <2>");
+ ASSERT.that(e.getMessage()).contains("is strictly ordered").contains("<2> <2>");
}
}
@@ -104,7 +104,7 @@
ASSERT.that(Arrays.asList(1, 3, 2, 4)).isPartiallyOrdered();
fail("Should have thrown.");
} catch (AssertionError e) {
- ASSERT.that(e.getMessage()).contains("is partially ordered").and().contains("<3> <2>");
+ ASSERT.that(e.getMessage()).contains("is partially ordered").contains("<3> <2>");
}
}
@@ -127,7 +127,7 @@
ASSERT.that(Arrays.asList("1", "2", "2", "10")).isOrdered(COMPARE_AS_DECIMAL);
fail("Should have thrown.");
} catch (AssertionError e) {
- ASSERT.that(e.getMessage()).contains("is strictly ordered").and().contains("<2> <2>");
+ ASSERT.that(e.getMessage()).contains("is strictly ordered").contains("<2> <2>");
}
}
@@ -143,7 +143,7 @@
ASSERT.that(Arrays.asList("1", "10", "2", "20")).isPartiallyOrdered(COMPARE_AS_DECIMAL);
fail("Should have thrown.");
} catch (AssertionError e) {
- ASSERT.that(e.getMessage()).contains("is partially ordered").and().contains("<10> <2>");
+ ASSERT.that(e.getMessage()).contains("is partially ordered").contains("<10> <2>");
}
}
View
2 src/test/java/org/junit/contrib/truth/StringTest.java
@@ -48,7 +48,7 @@
}
@Test public void chain() {
- ASSERT.that("abc").contains("a").and().contains("b");
+ ASSERT.that("abc").contains("a").contains("b");
}
@Test public void stringEquality() {
View
6 src/test/java/org/junit/contrib/truth/delegatetest/FooSubject.java
@@ -25,7 +25,7 @@
*
* @author Christian Gruber (christianedwardgruber@gmail.com)
*/
-public class FooSubject extends Subject<FooSubject, Foo> {
+public class FooSubject extends Subject<Foo> {
public static final SubjectFactory<FooSubject, Foo> FOO =
new SubjectFactory<FooSubject, Foo>() {
@@ -38,11 +38,11 @@ public FooSubject(FailureStrategy failureStrategy, Foo subject) {
super(failureStrategy, subject);
}
- public And<FooSubject> matches(Foo object) {
+ public FooSubject matches(Foo object) {
if (getSubject().value != object.value) {
fail("matches", getSubject(), object);
}
- return nextChain();
+ return this;
}
}
View
8 src/test/java/org/junit/contrib/truth/extensiontest/MySubject.java
@@ -25,17 +25,15 @@
*
* @author Christian Gruber (christianedwardgruber@gmail.com)
*/
-public class MySubject extends Subject<MySubject, MyType> {
-
+public class MySubject extends Subject<MyType> {
public MySubject(FailureStrategy failureStrategy, MyType subject) {
super(failureStrategy, subject);
}
- public And<MySubject> matches(MyType object) {
+ public MySubject matches(MyType object) {
if (getSubject().value != object.value) {
fail("matches", getSubject(), object);
}
- return nextChain();
+ return this;
}
-
}

0 comments on commit ff5bc6c

Please sign in to comment.
Something went wrong with that request. Please try again.