This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testDelegateKeepsWeakReference() { | |
// Arrange | |
let sut = MasterClass() | |
// Act | |
let listener = weaklyScoped(MasterListener()) { | |
sut.add($0) | |
} | |
// Assert | |
XCTAssertNil(listener) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func weaklyScoped<T: AnyObject>(_ v: @autoclosure () -> T, action: (T) throws -> ()) rethrows -> T? { | |
weak var weakValue: T? | |
do { // optionally autoreleasepool | |
let value = v() | |
try action(value) | |
weakValue = value | |
} | |
return weakValue | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func weaklyScopedNaive<T: AnyObject>(_ v: T, action: (inout T) -> ()) -> T? { | |
weak var weakValue: T? | |
do { | |
action(&v) | |
weakValue = v | |
} | |
return weakValue | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func with<T>(x: T, _ f: (inout T) -> Void) -> T { | |
var value = x | |
f(&value) | |
return value | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testListenerKeptWeakReferenceUsingTeardown() { | |
// Arrange | |
let sut = MasterClass() | |
let listener = MasterListener() | |
// Act | |
sut.add(listener) | |
// Assert | |
addTeardownBlock { [weak listener] in | |
XCTAssertNil(listener) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testListenerKeptWithWeakReference() { | |
// Arrange | |
let sut = MasterClass() | |
var listener: MasterClassListener? = MasterListener() | |
weak var weakListener = listener | |
// Act | |
sut.add(listener!) | |
listener = nil | |
// Assert | |
XCTAssertNil(weakListener) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testListenerIsKeeptWithWeakReference() { | |
// Arrange | |
let sut = MasterClass() | |
weak var weakListener: MasterClassListener? | |
do { | |
let listener = MasterListener() | |
weakListener = listener | |
// Act | |
sut.add(listener) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func stub<T>(of v: T) -> T { | |
return nil! | |
} | |
class DatabaseStub: Database { | |
lazy var addUserAction = stub(of: addUser) | |
func addUser(name: String) -> Bool { | |
return addUserAction(name) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Loader{ | |
func load(_ url: @autoclosure ()->(URL), completion: @escaping (URLResponse)->()) throws -> CancelToken | |
} | |
// Before | |
class LoaderStub: Loader { | |
var loadAction: ((@autoclosure () -> (URL), (URLResponse) -> ()) throws -> CancelToken)? | |
func load(_ url: @autoclosure...){...} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DatabaseStub { | |
lazy var addUserAction = asNil(addUser) | |
func addUser(name: String) -> Bool { | |
return addUserAction(name) ?? false | |
} | |
} |