From 6a4f5963356726486064c9353592cd48b26aeb0b Mon Sep 17 00:00:00 2001 From: Rachel Brindle Date: Sun, 26 May 2024 15:59:18 -0700 Subject: [PATCH] Provide default initializers for Spy when throwing any Swift.Error Rename PendableDefaultError to EmptyError, and make its initializer public --- Sources/Fakes/EmptyError.swift | 4 ++++ Sources/Fakes/Pendable/Pendable.swift | 5 +---- Sources/Fakes/Spy/Spy+Result.swift | 4 ++++ Sources/Fakes/Spy/Spy+ThrowingPendable.swift | 9 +++++++-- 4 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 Sources/Fakes/EmptyError.swift diff --git a/Sources/Fakes/EmptyError.swift b/Sources/Fakes/EmptyError.swift new file mode 100644 index 0000000..48bf2bb --- /dev/null +++ b/Sources/Fakes/EmptyError.swift @@ -0,0 +1,4 @@ +/// An error that can be used as a default error in tests +public struct EmptyError: Error, Sendable { + public init() {} +} diff --git a/Sources/Fakes/Pendable/Pendable.swift b/Sources/Fakes/Pendable/Pendable.swift index 10c1145..bed4dc1 100644 --- a/Sources/Fakes/Pendable/Pendable.swift +++ b/Sources/Fakes/Pendable/Pendable.swift @@ -184,9 +184,6 @@ extension Pendable { /// Creatse a new pending `Pendable` with a fallback value of an error. public static func pending() -> Pendable where Value == Result { - Pendable(fallbackValue: Result.failure(PendableDefaultError())) + Pendable(fallbackValue: Result.failure(EmptyError())) } } - -/// An error that can be used as a default error for ``Pendable`` when returning a `Result<..., Error>`. -public struct PendableDefaultError: Error, Sendable {} diff --git a/Sources/Fakes/Spy/Spy+Result.swift b/Sources/Fakes/Spy/Spy+Result.swift index 6b92af2..693e8fc 100644 --- a/Sources/Fakes/Spy/Spy+Result.swift +++ b/Sources/Fakes/Spy/Spy+Result.swift @@ -15,6 +15,10 @@ extension Spy { public convenience init(failure: Failure) where Returning == Result { self.init(.failure(failure)) } + + public convenience init() where Returning == Result { + self.init(.failure(EmptyError())) + } } extension Spy { diff --git a/Sources/Fakes/Spy/Spy+ThrowingPendable.swift b/Sources/Fakes/Spy/Spy+ThrowingPendable.swift index d9056ab..bb0962d 100644 --- a/Sources/Fakes/Spy/Spy+ThrowingPendable.swift +++ b/Sources/Fakes/Spy/Spy+ThrowingPendable.swift @@ -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() where Returning == ThrowingPendable { + 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: Success) where Returning == ThrowingPendable { self.init(.finished(.success(success))) @@ -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() where Returning == ThrowingPendable { - self.stub(pendingFailure: PendableDefaultError()) + self.stub(pendingFailure: EmptyError()) } /// Update the throwing pendable Spy's stub to be successful, with the given value. @@ -73,7 +78,7 @@ extension Spy { /// Update the throwing pendable Spy's stub to throw an error. public func stubFailure() where Returning == ThrowingPendable { - self.stub(failure: PendableDefaultError()) + self.stub(failure: EmptyError()) } }