/// <summary> /// Asserts that at least one occurrence of the event had arguments matching all predicates. /// </summary> public static IEventRecorder WithArgs <T>(this IEventRecorder eventRecorder, params Expression <Func <T, bool> >[] predicates) { Func <T, bool>[] compiledPredicates = predicates.Select(p => p?.Compile()).ToArray(); if (!eventRecorder.First().Parameters.OfType <T>().Any()) { throw new ArgumentException("No argument of event " + eventRecorder.EventName + " is of type <" + typeof(T) + ">."); } bool expected = eventRecorder.Any(recordedEvent => { T[] parameters = recordedEvent.Parameters.OfType <T>().ToArray(); int parametersToCheck = Math.Min(parameters.Length, predicates.Length); bool isMatch = true; for (int i = 0; i < parametersToCheck && isMatch; i++) { isMatch = compiledPredicates[i]?.Invoke(parameters[i]) ?? true; } return(isMatch); }); if (!expected) { Execute.Assertion .FailWith("Expected at least one event with arguments matching {0}, but found none.", string.Join(" | ", predicates.Where(p => p != null).Select(p => p.Body.ToString()))); } return(eventRecorder); }
/// <summary> /// Asserts that at least one occurrence of the event had an <see cref="EventArgs"/> object matching a predicate. /// </summary> public static IEventRecorder WithArgs <T>(this IEventRecorder eventRecorder, Expression <Func <T, bool> > predicate) where T : EventArgs { Func <T, bool> compiledPredicate = predicate.Compile(); if (!eventRecorder.First().Parameters.OfType <T>().Any()) { throw new ArgumentException("No argument of event " + eventRecorder.EventName + " is of type <" + typeof(T) + ">."); } if (!eventRecorder.Any(@event => compiledPredicate(@event.Parameters.OfType <T>().Single()))) { Execute.Assertion .FailWith("Expected at least one event with arguments matching {0}, but found none.", predicate.Body); } return(eventRecorder); }