/// <summary> /// Constructor for message generator with two message lambdas /// - the regular message generator /// - the custom message generator (user-provided) /// if the latter produces NULL, it's not echoed back /// </summary> /// <param name="passed"></param> /// <param name="regularMessageGenerator"></param> /// <param name="customMessageGenerator"></param> public MatcherResult( bool passed, Func <string> regularMessageGenerator, Func <string> customMessageGenerator ) : this(passed, MessageHelpers.FinalMessageFor(regularMessageGenerator, customMessageGenerator)) { }
/// <summary> /// Tests if a substitute has received any calls whatsoever /// </summary> /// <param name="been"></param> /// <param name="messageGenerator"></param> /// <typeparam name="T"></typeparam> /// <returns>IMore<T> continuation</returns> public static IMore <T> Called <T>( this IBeen <T> been, Func <string> messageGenerator ) where T : class { return(been.AddMatcher(actual => { try { var all = actual.ReceivedCalls().ToArray(); var passed = all.Any(); return new MatcherResult( passed, MessageHelpers.FinalMessageFor( () => DumpCalls(all, passed), messageGenerator ) ); } catch (NotASubstituteException) { // can't have a matcher result as negation gets in the way throw new UnmetExpectationException( $"{actual} is not a substitute" ); } })); }
/// <summary> /// Tests if the actual DateTime is approximately equal to the /// expected value within the provided allowed drift value /// </summary> /// <param name="continuation">Continuation to operate on</param> /// <param name="expected">Expected value</param> /// <param name="allowedDrift">How much the actual value may drift from the expected /// value</param> /// <param name="customMessageGenerator">Generates a custom message to include when /// this expectation fails</param> /// <returns></returns> public static IMore <DateTime?> Equal( this IApproximately <DateTime?> continuation, DateTime expected, TimeSpan allowedDrift, Func <string> customMessageGenerator) { continuation.AddMatcher(actual => { var delta = actual == null ? allowedDrift.TotalMilliseconds + 1 : Math.Abs((actual.Value - expected) .TotalMilliseconds); var allowed = Math.Abs(allowedDrift.TotalMilliseconds); var passed = delta <= allowed; return(new MatcherResult(passed, () => MessageHelpers.FinalMessageFor(() => $@"Expected { actual.Stringify() } to approximately equal { expected.Stringify() } within a timespan of { allowedDrift.Stringify() }", customMessageGenerator))); }); return(continuation.More()); }
private static Func <T, IMatcherResult> MatchMatcherFor <T>( Func <T, bool> test, string customMessage ) { return(actual => { var passed = test(actual); var message = passed ? $"Expected {actual} not to be matched" : $"Expected {actual} to be matched"; return new MatcherResult( passed, MessageHelpers.FinalMessageFor(message, customMessage) ); }); }
private static Func <T, MatcherResult> TruthTestFor <T>( T expected, string message ) { return(actual => { if (actual.Equals(expected)) { return new MatcherResult(true, $"Did not expect {true}"); } return new MatcherResult( false, MessageHelpers.FinalMessageFor( $"Expected {expected} but got {actual}", message )); }); }
/// <summary> /// Tests if the actual value is approximately equal, using the /// provided comparer /// </summary> /// <param name="approx"></param> /// <param name="expected"></param> /// <param name="comparer"></param> /// <param name="customMessageGenerator"></param> /// <returns></returns> public static IMore <double> Equal(this IApproximately <double> approx, double expected, IEqualityComparer <double> comparer, Func <string> customMessageGenerator) { approx.AddMatcher(actual => { var passed = comparer.Equals(actual, expected); return(new MatcherResult(passed, () => MessageHelpers.FinalMessageFor( () => $@"Expected { actual.Stringify() } to approxmiately equal { expected.Stringify() }", customMessageGenerator))); }); return(approx.More()); }