Title: [725] trunk/core/src/java/org/jbehave/core/matchers: [EK] More collection constraints
Revision
725
Author
sirenian
Date
2007-05-14 14:58:06 -0500 (Mon, 14 May 2007)

Log Message

[EK] More collection constraints

Modified Paths


Diff

Modified: trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java (724 => 725)

--- trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java	2007-05-14 18:10:05 UTC (rev 724)
+++ trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java	2007-05-14 19:58:06 UTC (rev 725)
@@ -4,38 +4,64 @@
 
 import org.jbehave.core.Ensure;
 import org.jbehave.core.exception.VerificationException;
+import org.jbehave.core.mock.Matcher;
 import org.jbehave.core.mock.UsingMatchers;
 
 public class UsingCollectionMatchersBehaviour {
     private static final String NL = System.getProperty("line.separator");
 
+    private Matcher eq(Integer value) {
+    	return UsingEqualityMatchers.eq(value);
+	}
+
+	private Matcher not(Matcher matcher) {
+		return UsingLogicalMatchers.not(matcher);
+	}
+    
     public void shouldProvideMatchersForCollectionsContainingAThing() {
         ArrayList list = new ArrayList();
-        list.add(new Integer(3));
+        list.add(Integer.valueOf(3));
         
-        Ensure.that(list, UsingCollectionMatchers.collectionContaining(new Integer(3)));
-        Ensure.that(list, UsingCollectionMatchers.collectionContaining(UsingEqualityMatchers.eq(new Integer(3))));
+        Ensure.that(list, UsingCollectionMatchers.collectionContaining(Integer.valueOf(3)));
+        Ensure.that(list, UsingCollectionMatchers.collectionContaining(eq(Integer.valueOf(3))));
         
-        Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.collectionContaining(new Integer(5))));
-        Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.collectionContaining(UsingEqualityMatchers.eq(new Integer(5)))));
+        Ensure.that(list, not(UsingCollectionMatchers.collectionContaining(Integer.valueOf(5))));
+        Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.collectionContaining(eq(Integer.valueOf(5)))));
     }
+
+	public void shouldProvideMatchersForCollectionsContainingSomeThings() {
+        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.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, 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))}));
+    }
     
     public void shouldDescribeMatchersForCollections() {
         UsingMatchers m = new UsingMatchers() {};
         
         ArrayList list = new ArrayList();
-        list.add(new Integer(5));
+        list.add(Integer.valueOf(5));
         
         try {
-            Ensure.that(list, UsingCollectionMatchers.collectionContaining(new Integer(3)));
+            Ensure.that(list, UsingCollectionMatchers.collectionContaining(Integer.valueOf(3)));
         } catch (VerificationException e) {
             Ensure.that(e.getMessage(), UsingEqualityMatchers.eq("Expected: " + NL + "a collection containing [equal to <3>]" + NL + "but got: " + NL + "[5]"));
         }
         
-        list.add(new Integer(6));
+        list.add(Integer.valueOf(6));
         
         try {
-            Ensure.that(list, UsingCollectionMatchers.collectionContaining(new Integer(4), new Integer(5)));
+            Ensure.that(list, UsingCollectionMatchers.collectionContaining(Integer.valueOf(4), Integer.valueOf(5)));
         } 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]"));
         }

Modified: trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java (724 => 725)

--- trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java	2007-05-14 18:10:05 UTC (rev 724)
+++ trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java	2007-05-14 19:58:06 UTC (rev 725)
@@ -4,11 +4,20 @@
 import java.util.Collection;
 import java.util.Iterator;
 
+import org.jbehave.core.mock.Matcher;
 import org.jbehave.core.mock.UsingMatchers.CustomMatcher;
 
 public class UsingCollectionMatchers {
 
-    public static CustomMatcher collectionContaining(final CustomMatcher[] matchers) {
+    public static CustomMatcher isContainedIn(final Collection collection) {
+        return new CustomMatcher("is contained in " + collection) {
+            public boolean matches(Object arg) {
+                return collection.contains(arg);
+            }
+        };
+    }
+	
+    public static CustomMatcher collectionContaining(final Matcher[] matchers) {
         if (matchers.length == 0) {
             return collectionContaining(UsingEqualityMatchers.nothing());
         }
@@ -16,7 +25,7 @@
         
         CustomMatcher matcher = collectionContainingA(matchers[0]);
         for (int i = 1; i < matchers.length; i++) {
-            matcher = matchers[i].and(collectionContainingA(matcher));
+            matcher = UsingLogicalMatchers.and(matcher, collectionContainingA(matchers[i]));
         }
         
         final CustomMatcher finalMatcher = matcher;
@@ -28,7 +37,12 @@
 
             public String describe(Object arg) {
                 Collection collection = (Collection) arg;
-                StringBuffer buffer = new StringBuffer().append("[");
+                StringBuffer buffer = describe(collection);
+                return buffer.toString();
+            }
+
+			private StringBuffer describe(Collection collection) {
+				StringBuffer buffer = new StringBuffer().append("[");
                 for (Iterator iter = collection.iterator(); iter.hasNext();) {
                     buffer.append(iter.next());
                     if(iter.hasNext()) {
@@ -36,8 +50,8 @@
                     }                    
                 }
                 buffer.append("]");
-                return buffer.toString();
-            }
+				return buffer;
+			}
             
             public String toString() {
                 return "a collection containing " + describe(Arrays.asList(matchers));
@@ -46,7 +60,7 @@
     }
     
 
-    private static CustomMatcher collectionContainingA(final CustomMatcher matcher) {
+    private static CustomMatcher collectionContainingA(final Matcher matcher) {
         return new CustomMatcher("" + matcher) {
             public boolean matches(Object arg) {
                 Collection collection = (Collection) arg;
@@ -61,27 +75,47 @@
     }
 
 
-    public static CustomMatcher collectionContaining(CustomMatcher matcher) {
-        return collectionContaining(new CustomMatcher[] {matcher});
+    public static CustomMatcher collectionContaining(Matcher matcher) {
+        return collectionContaining(new Matcher[] {matcher});
     }
 
 
     public static CustomMatcher collectionContaining(Object object) {
-        return collectionContaining(UsingEqualityMatchers.eq(object));
+        return collectionContaining(new Object[]{object});
     }
 
 
-    public static CustomMatcher collectionContaining(Object object1, Object object2) {
-        return collectionContaining(new CustomMatcher[] {UsingEqualityMatchers.eq(object1), UsingEqualityMatchers.eq(object2)});
+    public static CustomMatcher collectionContaining(Object a, Object b) {
+        return collectionContaining(new Object[]{a, b});
     }
 
+	public static CustomMatcher collectionContaining(Object a, Object b, Object c) {
+		return collectionContaining(new Object[]{a, b, c});
+	}
 
-    public static CustomMatcher isContainedIn(final Collection collection) {
-        return new CustomMatcher("is contained in " + collection) {
-            public boolean matches(Object arg) {
-                return collection.contains(arg);
-            }
-        };
-    }    
+	public static CustomMatcher collectionContaining(Matcher a, Matcher b) {
+		return collectionContaining(new Matcher[] {a, b});
+	} 
 
+	public static CustomMatcher collectionContaining(Matcher a, Matcher b, Matcher c) {
+		return collectionContaining(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 collectionContainingOnly(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);
+	}
+
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to