public static AndConstraint <NumericAssertions <T> > BeGreaterOrEqualTo <T>(this NumericAssertions <T> assertion, T expected, T accuracy, string because = "", params object[] becauseArgs) where T : struct, IConvertible { var expectedAsDouble = Convert.ToDouble(expected); var accuracyAsDouble = Convert.ToDouble(accuracy); return(assertion.BeGreaterOrEqualTo((T)Convert.ChangeType(expectedAsDouble - accuracyAsDouble, typeof(T)), because, becauseArgs)); }
private static void FailIfDifferenceOutsidePrecision <T>( bool differenceWithinPrecision, NumericAssertions <T> parent, T expectedValue, T precision, T actualDifference, string because, object[] becauseArgs) where T : struct { Execute.Assertion.ForCondition(differenceWithinPrecision).BecauseOf(because, becauseArgs).FailWith("Expected {context:value} to approximate {1} +/- {2}{reason}, but {0} differed by {3}.", (object)parent.Subject, (object)expectedValue, (object)precision, (object)actualDifference); }
/// <summary> /// Wraps /// <see /// cref="NumericAssertionsExtensions.BeApproximately(NumericAssertions{float}, float, float, string, object[])" /> /// to provide the expected comparison epsilon. /// </summary> /// <param name="parent"></param> /// <param name="expectedValue"></param> /// <param name="because"></param> /// <param name="becauseArgs"></param> /// <returns></returns> public static AndConstraint <NumericAssertions <float> > BeApproximately( this NumericAssertions <float> parent, float expectedValue, string because = "", params object[] becauseArgs ) => parent.BeApproximately( expectedValue, FloatExtensions.ComparisonEpsilon, because, becauseArgs );
/// <summary> /// Asserts a decimal value approximates another value as close as possible. /// </summary> /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param> /// <param name="expectedValue"> /// The expected value to compare the actual value with. /// </param> /// <param name="precision"> /// The maximum amount of which the two values may differ. /// </param> /// <param name="reason"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="reasonArgs"> /// Zero or more objects to format using the placeholders in <see cref="reason"/>. /// </param> public static AndConstraint <NumericAssertions <decimal> > BeApproximately(this NumericAssertions <decimal> parent, decimal expectedValue, decimal precision, string reason = "", params object[] reasonArgs) { decimal actualDifference = Math.Abs(expectedValue - (decimal)parent.Subject); Execute.Assertion .ForCondition(actualDifference <= precision) .BecauseOf(reason, reasonArgs) .FailWith("Expected value {0} to approximate {1} +/- {2}{reason}, but it differed by {3}.", parent.Subject, expectedValue, precision, actualDifference); return(new AndConstraint <NumericAssertions <decimal> >(parent)); }
/// <summary> /// Asserts a double value does not approximate another value by a given amount. /// </summary> /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param> /// <param name="unexpectedValue"> /// The unexpected value to compare the actual value with. /// </param> /// <param name="precision"> /// The minimum exclusive amount of which the two values should differ. /// </param> /// <param name="because"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="becauseArgs"> /// Zero or more objects to format using the placeholders in <see cref="because"/>. /// </param> public static AndConstraint <NumericAssertions <double> > NotBeApproximately(this NumericAssertions <double> parent, double unexpectedValue, double precision, string because = "", params object[] becauseArgs) { double actualDifference = Math.Abs(unexpectedValue - (double)parent.Subject); Execute.Assertion .ForCondition(actualDifference > precision) .BecauseOf(because, becauseArgs) .FailWith("Expected {context:value} {0} to not approximate {1} +/- {2}{reason}, but it only differed by {3}.", parent.Subject, unexpectedValue, precision, actualDifference); return(new AndConstraint <NumericAssertions <double> >(parent)); }
/// <summary> /// Asserts a floating point value approximates another value as close as possible. /// </summary> /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param> /// <param name="expectedValue"> /// The expected value to compare the actual value with. /// </param> /// <param name="precision"> /// The maximum amount of which the two values may differ. /// </param> /// <param name="because"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="becauseArgs"> /// Zero or more objects to format using the placeholders in <see cref="because"/>. /// </param> public static AndConstraint <NumericAssertions <float> > BeApproximately(this NumericAssertions <float> parent, float expectedValue, float precision, string because = "", params object [] becauseArgs) { float actualDifference = Math.Abs(expectedValue - (float)parent.Subject); Execute.Assertion .ForCondition(actualDifference <= precision) .BecauseOf(because, becauseArgs) .FailWith("Expected {context:value} {0} to approximate {1} +/- {2}{reason}, but it differed by {3}.", parent.Subject, expectedValue, precision, actualDifference); return(new AndConstraint <NumericAssertions <float> >(parent)); }
/// <summary> /// Asserts a floating point value approximates another value as close as possible. /// </summary> /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param> /// <param name="expectedValue"> /// The expected value to compare the actual value with. /// </param> /// <param name="precision"> /// The maximum amount of which the two values may differ. /// </param> /// <param name="reason"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="reasonArgs"> /// Zero or more objects to format using the placeholders in <see cref="reason"/>. /// </param> public static AndConstraint<NullableNumericAssertions<float>> BeApproximately(this NullableNumericAssertions<float> parent, float expectedValue, float precision, string reason = "", params object [] reasonArgs) { Execute.Assertion .ForCondition(parent.Subject != null) .BecauseOf(reason, reasonArgs) .FailWith("Expected value to approximate {0} +/- {1}{reason}, but it was <null>.", expectedValue, precision); var nonNullableAssertions = new NumericAssertions<float>((float) parent.Subject); nonNullableAssertions.BeApproximately(expectedValue, precision, reason, reasonArgs); return new AndConstraint<NullableNumericAssertions<float>>(parent); }
/// <summary> /// Asserts a floating point value approximates another value as close as possible. /// </summary> /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param> /// <param name="expectedValue"> /// The expected value to compare the actual value with. /// </param> /// <param name="precision"> /// The maximum amount of which the two values may differ. /// </param> /// <param name="reason"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="reasonArgs"> /// Zero or more objects to format using the placeholders in <see cref="reason"/>. /// </param> public static AndConstraint <NullableNumericAssertions <float> > BeApproximately(this NullableNumericAssertions <float> parent, float expectedValue, float precision, string reason = "", params object [] reasonArgs) { Execute.Assertion .ForCondition(parent.Subject != null) .BecauseOf(reason, reasonArgs) .FailWith("Expected value to approximate {0} +/- {1}{reason}, but it was <null>.", expectedValue, precision); var nonNullableAssertions = new NumericAssertions <float>((float)parent.Subject); nonNullableAssertions.BeApproximately(expectedValue, precision, reason, reasonArgs); return(new AndConstraint <NullableNumericAssertions <float> >(parent)); }
/// <summary> /// Asserts a floating point value does not approximate another value by a given amount. /// </summary> /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param> /// <param name="unexpectedValue"> /// The unexpected value to compare the actual value with. /// </param> /// <param name="precision"> /// The minimum exclusive amount of which the two values should differ. /// </param> /// <param name="because"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="becauseArgs"> /// Zero or more objects to format using the placeholders in <see cref="because"/>. /// </param> public static AndConstraint <NullableNumericAssertions <float> > NotBeApproximately(this NullableNumericAssertions <float> parent, float unexpectedValue, float precision, string because = "", params object[] becauseArgs) { Execute.Assertion .ForCondition(parent.Subject != null) .BecauseOf(because, becauseArgs) .FailWith("Expected {context:value} to not approximate {0} +/- {1}{reason}, but it was <null>.", unexpectedValue, precision); var nonNullableAssertions = new NumericAssertions <float>((float)parent.Subject); nonNullableAssertions.NotBeApproximately(unexpectedValue, precision, because, becauseArgs); return(new AndConstraint <NullableNumericAssertions <float> >(parent)); }
/// <summary> /// Asserts a double value approximates another value as close as possible. /// </summary> /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param> /// <param name="expectedValue"> /// The expected value to compare the actual value with. /// </param> /// <param name="precision"> /// The maximum amount of which the two values may differ. /// </param> /// <param name="because"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="becauseArgs"> /// Zero or more objects to format using the placeholders in <see cref="because"/>. /// </param> public static AndConstraint <NullableNumericAssertions <double> > BeApproximately(this NullableNumericAssertions <double> parent, double expectedValue, double precision, string because = "", params object [] becauseArgs) { Execute.Assertion .ForCondition(parent.Subject != null) .BecauseOf(because, becauseArgs) .FailWith("Expected {context:value} to approximate {0} +/- {1}{reason}, but it was <null>.", expectedValue, precision); var nonNullableAssertions = new NumericAssertions <double>((double)parent.Subject); BeApproximately(nonNullableAssertions, expectedValue, precision, because, becauseArgs); return(new AndConstraint <NullableNumericAssertions <double> >(parent)); }
public static AndConstraint <NumericAssertions <Length> > NotBeApproximately(this NumericAssertions <Length> parent, Length expectedValue, Length precision, string because = "", params object[] becauseArgs) { string failMessage = ((Length?)parent.Subject).HasValue ? "Did not expect {context:Length} to be approximately {0} +/- {1}{reason}, but found {2} which differs by {3}." : "Did not expect {context:Length} to be approximately {0} +/- {1}{reason}, but found {2}."; var difference = (Length?)parent.Subject - expectedValue; Execute.Assertion .ForCondition( ((Length?)parent.Subject).HasValue && Length.Abs(difference) > precision) .BecauseOf(because, becauseArgs) .FailWith(failMessage, expectedValue, precision, ((Length?)parent.Subject), difference); return(new AndConstraint <NumericAssertions <Length> >(parent)); }
public static AndConstraint <NumericAssertions <float> > BeApproximatelyOrBothNaNOrInfinity( this NumericAssertions <float> parent, float expectedValue, int precisionDigits = 5, string because = "", params object[] becauseArgs) { if (bothMatch(IsNaN) || bothMatch(IsPositiveInfinity) || bothMatch(IsNegativeInfinity)) { return(new AndConstraint <NumericAssertions <float> >(parent)); } var acceptedPrecision = Max(Epsilon, Abs(expectedValue) * (float)Pow(0.1, precisionDigits)); return(parent.BeApproximately(expectedValue, acceptedPrecision, because, becauseArgs)); bool bothMatch(Func <float, bool> predicate) => predicate(expectedValue) && predicate((float)parent.Subject); }
public static AndConstraint <NumericAssertions <int> > BeApproximately(this NumericAssertions <int> parent, int target, double percent, string because = "", params object[] becauseArgs) { if (percent > 1) { throw new LPGException("PercentageRange > 1"); } if (percent < 0) { throw new LPGException("PercentageRange<0"); } int actualValue = (int)parent.Subject; int minValue = (int)(target - target * percent); int maxValue = (int)(target + target * percent); FailIfValueOutsideBounds(minValue <= actualValue && actualValue <= maxValue, minValue, maxValue, actualValue, because, becauseArgs); return(new AndConstraint <NumericAssertions <int> >(parent)); }
/// <summary> /// Asserts a double value approximates another value as close as possible. /// </summary> /// <param name="parent">The <see cref="T:FluentAssertions.Numeric.NumericAssertions`1" /> object that is being extended.</param> /// <param name="expectedValue"> /// The expected value to compare the actual value with. /// </param> /// <param name="precision"> /// The maximum amount of which the two values may differ. /// </param> /// <param name="because"> /// A formatted phrase as is supported by <see cref="M:System.String.Format(System.String,System.Object[])" /> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="becauseArgs"> /// Zero or more objects to format using the placeholders in <see cref="!:because" />. /// </param> public static AndConstraint <NumericAssertions <double> > BeApproximately( this NumericAssertions <double> parent, double expectedValue, string because = "", params object[] becauseArgs) { if (double.IsNaN(expectedValue) && double.IsNaN(expectedValue)) { return(new AndConstraint <NumericAssertions <double> >(parent)); } if (double.IsPositiveInfinity(expectedValue) && double.IsPositiveInfinity(expectedValue)) { return(new AndConstraint <NumericAssertions <double> >(parent)); } if (double.IsNegativeInfinity(expectedValue) && double.IsNegativeInfinity(expectedValue)) { return(new AndConstraint <NumericAssertions <double> >(parent)); } double actualDifference = Math.Abs(expectedValue - (double)parent.Subject); FailIfDifferenceOutsidePrecision <double>(actualDifference <= Constants.ZeroEpsilon, parent, expectedValue, Constants.ZeroEpsilon, actualDifference, because, becauseArgs); return(new AndConstraint <NumericAssertions <double> >(parent)); }
public static AndConstraint <NumericAssertions <float> > BeApproximately(this NumericAssertions <float> source, float expected, string reason, params object[] reasonArgs) { return(source.BeApproximately(expected, Single.Epsilon, reason, reasonArgs)); }
public static AndConstraint <NumericAssertions <decimal> > BeApproximately(this NumericAssertions <decimal> parent, decimal expectedValue, string because = "", params object[] becauseArgs) { return(parent.BeApproximately(expectedValue, NumericPrecision, because, becauseArgs)); }
public static AndConstraint <NumericAssertions <double> > BeApproximately (this NumericAssertions <double> This, double val, double eps) { return(This.BeInRange(val - eps / 2, val + eps / 2)); }
/// <summary> /// Asserts a double value approximates another value as close as possible. /// </summary> /// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param> /// <param name="expectedValue"> /// The expected value to compare the actual value with. /// </param> /// <param name="precision"> /// The maximum amount of which the two values may differ. /// </param> /// <param name="because"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="becauseArgs"> /// Zero or more objects to format using the placeholders in <see cref="because"/>. /// </param> public static AndConstraint<NullableNumericAssertions<double>> BeApproximately(this NullableNumericAssertions<double> parent, double expectedValue, double precision, string because = "", params object [] becauseArgs) { Execute.Assertion .ForCondition(parent.Subject != null) .BecauseOf(because, becauseArgs) .FailWith("Expected value to approximate {0} +/- {1}{reason}, but it was <null>.", expectedValue, precision); var nonNullableAssertions = new NumericAssertions<double>((double) parent.Subject); BeApproximately(nonNullableAssertions, expectedValue, precision, because, becauseArgs); return new AndConstraint<NullableNumericAssertions<double>>(parent); }
public static AndConstraint <NumericAssertions <double> > BeApproximately(this NumericAssertions <double> assertions, double expectedValue) { return(assertions.BeApproximately(expectedValue, 0.0001)); }
public static AndConstraint <NumericAssertions <decimal> > BeExactly(this NumericAssertions <decimal> source, int expected) { return(source.BeInRange(expected, expected)); }
public static AndConstraint <NumericAssertions <double> > BeApproximately(this NumericAssertions <double> parent, decimal expectedValue, string because = "", params object[] becauseArgs) => parent.BeApproximately((double)expectedValue, Math.Abs((double)expectedValue * TestsBase.DoublePrecisionPercentage), because, becauseArgs);
public static void BeNotEqualToDefaultHashCodeOf <T>(this NumericAssertions <int> assertions, T value) { Func <T, int> defaultHashFn = new GetHashCodeExpressionBuilder <T>().Build().Compile(); assertions.Should().NotBe(defaultHashFn(value)); }
public static void BeInvoked(this NumericAssertions <int> assertions, int ntimes) { assertions.Be(ntimes); }
public static AndConstraint <NumericAssertions <float> > BeApproximately(this NumericAssertions <float> source, float expected) { return(source.BeApproximately(expected, Single.Epsilon, "")); }
public static AndConstraint <NumericAssertions <double> > BeApproximately(this NumericAssertions <double> source, double expected) { return(source.BeApproximately(expected, Double.Epsilon, "")); }
public static AndConstraint <NumericAssertions <float> > BeExactly(this NumericAssertions <float> source, int expected) { return(BeExactly(source, expected, "")); }
public static AndConstraint <NumericAssertions <decimal> > BeApproximately(this NumericAssertions <decimal> source, decimal expected) { return(source.BeInRange(expected, expected)); }
public static AndConstraint <NumericAssertions <float> > BeExactly(this NumericAssertions <float> source, int expected, string reason, params object[] reasonArgs) { return(source.BeInRange(expected, expected, reason, reasonArgs)); }
public static AndConstraint <NumericAssertions <double> > BeCloseTo(this NumericAssertions <double> parent, double nearbyValue, double delta, string because = "", params object[] becauseArgs) { return(parent.BeInRange(nearbyValue - delta, nearbyValue + delta, because, becauseArgs)); }
public static AndConstraint <NumericAssertions <double> > Be(this NumericAssertions <double> parent, decimal expectedValue, string because = "", params object[] becauseArgs) => parent.Be((double)expectedValue, because, becauseArgs);
public static AndConstraint <NumericAssertions <decimal> > BeApproximately(this NumericAssertions <decimal> source, decimal expected, string reason, params object[] reasonArgs) { return(source.BeInRange(expected, expected, reason, reasonArgs)); }
public static AndConstraint <NumericAssertions <T> > BeZero <T>(this NumericAssertions <T> value) where T : struct { return(value.Be(default(T))); }