This file contains hidden or 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 DefaultProvidable { | |
static var defaultValue: Self {get} | |
} | |
extension Bool: DefaultProvidable { | |
static let defaultValue = false | |
} | |
extension Optional: DefaultProvidable { | |
static var defaultValue: Optional<Wrapped> {return nil} | |
} | |
... |
This file contains hidden or 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: Database { | |
lazy var addUserAction = niceStub(of: addUser, thatReturns: true) | |
func addUser(name: String) -> Bool { | |
return addUserAction(name) | |
} | |
} | |
func testAddingUser_savesToDatabase () { | |
// Act | |
database.addUser(name: "user1") // ✅, doesn't crash | |
... |
This file contains hidden or 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 niceStub<I,O>(of: (I) -> (O), thatReturns: O) -> (I) -> (O){ | |
return { _ in thatReturns } | |
} |
This file contains hidden or 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
// compareAction: ((Int,Int)) -> CompareResult | |
lazy var compareAction = stub(of: compare) | |
func compare(_ element1:Int, _ element2:Int) -> CompareResult { | |
return compareAction((element1, element2)) | |
} |
This file contains hidden or 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<I,O>(of: (I) -> (O)) -> (I) -> (O){ | |
return nil! | |
} |
This file contains hidden or 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 testAddingUser_savesToDatabase () { | |
// Arrange | |
var addedUser: String? | |
database.addUserAction = { name in | |
addedUser = name | |
return false | |
} | |
// Act | |
... | |
// Assert |
This file contains hidden or 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
// helper global function | |
func stub<T>(of v: T) -> T { | |
return nil! | |
} | |
protocol Database { | |
func addUser(name: String) -> Bool | |
} | |
class DatabaseStub: Database { | |
lazy var addUserAction = stub(of: addUser) // (String) -> Bool | |
func addUser(name: String) -> Bool { |
This file contains hidden or 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
## Counting code coverage on a subset of files | |
# "Spies" is a regex in the example | |
xcrun xccov view --files-for-target StubKit.framework action.xccovreport | awk '/Spies/{print $5}' | awk '-F[/()]' '{covered += $2; all += $3} END {print covered/all*100}' |
This file contains hidden or 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
let RFC3339DateFormatter = with(DateFormatter()) { | |
$0.locale = Locale(identifier: "en_US_POSIX") | |
$0.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" | |
$0.timeZone = TimeZone(secondsFromGMT: 0) | |
} |
This file contains hidden or 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 testCallbackIsNotCalledAfterCancel() { | |
// Arrange | |
let sut = MasterClass() | |
// Act | |
let tokenMarker = weaklyScoped(NSObject()) { marker in | |
sut.start { | |
// keep a strong reference to a marker | |
_ = marker | |
} | |
// cancel should clear start's completion |