Skip to content

Commit

Permalink
Merge pull request #18 from Quick/default_inits_for_error_in_spy
Browse files Browse the repository at this point in the history
Provide default initializers for Spy when throwing any Swift.Error
  • Loading branch information
younata authored May 26, 2024
2 parents 2d66fff + 6a4f596 commit 380cce3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Sources/Fakes/EmptyError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// An error that can be used as a default error in tests
public struct EmptyError: Error, Sendable {
public init() {}
}
5 changes: 1 addition & 4 deletions Sources/Fakes/Pendable/Pendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ extension Pendable {

/// Creatse a new pending `Pendable` with a fallback value of an error.
public static func pending<Success>() -> Pendable<Value> where Value == Result<Success, Error> {
Pendable(fallbackValue: Result<Success, Error>.failure(PendableDefaultError()))
Pendable(fallbackValue: Result<Success, Error>.failure(EmptyError()))
}
}

/// An error that can be used as a default error for ``Pendable`` when returning a `Result<..., Error>`.
public struct PendableDefaultError: Error, Sendable {}
4 changes: 4 additions & 0 deletions Sources/Fakes/Spy/Spy+Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ extension Spy {
public convenience init<Success, Failure: Error>(failure: Failure) where Returning == Result<Success, Failure> {
self.init(.failure(failure))
}

public convenience init<Success>() where Returning == Result<Success, Error> {
self.init(.failure(EmptyError()))
}
}

extension Spy {
Expand Down
9 changes: 7 additions & 2 deletions Sources/Fakes/Spy/Spy+ThrowingPendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ extension Spy {
self.init(.pending(fallback: .failure(pendingFailure)))
}

/// Create a throwing pendable Spy that is pre-stubbed to return a pending that will block for a bit before throwing an error
public convenience init<Success: Sendable>() where Returning == ThrowingPendable<Success, Error> {
self.init(.pending(fallback: .failure(EmptyError())))
}

/// Create a throwing pendable Spy that is pre-stubbed to return a finished & successful value.
public convenience init<Success: Sendable, Failure: Error>(success: Success) where Returning == ThrowingPendable<Success, Failure> {
self.init(.finished(.success(success)))
Expand Down Expand Up @@ -54,7 +59,7 @@ extension Spy {

/// Update the pendable Spy's stub to be in a pending state with a default failure value.
public func stubPendingFailure<Success: Sendable>() where Returning == ThrowingPendable<Success, Error> {
self.stub(pendingFailure: PendableDefaultError())
self.stub(pendingFailure: EmptyError())
}

/// Update the throwing pendable Spy's stub to be successful, with the given value.
Expand All @@ -73,7 +78,7 @@ extension Spy {

/// Update the throwing pendable Spy's stub to throw an error.
public func stubFailure<Success: Sendable>() where Returning == ThrowingPendable<Success, Error> {
self.stub(failure: PendableDefaultError())
self.stub(failure: EmptyError())
}
}

Expand Down

0 comments on commit 380cce3

Please sign in to comment.