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
struct Post { | |
class TextFormatter { | |
enum Option { | |
case highlightNames | |
case highlightLinks | |
} | |
private let options: Set<Option> | |
init(options: Set<Option>) { |
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
struct Post { | |
class TextFormatter { | |
enum Option { | |
case highlightNames | |
case highlightLinks | |
} | |
private let options: Set<Option> | |
init(options: Set<Option>) { |
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
struct Post { | |
let id: Int | |
let author: User | |
let title: String | |
let text: String | |
} | |
class PostTextFormatter { | |
private let options: Set<Option> |
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
extension Command { | |
enum Error: Swift.Error { | |
case missing | |
case invalid(String) | |
} | |
} |
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 testLoadingData() { | |
class NetworkEngineMock: NetworkEngine { | |
typealias Handler = NetworkEngine.Handler | |
var requestedURL: URL? | |
func performRequest(for url: URL, completionHandler: @escaping Handler) { | |
requestedURL = url | |
let data = “Hello world”.data(using: .utf8) |
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 DataLoader { | |
enum Result { | |
case data(Data) | |
case error(Error) | |
} | |
private let engine: NetworkEngine | |
init(engine: NetworkEngine = URLSession.shared) { | |
self.engine = engine |
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 NetworkEngine { | |
typealias Handler = (Data?, URLResponse?, Error?) -> Void | |
func performRequest(for url: URL, completionHandler: @escaping Handler) | |
} | |
extension URLSession: NetworkEngine { | |
typealias Handler = NetworkEngine.Handler | |
func performRequest(for url: URL, completionHandler: @escaping Handler) { |
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 DataLoader { | |
enum Result { | |
case data(Data) | |
case error(Error) | |
} | |
func load(from url: URL, completionHandler: @escaping (Result) -> Void) { | |
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in | |
if let error = error { | |
return completionHandler(.error(error)) |
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 CardGameTests: XCTestCase { | |
func testDrawingRandomCard() { | |
var randomizationUpperBound: UInt32? | |
let deck = Deck(cards: [Card(value: .ace, suite: .spades)]) | |
let game = CardGame(deck: deck, randomizer: { upperBound in | |
// Capture the upper bound to be able to assert it later | |
randomizationUpperBound = upperBound | |
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 CardGame { | |
typealias Randomizer = (UInt32) -> UInt32 | |
private let deck: Deck | |
private let randomizer: Randomizer | |
init(deck: Deck, randomizer: @escaping Randomizer = arc4random_uniform) { | |
self.deck = deck | |
self.randomizer = randomizer | |
} |