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#IPolicyWrapExtension #2229

Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 23 additions & 14 deletions src/Polly/Wrap/IPolicyWrapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// <summary>
/// Extension methods for IPolicyWrap.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public static class IPolicyWrapExtension
{
/// <summary>
Expand All @@ -13,21 +12,12 @@ public static class IPolicyWrapExtension
/// <returns>An <see cref="IEnumerable{IsPolicy}"/> of all the policies in the wrap.</returns>
public static IEnumerable<IsPolicy> GetPolicies(this IPolicyWrap policyWrap)
{
var childPolicies = new[] { policyWrap.Outer, policyWrap.Inner };
foreach (var childPolicy in childPolicies)
if (policyWrap is null)
{
if (childPolicy is IPolicyWrap anotherWrap)
{
foreach (var policy in anotherWrap.GetPolicies())
{
yield return policy;
}
}
else if (childPolicy != null)
{
yield return childPolicy;
}
throw new ArgumentNullException(nameof(policyWrap));
}

return GetPoliciesIterator(policyWrap);
}

/// <summary>
Expand Down Expand Up @@ -83,4 +73,23 @@ public static TPolicy GetPolicy<TPolicy>(this IPolicyWrap policyWrap, Func<TPoli

return policyWrap.GetPolicies().OfType<TPolicy>().SingleOrDefault(filter);
}

private static IEnumerable<IsPolicy> GetPoliciesIterator(IPolicyWrap policyWrap)
{
var childPolicies = new[] { policyWrap.Outer, policyWrap.Inner };
foreach (var childPolicy in childPolicies)
{
if (childPolicy is IPolicyWrap anotherWrap)
{
foreach (var policy in anotherWrap.GetPolicies())
{
yield return policy;
}
}
else if (childPolicy != null)
{
yield return childPolicy;
}
}
}
}
9 changes: 9 additions & 0 deletions test/Polly.Specs/Wrap/IPolicyWrapExtensionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

public class IPolicyWrapExtensionSpecs
{
[Fact]
public void Should_throw_when_policy_wrap_is_null()
{
IPolicyWrap policyWrap = null!;

var action = () => policyWrap.GetPolicies();
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("policyWrap");
}

[Fact]
public void Should_pass_all_nested_policies_from_PolicyWrap_in_same_order_they_were_added()
{
Expand Down