Title: [727] trunk/core/src/java/org/jbehave/core/mock: [EK] Finished Collection matchers; descriptions are now much better too.

Diff

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

--- trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java	2007-05-17 18:12:12 UTC (rev 726)
+++ trunk/core/src/behaviour/org/jbehave/core/matchers/UsingCollectionMatchersBehaviour.java	2007-05-20 17:55:12 UTC (rev 727)
@@ -6,85 +6,85 @@
 import org.jbehave.core.exception.VerificationException;
 import org.jbehave.core.mock.Matcher;
 import org.jbehave.core.mock.UsingMatchers;
+import org.jbehave.core.mock.UsingMatchers.CustomMatcher;
 
 public class UsingCollectionMatchersBehaviour {
     private static final String NL = System.getProperty("line.separator");
 
-    private Matcher eq(Integer value) {
+    private Matcher eq(Object value) {
     	return UsingEqualityMatchers.eq(value);
 	}
 
 	private Matcher not(Matcher matcher) {
 		return UsingLogicalMatchers.not(matcher);
 	}
+//    
+//    public void shouldProvideMatchersForCollectionsContainingAThing() {
+//        ArrayList list = new ArrayList();
+//        list.add(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.contains(Integer.valueOf(5))));
+//        Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.contains(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.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.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 shouldProvideMatchersForCollectionsContainingThingsInOrder() {
+//        ArrayList list = new ArrayList();
+//        list.add(Integer.valueOf(3));
+//        list.add(Integer.valueOf(4));
+//        list.add(Integer.valueOf(5));
+//    	
+//    	Ensure.that(list, not(UsingCollectionMatchers.containsInOrder(Integer.valueOf(3), Integer.valueOf(5), Integer.valueOf(4))));
+//    	Ensure.that(list, UsingCollectionMatchers.containsInOrder(Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5)));
+//	}
     
-    public void shouldProvideMatchersForCollectionsContainingAThing() {
-        ArrayList list = new ArrayList();
-        list.add(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.contains(Integer.valueOf(5))));
-        Ensure.that(list, UsingLogicalMatchers.not(UsingCollectionMatchers.contains(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.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.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() {};
-        
         ArrayList list = new ArrayList();
-        list.add(Integer.valueOf(5));
+        list.add(Integer.valueOf(5));        
+        list.add(Integer.valueOf(6));
         
-        try {
-            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 + 
-            		"a ArrayList containing [5]"));
-        }
+        CustomMatcher contains = UsingCollectionMatchers.contains(Integer.valueOf(4), Integer.valueOf(5));
+        CustomMatcher containsOnly = UsingCollectionMatchers.containsOnly(Integer.valueOf(4), Integer.valueOf(5));
+        CustomMatcher containsInOrder = UsingCollectionMatchers.containsInOrder(Integer.valueOf(4), Integer.valueOf(5));
         
-        list.add(Integer.valueOf(6));
+        // Describe what we expected
+        Ensure.that(contains.toString(), eq("a collection containing [equal to <4>, equal to <5>]"));
+        Ensure.that(containsOnly.toString(), eq("a collection containing [equal to <4>, equal to <5>] and nothing else"));
+        Ensure.that(containsInOrder.toString(), eq("a collection containing [equal to <4>, equal to <5>] and in order"));
         
-        try {
-            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 + 
-            		"a ArrayList containing [5, 6]"));
-        }
+        // Describe what we got
+		Ensure.that(contains.describe(list), eq("a ArrayList containing [5, 6]"));
+		Ensure.that(containsOnly.describe(list), eq("a ArrayList containing [5, 6]"));
+		Ensure.that(containsInOrder.describe(list), eq("a ArrayList containing [5, 6]"));
     }
 }

Modified: trunk/core/src/behaviour/org/jbehave/core/matchers/UsingLogicalMatchersBehaviour.java (726 => 727)

--- trunk/core/src/behaviour/org/jbehave/core/matchers/UsingLogicalMatchersBehaviour.java	2007-05-17 18:12:12 UTC (rev 726)
+++ trunk/core/src/behaviour/org/jbehave/core/matchers/UsingLogicalMatchersBehaviour.java	2007-05-20 17:55:12 UTC (rev 727)
@@ -1,21 +1,44 @@
 package org.jbehave.core.matchers;
 
 import org.jbehave.core.Ensure;
-import org.jbehave.core.matchers.UsingLogicalMatchers;
-import org.jbehave.core.mock.UsingMatchers;
+import org.jbehave.core.mock.Matcher;
+import org.jbehave.core.mock.UsingMatchers.CustomMatcher;
 
 public class UsingLogicalMatchersBehaviour {
     
     public void shouldProvideConditionalMatchers() throws Exception {
-        UsingMatchers m = new UsingMatchers() {};
-        
         String horse = "horse";
         String cow = "cow";
         
-        Ensure.that(horse, UsingLogicalMatchers.or(m.eq(horse), m.eq(cow)));
-        Ensure.that(cow, UsingLogicalMatchers.either(m.eq(horse), m.eq(cow)));
+        Ensure.that(horse, UsingLogicalMatchers.or(eq(horse), eq(cow)));
+        Ensure.that(cow, UsingLogicalMatchers.either(eq(horse), eq(cow)));
         
-        Ensure.that(horse, UsingLogicalMatchers.and(m.eq(horse), m.contains("ors")));
-        Ensure.that(cow, UsingLogicalMatchers.both(m.eq(cow), m.endsWith("ow")));
+        Ensure.that(horse, UsingLogicalMatchers.and(eq(horse), UsingStringMatchers.contains("ors")));
+        Ensure.that(cow, UsingLogicalMatchers.both(eq(cow), UsingStringMatchers.endsWith("ow")));        
     }
+    
+    public void shouldProvideNegativeMatcher() throws Exception {
+    	Ensure.that("horse", UsingLogicalMatchers.not(eq("cow")));
+    }
+
+	public void shouldUseFirstFailingDelegateMatcherToDescribeArgs() {
+    	CustomMatcher customizedDescription = new CustomMatcher("") {
+					public boolean matches(Object arg) { return false; }
+					public String describe(Object arg) { return "a customized description of " + arg; }
+		    	};
+		    	
+		CustomMatcher both4And5 = UsingLogicalMatchers.and(eq(Integer.valueOf(4)), customizedDescription);
+		Ensure.that(both4And5.describe(Integer.valueOf(4)),
+    			eq("a customized description of 4"));
+
+    	CustomMatcher either4Or5 = UsingLogicalMatchers.or(customizedDescription, eq(Integer.valueOf(4)));
+		Ensure.that(either4Or5.describe(Integer.valueOf(4)),
+    			eq("a customized description of 4"));	
+		
+		Ensure.that(UsingLogicalMatchers.not(customizedDescription).describe(Integer.valueOf(4)), eq("a customized description of 4"));
+	}
+	
+	private Matcher eq(Object value) {
+		return UsingEqualityMatchers.eq(value);
+	}
 }

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

--- trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java	2007-05-17 18:12:12 UTC (rev 726)
+++ trunk/core/src/java/org/jbehave/core/matchers/UsingCollectionMatchers.java	2007-05-20 17:55:12 UTC (rev 727)
@@ -21,16 +21,17 @@
         if (matchers.length == 0) {
             return contains(UsingEqualityMatchers.nothing());
         }
-        
-        
+                
         CustomMatcher matcher = containsA(matchers[0]);
         for (int i = 1; i < matchers.length; i++) {
             matcher = UsingLogicalMatchers.and(matcher, containsA(matchers[i]));
         }
         
-        final CustomMatcher finalMatcher = matcher;
-        
-        return new CustomMatcher(""){
+        return suitablyDescriptiveMatcher(matchers, matcher);
+    }
+
+	private static CustomMatcher suitablyDescriptiveMatcher(final Matcher[] matchers, final CustomMatcher finalMatcher) {
+		return new CustomMatcher(""){
             public boolean matches(Object arg) {
                 return finalMatcher.matches(arg);
             }
@@ -60,7 +61,7 @@
 				return "a collection containing " + buffer;
             }
         };
-    }
+	}
     
 
     private static CustomMatcher containsA(final Matcher matcher) {
@@ -153,5 +154,60 @@
 		return matchers;
 	}
 	
+	public static CustomMatcher containsInOrder(Object a) {
+		return containsInOrder(new Object[] {a});
+	}
+	
+	public static CustomMatcher containsInOrder(Object a, Object b) {
+		return containsInOrder(new Object[] {a, b});
+	}
+	
+	public static CustomMatcher containsInOrder(Object a, Object b, Object c) {
+		return containsInOrder(new Object[] {a, b, c});
+	}
+	
+	public static CustomMatcher containsInOrder(Object[] objects) {
+		return containsInOrder(allEq(objects));
+	}
 
+	public static CustomMatcher containsInOrder(Matcher a) {
+		return containsInOrder(new Matcher[] {a});
+	}
+	
+	public static CustomMatcher containsInOrder(Matcher a, Matcher b) {
+		return containsInOrder(new Matcher[] {a, b});
+	}
+	
+	public static CustomMatcher containsInOrder(Matcher a, Matcher b, Matcher c) {
+		return containsInOrder(new Matcher[] {a, b, c});
+	}
+	
+	public static CustomMatcher containsInOrder(final Matcher[] matchers) {
+        if (matchers.length == 0) {
+            return contains(UsingEqualityMatchers.nothing());
+        }
+                
+        CustomMatcher aggregateMatcher = containsAt(matchers[0], 0);
+        
+        for (int i = 1; i < matchers.length; i++) {
+            aggregateMatcher = UsingLogicalMatchers.and(aggregateMatcher, containsAt(matchers[i], i));
+        }
+        
+        Matcher inOrderDescription = new Matcher() {
+			public boolean matches(Object arg) { return true; }
+			public String toString() { return "in order"; }
+        };
+        
+        return suitablyDescriptiveMatcher(matchers, aggregateMatcher).and(inOrderDescription);
+	}
+
+	private static CustomMatcher containsAt(final Matcher matcher, final int i) {
+        return new CustomMatcher("" + matcher) {
+            public boolean matches(Object arg) {
+                Collection collection = (Collection) arg;
+                return matcher.matches(collection.toArray()[i]);
+            }
+        };
+	}	
 }
+

Modified: trunk/core/src/java/org/jbehave/core/matchers/UsingLogicalMatchers.java (726 => 727)

--- trunk/core/src/java/org/jbehave/core/matchers/UsingLogicalMatchers.java	2007-05-17 18:12:12 UTC (rev 726)
+++ trunk/core/src/java/org/jbehave/core/matchers/UsingLogicalMatchers.java	2007-05-20 17:55:12 UTC (rev 727)
@@ -5,19 +5,13 @@
 
 public class UsingLogicalMatchers {
 
-    public static Matcher not(final CustomMatcher matcher) {
-        return new CustomMatcher("not (" + matcher + ")") {
-            public boolean matches(Object arg) {
-                return !matcher.matches(arg);
-            }
-        };
-    }
-
     public static CustomMatcher and(final Matcher a, final Matcher b) {
-        return new CustomMatcher("(" + a + " and " + b + ")") {
+        return new DoubleMatcherMatcher(a + " and " + b, a, b) {
             public boolean matches(Object arg) {
                 return a.matches(arg) && b.matches(arg);
             }
+            
+
         };
     }
 
@@ -26,7 +20,7 @@
     }
 
     public static CustomMatcher or(final Matcher a, final Matcher b) {
-        return new CustomMatcher("(" + a + " or " + b + ")") {
+        return new DoubleMatcherMatcher(a + " or " + b, a, b) {
             public boolean matches(Object arg) {
                 return a.matches(arg) || b.matches(arg);
             }
@@ -38,12 +32,37 @@
     }    
 
     public static CustomMatcher not(final Matcher c) {
-        return new CustomMatcher("not (" + c + ")") {
+        return new CustomMatcher("not " + c) {
             public boolean matches(Object arg) {
                 return !c.matches(arg);
             }
+            
+            public String describe(Object arg) {
+            	if (c instanceof CustomMatcher) {
+            		return ((CustomMatcher)c).describe(arg);
+            	}
+            	return super.describe(arg);
+            }
         };
     }
 
+    private static abstract class DoubleMatcherMatcher extends CustomMatcher {
+		private final Matcher a;
+		private final Matcher b;
 
+		public DoubleMatcherMatcher(String description, Matcher a, Matcher b) {
+			super(description);
+			this.a = a;
+			this.b = b; 
+		}
+    	
+        public String describe(Object arg) {            	
+        	if (!a.matches(arg) && a instanceof CustomMatcher) {
+    			return ((CustomMatcher)a).describe(arg);
+        	} else if (!b.matches(arg) && b instanceof CustomMatcher) {
+        		return ((CustomMatcher)b).describe(arg);
+        	}
+        	return super.describe(arg);
+        }
+    }
 }

Modified: trunk/core/src/java/org/jbehave/core/mock/Matcher.java (726 => 727)

--- trunk/core/src/java/org/jbehave/core/mock/Matcher.java	2007-05-17 18:12:12 UTC (rev 726)
+++ trunk/core/src/java/org/jbehave/core/mock/Matcher.java	2007-05-20 17:55:12 UTC (rev 727)
@@ -13,6 +13,8 @@
  * 
  * @author <a href="" PROTECTED]">Dan North</a>
  */
+/* TODO 2.0 Move this to org.jbehave.core.matchers */
+/* TODO 2.0 Add the describe(arg) method and remove all CustomMatcher ifs / casts */
 public interface Matcher {
     boolean matches(Object arg);
 }
\ No newline at end of file

Modified: trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java (726 => 727)

--- trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java	2007-05-17 18:12:12 UTC (rev 726)
+++ trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java	2007-05-20 17:55:12 UTC (rev 727)
@@ -32,8 +32,11 @@
  * <p>You may find it more useful to extend or delegate to 
  * [EMAIL PROTECTED] org.jbehave.core.minimock.UsingMiniMock} than to UsingMatchers.
  */
+/* TODO 2.0 : Move this into org.jbehave.core.matchers Liz 20070520 */
 public abstract class UsingMatchers {
     
+	/* TODO 2.0 : This shouldn't extend UsingMatchers. Liz 20070520 */
+	/* TODO 2.0 : Move this into a top-level class under org.jbehave.core.matchers Liz 20070520 */
     public static abstract class CustomMatcher extends UsingMatchers implements Matcher {
         private final String description;
 


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to