/// <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="comparer"></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, IEqualityComparer <DateTime> comparer, Func <string> customMessageGenerator) { continuation.AddMatcher(actual => { var passed = comparer.Equals(actual, expected); return(new MatcherResult(passed, () => { var allowed = comparer.TryGetPropertyValue <TimeSpan?>( nameof(EqualWithinTimespan.AllowedDrift)); var message = $@"Expected { actual.Stringify() } to approximately equal { expected.Stringify() }"; if (allowed.HasValue) { message += $" within a timespan of {allowed}"; } return FinalMessageFor(() => message, customMessageGenerator); })); }); return(continuation.More()); }
/// <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()); }
/// <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 <decimal> Equal(this IApproximately <decimal> approx, decimal expected, IEqualityComparer <decimal> comparer, Func <string> customMessageGenerator) { approx.AddMatcher(actual => { var passed = comparer.Equals(actual, expected); return(new MatcherResult(passed, () => FinalMessageFor( () => $@"Expected { actual.Stringify() } to approxmiately equal { expected.Stringify() }", customMessageGenerator))); }); return(approx.More()); }