Skip to content

Commit

Permalink
Fix warning CA1062#IAsyncPolicyPolicyWrapExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zombach committed Jul 22, 2024
1 parent c07cc48 commit dac53a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/Polly/Wrap/IAsyncPolicyPolicyWrapExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// <summary>
/// Defines extensions for configuring <see cref="PolicyWrap"/> instances on an <see cref="IAsyncPolicy"/> or <see cref="IAsyncPolicy{TResult}"/>.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public static class IAsyncPolicyPolicyWrapExtensions
{
/// <summary>
Expand Down Expand Up @@ -159,13 +158,21 @@ public partial class Policy
/// <param name="policies">The policies to place in the wrap, outermost (at left) to innermost (at right).</param>
/// <returns>The PolicyWrap.</returns>
/// <exception cref="ArgumentException">The enumerable of policies to form the wrap must contain at least two policies.</exception>
public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) =>
policies.Length switch
public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies)
{
if (policies is null)
{
< MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)),
throw new ArgumentNullException(nameof(policies));
}

return policies.Length switch
{
< MinimumPoliciesRequiredForWrap => throw new ArgumentException(
"The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)),
MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]),
_ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())),
};
}

/// <summary>
/// Creates a <see cref="PolicyWrap" /> of the given policies governing delegates returning values of type <typeparamref name="TResult" />.
Expand All @@ -174,11 +181,20 @@ public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) =>
/// <typeparam name="TResult">The return type of delegates which may be executed through the policy.</typeparam>
/// <returns>The PolicyWrap.</returns>
/// <exception cref="ArgumentException">The enumerable of policies to form the wrap must contain at least two policies.</exception>
public static AsyncPolicyWrap<TResult> WrapAsync<TResult>(params IAsyncPolicy<TResult>[] policies) =>
policies.Length switch
public static AsyncPolicyWrap<TResult> WrapAsync<TResult>(params IAsyncPolicy<TResult>[] policies)
{
if (policies is null)
{
< MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)),
MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap<TResult>((AsyncPolicy<TResult>)policies[0], policies[1]),
throw new ArgumentNullException(nameof(policies));
}

return policies.Length switch
{
< MinimumPoliciesRequiredForWrap => throw new ArgumentException(
"The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)),
MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap<TResult>((AsyncPolicy<TResult>)policies[0],
policies[1]),
_ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())),
};
}
}
12 changes: 12 additions & 0 deletions test/Polly.Specs/Caching/CacheAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public void Should_throw_when_cache_key_strategy_is_null()
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheKeyStrategy");
}

[Fact]
public void Should_throw_when_policies_is_null()
{
IAsyncPolicy[] policies = null!;
IAsyncPolicy<int>[] policiesGeneric = null!;

Action action = () => Policy.WrapAsync(policies);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("policies");

action = () => Policy.WrapAsync<int>(policiesGeneric);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("policies");
}
#endregion

#region Caching behaviours
Expand Down

0 comments on commit dac53a0

Please sign in to comment.