diff --git a/Src/FluentAssertions/NumericAssertionsExtensions.cs b/Src/FluentAssertions/NumericAssertionsExtensions.cs index 5affa50e5d..2f870b870a 100644 --- a/Src/FluentAssertions/NumericAssertionsExtensions.cs +++ b/Src/FluentAssertions/NumericAssertionsExtensions.cs @@ -1100,6 +1100,47 @@ public static class NumericAssertionsExtensions return new AndConstraint>(parent); } + /// + /// Asserts a double value does not approximate another value by a given amount. + /// Does not throw if null subject value approximates null value. + /// + /// The object that is being extended. + /// + /// The unexpected value to compare the actual value with. + /// + /// + /// The minimum exclusive amount of which the two values should differ. + /// + /// + /// A formatted phrase as is supported by explaining why the assertion + /// is needed. If the phrase does not start with the word because, it is prepended automatically. + /// + /// + /// Zero or more objects to format using the placeholders in . + /// + public static AndConstraint> NotBeApproximately(this NullableNumericAssertions parent, + double? unexpectedValue, double precision, string because = "", + params object[] becauseArgs) + { + if (parent.Subject == null && unexpectedValue == null) + { + return new AndConstraint>(parent); + } + + bool succeeded = Execute.Assertion + .ForCondition(unexpectedValue != null) + .BecauseOf(because, becauseArgs) + .FailWith("Expected {context:value} to not approximate {0} +/- {1}{reason}, but it was {2}.", unexpectedValue, precision, parent.Subject); + + if (succeeded) + { + // ReSharper disable once PossibleInvalidOperationException + parent.NotBeApproximately(unexpectedValue.Value, precision, because, becauseArgs); + } + + return new AndConstraint>(parent); + } + /// /// Asserts a double value does not approximate another value by a given amount. /// diff --git a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs index 45bf4e34a9..630ca86b90 100644 --- a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs @@ -824,6 +824,107 @@ public void When_asserting_not_approximately_and_nullable_double_is_indeed_appro .WithMessage("Expected value to not approximate 3.14 +/- 0.1, but 3.14*only differed by*"); } + [Fact] + public void When_asserting_not_approximately_and_nullable_double_is_not_approximating_a_nullable_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = 3.1415927F; + double? expected = 1.0F; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1F); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_not_approximately_and_nullable_double_is_not_approximating_a_null_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = 3.1415927F; + double? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1F); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw().WithMessage( + "Expected value to not approximate** +/-*0.1*, but it was 3.14*"); + } + + [Fact] + public void When_asserting_not_approximately_and_null_double_is_not_approximating_a_nullable_double_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = null; + double? expected = 20.0f; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1F); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw(); + } + + [Fact] + public void When_asserting_not_approximately_and_null_double_is_not_approximating_a_null_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = null; + double? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1F); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_not_approximately_and_nullable_double_is_approximating_a_nullable_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = 3.1415927F; + double? expected = 3.1F; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1F); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw(); + } + [Fact] public void When_asserting_not_approximately_and_nullable_float_is_not_approximating_a_value_it_should_not_throw() {