Allow OneOfPolicies and AllOfPolicies to take throwing policy builders, to match Verifier (#173)

* Allow OneOfPolicies and AllOfPolicies to take throwing policy builders, to match Verifier

* add tests

* avoid api break

* soundness

* fix

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
hamzahrmalikandCory Benfield authored and GitHub committed 2024-05-17 10:04:40 -07:00
1 parent 6f2dc4affc
commit d93756ba92
3 files changed
+58

No files matched your search

@@ -36,6 +36,11 @@ public struct AllOfPolicies<Policy: VerifierPolicy>: VerifierPolicy {
@usableFromInline
var policy: Policy
@inlinable
public init(@PolicyBuilder policy: () throws -> Policy) throws {
self.policy = try policy()
}
@inlinable
public init(@PolicyBuilder policy: () -> Policy) {
self.policy = policy()
@@ -176,6 +176,11 @@ public struct OneOfPolicies<Policy: VerifierPolicy>: VerifierPolicy {
@usableFromInline
var policy: Policy
@inlinable
public init(@OneOfPolicyBuilder policy: () throws -> Policy) throws {
self.policy = try policy()
}
@inlinable
public init(@OneOfPolicyBuilder policy: () -> Policy) {
self.policy = policy()
+48
View File
@@ -576,4 +576,52 @@ final class PolicyBuilderTests: XCTestCase {
}
}
}
func testAllOfPoliciesThrowing() {
// Creating a AllOfPolicies which throws an error inside will itself throw
struct TestError: Error {}
func throwingPolicyBuilder() throws -> Policy {
throw TestError()
}
XCTAssertThrowsError(
try AllOfPolicies {
try throwingPolicyBuilder()
}
) { error in
XCTAssertTrue(error is TestError)
}
}
func testAllOfPoliciesNonThrowing() {
// creating a AllOfPolicies with a non-throwing closure can't throw
// This is tested at compile time (lack of `try` keyword)
_ = AllOfPolicies {
Policy(result: .meetsPolicy)
}
}
func testOneOfPoliciesThrowing() {
// Creating a OneOfPolicies which throws an error inside will itself throw
struct TestError: Error {}
func throwingPolicyBuilder() throws -> Policy {
throw TestError()
}
XCTAssertThrowsError(
try OneOfPolicies {
try throwingPolicyBuilder()
}
) { error in
XCTAssertTrue(error is TestError)
}
}
func testOneOfPoliciesNonThrowing() {
// creating a OneOfPolicies with a non-throwing closure can't throw
// This is tested at compile time (lack of `try` keyword)
_ = OneOfPolicies {
Policy(result: .meetsPolicy)
}
}
}