Skip to content

Commit

Permalink
SAVEPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Mar 5, 2022
1 parent 1fe9df7 commit fb53da1
Show file tree
Hide file tree
Showing 11 changed files with 522 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Src/FluentAssertions/AssertionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ public static NullableNumericAssertions<ulong> Should(this ulong? actualValue)
[Pure]
public static NumericAssertions<float> Should(this float actualValue)
{
return new NumericAssertions<float>(actualValue);
return new FloatAssertions(actualValue);
}

/// <summary>
Expand All @@ -651,7 +651,7 @@ public static NullableNumericAssertions<float> Should(this float? actualValue)
[Pure]
public static NumericAssertions<double> Should(this double actualValue)
{
return new NumericAssertions<double>(actualValue);
return new DoubleAssertions(actualValue);
}

/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions Src/FluentAssertions/Numeric/DoubleAssertions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace FluentAssertions.Numeric
{
internal class DoubleAssertions : NumericAssertions<double>
{
public DoubleAssertions(double value) : base(value)
{
}

protected override bool IsNaN(double value) => double.IsNaN(value);
}
}
11 changes: 11 additions & 0 deletions Src/FluentAssertions/Numeric/FloatAssertions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace FluentAssertions.Numeric
{
internal class FloatAssertions : NumericAssertions<float>
{
public FloatAssertions(float value) : base(value)
{
}

protected override bool IsNaN(float value) => float.IsNaN(value);
}
}
9 changes: 8 additions & 1 deletion Src/FluentAssertions/Numeric/NumericAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public AndConstraint<TAssertions> BePositive(string because = "", params object[
public AndConstraint<TAssertions> BeNegative(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(default(T)) < 0)
.ForCondition(Subject.HasValue && !IsNaN(Subject.Value) && Subject.Value.CompareTo(default(T)) < 0)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be negative{reason}, but found {0}.", Subject);

Expand Down Expand Up @@ -254,6 +254,11 @@ public AndConstraint<TAssertions> BeLessThan(T expected, string because = "", pa
public AndConstraint<TAssertions> BeGreaterThanOrEqualTo(T expected, string because = "",
params object[] becauseArgs)
{
if (IsNaN(expected))
{
throw new ArgumentException("A value can never be greater than or equal to a NaN", nameof(expected));
}

Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) >= 0)
.BecauseOf(because, becauseArgs)
Expand Down Expand Up @@ -449,5 +454,7 @@ public AndConstraint<TAssertions> NotBeOfType(Type unexpectedType, string becaus
/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");

protected virtual bool IsNaN(T value) => false;
}
}
10 changes: 10 additions & 0 deletions Src/FluentAssertions/NumericAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,11 @@ public static class NumericAssertionsExtensions
float expectedValue, float precision, string because = "",
params object[] becauseArgs)
{
if (float.IsNaN(expectedValue))
{
throw new ArgumentException("Cannot determine approximation of a float to NaN", nameof(expectedValue));
}

if (precision < 0)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
Expand Down Expand Up @@ -879,6 +884,11 @@ public static class NumericAssertionsExtensions
double expectedValue, double precision, string because = "",
params object[] becauseArgs)
{
if (double.IsNaN(expectedValue))
{
throw new ArgumentException("Cannot determine approximation of a double to NaN", nameof(expectedValue));
}

if (precision < 0)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,7 @@ namespace FluentAssertions.Numeric
public FluentAssertions.AndConstraint<TAssertions> BeOneOf(System.Collections.Generic.IEnumerable<T> validValues, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BePositive(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
protected virtual bool IsNaN(T value) { }
public FluentAssertions.AndConstraint<TAssertions> Match(System.Linq.Expressions.Expression<System.Func<T, bool>> predicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T? unexpected, string because = "", params object[] becauseArgs) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,7 @@ namespace FluentAssertions.Numeric
public FluentAssertions.AndConstraint<TAssertions> BeOneOf(System.Collections.Generic.IEnumerable<T> validValues, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BePositive(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
protected virtual bool IsNaN(T value) { }
public FluentAssertions.AndConstraint<TAssertions> Match(System.Linq.Expressions.Expression<System.Func<T, bool>> predicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T? unexpected, string because = "", params object[] becauseArgs) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,7 @@ namespace FluentAssertions.Numeric
public FluentAssertions.AndConstraint<TAssertions> BeOneOf(System.Collections.Generic.IEnumerable<T> validValues, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BePositive(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
protected virtual bool IsNaN(T value) { }
public FluentAssertions.AndConstraint<TAssertions> Match(System.Linq.Expressions.Expression<System.Func<T, bool>> predicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T? unexpected, string because = "", params object[] becauseArgs) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,7 @@ namespace FluentAssertions.Numeric
public FluentAssertions.AndConstraint<TAssertions> BeOneOf(System.Collections.Generic.IEnumerable<T> validValues, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BePositive(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
protected virtual bool IsNaN(T value) { }
public FluentAssertions.AndConstraint<TAssertions> Match(System.Linq.Expressions.Expression<System.Func<T, bool>> predicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T? unexpected, string because = "", params object[] becauseArgs) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,7 @@ namespace FluentAssertions.Numeric
public FluentAssertions.AndConstraint<TAssertions> BeOneOf(System.Collections.Generic.IEnumerable<T> validValues, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BePositive(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
protected virtual bool IsNaN(T value) { }
public FluentAssertions.AndConstraint<TAssertions> Match(System.Linq.Expressions.Expression<System.Func<T, bool>> predicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(T? unexpected, string because = "", params object[] becauseArgs) { }
Expand Down

0 comments on commit fb53da1

Please sign in to comment.