Skip to content

Commit

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

/// <summary>
/// Asserts a floating point value approximates another value as close as possible.
/// </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<float>> BeApproximately(this NullableNumericAssertions<float> parent,
float? expectedValue, float precision, string because = "",
params object[] becauseArgs)
{
if (parent.Subject == null && expectedValue == null)
{
return new AndConstraint<NullableNumericAssertions<float>>(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<float>>(parent);
}

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

[Fact]
public void When_nullable_float_is_indeed_approximating_a_nullable_value_it_should_not_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
float? value = 3.1415927f;
float? expected = 3.142f;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[Fact]
public void When_nullable_float_is_not_approximating_a_value_it_should_throw()
{
Expand Down

0 comments on commit 675c41f

Please sign in to comment.