001    package groovy.mock;
002    
003    import groovy.lang.Closure;
004    import groovy.lang.ParameterArray;
005    
006    import com.mockobjects.constraint.Constraint;
007    
008    /**
009     * 
010     * @author Joe Walnes
011     * @author Chris Stevenson
012     * @version $Revision: 1.2 $
013     */
014    public class ClosureConstraintMatcher implements Constraint {
015        private Closure closure;
016        private String message = "closure";
017    
018        public ClosureConstraintMatcher(Closure closure) {
019            this.closure = closure;
020        }
021    
022        public boolean eval(Object object) {
023            try {
024                closure.call(new ParameterArray(object));
025                return true;
026            }
027            catch (AssertionError e) {
028                message = e.getMessage();
029                return false;
030            }
031        }
032    
033        public String toString() {
034            return message;
035        }
036    
037    }