Skip to content

Instantly share code, notes, and snippets.

View polac24's full-sized avatar

Bartosz Polaczyk polac24

  • Apple
  • Cupertino
View GitHub Profile
// Custom RawRepresentable type for BestEnum
struct BestEnumRaw: ExpressibleByStringLiteral{
let rawString:String?
init(){
rawString = nil
}
init(stringLiteral value: String){
rawString = value
func testDownloaderCallsCompletionOnResponse(){
// Arange
let url = URL(string: "https://address.com")!
let completionExpectation = expectation(description: "completionExpectation")
// Act
let downloader = Downloader(url:url, completionHandler:{
completionExpectation.fulfill()
})
networkLayer.responseWithJSON(json:{})
class ReferenceObserver{
private let deinitHandler:()->()
init(deinitHandler:@escaping ()->()) {
self.deinitHandler = deinitHandler
}
deinit {
deinitHandler()
}
}
// Arange
let url = URL(string: "https://address.com")!
let deinitExpectation = expectation(description: "deinit completionHandler")
var reference:ReferenceObserver? = ReferenceObserver(deinitHandler: deinitExpectation.fulfill)
// Act
let downloader = Downloader(url:url, completionHandler:{[capturedReference = reference] in
_ = capturedReference
XCTFail()
})
func testDownloaderDoesNotCallCompletionOnCancelledDownload(){
// Arange
let url = URL(string: "https://address.com")!
let deinitExpectation = expectation(description: "deinit completionHandler")
var reference:ReferenceObserver? = ReferenceObserver(deinitHandler: deinitExpectation.fulfill)
let downloader = Downloader(url:url, completionHandler:{[capturedReference = reference] in
_ = capturedReference
XCTFail()
})
reference = nil
class ReferenceObserver{
private let deinitHandler:()->()
private var allowedToDealloc = false
init(deinitHandler:@escaping ()->()) {
self.deinitHandler = deinitHandler
}
deinit {
guard allowedToDealloc else {
XCTFail("ReferenceObserver deallocated too early!")
func testDownloaderDoesNotCallCompletionOnCancelledDownload(){
// Arange
let url = URL(string: "https://address.com")!
let deinitExpectation = expectation(description: "deinit completionHandler")
let downloader:Downloader
do {
let reference = ReferenceObserver(deinitHandler: deinitExpectation.fulfill)
downloader = Downloader(url:url, completionHandler:{[capturedReference = reference] in
_ = capturedReference
XCTFail()
let error = // some error received in a completion handler
switch (error) {
case .invalidValue:
// highlight textfield that user has to modify
case .noInternet:
// present an alert
case .sessionInvalidated:
// pass error to the parent layer (e.g. by delegate)
delegate.didObserveInvalidatedSession()
}
public typealias HandleAction<T> = (T) throws -> ()
public protocol ErrorHandleable: class{
func `throw`(_ :Error, finally: @escaping (Bool)->Void)
func `catch`(action: @escaping HandleAction<Error>) -> ErrorHandleable
}
let previousErrorHandler:ErrorHandleable = /// existing handler
// setup a handler
let errorHandler = previousErrorHandler.catch {error in
// Log all errors into console and pass it further
print(error)
throw error
}.catch {
// Stop propagation for all errors(no throwing)
}
...