diff --git a/Src/FluentAssertions/NumericAssertionsExtensions.cs b/Src/FluentAssertions/NumericAssertionsExtensions.cs index 4bafd8d773..d09f379204 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); } @@ -1048,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); } @@ -1108,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 f461c60d69..943e3517f3 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() { @@ -859,8 +959,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 +983,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() { @@ -919,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] @@ -944,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 0020df58f3..200540b604 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 @@ -1890,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 @@ -2060,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