Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/CatchingFire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import XCTest

#if !swift(>=3.0)
typealias ErrorProtocol = ErrorType
#endif


/// This allows you to write safe tests for the happy path of failable functions.
/// It helps you to avoid the `try!` operator in tests.
Expand Down Expand Up @@ -77,11 +81,11 @@ public func AssertNoThrow(@noescape closure: () throws -> ()) {
///
/// If the expression or closure doesn't throw the expected error, your test fails.
///
public func AssertThrows<R, E where E: ErrorType>(expectedError: E, @autoclosure _ closure: () throws -> R) -> () {
public func AssertThrows<R, E where E: ErrorProtocol>(expectedError: E, @autoclosure(escaping) _ closure: () throws -> R) -> () {
AssertThrows(expectedError) { try closure() }
}

public func AssertThrows<E where E: ErrorType>(expectedError: E, @noescape _ closure: () throws -> ()) -> () {
public func AssertThrows<E where E: ErrorProtocol>(expectedError: E, @noescape _ closure: () throws -> ()) -> () {
do {
try closure()
XCTFail("Expected to catch <\(expectedError)>, "
Expand All @@ -95,11 +99,11 @@ public func AssertThrows<E where E: ErrorType>(expectedError: E, @noescape _ clo
}
}

public func AssertThrows<R, E where E: ErrorType, E: Equatable>(expectedError: E, @autoclosure _ closure: () throws -> R) -> () {
public func AssertThrows<R, E where E: ErrorProtocol, E: Equatable>(expectedError: E, @autoclosure(escaping) _ closure: () throws -> R) -> () {
AssertThrows(expectedError) { try closure() }
}

public func AssertThrows<E where E: ErrorType, E: Equatable>(expectedError: E, @noescape _ closure: () throws -> ()) -> () {
public func AssertThrows<E where E: ErrorProtocol, E: Equatable>(expectedError: E, @noescape _ closure: () throws -> ()) -> () {
do {
try closure()
XCTFail("Expected to catch <\(expectedError)>, "
Expand All @@ -115,23 +119,23 @@ public func AssertThrows<E where E: ErrorType, E: Equatable>(expectedError: E, @
}
}

/// Implement pattern matching for ErrorTypes
internal func ~=(lhs: ErrorType, rhs: ErrorType) -> Bool {
/// Implement pattern matching for ErrorProtocol
internal func ~=(lhs: ErrorProtocol, rhs: ErrorProtocol) -> Bool {
return lhs._domain == rhs._domain
&& rhs._code == rhs._code
}

/// Helper struct to catch errors thrown by others, which aren't publicly exposed.
///
/// Note:
/// Don't use this when a given ErrorType implementation exists.
/// Don't use this when a given ErrorProtocol implementation exists.
/// If you want to use that to test errors thrown in your own framework, you should
/// consider to adopt an enumeration-based approach first in the framework code itself and expose
/// them instead as part of the public API. Even if you have some internal error cases, you can
//// put them in a separate enum and import your framework as `@testable` in your tests without
/// affecting the public API, if that matters.
///
public struct Error : ErrorType {
public struct Error : ErrorProtocol {
public let domain: String
public let code: Int

Expand All @@ -152,8 +156,8 @@ public func ==(lhs: Error, rhs: Error) -> Bool {
&& lhs._code == rhs._code
}

/// Implement pattern matching for Error & ErrorType
public func ~=(lhs: Error, rhs: ErrorType) -> Bool {
/// Implement pattern matching for Error & ErrorProtocol
public func ~=(lhs: Error, rhs: ErrorProtocol) -> Bool {
return lhs._domain == rhs._domain
&& rhs._code == rhs._code
}
2 changes: 1 addition & 1 deletion test/CatchingFireTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import XCTest
import CatchingFire

enum Error : ErrorType {
enum Error : ErrorProtocol {
case MayNotBeZero
}

Expand Down
2 changes: 1 addition & 1 deletion test/ExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import XCTest
import CatchingFire


enum BombError: ErrorType {
enum BombError: ErrorProtocol {
case WrongWire
case TimeElapsed
}
Expand Down