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 7, 2018
1 parent f9c638f commit e855ab7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Src/FluentAssertions/NumericAssertionsExtensions.cs
Expand Up @@ -1100,6 +1100,47 @@ public static class NumericAssertionsExtensions
return new AndConstraint<NullableNumericAssertions<double>>(parent);
}

/// <summary>
/// Asserts a double value does not approximate another value by a given amount.
/// Does not throw if null subject value approximates null <paramref name="unexpectedValue"/> value.
/// </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)
{
return new AndConstraint<NullableNumericAssertions<double>>(parent);
}

bool succeeded = Execute.Assertion
.ForCondition(unexpectedValue != null)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to not approximate {0} +/- {1}{reason}, but it was {2}.", unexpectedValue, precision, parent.Subject);

if (succeeded)
{
// ReSharper disable once PossibleInvalidOperationException
parent.NotBeApproximately(unexpectedValue.Value, precision, because, becauseArgs);
}

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

/// <summary>
/// Asserts a double value does not approximate another value by a given amount.
/// </summary>
Expand Down
101 changes: 101 additions & 0 deletions Tests/Shared.Specs/NullableNumericAssertionSpecs.cs
Expand Up @@ -824,6 +824,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.1415927F;
double? 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_double_is_not_approximating_a_null_value_it_should_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
double? value = 3.1415927F;
double? expected = null;

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => value.Should().NotBeApproximately(expected, 0.1F);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage(
"Expected value to not approximate*<null>* +/-*0.1*, but it was 3.14*");
}

[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.0f;

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => value.Should().NotBeApproximately(expected, 0.1F);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>();
}

[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.1F);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().NotThrow();
}

[Fact]
public void When_asserting_not_approximately_and_nullable_double_is_approximating_a_nullable_value_it_should_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
double? value = 3.1415927F;
double? expected = 3.1F;

//-----------------------------------------------------------------------------------------------------------
// 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

0 comments on commit e855ab7

Please sign in to comment.