Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NotBeApproximately version accepting nullable subject and expected #939

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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