/// <summary> /// Succeeds if either left or right succeeds. /// If both fail, merges and flattens messages. /// </summary> public static INamedPredicate Or(this INamedPredicate left, INamedPredicate right) { return new NamedPredicate( o => { string dummy; return left.Matches(o, out dummy) || right.Matches(o, out dummy); }, o => { string leftMsg, rightMsg; left.Matches(o, out leftMsg); right.Matches(o, out rightMsg); return leftMsg + ", " + rightMsg; } ); }
/// <summary> /// Applies only to enumerable nodes. /// The source predicate will only be applied to nodes where the filter returns true. /// </summary> /// <param name="source">A predicate, like `Should.Have(3)`</param> /// <param name="filter">A filter, like `thing => thing.Attribute = "target"`</param> public static INamedPredicate Where(this INamedPredicate source, Func <dynamic, bool> filter) { return(new NamedPredicate( o => { var filtered = ((IEnumerable <object>)o).Where(filter); string dummy; return source.Matches(filtered, out dummy); }, o => { var filtered = ((IEnumerable <object>)o).Where(filter); string message; source.Matches(filtered, out message); return message; } )); }
/// <summary> /// Succeeds if either left or right succeeds. /// If both fail, merges and flattens messages. /// </summary> public static INamedPredicate Or(this INamedPredicate left, INamedPredicate right) { return(new NamedPredicate( o => { string dummy; return left.Matches(o, out dummy) || right.Matches(o, out dummy); }, o => { string leftMsg, rightMsg; left.Matches(o, out leftMsg); right.Matches(o, out rightMsg); return leftMsg + ", " + rightMsg; } )); }
/// <summary> /// Applies only to enumerable nodes. /// The source predicate will only be applied to nodes where the filter returns true. /// </summary> /// <param name="source">A predicate, like `Should.Have(3)`</param> /// <param name="filter">A named predicate, only the items that are successful in this predicate will /// be passed to the source. The last item's message will be included in case of source predicate /// failure</param> public static INamedPredicate Where(this INamedPredicate source, INamedPredicate filter) { return new NamedPredicate( o => { string dummy; var filtered = ((IEnumerable<object>)o).Where( a => filter.Matches(a, out dummy)); return source.Matches(filtered, out dummy); }, o => { var lastWhereMessage = ""; var filtered = ((IEnumerable<object>)o).Where( a => filter.Matches(a, out lastWhereMessage)); string message; source.Matches(filtered, out message); return message + " where " + lastWhereMessage; } ); }
/// <summary> /// Applies only to enumerable nodes. /// The source predicate will only be applied to nodes where the filter returns true. /// </summary> /// <param name="source">A predicate, like `Should.Have(3)`</param> /// <param name="filter">A named predicate, only the items that are successful in this predicate will /// be passed to the source. The last item's message will be included in case of source predicate /// failure</param> public static INamedPredicate Where(this INamedPredicate source, INamedPredicate filter) { return(new NamedPredicate( o => { string dummy; var filtered = ((IEnumerable <object>)o).Where( a => filter.Matches(a, out dummy)); return source.Matches(filtered, out dummy); }, o => { var lastWhereMessage = ""; var filtered = ((IEnumerable <object>)o).Where( a => filter.Matches(a, out lastWhereMessage)); string message; source.Matches(filtered, out message); return message + " where " + lastWhereMessage; } )); }
/// <summary> /// Given an actual, does it meet the predicate /// </summary> public bool Matches(object actual, out string message) { var messages = new List <string>(); var result = true; foreach (var obj in ((IEnumerable <object>)actual)) { string temp; if (constraint.Matches(obj, out temp)) { continue; } result = false; messages.Add(temp); } message = string.Join(", ", messages); return(result); }