Skip to content

Commit

Permalink
Add BeNaN and NotBeNaN assertions (#2606)
Browse files Browse the repository at this point in the history
  • Loading branch information
arocheleau committed Mar 19, 2024
1 parent b045844 commit 1dc38ae
Show file tree
Hide file tree
Showing 8 changed files with 692 additions and 1 deletion.
202 changes: 202 additions & 0 deletions Src/FluentAssertions/NumericAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,208 @@ public static class NumericAssertionsExtensions

#endregion

#region BeNaN

/// <summary>
/// Asserts that the number is seen as not a number (NaN).
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public static AndConstraint<NumericAssertions<float>> BeNaN(this NumericAssertions<float> parent,
string because = "",
params object[] becauseArgs)
{
float actualValue = parent.Subject.Value;

Execute.Assertion
.ForCondition(float.IsNaN(actualValue))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be NaN{reason}, but found {0}.", actualValue);

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

/// <summary>
/// Asserts that the number is seen as not a number (NaN).
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public static AndConstraint<NumericAssertions<double>> BeNaN(this NumericAssertions<double> parent,
string because = "",
params object[] becauseArgs)
{
double actualValue = parent.Subject.Value;

Execute.Assertion
.ForCondition(double.IsNaN(actualValue))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be NaN{reason}, but found {0}.", actualValue);

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

/// <summary>
/// Asserts that the number is seen as not a number (NaN).
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public static AndConstraint<NullableNumericAssertions<float>> BeNaN(this NullableNumericAssertions<float> parent,
string because = "",
params object[] becauseArgs)
{
float? actualValue = parent.Subject;

Execute.Assertion
.ForCondition(actualValue is { } value && float.IsNaN(value))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be NaN{reason}, but found {0}.", actualValue);

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

/// <summary>
/// Asserts that the number is seen as not a number (NaN).
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public static AndConstraint<NullableNumericAssertions<double>> BeNaN(this NullableNumericAssertions<double> parent,
string because = "",
params object[] becauseArgs)
{
double? actualValue = parent.Subject;

Execute.Assertion
.ForCondition(actualValue is { } value && double.IsNaN(value))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be NaN{reason}, but found {0}.", actualValue);

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

#endregion

#region NotBeNaN

/// <summary>
/// Asserts that the number is not seen as the special value not a number (NaN).
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public static AndConstraint<NumericAssertions<float>> NotBeNaN(this NumericAssertions<float> parent,
string because = "",
params object[] becauseArgs)
{
float actualValue = parent.Subject.Value;

Execute.Assertion
.ForCondition(!float.IsNaN(actualValue))
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:value} to be NaN{reason}.");

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

/// <summary>
/// Asserts that the number is not seen as the special value not a number (NaN).
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public static AndConstraint<NumericAssertions<double>> NotBeNaN(this NumericAssertions<double> parent,
string because = "",
params object[] becauseArgs)
{
double actualValue = parent.Subject.Value;

Execute.Assertion
.ForCondition(!double.IsNaN(actualValue))
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:value} to be NaN{reason}.");

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

/// <summary>
/// Asserts that the number is not seen as the special value not a number (NaN).
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public static AndConstraint<NullableNumericAssertions<float>> NotBeNaN(this NullableNumericAssertions<float> parent,
string because = "",
params object[] becauseArgs)
{
float? actualValue = parent.Subject;
bool actualValueIsNaN = actualValue is { } value && float.IsNaN(value);

Execute.Assertion
.ForCondition(!actualValueIsNaN)
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:value} to be NaN{reason}.");

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

/// <summary>
/// Asserts that the number is not seen as the special value not a number (NaN).
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public static AndConstraint<NullableNumericAssertions<double>> NotBeNaN(this NullableNumericAssertions<double> parent,
string because = "",
params object[] becauseArgs)
{
double? actualValue = parent.Subject;
bool actualValueIsNaN = actualValue is { } value && double.IsNaN(value);

Execute.Assertion
.ForCondition(!actualValueIsNaN)
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:value} to be NaN{reason}.");

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

#endregion

private static long GetMinValue(long value, ulong delta)
{
long minValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ namespace FluentAssertions
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<ushort>> BeCloseTo(this FluentAssertions.Numeric.NumericAssertions<ushort> parent, ushort nearbyValue, ushort delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<uint>> BeCloseTo(this FluentAssertions.Numeric.NumericAssertions<uint> parent, uint nearbyValue, uint delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<ulong>> BeCloseTo(this FluentAssertions.Numeric.NumericAssertions<ulong> parent, ulong nearbyValue, ulong delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<double>> BeNaN(this FluentAssertions.Numeric.NullableNumericAssertions<double> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<float>> BeNaN(this FluentAssertions.Numeric.NullableNumericAssertions<float> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<double>> BeNaN(this FluentAssertions.Numeric.NumericAssertions<double> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<float>> BeNaN(this FluentAssertions.Numeric.NumericAssertions<float> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<decimal>> NotBeApproximately(this FluentAssertions.Numeric.NullableNumericAssertions<decimal> parent, decimal unexpectedValue, decimal precision, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<decimal>> NotBeApproximately(this FluentAssertions.Numeric.NullableNumericAssertions<decimal> parent, decimal? unexpectedValue, decimal precision, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<double>> NotBeApproximately(this FluentAssertions.Numeric.NullableNumericAssertions<double> parent, double unexpectedValue, double precision, string because = "", params object[] becauseArgs) { }
Expand All @@ -314,6 +318,10 @@ namespace FluentAssertions
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<ushort>> NotBeCloseTo(this FluentAssertions.Numeric.NumericAssertions<ushort> parent, ushort distantValue, ushort delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<uint>> NotBeCloseTo(this FluentAssertions.Numeric.NumericAssertions<uint> parent, uint distantValue, uint delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<ulong>> NotBeCloseTo(this FluentAssertions.Numeric.NumericAssertions<ulong> parent, ulong distantValue, ulong delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<double>> NotBeNaN(this FluentAssertions.Numeric.NullableNumericAssertions<double> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<float>> NotBeNaN(this FluentAssertions.Numeric.NullableNumericAssertions<float> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<double>> NotBeNaN(this FluentAssertions.Numeric.NumericAssertions<double> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<float>> NotBeNaN(this FluentAssertions.Numeric.NumericAssertions<float> parent, string because = "", params object[] becauseArgs) { }
}
public static class ObjectAssertionsExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ namespace FluentAssertions
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<ushort>> BeCloseTo(this FluentAssertions.Numeric.NumericAssertions<ushort> parent, ushort nearbyValue, ushort delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<uint>> BeCloseTo(this FluentAssertions.Numeric.NumericAssertions<uint> parent, uint nearbyValue, uint delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<ulong>> BeCloseTo(this FluentAssertions.Numeric.NumericAssertions<ulong> parent, ulong nearbyValue, ulong delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<double>> BeNaN(this FluentAssertions.Numeric.NullableNumericAssertions<double> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<float>> BeNaN(this FluentAssertions.Numeric.NullableNumericAssertions<float> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<double>> BeNaN(this FluentAssertions.Numeric.NumericAssertions<double> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<float>> BeNaN(this FluentAssertions.Numeric.NumericAssertions<float> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<decimal>> NotBeApproximately(this FluentAssertions.Numeric.NullableNumericAssertions<decimal> parent, decimal unexpectedValue, decimal precision, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<decimal>> NotBeApproximately(this FluentAssertions.Numeric.NullableNumericAssertions<decimal> parent, decimal? unexpectedValue, decimal precision, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<double>> NotBeApproximately(this FluentAssertions.Numeric.NullableNumericAssertions<double> parent, double unexpectedValue, double precision, string because = "", params object[] becauseArgs) { }
Expand All @@ -327,6 +331,10 @@ namespace FluentAssertions
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<ushort>> NotBeCloseTo(this FluentAssertions.Numeric.NumericAssertions<ushort> parent, ushort distantValue, ushort delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<uint>> NotBeCloseTo(this FluentAssertions.Numeric.NumericAssertions<uint> parent, uint distantValue, uint delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<ulong>> NotBeCloseTo(this FluentAssertions.Numeric.NumericAssertions<ulong> parent, ulong distantValue, ulong delta, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<double>> NotBeNaN(this FluentAssertions.Numeric.NullableNumericAssertions<double> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NullableNumericAssertions<float>> NotBeNaN(this FluentAssertions.Numeric.NullableNumericAssertions<float> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<double>> NotBeNaN(this FluentAssertions.Numeric.NumericAssertions<double> parent, string because = "", params object[] becauseArgs) { }
public static FluentAssertions.AndConstraint<FluentAssertions.Numeric.NumericAssertions<float>> NotBeNaN(this FluentAssertions.Numeric.NumericAssertions<float> parent, string because = "", params object[] becauseArgs) { }
}
public static class ObjectAssertionsExtensions
{
Expand Down

0 comments on commit 1dc38ae

Please sign in to comment.