Skip to content

Commit

Permalink
Fix CA1062 warnings (#2230)
Browse files Browse the repository at this point in the history
Fix CA1062 warnings for `ISyncPolicyPolicyWrapExtensions`.
  • Loading branch information
Zombach authored Jul 23, 2024
1 parent b703130 commit f054ef7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/Polly/Wrap/ISyncPolicyPolicyWrapExtensions.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="ISyncPolicy"/> or <see cref="ISyncPolicy{TResult}"/>.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public static class ISyncPolicyPolicyWrapExtensions
{
/// <summary>
Expand Down Expand Up @@ -157,13 +156,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 PolicyWrap Wrap(params ISyncPolicy[] policies) =>
policies.Length switch
public static PolicyWrap Wrap(params ISyncPolicy[] 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 PolicyWrap((Policy)policies[0], policies[1]),
_ => Wrap(policies[0], Wrap(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 @@ -172,11 +179,19 @@ public static PolicyWrap Wrap(params ISyncPolicy[] 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 PolicyWrap<TResult> Wrap<TResult>(params ISyncPolicy<TResult>[] policies) =>
policies.Length switch
public static PolicyWrap<TResult> Wrap<TResult>(params ISyncPolicy<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)),
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 PolicyWrap<TResult>((Policy<TResult>)policies[0], policies[1]),
_ => Wrap(policies[0], Wrap(policies.Skip(1).ToArray())),
};
}
}
13 changes: 13 additions & 0 deletions test/Polly.Specs/Caching/CacheSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ 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()
{
ISyncPolicy[] policies = null!;
ISyncPolicy<int>[] policiesGeneric = null!;

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

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

#endregion

#region Caching behaviours
Expand Down

0 comments on commit f054ef7

Please sign in to comment.