diff --git a/Src/FluentAssertions/NumericAssertionsExtensions.cs b/Src/FluentAssertions/NumericAssertionsExtensions.cs index 86d06c2b4c..3d4352e268 100644 --- a/Src/FluentAssertions/NumericAssertionsExtensions.cs +++ b/Src/FluentAssertions/NumericAssertionsExtensions.cs @@ -1088,13 +1088,53 @@ public static class NumericAssertionsExtensions double unexpectedValue, double precision, string because = "", params object[] becauseArgs) { - Execute.Assertion - .ForCondition(parent.Subject != null) + if (parent.Subject != null) + { + var nonNullableAssertions = new NumericAssertions((double)parent.Subject); + nonNullableAssertions.NotBeApproximately(unexpectedValue, precision, because, becauseArgs); + } + + return new AndConstraint>(parent); + } + + /// + /// Asserts a double value does not approximate another value by a given amount. + /// Throws if both subject and are null. + /// + /// 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 || + parent.Subject != null && unexpectedValue == null) + { + return new AndConstraint>(parent); + } + + bool succeeded = Execute.Assertion + .ForCondition(parent.Subject != null && unexpectedValue != null) .BecauseOf(because, becauseArgs) - .FailWith("Expected {context:value} to not approximate {0} +/- {1}{reason}, but it was .", unexpectedValue, precision); + .FailWith("Expected {context:value} to not approximate {0} +/- {1}{reason}, but it was {2}.", unexpectedValue, precision, parent.Subject); - var nonNullableAssertions = new NumericAssertions((double)parent.Subject); - NotBeApproximately(nonNullableAssertions, unexpectedValue, precision, because, becauseArgs); + if (succeeded) + { + // ReSharper disable once PossibleInvalidOperationException + parent.NotBeApproximately(unexpectedValue.Value, precision, because, becauseArgs); + } return new AndConstraint>(parent); } diff --git a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs index c21e9fe538..0853c3c67c 100644 --- a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs @@ -799,8 +799,7 @@ public void When_asserting_not_approximately_and_nullable_double_has_no_value_it //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - act.Should().Throw().WithMessage( - "Expected value to not approximate 3.14 +/- 0.001, but it was ."); + act.Should().NotThrow(); } [Fact] @@ -824,6 +823,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.1415927; + double? expected = 1.0; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1); + + //----------------------------------------------------------------------------------------------------------- + // 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.1415927; + double? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [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.0; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [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.1); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw() + .WithMessage("Expected*null*0.1*but*null*"); + } + + [Fact] + public void When_asserting_not_approximately_and_nullable_double_is_approximating_a_nullable_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = 3.1415927; + double? expected = 3.1; + + //----------------------------------------------------------------------------------------------------------- + // 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() { diff --git a/Tests/Shared.Specs/NumericAssertionSpecs.cs b/Tests/Shared.Specs/NumericAssertionSpecs.cs index 9b08c5f373..24b1a2fcfb 100644 --- a/Tests/Shared.Specs/NumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NumericAssertionSpecs.cs @@ -1888,9 +1888,7 @@ public void When_a_nullable_double_has_no_value_and_should_not_approximate_it_sh //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - act - .Should().Throw() - .WithMessage("Expected value to not approximate*3.14* +/-*0.001*, but it was ."); + act.Should().NotThrow(); } #endregion