diff --git a/CHANGELOG.md b/CHANGELOG.md index 433c95f..e297c36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,21 @@ # DevTesting Changelog +## 1.1.0: September 17, 2025 + +Adds two initializers to `ThrowingStub`: `init(defaultReturnValue:resultQueue:)` and +`init(defaultError:resultQueue:)`. These initializers enable cleaner call sites, as in the following +example: + + // Before + mock.doSomethingSuccessfullyStub = ThrowingStub(defaultResult: .success(value)) + mock.doSomethingUnsuccessfullyStub = ThrowingStub(defaultResult: .failure(error)) + + // After + mock.doSomethingSuccessfullyStub = ThrowingStub(defaultReturnValue: value) + mock.doSomethingUnsuccessfullyStub = ThrowingStub(defaultError: error) + + ## 1.0.0: September 1, 2025 This is the first release of DevTesting. The initial feature set includes diff --git a/Sources/DevTesting/Stubbing/Stub.swift b/Sources/DevTesting/Stubbing/Stub.swift index f20eb34..beaa76a 100644 --- a/Sources/DevTesting/Stubbing/Stub.swift +++ b/Sources/DevTesting/Stubbing/Stub.swift @@ -126,6 +126,32 @@ public final class ThrowingStub where ErrorTyp extension ThrowingStub { + /// Creates a new throwing stub with a default success result. + /// + /// - Parameters: + /// - defaultReturnValue: The successful return value of the stub when the result queue is empty. + /// - resultQueue: A queue of call results to use. If empty, `defaultResult` is used. + public convenience init( + defaultReturnValue: ReturnType, + resultQueue: [Result] = [] + ) { + self.init(defaultResult: .success(defaultReturnValue), resultQueue: resultQueue) + } + + + /// Creates a new throwing stub with a default failure result. + /// + /// - Parameters: + /// - defaultError: The error that the stub throws when its result queue is empty. + /// - resultQueue: A queue of call results to use. If empty, `defaultResult` is used. + public convenience init( + defaultError: ErrorType, + resultQueue: [Result] = [] + ) { + self.init(defaultResult: .failure(defaultError), resultQueue: resultQueue) + } + + /// The arguments of the stub’s recorded calls. public var callArguments: [Arguments] { return calls.map(\.arguments) @@ -162,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?, errorQueue: [ErrorType?] = []) { + public convenience init(defaultError: ErrorType? = nil, errorQueue: [ErrorType?] = []) { self.init( defaultResult: defaultError.map(Result.failure(_:)) ?? .success(()), resultQueue: errorQueue.map { $0.map(Result.failure(_:)) ?? .success(()) } @@ -280,13 +306,3 @@ 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 5fb5599..051ac23 100644 --- a/Tests/DevTestingTests/Stubbing/StubTests.swift +++ b/Tests/DevTestingTests/Stubbing/StubTests.swift @@ -28,6 +28,42 @@ struct StubTests { } + @Test + func initDefaultReturnValueSetsProperties() { + let defaultReturnValue = 10 + let resultQueue: [Result] = (0 ..< 5).map { (i) in + i.isMultiple(of: 2) ? .success(i) : .failure(.init(id: i)) + } + + let stub = ThrowingStub( + defaultReturnValue: 10, + resultQueue: resultQueue + ) + + #expect(stub.defaultResult == .success(defaultReturnValue)) + #expect(stub.resultQueue == resultQueue) + #expect(stub.calls.isEmpty) + } + + + @Test + func initDefaultErrorSetsProperties() { + let defaultError = HashableError(id: 7) + let resultQueue: [Result] = (0 ..< 5).map { (i) in + i.isMultiple(of: 2) ? .success(i) : .failure(.init(id: i)) + } + + let stub = ThrowingStub( + defaultError: defaultError, + resultQueue: resultQueue + ) + + #expect(stub.defaultResult == .failure(defaultError)) + #expect(stub.resultQueue == resultQueue) + #expect(stub.calls.isEmpty) + } + + @Test func accessingProperties() { let stub = ThrowingStub(defaultResult: .success(0))