Skip to content

Commit 380cce3

Browse files
authored
Merge pull request #18 from Quick/default_inits_for_error_in_spy
Provide default initializers for Spy when throwing any Swift.Error
2 parents 2d66fff + 6a4f596 commit 380cce3

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

Sources/Fakes/EmptyError.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// An error that can be used as a default error in tests
2+
public struct EmptyError: Error, Sendable {
3+
public init() {}
4+
}

Sources/Fakes/Pendable/Pendable.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ extension Pendable {
184184

185185
/// Creatse a new pending `Pendable` with a fallback value of an error.
186186
public static func pending<Success>() -> Pendable<Value> where Value == Result<Success, Error> {
187-
Pendable(fallbackValue: Result<Success, Error>.failure(PendableDefaultError()))
187+
Pendable(fallbackValue: Result<Success, Error>.failure(EmptyError()))
188188
}
189189
}
190-
191-
/// An error that can be used as a default error for ``Pendable`` when returning a `Result<..., Error>`.
192-
public struct PendableDefaultError: Error, Sendable {}

Sources/Fakes/Spy/Spy+Result.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ extension Spy {
1515
public convenience init<Success, Failure: Error>(failure: Failure) where Returning == Result<Success, Failure> {
1616
self.init(.failure(failure))
1717
}
18+
19+
public convenience init<Success>() where Returning == Result<Success, Error> {
20+
self.init(.failure(EmptyError()))
21+
}
1822
}
1923

2024
extension Spy {

Sources/Fakes/Spy/Spy+ThrowingPendable.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ extension Spy {
1818
self.init(.pending(fallback: .failure(pendingFailure)))
1919
}
2020

21+
/// Create a throwing pendable Spy that is pre-stubbed to return a pending that will block for a bit before throwing an error
22+
public convenience init<Success: Sendable>() where Returning == ThrowingPendable<Success, Error> {
23+
self.init(.pending(fallback: .failure(EmptyError())))
24+
}
25+
2126
/// Create a throwing pendable Spy that is pre-stubbed to return a finished & successful value.
2227
public convenience init<Success: Sendable, Failure: Error>(success: Success) where Returning == ThrowingPendable<Success, Failure> {
2328
self.init(.finished(.success(success)))
@@ -54,7 +59,7 @@ extension Spy {
5459

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

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

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

0 commit comments

Comments
 (0)