Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning CA1062#IAsyncPolicyPolicyWrapExtensions #2231

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)),
martincostello marked this conversation as resolved.
Show resolved Hide resolved
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]),
martincostello marked this conversation as resolved.
Show resolved Hide resolved
_ => 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