Skip to content

Commit

Permalink
Merge pull request #58 from ddddxxx/rethrows
Browse files Browse the repository at this point in the history
Make blocks throwable
  • Loading branch information
devxoul authored Nov 14, 2017
2 parents f12a47f + 9016109 commit 651cb5b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Sources/Then/Then.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ extension Then where Self: Any {
/// $0.origin.x = 10
/// $0.size.width = 100
/// }
public func with(_ block: (inout Self) -> Void) -> Self {
public func with(_ block: (inout Self) throws -> Void) rethrows -> Self {
var copy = self
block(&copy)
try block(&copy)
return copy
}

Expand All @@ -49,8 +49,8 @@ extension Then where Self: Any {
/// $0.set("devxoul@gmail.com", forKey: "email")
/// $0.synchronize()
/// }
public func `do`(_ block: (Self) -> Void) {
block(self)
public func `do`(_ block: (Self) throws -> Void) rethrows {
try block(self)
}

}
Expand All @@ -64,8 +64,8 @@ extension Then where Self: AnyObject {
/// $0.textColor = UIColor.blackColor()
/// $0.text = "Hello, World!"
/// }
public func then(_ block: (Self) -> Void) -> Self {
block(self)
public func then(_ block: (Self) throws -> Void) rethrows -> Self {
try block(self)
return self
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/ThenTests/ThenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ class ThenTests: XCTestCase {
XCTAssertEqual(UserDefaults.standard.string(forKey: "username"), "devxoul")
}

func testRethrows() {
XCTAssertThrowsError(
try NSObject().do { _ in
throw NSError(domain: "", code: 0)
}
)
}

}

0 comments on commit 651cb5b

Please sign in to comment.