Skip to content

Commit 90e9986

Browse files
Prachi Gauriarprachigauriar
authored andcommitted
Eliminate ambiguous init errors by removing some default parameters
1 parent 6dfdd1a commit 90e9986

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Sources/DevTesting/Stubbing/Stub.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ extension ThrowingStub where ReturnType == Void {
188188
/// - Parameters:
189189
/// - defaultError: The error that the stub will throw when the error queue is empty.
190190
/// - errorQueue: A queue of errors to throw. If empty, `defaultError` is used instead.
191-
public convenience init(defaultError: ErrorType? = nil, errorQueue: [ErrorType?] = []) {
191+
public convenience init(defaultError: ErrorType?, errorQueue: [ErrorType?] = []) {
192192
self.init(
193193
defaultResult: defaultError.map(Result.failure(_:)) ?? .success(()),
194194
resultQueue: errorQueue.map { $0.map(Result.failure(_:)) ?? .success(()) }
@@ -257,7 +257,7 @@ extension ThrowingStub where ErrorType == Never {
257257
/// - Parameters:
258258
/// - defaultReturnValue: The return value of the stub when the return value queue is empty.
259259
/// - returnValueQueue: A queue of values to return. If empty, `defaultReturnValue` is used.
260-
public convenience init(defaultReturnValue: ReturnType, returnValueQueue: [ReturnType] = []) {
260+
public convenience init(defaultReturnValue: ReturnType, returnValueQueue: [ReturnType]) {
261261
self.init(
262262
defaultResult: .success(defaultReturnValue),
263263
resultQueue: returnValueQueue.map(Result.success(_:))
@@ -306,3 +306,13 @@ extension ThrowingStub.Call where ErrorType == Never {
306306
}
307307
}
308308
}
309+
310+
311+
// MARK: - ReturnType is Void and ErrorType is Never
312+
313+
extension ThrowingStub where ReturnType == Void, ErrorType == Never {
314+
/// Creates a new stub with an empty tuple as the default return value.
315+
public convenience init() {
316+
self.init(defaultReturnValue: ())
317+
}
318+
}

Tests/DevTestingTests/Stubbing/StubTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ struct StubTests {
185185

186186

187187
@Test
188-
func convenenienceInitWhenReturnTypeIsVoidAndErrorTypeIsNever() {
188+
func convenenienceInitsOverloadWithoutColision() {
189189
let stub = Stub<Void, Void>()
190+
let stub2 = Stub<Void, Int>(defaultReturnValue: 3)
191+
let stub4 = ThrowingStub<Void, Void, any Error>(defaultError: nil)
192+
let stub3 = ThrowingStub<Void, Int, any Error>(defaultError: HashableError(id: 2))
190193

191194
// There’s nothing to actually test here, as the compiler guarantees everything we would test. Check the result
192195
// queue just for posterity

0 commit comments

Comments
 (0)