From 675c41fc4f89ed47c0e5cd43a86f463414b3e8d2 Mon Sep 17 00:00:00 2001 From: Artur Krajewski Date: Tue, 2 Oct 2018 19:43:54 +0200 Subject: [PATCH] Add `BeApproximately` for both subject and expected of type `float?` --- .../NumericAssertionsExtensions.cs | 40 +++++++ .../NullableNumericAssertionSpecs.cs | 102 ++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/Src/FluentAssertions/NumericAssertionsExtensions.cs b/Src/FluentAssertions/NumericAssertionsExtensions.cs index 65bd39d7ac..e1be9e537f 100644 --- a/Src/FluentAssertions/NumericAssertionsExtensions.cs +++ b/Src/FluentAssertions/NumericAssertionsExtensions.cs @@ -711,6 +711,46 @@ public static class NumericAssertionsExtensions return new AndConstraint>(parent); } + /// + /// Asserts a floating point value approximates another value as close as possible. + /// + /// 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, + float? expectedValue, float 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 floating point value approximates another value as close as possible. /// diff --git a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs index c3bb3989bf..44ed4a6736 100644 --- a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs @@ -476,6 +476,108 @@ public void When_nullable_float_is_indeed_approximating_a_value_it_should_not_th act.Should().NotThrow(); } + [Fact] + public void When_nullable_float_is_indeed_approximating_a_nullable_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = 3.1415927f; + float? expected = 3.142f; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1f); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_nullable_float_is_null_approximating_a_nullable_null_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = null; + float? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1f); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_nullable_float_with_value_is_not_approximating_a_non_null_nullable_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = 13; + float? expected = 12; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1f); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw().WithMessage("Expected*12*0.1*13*"); + } + + [Fact] + public void When_nullable_float_is_null_approximating_a_non_null_nullable_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = null; + float? expected = 12; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1f); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw().WithMessage( + "Expected value to approximate 12F +/- 0.1F, but it was ."); + } + + [Fact] + public void When_nullable_float_is_not_null_approximating_a_null_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = 12; + float? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().BeApproximately(expected, 0.1f); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw().WithMessage( + "Expected value to approximate +/- 0.1F, but it was 12F."); + } + [Fact] public void When_nullable_float_is_not_approximating_a_value_it_should_throw() {