Title: [719] trunk/core/src/java/org/jbehave/core/mock: [EK] Started creating some Collection matchers.
Revision
719
Author
sirenian
Date
2007-04-11 12:58:28 -0500 (Wed, 11 Apr 2007)

Log Message

[EK] Started creating some Collection matchers.

Modified Paths


Diff

Modified: trunk/core/src/behaviour/org/jbehave/core/UsingMatchersBehaviour.java (718 => 719)

--- trunk/core/src/behaviour/org/jbehave/core/UsingMatchersBehaviour.java	2007-04-01 10:28:19 UTC (rev 718)
+++ trunk/core/src/behaviour/org/jbehave/core/UsingMatchersBehaviour.java	2007-04-11 17:58:28 UTC (rev 719)
@@ -1,9 +1,14 @@
 package org.jbehave.core;
 
+import java.util.ArrayList;
+
+import org.jbehave.core.exception.VerificationException;
 import org.jbehave.core.mock.UsingMatchers;
 
 public class UsingMatchersBehaviour {
 
+    private static final String NL = System.getProperty("line.separator");
+
     Block EXCEPTION_BLOCK = new Block() {
         public void run() throws Exception {
             throw new NumberFormatException();
@@ -118,4 +123,38 @@
         Ensure.that(horse, m.and(m.eq(horse), m.contains("ors")));
         Ensure.that(cow, m.both(m.eq(cow), m.endsWith("ow")));
     }
+    
+    public void shouldProvideMatchersForCollectionsContainingAThing() {
+        UsingMatchers m = new UsingMatchers() {};
+        
+        ArrayList list = new ArrayList();
+        list.add(new Integer(3));
+        
+        Ensure.that(list, m.collectionContaining(new Integer(3)));
+        Ensure.that(list, m.collectionContaining(m.eq(new Integer(3))));
+        
+        Ensure.that(list, m.not(m.collectionContaining(new Integer(5))));
+        Ensure.that(list, m.not(m.collectionContaining(m.eq(new Integer(5)))));
+    }
+    
+    public void shouldDescribeMatchersForCollections() {
+        UsingMatchers m = new UsingMatchers() {};
+        
+        ArrayList list = new ArrayList();
+        list.add(new Integer(5));
+        
+        try {
+            Ensure.that(list, m.collectionContaining(new Integer(3)));
+        } catch (VerificationException e) {
+            Ensure.that(e.getMessage(), m.eq("Expected: " + NL + "a collection containing [equal to <3>]" + NL + "but got: " + NL + "[5]"));
+        }
+        
+        list.add(new Integer(6));
+        
+        try {
+            Ensure.that(list, m.collectionContaining(new Integer(4), new Integer(5)));
+        } catch (VerificationException e) {
+            Ensure.that(e.getMessage(), m.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/mock/UsingMatchers.java (718 => 719)

--- trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java	2007-04-01 10:28:19 UTC (rev 718)
+++ trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java	2007-04-11 17:58:28 UTC (rev 719)
@@ -1,6 +1,8 @@
 package org.jbehave.core.mock;
 
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.Iterator;
 
 import org.jbehave.core.Block;
 import org.jbehave.core.exception.PendingException;
@@ -29,6 +31,9 @@
  * [EMAIL PROTECTED] org.jbehave.core.minimock.UsingMiniMock} than to UsingMatchers.
  */
 public abstract class UsingMatchers {
+    
+    private static final String NL = System.getProperty("line.separator");
+    
 	public abstract static class CustomMatcher extends UsingMatchers implements Matcher {
 		private final String description;
 
@@ -43,9 +48,14 @@
 		public CustomMatcher and(Matcher that) {
 			return and(this, that);
 		}
+        
 		public CustomMatcher or(Matcher that) {
 			return or(this, that);
 		}
+        
+        public String describe(Object arg) {
+            return "" + arg;
+        }
 	}
 
     /** ensures object is not null */
@@ -184,7 +194,70 @@
 			}
 		};
 	}
+    
+    public CustomMatcher collectionContaining(final CustomMatcher[] matchers) {
+        if (matchers.length == 0) {
+            return collectionContaining(nothing());
+        }
+        
+        
+        CustomMatcher matcher = collectionContainingA(matchers[0]);
+        for (int i = 1; i < matchers.length; i++) {
+            matcher = matchers[i].and(collectionContainingA(matcher));
+        }
+        
+        final CustomMatcher finalMatcher = matcher;
+        
+        return new CustomMatcher(""){
+            public boolean matches(Object arg) {
+                return finalMatcher.matches(arg);
+            }
 
+            public String describe(Object arg) {
+                Collection collection = (Collection) arg;
+                StringBuffer buffer = new StringBuffer().append("[");
+                for (Iterator iter = collection.iterator(); iter.hasNext();) {
+                    buffer.append(iter.next());
+                    if(iter.hasNext()) {
+                        buffer.append(", ");
+                    }                    
+                }
+                buffer.append("]");
+                return buffer.toString();
+            }
+            
+            public String toString() {
+                return "a collection containing " + describe(Arrays.asList(matchers));
+            }
+        };
+    }
+    
+    public CustomMatcher collectionContaining(final CustomMatcher matcher) {
+        return collectionContaining(new CustomMatcher[] {matcher});
+    }
+
+    private CustomMatcher collectionContainingA(final CustomMatcher matcher) {
+        return new CustomMatcher("" + matcher) {
+            public boolean matches(Object arg) {
+                Collection collection = (Collection) arg;
+                for (Iterator iter = collection.iterator(); iter.hasNext();) {
+                    if (matcher.matches(iter.next())) {
+                        return true;
+                    }
+                }
+                return false;
+            }
+        };
+    }
+    
+    public CustomMatcher collectionContaining(final Object object) {
+        return collectionContaining(eq(object));
+    }
+    
+    public CustomMatcher collectionContaining(final Object object1, final Object object2) {
+        return collectionContaining(new CustomMatcher[] {eq(object1), eq(object2)});
+    }
+    
 	public CustomMatcher and(final Matcher a, final Matcher b) {
 	    return new CustomMatcher("(" + a + " and " + b + ")") {
 	        public boolean matches(Object arg) {
@@ -223,13 +296,23 @@
                 return collection.contains(arg);
             }
         };
-    }    
+    }
+    
+    public void ensureThat(Object arg, CustomMatcher matcher, String message) {
+        if (!matcher.matches(arg)) {
+            fail("Expected: " +
+                    (message != null ? "[" + message + "] ": "") + NL +
+                    matcher + NL +
+                    "but got: " + NL + matcher.describe(arg));
+        }
+    }
+    
     public void ensureThat(Object arg, Matcher matcher, String message) {
     	if (!matcher.matches(arg)) {
-    		fail("\nExpected: " +
-    				(message != null ? "[" + message + "] " : "") +
-    				matcher +
-    				"\nbut got:  <" + arg + ">");
+    		fail("Expected: " +
+    				(message != null ? "[" + message + "] " : "") + NL + 
+    				matcher + NL +
+    				"but got: " + NL + arg);
     	}
 	}
     
@@ -318,4 +401,8 @@
     
     public void todo() { pending(); }
     public void todo(String message) { pending(message); }
+
+
+
+
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to