Skip to content

Commit

Permalink
NotBeApproximately decimal version accepting nullable subject and exp…
Browse files Browse the repository at this point in the history
…ected
  • Loading branch information
krajek committed Oct 13, 2018
1 parent 2ba8c45 commit a802df3
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 @@ -1188,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>((decimal)parent.Subject);
NotBeApproximately(nonNullableAssertions, unexpectedValue, precision, because, becauseArgs);
}

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

/// <summary>
/// Asserts a decimal 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<decimal>> NotBeApproximately(this NullableNumericAssertions<decimal> parent,
decimal? unexpectedValue, decimal precision, string because = "",
params object[] becauseArgs)
{
if (parent.Subject == null && unexpectedValue != null ||
parent.Subject != null && unexpectedValue == null)
{
return new AndConstraint<NullableNumericAssertions<decimal>>(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<decimal>((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<NullableNumericAssertions<decimal>>(parent);
}
Expand Down
104 changes: 102 additions & 2 deletions Tests/Shared.Specs/NullableNumericAssertionSpecs.cs
Expand Up @@ -1118,8 +1118,7 @@ public void When_asserting_not_approximately_and_nullable_decimal_has_no_value_i
//-----------------------------------------------------------------------------------------------------------
// 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 @@ -1143,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<XunitException>()
.WithMessage("Expected*<null>*0.1M*<null>*");
}

[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<XunitException>();
}

#endregion

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

#endregion
Expand Down

0 comments on commit a802df3

Please sign in to comment.