Skip to content

Commit

Permalink
Extract method for failing of BeApproximately and NotBeApproximately
Browse files Browse the repository at this point in the history
  • Loading branch information
krajek committed Oct 4, 2018
1 parent bf22bde commit 21703f7
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions Src/FluentAssertions/NumericAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -785,11 +785,7 @@ public static class NumericAssertionsExtensions
{
float actualDifference = Math.Abs(expectedValue - (float)parent.Subject);

Execute.Assertion
.ForCondition(actualDifference <= precision)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to approximate {1} +/- {2}{reason}, but {0} differed by {3}.",
parent.Subject, expectedValue, precision, actualDifference);
FailIfDifferenceOutsidePrecision(actualDifference <= precision, parent, expectedValue, precision, actualDifference, because, becauseArgs);

return new AndConstraint<NumericAssertions<float>>(parent);
}
Expand Down Expand Up @@ -890,11 +886,7 @@ public static class NumericAssertionsExtensions
{
double actualDifference = Math.Abs(expectedValue - (double)parent.Subject);

Execute.Assertion
.ForCondition(actualDifference <= precision)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to approximate {1} +/- {2}{reason}, but {0} differed by {3}.",
parent.Subject, expectedValue, precision, actualDifference);
FailIfDifferenceOutsidePrecision(actualDifference <= precision, parent, expectedValue, precision, actualDifference, because, becauseArgs);

return new AndConstraint<NumericAssertions<double>>(parent);
}
Expand Down Expand Up @@ -995,13 +987,21 @@ public static class NumericAssertionsExtensions
{
decimal actualDifference = Math.Abs(expectedValue - (decimal)parent.Subject);

FailIfDifferenceOutsidePrecision(actualDifference <= precision, parent, expectedValue, precision, actualDifference, because, becauseArgs);

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

private static void FailIfDifferenceOutsidePrecision<T>(
bool differenceWithinPrecision,
NumericAssertions<T> parent, T expectedValue, T precision, T actualDifference,
string because, object[] becauseArgs) where T:struct
{
Execute.Assertion
.ForCondition(actualDifference <= precision)
.ForCondition(differenceWithinPrecision)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to approximate {1} +/- {2}{reason}, but {0} differed by {3}.",
parent.Subject, expectedValue, precision, actualDifference);

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

#endregion
Expand Down Expand Up @@ -1063,11 +1063,7 @@ public static class NumericAssertionsExtensions
{
float actualDifference = Math.Abs(unexpectedValue - (float)parent.Subject);

Execute.Assertion
.ForCondition(actualDifference > precision)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to not approximate {1} +/- {2}{reason}, but {0} only differed by {3}.",
parent.Subject, unexpectedValue, precision, actualDifference);
FailIfDifferenceWithinPrecision(parent, actualDifference > precision, unexpectedValue, precision, actualDifference, because, becauseArgs);

return new AndConstraint<NumericAssertions<float>>(parent);
}
Expand Down Expand Up @@ -1127,11 +1123,7 @@ public static class NumericAssertionsExtensions
{
double actualDifference = Math.Abs(unexpectedValue - (double)parent.Subject);

Execute.Assertion
.ForCondition(actualDifference > precision)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to not approximate {1} +/- {2}{reason}, but {0} only differed by {3}.",
parent.Subject, unexpectedValue, precision, actualDifference);
FailIfDifferenceWithinPrecision(parent, actualDifference > precision, unexpectedValue, precision, actualDifference, because, becauseArgs);

return new AndConstraint<NumericAssertions<double>>(parent);
}
Expand Down Expand Up @@ -1191,13 +1183,21 @@ public static class NumericAssertionsExtensions
{
decimal actualDifference = Math.Abs(unexpectedValue - (decimal)parent.Subject);

FailIfDifferenceWithinPrecision(parent, actualDifference > precision, unexpectedValue, precision, actualDifference, because, becauseArgs);

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

private static void FailIfDifferenceWithinPrecision<T>(
NumericAssertions<T> parent, bool differenceOutsidePrecision,
T unexpectedValue, T precision, T actualDifference,
string because, object[] becauseArgs) where T : struct
{
Execute.Assertion
.ForCondition(actualDifference > precision)
.ForCondition(differenceOutsidePrecision)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to not approximate {1} +/- {2}{reason}, but {0} only differed by {3}.",
parent.Subject, unexpectedValue, precision, actualDifference);

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

#endregion
Expand Down

0 comments on commit 21703f7

Please sign in to comment.