From 3f33cc2bb46768e5031ef9d20c9255ff6d2fabca Mon Sep 17 00:00:00 2001 From: Artur Krajewski Date: Mon, 1 Oct 2018 21:54:57 +0200 Subject: [PATCH] Add `BeApproximately` for both subject and expected of type `double?` --- .../NumericAssertionsExtensions.cs | 41 +++++++ .../NullableNumericAssertionSpecs.cs | 102 ++++++++++++++++++ 2 files changed, 143 insertions(+) diff --git a/Src/FluentAssertions/NumericAssertionsExtensions.cs b/Src/FluentAssertions/NumericAssertionsExtensions.cs index 3dd20af3e9..65bd39d7ac 100644 --- a/Src/FluentAssertions/NumericAssertionsExtensions.cs +++ b/Src/FluentAssertions/NumericAssertionsExtensions.cs @@ -775,6 +775,47 @@ public static class NumericAssertionsExtensions return new AndConstraint>(parent); } + /// + /// Asserts a double value approximates another value as close as possible. + /// Does not throw if both null value approximates another null value. + /// + /// The object that is being extended. + /// + /// The expected value to compare the actual value with. + /// + /// + /// The maximum amount of which the two values may 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> BeApproximately(this NullableNumericAssertions parent, + double? expectedValue, double precision, string because = "", + params object[] becauseArgs) + { + if(parent.Subject == null && expectedValue == null) + { + return new AndConstraint>(parent); + } + + bool succeeded = Execute.Assertion + .ForCondition(expectedValue != null) + .BecauseOf(because, becauseArgs) + .FailWith("Expected {context:value} to approximate {0} +/- {1}{reason}, but it was {2}.", expectedValue, precision, parent.Subject); + + if (succeeded) + { + // ReSharper disable once PossibleInvalidOperationException + parent.BeApproximately(expectedValue.Value, precision, because, becauseArgs); + } + + return new AndConstraint>(parent); + } + /// /// Asserts a double value approximates another value as close as possible. /// diff --git a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs index 1eaf57e231..c3bb3989bf 100644 --- a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs @@ -314,6 +314,108 @@ public void When_nullable_double_is_indeed_approximating_a_value_it_should_not_t act.Should().NotThrow(); } + [Fact] + public void When_nullable_double_is_indeed_approximating_a_nullable_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = 3.1415927; + double? expected = 3.142; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_nullable_double_is_null_approximating_a_nullable_null_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = null; + double? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_nullable_double_with_value_is_not_approximating_a_non_null_nullable_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = 13; + double? expected = 12; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw().WithMessage("Expected*12.0*0.1*13.0*"); + } + + [Fact] + public void When_nullable_double_is_null_approximating_a_non_null_nullable_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = null; + double? expected = 12; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw().WithMessage( + "Expected value to approximate 12.0 +/- 0.1, but it was ."); + } + + [Fact] + public void When_nullable_double_is_not_null_approximating_a_null_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + double? value = 12; + double? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw().WithMessage( + "Expected value to approximate +/- 0.1, but it was 12.0."); + } + [Fact] public void When_nullable_double_has_no_value_it_should_throw() {