Skip to content

Commit

Permalink
NotBeApproximately double version accepting nullable subject and expe…
Browse files Browse the repository at this point in the history
…cted
  • Loading branch information
krajek committed Oct 13, 2018
1 parent 19140c2 commit 2ba8c45
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 10 deletions.
50 changes: 45 additions & 5 deletions Src/FluentAssertions/NumericAssertionsExtensions.cs
Expand Up @@ -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>((double)parent.Subject);
nonNullableAssertions.NotBeApproximately(unexpectedValue, precision, because, becauseArgs);
}

return new AndConstraint<NullableNumericAssertions<double>>(parent);
}

/// <summary>
/// Asserts a double value does not approximate another value by a given amount.
/// Throws if both subject and <paramref name="unexpectedValue"/> are null.
/// </summary>
/// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param>
/// <param name="unexpectedValue">
/// The unexpected value to compare the actual value with.
/// </param>
/// <param name="precision">
/// The minimum exclusive amount of which the two values should differ.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])"/> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because"/>.
/// </param>
public static AndConstraint<NullableNumericAssertions<double>> NotBeApproximately(this NullableNumericAssertions<double> parent,
double? unexpectedValue, double precision, string because = "",
params object[] becauseArgs)
{
if (parent.Subject == null && unexpectedValue != null ||
parent.Subject != null && unexpectedValue == null)
{
return new AndConstraint<NullableNumericAssertions<double>>(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 <null>.", 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>((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<NullableNumericAssertions<double>>(parent);
}
Expand Down
104 changes: 102 additions & 2 deletions Tests/Shared.Specs/NullableNumericAssertionSpecs.cs
Expand Up @@ -799,8 +799,7 @@ public void When_asserting_not_approximately_and_nullable_double_has_no_value_it
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage(
"Expected value to not approximate 3.14 +/- 0.001, but it was <null>.");
act.Should().NotThrow();
}

[Fact]
Expand All @@ -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<XunitException>()
.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<XunitException>();
}

[Fact]
public void When_asserting_not_approximately_and_nullable_float_is_not_approximating_a_value_it_should_not_throw()
{
Expand Down
4 changes: 1 addition & 3 deletions Tests/Shared.Specs/NumericAssertionSpecs.cs
Expand Up @@ -1888,9 +1888,7 @@ public void When_a_nullable_double_has_no_value_and_should_not_approximate_it_sh
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act
.Should().Throw<XunitException>()
.WithMessage("Expected value to not approximate*3.14* +/-*0.001*, but it was <null>.");
act.Should().NotThrow();
}

#endregion
Expand Down

0 comments on commit 2ba8c45

Please sign in to comment.