Skip to content

Commit

Permalink
Add NotBeApproximately version accepting nullable subject and expec…
Browse files Browse the repository at this point in the history
…ted (#939)

* NotBeApproximately float version accepting nullable subject and expected

* NotBeApproximately double version accepting nullable subject and expected

* NotBeApproximately decimal version accepting nullable subject and expected
  • Loading branch information
krajek authored and jnyrup committed Oct 19, 2018
1 parent 241d95a commit b74b041
Show file tree
Hide file tree
Showing 3 changed files with 444 additions and 31 deletions.
150 changes: 135 additions & 15 deletions Src/FluentAssertions/NumericAssertionsExtensions.cs
Expand Up @@ -988,13 +988,53 @@ public static class NumericAssertionsExtensions
float unexpectedValue, float precision, string because = "",
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(parent.Subject != null)
if(parent.Subject != null)
{
var nonNullableAssertions = new NumericAssertions<float>((float)parent.Subject);
nonNullableAssertions.NotBeApproximately(unexpectedValue, precision, because, becauseArgs);
}

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

/// <summary>
/// Asserts a floating point 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<float>> NotBeApproximately(this NullableNumericAssertions<float> parent,
float? unexpectedValue, float precision, string because = "",
params object[] becauseArgs)
{
if (parent.Subject == null && unexpectedValue != null ||
parent.Subject != null && unexpectedValue == null)
{
return new AndConstraint<NullableNumericAssertions<float>>(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<float>((float)parent.Subject);
nonNullableAssertions.NotBeApproximately(unexpectedValue, precision, because, becauseArgs);
if (succeeded)
{
// ReSharper disable once PossibleInvalidOperationException
parent.NotBeApproximately(unexpectedValue.Value, precision, because, becauseArgs);
}

return new AndConstraint<NullableNumericAssertions<float>>(parent);
}
Expand Down Expand Up @@ -1048,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 Expand Up @@ -1108,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

0 comments on commit b74b041

Please sign in to comment.