Skip to content

Commit

Permalink
Add BeApproximately version for both subject and expected nullable (#…
Browse files Browse the repository at this point in the history
…934)

* Add `BeApproximately` for both subject and expected of type `double?`

* Add `BeApproximately` for both subject and expected of type `float?`

* Add `BeApproximately` for both subject and expected of type `decimal?`
  • Loading branch information
krajek authored and jnyrup committed Oct 2, 2018
1 parent 65f6753 commit 42ef7d9
Show file tree
Hide file tree
Showing 2 changed files with 429 additions and 0 deletions.
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

0 comments on commit 42ef7d9

Please sign in to comment.