Skip to content

Commit

Permalink
Add BeApproximately for both subject and expected of type double?
Browse files Browse the repository at this point in the history
  • Loading branch information
krajek committed Oct 2, 2018
1 parent 65f6753 commit 3f33cc2
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Src/FluentAssertions/NumericAssertionsExtensions.cs
Expand Up @@ -775,6 +775,47 @@ public static class NumericAssertionsExtensions
return new AndConstraint<NullableNumericAssertions<double>>(parent);
}

/// <summary>
/// Asserts a double value approximates another value as close as possible.
/// Does not throw if both null value approximates another null value.
/// </summary>
/// <param name="parent">The <see cref="NumericAssertions{T}"/> object that is being extended.</param>
/// <param name="expectedValue">
/// The expected value to compare the actual value with.
/// </param>
/// <param name="precision">
/// The maximum amount of which the two values may 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>> BeApproximately(this NullableNumericAssertions<double> parent,
double? expectedValue, double precision, string because = "",
params object[] becauseArgs)
{
if(parent.Subject == null && expectedValue == null)
{
return new AndConstraint<NullableNumericAssertions<double>>(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<NullableNumericAssertions<double>>(parent);
}

/// <summary>
/// Asserts a double value approximates another value as close as possible.
/// </summary>
Expand Down
102 changes: 102 additions & 0 deletions Tests/Shared.Specs/NullableNumericAssertionSpecs.cs
Expand Up @@ -314,6 +314,108 @@ public void When_nullable_double_is_indeed_approximating_a_value_it_should_not_t
act.Should().NotThrow();
}

[Fact]
public void When_nullable_double_is_indeed_approximating_a_nullable_value_it_should_not_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
double? value = 3.1415927;
double? expected = 3.142;

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

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

[Fact]
public void When_nullable_double_is_null_approximating_a_nullable_null_value_it_should_not_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
double? value = null;
double? expected = null;

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

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

[Fact]
public void When_nullable_double_with_value_is_not_approximating_a_non_null_nullable_value_it_should_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
double? value = 13;
double? expected = 12;

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

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>().WithMessage("Expected*12.0*0.1*13.0*");
}

[Fact]
public void When_nullable_double_is_null_approximating_a_non_null_nullable_value_it_should_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
double? value = null;
double? expected = 12;

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

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

[Fact]
public void When_nullable_double_is_not_null_approximating_a_null_value_it_should_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
double? value = 12;
double? expected = null;

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

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

[Fact]
public void When_nullable_double_has_no_value_it_should_throw()
{
Expand Down

0 comments on commit 3f33cc2

Please sign in to comment.