From 19140c2274029989160263658a7b1c9077a34f7b Mon Sep 17 00:00:00 2001 From: Artur Krajewski Date: Fri, 5 Oct 2018 20:59:22 +0200 Subject: [PATCH 1/3] NotBeApproximately float version accepting nullable subject and expected --- .../NumericAssertionsExtensions.cs | 50 ++++++++- .../NullableNumericAssertionSpecs.cs | 103 +++++++++++++++++- Tests/Shared.Specs/NumericAssertionSpecs.cs | 6 +- 3 files changed, 148 insertions(+), 11 deletions(-) diff --git a/Src/FluentAssertions/NumericAssertionsExtensions.cs b/Src/FluentAssertions/NumericAssertionsExtensions.cs index 4bafd8d773..86d06c2b4c 100644 --- a/Src/FluentAssertions/NumericAssertionsExtensions.cs +++ b/Src/FluentAssertions/NumericAssertionsExtensions.cs @@ -988,13 +988,53 @@ public static class NumericAssertionsExtensions float unexpectedValue, float precision, string because = "", params object[] becauseArgs) { - Execute.Assertion - .ForCondition(parent.Subject != null) + if(parent.Subject != null) + { + var nonNullableAssertions = new NumericAssertions((float)parent.Subject); + nonNullableAssertions.NotBeApproximately(unexpectedValue, precision, because, becauseArgs); + } + + return new AndConstraint>(parent); + } + + /// + /// Asserts a floating point 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, + float? unexpectedValue, float 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((float)parent.Subject); - nonNullableAssertions.NotBeApproximately(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 f461c60d69..c21e9fe538 100644 --- a/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NullableNumericAssertionSpecs.cs @@ -859,8 +859,7 @@ public void When_asserting_not_approximately_and_nullable_float_has_no_value_it_ //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - act.Should().Throw().WithMessage( - "Expected value to not approximate 3.14F* +/- 0.001F*, but it was ."); + act.Should().NotThrow(); } [Fact] @@ -884,6 +883,106 @@ public void When_asserting_not_approximately_and_nullable_float_is_indeed_approx .WithMessage("Expected value to not approximate *3.14F* +/- *0.1F* but 3.14* only differed by*"); } + [Fact] + public void When_asserting_not_approximately_and_nullable_float_is_not_approximating_a_nullable_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = 3.1415927F; + float? 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_float_is_not_approximating_a_null_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = 3.1415927F; + float? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1F); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_not_approximately_and_null_float_is_not_approximating_a_nullable_float_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = null; + float? expected = 20.0f; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1F); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + + [Fact] + public void When_asserting_not_approximately_and_null_float_is_not_approximating_a_null_value_it_should_not_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = null; + float? expected = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => value.Should().NotBeApproximately(expected, 0.1F); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw("Expected**+/-*0.1F**"); + } + + [Fact] + public void When_asserting_not_approximately_and_nullable_float_is_approximating_a_nullable_value_it_should_throw() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + float? value = 3.1415927F; + float? 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_decimal_is_not_approximating_a_value_it_should_not_throw() { diff --git a/Tests/Shared.Specs/NumericAssertionSpecs.cs b/Tests/Shared.Specs/NumericAssertionSpecs.cs index 0020df58f3..9b08c5f373 100644 --- a/Tests/Shared.Specs/NumericAssertionSpecs.cs +++ b/Tests/Shared.Specs/NumericAssertionSpecs.cs @@ -1666,7 +1666,7 @@ public void When_approximating_a_float_towards_nan_and_should_not_approximate_it } [Fact] - public void When_a_nullable_float_has_no_value_and_should_not_approximate_it_should_throw() + public void When_a_nullable_float_has_no_value_and_should_not_approximate_it_should_not_throw() { //----------------------------------------------------------------------------------------------------------- // Arrange @@ -1682,9 +1682,7 @@ public void When_a_nullable_float_has_no_value_and_should_not_approximate_it_sho //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - act - .Should().Throw() - .WithMessage("Expected value to not approximate*3.14* +/-*0.001*, but it was ."); + act.Should().NotThrow(); } #endregion From 2ba8c45ecb9433406aa2cbce54afccc1757315f6 Mon Sep 17 00:00:00 2001 From: Artur Krajewski Date: Sun, 7 Oct 2018 17:05:07 +0200 Subject: [PATCH 2/3] NotBeApproximately double 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 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 From a802df34915e131db27794e193895c9505d15723 Mon Sep 17 00:00:00 2001 From: Artur Krajewski Date: Sun, 7 Oct 2018 17:15:44 +0200 Subject: [PATCH 3/3] 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