From f971b8ff7d3501813ea6d7e1577bf7989be36f24 Mon Sep 17 00:00:00 2001 From: Prachi Gauriar Date: Wed, 17 Sep 2025 11:07:10 -0400 Subject: [PATCH] Eliminate ambiguous init errors by removing some default parameters --- Sources/DevTesting/Stubbing/Stub.swift | 14 ++++++++++++-- Tests/DevTestingTests/Stubbing/StubTests.swift | 5 ++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Sources/DevTesting/Stubbing/Stub.swift b/Sources/DevTesting/Stubbing/Stub.swift index beaa76a..d5a2cf9 100644 --- a/Sources/DevTesting/Stubbing/Stub.swift +++ b/Sources/DevTesting/Stubbing/Stub.swift @@ -188,7 +188,7 @@ extension ThrowingStub where ReturnType == Void { /// - Parameters: /// - defaultError: The error that the stub will throw when the error queue is empty. /// - errorQueue: A queue of errors to throw. If empty, `defaultError` is used instead. - public convenience init(defaultError: ErrorType? = nil, errorQueue: [ErrorType?] = []) { + public convenience init(defaultError: ErrorType?, errorQueue: [ErrorType?] = []) { self.init( defaultResult: defaultError.map(Result.failure(_:)) ?? .success(()), resultQueue: errorQueue.map { $0.map(Result.failure(_:)) ?? .success(()) } @@ -257,7 +257,7 @@ extension ThrowingStub where ErrorType == Never { /// - Parameters: /// - defaultReturnValue: The return value of the stub when the return value queue is empty. /// - returnValueQueue: A queue of values to return. If empty, `defaultReturnValue` is used. - public convenience init(defaultReturnValue: ReturnType, returnValueQueue: [ReturnType] = []) { + public convenience init(defaultReturnValue: ReturnType, returnValueQueue: [ReturnType]) { self.init( defaultResult: .success(defaultReturnValue), resultQueue: returnValueQueue.map(Result.success(_:)) @@ -306,3 +306,13 @@ extension ThrowingStub.Call where ErrorType == Never { } } } + + +// MARK: - ReturnType is Void and ErrorType is Never + +extension ThrowingStub where ReturnType == Void, ErrorType == Never { + /// Creates a new stub with an empty tuple as the default return value. + public convenience init() { + self.init(defaultReturnValue: ()) + } +} diff --git a/Tests/DevTestingTests/Stubbing/StubTests.swift b/Tests/DevTestingTests/Stubbing/StubTests.swift index 051ac23..426987d 100644 --- a/Tests/DevTestingTests/Stubbing/StubTests.swift +++ b/Tests/DevTestingTests/Stubbing/StubTests.swift @@ -185,8 +185,11 @@ struct StubTests { @Test - func convenenienceInitWhenReturnTypeIsVoidAndErrorTypeIsNever() { + func convenenienceInitsOverloadWithoutColision() { let stub = Stub() + let stub2 = Stub(defaultReturnValue: 3) + let stub4 = ThrowingStub(defaultError: nil) + let stub3 = ThrowingStub(defaultError: HashableError(id: 2)) // There’s nothing to actually test here, as the compiler guarantees everything we would test. Check the result // queue just for posterity