- Revision
- 726
- Author
- sirenian
- Date
- 2007-05-17 13:12:12 -0500 (Thu, 17 May 2007)
Log Message
[EK] Yet more collection constraints
Modified Paths
- trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java
- trunk/core/src/java/org/jbehave/core/Ensure.java
- trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java
- trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java
Diff
Modified: trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java (725 => 726)
--- trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java 2007-05-14 19:58:06 UTC (rev 725) +++ trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java 2007-05-17 18:12:12 UTC (rev 726) @@ -22,11 +22,11 @@ ArrayList list = new ArrayList(); list.add(Integer.valueOf(3)); - Ensure.that(list, UsingCollectionMatchers.collectionContaining(Integer.valueOf(3))); - Ensure.that(list, UsingCollectionMatchers.collectionContaining(eq(Integer.valueOf(3)))); + Ensure.that(list, UsingCollectionMatchers.contains(Integer.valueOf(3))); + Ensure.that(list, UsingCollectionMatchers.contains(eq(Integer.valueOf(3)))); - Ensure.that(list, not(UsingCollectionMatchers.collectionContaining(Integer.valueOf(5)))); - Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.collectionContaining(eq(Integer.valueOf(5))))); + Ensure.that(list, not(UsingCollectionMatchers.contains(Integer.valueOf(5)))); + Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.contains(eq(Integer.valueOf(5))))); } public void shouldProvideMatchersForCollectionsContainingSomeThings() { @@ -35,16 +35,27 @@ list.add(Integer.valueOf(4)); list.add(Integer.valueOf(5)); - Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.collectionContaining(Integer.valueOf(7)))); - Ensure.that(list, UsingCollectionMatchers.collectionContaining(Integer.valueOf(3), Integer.valueOf(4))); - Ensure.that(list, UsingCollectionMatchers.collectionContaining(Integer.valueOf(5), Integer.valueOf(4), Integer.valueOf(3))); - Ensure.that(list, UsingCollectionMatchers.collectionContaining(new Object[] {Integer.valueOf(3)})); + Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.contains(Integer.valueOf(7)))); + Ensure.that(list, UsingCollectionMatchers.contains(Integer.valueOf(3), Integer.valueOf(4))); + Ensure.that(list, UsingCollectionMatchers.contains(Integer.valueOf(5), Integer.valueOf(4), Integer.valueOf(3))); + Ensure.that(list, UsingCollectionMatchers.contains(new Object[] {Integer.valueOf(3)})); - Ensure.that(list, not(UsingCollectionMatchers.collectionContaining(eq(Integer.valueOf(7))))); - Ensure.that(list, UsingCollectionMatchers.collectionContaining(eq(Integer.valueOf(3)), eq(Integer.valueOf(4)))); - Ensure.that(list, UsingCollectionMatchers.collectionContaining(eq(Integer.valueOf(5)), eq(Integer.valueOf(4)), eq(Integer.valueOf(3)))); - Ensure.that(list, UsingCollectionMatchers.collectionContaining(new Matcher[] {eq(Integer.valueOf(3))})); + Ensure.that(list, not(UsingCollectionMatchers.contains(eq(Integer.valueOf(7))))); + Ensure.that(list, UsingCollectionMatchers.contains(eq(Integer.valueOf(3)), eq(Integer.valueOf(4)))); + Ensure.that(list, UsingCollectionMatchers.contains(eq(Integer.valueOf(5)), eq(Integer.valueOf(4)), eq(Integer.valueOf(3)))); + Ensure.that(list, UsingCollectionMatchers.contains(new Matcher[] {eq(Integer.valueOf(3))})); } + + public void shouldProvideMatchersForCollectionsContainingOnlyThings() { + ArrayList list = new ArrayList(); + list.add(Integer.valueOf(3)); + list.add(Integer.valueOf(4)); + list.add(Integer.valueOf(5)); + + Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.containsOnly(Integer.valueOf(3), Integer.valueOf(4)))); + Ensure.that(list, UsingCollectionMatchers.containsOnly(Integer.valueOf(5), Integer.valueOf(4), Integer.valueOf(3))); + Ensure.that(list, UsingCollectionMatchers.containsOnly(eq(Integer.valueOf(5)), eq(Integer.valueOf(4)), eq(Integer.valueOf(3)))); + } public void shouldDescribeMatchersForCollections() { UsingMatchers m = new UsingMatchers() {}; @@ -53,17 +64,27 @@ list.add(Integer.valueOf(5)); try { - Ensure.that(list, UsingCollectionMatchers.collectionContaining(Integer.valueOf(3))); + Ensure.that(list, UsingCollectionMatchers.contains(Integer.valueOf(3))); + UsingExceptions.fail("Didn't throw exception"); } catch (VerificationException e) { - Ensure.that(e.getMessage(), UsingEqualityMatchers.eq("Expected: " + NL + "a collection containing [equal to <3>]" + NL + "but got: " + NL + "[5]")); + Ensure.that(e.getMessage(), UsingEqualityMatchers.eq( + "Expected: " + NL + + "a collection containing [equal to <3>]" + NL + + "but got: " + NL + + "a ArrayList containing [5]")); } list.add(Integer.valueOf(6)); try { - Ensure.that(list, UsingCollectionMatchers.collectionContaining(Integer.valueOf(4), Integer.valueOf(5))); + Ensure.that(list, UsingCollectionMatchers.contains(Integer.valueOf(4), Integer.valueOf(5))); + UsingExceptions.fail("Didn't throw exception"); } catch (VerificationException e) { - Ensure.that(e.getMessage(), UsingEqualityMatchers.eq("Expected: " + NL + "a collection containing [equal to <4>, equal to <5>]" + NL + "but got: " + NL + "[5, 6]")); + Ensure.that(e.getMessage(), UsingEqualityMatchers.eq( + "Expected: " + NL + + "a collection containing [equal to <4>, equal to <5>]" + NL + + "but got: " + NL + + "a ArrayList containing [5, 6]")); } } }
Modified: trunk/core/src/java/org/jbehave/core/Ensure.java (725 => 726)
--- trunk/core/src/java/org/jbehave/core/Ensure.java 2007-05-14 19:58:06 UTC (rev 725) +++ trunk/core/src/java/org/jbehave/core/Ensure.java 2007-05-17 18:12:12 UTC (rev 726) @@ -27,26 +27,23 @@ /** Ensure.that(something, isBlah()) */ public static void that(Object arg, Matcher matcher) { that(arg, matcher, null); - } + } - public static void that(Object arg, Matcher matcher, String message) { - if (!matcher.matches(arg)) { + if (matcher instanceof CustomMatcher) { + if (!matcher.matches(arg)) { + UsingExceptions.fail("Expected: " + + (message != null ? "[" + message + "] ": "") + NL + + matcher + NL + + "but got: " + NL + ((CustomMatcher)matcher).describe(arg)); + } + } else if (!matcher.matches(arg)) { UsingExceptions.fail("Expected: " + (message != null ? "[" + message + "] " : "") + NL + matcher + NL + "but got: " + NL + arg); } } - - public static void that(Object arg, CustomMatcher matcher, String message) { - if (!matcher.matches(arg)) { - UsingExceptions.fail("Expected: " + - (message != null ? "[" + message + "] ": "") + NL + - matcher + NL + - "but got: " + NL + matcher.describe(arg)); - } - } public static void that(long arg, Matcher matcher) { that(arg, matcher, null);
Modified: trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java (725 => 726)
--- trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java 2007-05-14 19:58:06 UTC (rev 725) +++ trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java 2007-05-17 18:12:12 UTC (rev 726) @@ -17,15 +17,15 @@ }; } - public static CustomMatcher collectionContaining(final Matcher[] matchers) { + public static CustomMatcher contains(final Matcher[] matchers) { if (matchers.length == 0) { - return collectionContaining(UsingEqualityMatchers.nothing()); + return contains(UsingEqualityMatchers.nothing()); } - CustomMatcher matcher = collectionContainingA(matchers[0]); + CustomMatcher matcher = containsA(matchers[0]); for (int i = 1; i < matchers.length; i++) { - matcher = UsingLogicalMatchers.and(matcher, collectionContainingA(matchers[i])); + matcher = UsingLogicalMatchers.and(matcher, containsA(matchers[i])); } final CustomMatcher finalMatcher = matcher; @@ -37,12 +37,14 @@ public String describe(Object arg) { Collection collection = (Collection) arg; - StringBuffer buffer = describe(collection); + StringBuffer buffer = new StringBuffer(); + buffer.append("a " + arg.getClass().getSimpleName() + " containing "); + describe(collection, buffer); return buffer.toString(); } - private StringBuffer describe(Collection collection) { - StringBuffer buffer = new StringBuffer().append("["); + private void describe(Collection collection, StringBuffer buffer) { + buffer.append("["); for (Iterator iter = collection.iterator(); iter.hasNext();) { buffer.append(iter.next()); if(iter.hasNext()) { @@ -50,17 +52,18 @@ } } buffer.append("]"); - return buffer; } public String toString() { - return "a collection containing " + describe(Arrays.asList(matchers)); + StringBuffer buffer = new StringBuffer(); + describe(Arrays.asList(matchers), buffer); + return "a collection containing " + buffer; } }; } - private static CustomMatcher collectionContainingA(final Matcher matcher) { + private static CustomMatcher containsA(final Matcher matcher) { return new CustomMatcher("" + matcher) { public boolean matches(Object arg) { Collection collection = (Collection) arg; @@ -75,47 +78,80 @@ } - public static CustomMatcher collectionContaining(Matcher matcher) { - return collectionContaining(new Matcher[] {matcher}); + public static CustomMatcher contains(Matcher matcher) { + return contains(new Matcher[] {matcher}); } - public static CustomMatcher collectionContaining(Object object) { - return collectionContaining(new Object[]{object}); + public static CustomMatcher contains(Object object) { + return contains(new Object[]{object}); } - public static CustomMatcher collectionContaining(Object a, Object b) { - return collectionContaining(new Object[]{a, b}); + public static CustomMatcher contains(Object a, Object b) { + return contains(new Object[]{a, b}); } - public static CustomMatcher collectionContaining(Object a, Object b, Object c) { - return collectionContaining(new Object[]{a, b, c}); + public static CustomMatcher contains(Object a, Object b, Object c) { + return contains(new Object[]{a, b, c}); } - public static CustomMatcher collectionContaining(Matcher a, Matcher b) { - return collectionContaining(new Matcher[] {a, b}); + public static CustomMatcher contains(Matcher a, Matcher b) { + return contains(new Matcher[] {a, b}); } - public static CustomMatcher collectionContaining(Matcher a, Matcher b, Matcher c) { - return collectionContaining(new Matcher[] {a, b, c}); + public static CustomMatcher contains(Matcher a, Matcher b, Matcher c) { + return contains(new Matcher[] {a, b, c}); } - public static CustomMatcher collectionContaining(Object[] objects) { - Matcher[] matchers = new Matcher[objects.length]; - for (int i = 0; i < objects.length; i++) { - matchers[i] = UsingEqualityMatchers.eq(objects[i]); - } - return collectionContaining(matchers); + public static CustomMatcher contains(Object[] objects) { + return contains(allEq(objects)); } - public static CustomMatcher collectionContainingOnly(final Matcher[] matchers) { + public static CustomMatcher containsOnly(Object a) { + return containsOnly(new Object[] {a}); + } + + public static CustomMatcher containsOnly(Object a, Object b) { + return containsOnly(new Object[] {a, b}); + } + + public static CustomMatcher containsOnly(Object a, Object b, Object c) { + return containsOnly(new Object[] {a, b, c}); + } + + public static CustomMatcher containsOnly(Object[] objects) { + return containsOnly(allEq(objects)); + } + + public static CustomMatcher containsOnly(Matcher a) { + return containsOnly(new Matcher[] {a}); + } + + public static CustomMatcher containsOnly(Matcher a, Matcher b) { + return containsOnly(new Matcher[] {a, b}); + } + + public static CustomMatcher containsOnly(Matcher a, Matcher b, Matcher c) { + return containsOnly(new Matcher[] {a, b, c}); + } + + public static CustomMatcher containsOnly(final Matcher[] matchers) { CustomMatcher lengthMatcher = new CustomMatcher("nothing else"){ public boolean matches(Object arg) { return ((Collection)arg).size() == matchers.length; }}; - return collectionContaining(matchers).and(lengthMatcher); + return contains(matchers).and(lengthMatcher); } + private static Matcher[] allEq(Object[] objects) { + Matcher[] matchers = new Matcher[objects.length]; + for (int i = 0; i < objects.length; i++) { + matchers[i] = UsingEqualityMatchers.eq(objects[i]); + } + return matchers; + } + + }
Modified: trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java (725 => 726)
--- trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java 2007-05-14 19:58:06 UTC (rev 725) +++ trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java 2007-05-17 18:12:12 UTC (rev 726) @@ -133,22 +133,38 @@ return UsingStringMatchers.contains(fragment); } - public CustomMatcher collectionContaining(CustomMatcher[] matchers) { - return UsingCollectionMatchers.collectionContaining(matchers); + public CustomMatcher collectionContaining(Matcher[] matchers) { + return UsingCollectionMatchers.contains(matchers); } - public CustomMatcher collectionContaining(final CustomMatcher matcher) { - return UsingCollectionMatchers.collectionContaining(matcher); + public CustomMatcher collectionContaining(Object[] objects) { + return UsingCollectionMatchers.contains(objects); } - public CustomMatcher collectionContaining(Object object) { - return UsingCollectionMatchers.collectionContaining(object); + public CustomMatcher collectionContaining(Matcher a) { + return UsingCollectionMatchers.contains(a); } - public CustomMatcher collectionContaining(Object object1, Object object2) { - return UsingCollectionMatchers.collectionContaining(object1, object2); + public CustomMatcher collectionContaining(Matcher a, Matcher b) { + return UsingCollectionMatchers.contains(a, b); } + public CustomMatcher collectionContaining(Matcher a, Matcher b, Matcher c) { + return UsingCollectionMatchers.contains(a, b, c); + } + + public CustomMatcher collectionContaining(Object a) { + return UsingCollectionMatchers.contains(a); + } + + public CustomMatcher collectionContaining(Object a, Object b) { + return UsingCollectionMatchers.contains(a, b); + } + + public CustomMatcher collectionContaining(Object a, Object b, Object c) { + return UsingCollectionMatchers.contains(a, b, c); + } + public CustomMatcher and(Matcher a, Matcher b) { return UsingLogicalMatchers.and(a, b); }
To unsubscribe from this list please visit:
