From a802df34915e131db27794e193895c9505d15723 Mon Sep 17 00:00:00 2001 From: Artur Krajewski Date: Sun, 7 Oct 2018 17:15:44 +0200 Subject: [PATCH] NotBeApproximately decimal version accepting nullable subject and expected --- .../NumericAssertionsExtensions.cs | 50 ++++++++- .../NullableNumericAssertionSpecs.cs | 104 +++++++++++++++++- Tests/Shared.Specs/NumericAssertionSpecs.cs | 4 +- 3 files changed, 148 insertions(+), 10 deletions(-) diff --git a/Src/FluentAssertions/NumericAssertionsExtensions.cs b/Src/FluentAssertions/NumericAssertionsExtensions.cs index 3d4352e268..d09f379204 100644 --- a/Src/FluentAssertions/NumericAssertionsExtensions.cs +++ b/Src/FluentAssertions/NumericAssertionsExtensions.cs @@ -1188,13 +1188,53 @@ public static class NumericAssertionsExtensions decimal unexpectedValue, decimal precision, string because = "", params object[] becauseArgs) { - Execute.Assertion - .ForCondition(parent.Subject != null) + if (parent.Subject != null) + { + var nonNullableAssertions = new NumericAssertions((decimal)parent.Subject); + NotBeApproximately(nonNullableAssertions, unexpectedValue, precision, because, becauseArgs); + } + + return new AndConstraint>(parent); + } + + /// + /// Asserts a decimal 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, + decimal? unexpectedValue, decimal 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((decimal)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 0853c3c67c..943e3517f3 100644 --- a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs @@ -1118,8 +1118,7 @@ public void When_asserting_not_approximately_and_nullable_decimal_has_no_value_i //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - act.Should().Throw().WithMessage( - "Expected value to not approximate*3.14* +/-*0.001*, but it was ."); + act.Should().NotThrow(); } [Fact] @@ -1143,6 +1142,107 @@ public void When_asserting_not_approximately_and_nullable_decimal_is_indeed_appr .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_decimal_is_not_approximating_a_nullable_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + decimal? value = 3.1415927m; + decimal? expected = 1.0m; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1m); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_not_approximately_and_nullable_decimal_is_not_approximating_a_null_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + decimal? value = 3.1415927m; + decimal? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1m); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_not_approximately_and_null_decimal_is_not_approximating_a_nullable_decimal_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + decimal? value = null; + decimal? expected = 20.0m; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1m); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_not_approximately_and_null_decimal_is_not_approximating_a_null_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + decimal? value = null; + decimal? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1m); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw() + .WithMessage("Expected**0.1M**"); + } + + [Fact] + public void When_asserting_not_approximately_and_nullable_decimal_is_approximating_a_nullable_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + decimal? value = 3.1415927m; + decimal? expected = 3.1m; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1m); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw(); + } + #endregion [Fact] diff --git a/Tests/Shared.Specs/NumericAssertionSpecs.cs b/Tests/Shared.Specs/NumericAssertionSpecs.cs index 24b1a2fcfb..200540b604 100644 --- a/Tests/Shared.Specs/NumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NumericAssertionSpecs.cs @@ -2056,9 +2056,7 @@ public void When_a_nullable_decimal_has_no_value_and_should_not_approximate_it_s //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - act - .Should().Throw() - .WithMessage("Expected value to not approximate*3.5* +/-*0.001*, but it was ."); + act.Should().NotThrow(); } #endregion