Skip to content

Commit

Permalink
Add ThrowIfArgumentIsNegative
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Oct 27, 2022
1 parent b36399a commit e9ca18f
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 66 deletions.
8 changes: 8 additions & 0 deletions Src/FluentAssertions/Common/Guard.cs
Expand Up @@ -71,6 +71,14 @@ public static void ThrowIfArgumentIsEmpty(string str, string paramName, string m
}
}

public static void ThrowIfArgumentIsNegative(TimeSpan timeSpan, string paramName, string message)
{
if (timeSpan < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(paramName, message);
}
}

/// <summary>
/// Workaround to make dotnet_code_quality.null_check_validation_methods work
/// https://github.com/dotnet/roslyn-analyzers/issues/3451#issuecomment-606690452
Expand Down
11 changes: 3 additions & 8 deletions Src/FluentAssertions/Primitives/DateTimeAssertions.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using FluentAssertions.Common;
using FluentAssertions.Execution;

namespace FluentAssertions.Primitives;
Expand Down Expand Up @@ -157,10 +158,7 @@ public AndConstraint<TAssertions> Be(DateTime? expected, string because = "", pa
public AndConstraint<TAssertions> BeCloseTo(DateTime nearbyTime, TimeSpan precision, string because = "",
params object[] becauseArgs)
{
if (precision < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(precision, nameof(precision), "The value must be non-negative.");

long distanceToMinInTicks = (nearbyTime - DateTime.MinValue).Ticks;
DateTime minimumValue = nearbyTime.AddTicks(-Math.Min(precision.Ticks, distanceToMinInTicks));
Expand Down Expand Up @@ -208,10 +206,7 @@ public AndConstraint<TAssertions> Be(DateTime? expected, string because = "", pa
public AndConstraint<TAssertions> NotBeCloseTo(DateTime distantTime, TimeSpan precision, string because = "",
params object[] becauseArgs)
{
if (precision < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(precision, nameof(precision), "The value must be non-negative.");

long distanceToMinInTicks = (distantTime - DateTime.MinValue).Ticks;
DateTime minimumValue = distantTime.AddTicks(-Math.Min(precision.Ticks, distanceToMinInTicks));
Expand Down
11 changes: 3 additions & 8 deletions Src/FluentAssertions/Primitives/DateTimeOffsetAssertions.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using FluentAssertions.Common;
using FluentAssertions.Execution;

namespace FluentAssertions.Primitives;
Expand Down Expand Up @@ -293,10 +294,7 @@ public DateTimeOffsetAssertions(DateTimeOffset? value)
string because = "",
params object[] becauseArgs)
{
if (precision < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(precision, nameof(precision), "The value must be non-negative.");

long distanceToMinInTicks = (nearbyTime - DateTimeOffset.MinValue).Ticks;
DateTimeOffset minimumValue = nearbyTime.AddTicks(-Math.Min(precision.Ticks, distanceToMinInTicks));
Expand Down Expand Up @@ -344,10 +342,7 @@ public DateTimeOffsetAssertions(DateTimeOffset? value)
public AndConstraint<TAssertions> NotBeCloseTo(DateTimeOffset distantTime, TimeSpan precision, string because = "",
params object[] becauseArgs)
{
if (precision < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(precision, nameof(precision), "The value must be non-negative.");

long distanceToMinInTicks = (distantTime - DateTimeOffset.MinValue).Ticks;
DateTimeOffset minimumValue = distantTime.AddTicks(-Math.Min(precision.Ticks, distanceToMinInTicks));
Expand Down
11 changes: 3 additions & 8 deletions Src/FluentAssertions/Primitives/SimpleTimeSpanAssertions.cs
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using FluentAssertions.Common;
using FluentAssertions.Execution;

namespace FluentAssertions.Primitives;
Expand Down Expand Up @@ -239,10 +240,7 @@ public AndConstraint<TAssertions> BeGreaterThan(TimeSpan expected, string becaus
public AndConstraint<TAssertions> BeCloseTo(TimeSpan nearbyTime, TimeSpan precision, string because = "",
params object[] becauseArgs)
{
if (precision < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(precision, nameof(precision), "The value must be non-negative.");

TimeSpan minimumValue = nearbyTime - precision;
TimeSpan maximumValue = nearbyTime + precision;
Expand Down Expand Up @@ -281,10 +279,7 @@ public AndConstraint<TAssertions> BeGreaterThan(TimeSpan expected, string becaus
public AndConstraint<TAssertions> NotBeCloseTo(TimeSpan distantTime, TimeSpan precision, string because = "",
params object[] becauseArgs)
{
if (precision < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(precision, nameof(precision), "The value must be non-negative.");

TimeSpan minimumValue = distantTime - precision;
TimeSpan maximumValue = distantTime + precision;
Expand Down
1 change: 1 addition & 0 deletions Src/FluentAssertions/Primitives/StringAssertions.cs
Expand Up @@ -1084,6 +1084,7 @@ public AndConstraint<TAssertions> ContainAny(params string[] values)
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="unexpected"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="unexpected"/> is empty.</exception>
public AndConstraint<TAssertions> NotContain(string unexpected, string because = "",
params object[] becauseArgs)
{
Expand Down
12 changes: 2 additions & 10 deletions Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs
Expand Up @@ -251,16 +251,8 @@ public async Task<AndConstraint<TAssertions>> NotThrowAsync<TException>(string b
/// <exception cref="ArgumentOutOfRangeException">Throws if waitTime or pollInterval are negative.</exception>
public Task<AndConstraint<TAssertions>> NotThrowAfterAsync(TimeSpan waitTime, TimeSpan pollInterval, string because = "", params object[] becauseArgs)
{
if (waitTime < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative.");
}

if (pollInterval < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(pollInterval),
$"The value of {nameof(pollInterval)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(waitTime, nameof(waitTime), "The value must be non-negative.");
Guard.ThrowIfArgumentIsNegative(pollInterval, nameof(pollInterval), "The value must be non-negative.");

Execute.Assertion
.ForCondition(Subject is not null)
Expand Down
11 changes: 2 additions & 9 deletions Src/FluentAssertions/Specialized/DelegateAssertions.cs
Expand Up @@ -164,15 +164,8 @@ public AndConstraint<TAssertions> NotThrowAfter(TimeSpan waitTime, TimeSpan poll
.FailWith("Expected {context} not to throw after {0}{reason}, but found <null>.", waitTime);

FailIfSubjectIsAsyncVoid();
if (waitTime < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative.");
}

if (pollInterval < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(pollInterval), $"The value of {nameof(pollInterval)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(waitTime, nameof(waitTime), "The value must be non-negative.");
Guard.ThrowIfArgumentIsNegative(pollInterval, nameof(pollInterval), "The value must be non-negative.");

TimeSpan? invocationEndTime = null;
Exception exception = null;
Expand Down
5 changes: 1 addition & 4 deletions Src/FluentAssertions/Specialized/ExecutionTimeAssertions.cs
Expand Up @@ -201,10 +201,7 @@ public AndConstraint<ExecutionTimeAssertions> BeGreaterThan(TimeSpan minDuration
/// </param>
public AndConstraint<ExecutionTimeAssertions> BeCloseTo(TimeSpan expectedDuration, TimeSpan precision, string because = "", params object[] becauseArgs)
{
if (precision < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(precision, nameof(precision), "The value must be non-negative.");

TimeSpan minimumValue = expectedDuration - precision;
TimeSpan maximumValue = expectedDuration + precision;
Expand Down
11 changes: 2 additions & 9 deletions Src/FluentAssertions/Specialized/FunctionAssertionHelpers.cs
Expand Up @@ -25,15 +25,8 @@ internal static T NotThrow<T>(Func<T> subject, string because, object[] becauseA

internal static TResult NotThrowAfter<TResult>(Func<TResult> subject, IClock clock, TimeSpan waitTime, TimeSpan pollInterval, string because, object[] becauseArgs)
{
if (waitTime < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative.");
}

if (pollInterval < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(pollInterval), $"The value of {nameof(pollInterval)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(waitTime, nameof(waitTime), "The value must be non-negative.");
Guard.ThrowIfArgumentIsNegative(pollInterval, nameof(pollInterval), "The value must be non-negative.");

TimeSpan? invocationEndTime = null;
Exception exception = null;
Expand Down
12 changes: 2 additions & 10 deletions Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs
Expand Up @@ -122,16 +122,8 @@ public GenericAsyncFunctionAssertions(Func<Task<TResult>> subject, IExtractExcep
public new Task<AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, TResult>> NotThrowAfterAsync(
TimeSpan waitTime, TimeSpan pollInterval, string because = "", params object[] becauseArgs)
{
if (waitTime < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative.");
}

if (pollInterval < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(pollInterval),
$"The value of {nameof(pollInterval)} must be non-negative.");
}
Guard.ThrowIfArgumentIsNegative(waitTime, nameof(waitTime), "The value must be non-negative.");
Guard.ThrowIfArgumentIsNegative(pollInterval, nameof(pollInterval), "The value must be non-negative.");

Execute.Assertion
.ForCondition(Subject is not null)
Expand Down

0 comments on commit e9ca18f

Please sign in to comment.