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 5b6eef6 commit fb9709c
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 81 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
Expand Up @@ -1149,7 +1149,8 @@ public async Task When_wait_time_is_negative_for_async_func_it_should_throw()

// Assert
await act.Should().ThrowAsync<ArgumentOutOfRangeException>()
.WithMessage("* value of waitTime must be non-negative*");
.WithParameterName("waitTime")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand All @@ -1167,7 +1168,8 @@ public async Task When_poll_interval_is_negative_for_async_func_it_should_throw(

// Assert
await act.Should().ThrowAsync<ArgumentOutOfRangeException>()
.WithMessage("* value of pollInterval must be non-negative*");
.WithParameterName("pollInterval")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down
Expand Up @@ -468,7 +468,8 @@ public void When_wait_time_is_negative_it_should_throw()

// Assert
action.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of waitTime must be non-negative*");
.WithParameterName("waitTime")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand All @@ -485,7 +486,8 @@ public void When_poll_interval_is_negative_it_should_throw()

// Assert
action.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of pollInterval must be non-negative*");
.WithParameterName("pollInterval")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down
6 changes: 4 additions & 2 deletions Tests/FluentAssertions.Specs/Exceptions/NotThrowSpecs.cs
Expand Up @@ -136,7 +136,8 @@ public void When_wait_time_is_negative_it_should_throw()

// Assert
action.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of waitTime must be non-negative*");
.WithParameterName("waitTime")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand All @@ -153,7 +154,8 @@ public void When_poll_interval_is_negative_it_should_throw()

// Assert
action.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of pollInterval must be non-negative*");
.WithParameterName("pollInterval")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down
Expand Up @@ -383,7 +383,8 @@ public void When_asserting_that_time_is_close_to_a_negative_precision_it_should_

// Assert
act.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of precision must be non-negative*");
.WithParameterName("precision")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down Expand Up @@ -590,7 +591,8 @@ public void When_asserting_that_time_is_not_close_to_a_negative_precision_it_sho

// Assert
act.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of precision must be non-negative*");
.WithParameterName("precision")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down
Expand Up @@ -561,7 +561,8 @@ public void When_asserting_that_time_is_close_to_a_negative_precision_it_should_

// Assert
act.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of precision must be non-negative*");
.WithParameterName("precision")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down Expand Up @@ -768,7 +769,8 @@ public void When_asserting_that_time_is_not_close_to_a_negative_precision_it_sho

// Assert
act.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of precision must be non-negative*");
.WithParameterName("precision")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down
Expand Up @@ -542,7 +542,8 @@ public void When_asserting_that_time_is_close_to_a_negative_precision_it_should_

// Assert
act.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of precision must be non-negative*");
.WithParameterName("precision")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down Expand Up @@ -666,7 +667,8 @@ public void When_asserting_that_time_is_not_close_to_a_negative_precision_it_sho

// Assert
act.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of precision must be non-negative*");
.WithParameterName("precision")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down
Expand Up @@ -414,7 +414,8 @@ public void When_asserting_that_execution_time_is_close_to_a_negative_precision_

// Assert
act.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("* value of precision must be non-negative*");
.WithParameterName("precision")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down
Expand Up @@ -308,7 +308,8 @@ public async Task When_wait_time_is_negative_it_should_fail()

// Assert
await act.Should().ThrowAsync<ArgumentOutOfRangeException>()
.WithMessage("* value of waitTime must be non-negative*");
.WithParameterName("waitTime")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand All @@ -326,7 +327,8 @@ public async Task When_poll_interval_is_negative_it_should_fail()

// Assert
await act.Should().ThrowAsync<ArgumentOutOfRangeException>()
.WithMessage("* value of pollInterval must be non-negative*");
.WithParameterName("pollInterval")
.WithMessage("*must be non-negative*");
}

[Fact]
Expand Down

0 comments on commit fb9709c

Please sign in to comment.