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 BeApproximately version for both subject and expected nullable #934

Merged
merged 3 commits into from Oct 2, 2018
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
123 changes: 123 additions & 0 deletions Src/FluentAssertions/NumericAssertionsExtensions.cs
Expand Up @@ -711,6 +711,47 @@ public static class NumericAssertionsExtensions
return new AndConstraint<NullableNumericAssertions<float>>(parent);
}

/// <summary>
/// Asserts a floating point value approximates another value as close as possible.
/// Does not throw if null subject value approximates null <paramref name="expectedValue"/> 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<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 Expand Up @@ -775,6 +816,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 null subject value approximates null <paramref name="expectedValue"/> 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 Expand Up @@ -839,6 +921,47 @@ public static class NumericAssertionsExtensions
return new AndConstraint<NullableNumericAssertions<decimal>>(parent);
}

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

/// <summary>
/// Asserts a decimal value approximates another value as close as possible.
/// </summary>
Expand Down