|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// A DynamicResult is specifically for mocking out when multiple results for a call can happen. |
| 4 | +/// |
| 5 | +/// DynamicResult is intended to be an implementation detail of ``Spy``, |
| 6 | +/// but is exposed publicly to be composed with other types as desired. |
| 7 | +public final class DynamicResult<Arguments, Returning> { |
| 8 | + /// A value or closure to be used by the DynamicResult |
| 9 | + public enum Stub { |
| 10 | + /// A static value |
| 11 | + case value(Returning) |
| 12 | + /// A closure to be called. |
| 13 | + case closure(@Sendable (Arguments) -> Returning) |
| 14 | + |
| 15 | + /// Call the stub. |
| 16 | + /// If the stub is a `.value`, then return the value. |
| 17 | + /// If the stub is a `.closure`, then call the closure with the arguments. |
| 18 | + func call(_ arguments: Arguments) -> Returning { |
| 19 | + switch self { |
| 20 | + case .value(let returning): |
| 21 | + return returning |
| 22 | + case .closure(let closure): |
| 23 | + return closure(arguments) |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private let lock = NSRecursiveLock() |
| 29 | + private var stubs: [Stub] |
| 30 | + |
| 31 | + private var _stubHistory: [Returning] = [] |
| 32 | + var stubHistory: [Returning] { |
| 33 | + lock.lock() |
| 34 | + defer { lock.unlock () } |
| 35 | + return _stubHistory |
| 36 | + } |
| 37 | + |
| 38 | + /// Create a new DynamicResult stubbed to return the values in the given order. |
| 39 | + /// That is, given `DynamicResult<Void, Int>(1, 2, 3)`, |
| 40 | + /// if you call `.call` 5 times, you will get back `1, 2, 3, 3, 3`. |
| 41 | + public init(_ value: Returning, _ values: Returning...) { |
| 42 | + self.stubs = Array(value, values).map { Stub.value($0) } |
| 43 | + } |
| 44 | + |
| 45 | + internal init(_ values: [Returning]) { |
| 46 | + self.stubs = values.map { Stub.value($0) } |
| 47 | + } |
| 48 | + |
| 49 | + /// Create a new DynamicResult stubbed to call the given closure. |
| 50 | + public init(_ closure: @escaping @Sendable (Arguments) -> Returning) { |
| 51 | + self.stubs = [.closure(closure)] |
| 52 | + } |
| 53 | + |
| 54 | + /// Create a new DynamicResult stubbed to call the given stubs. |
| 55 | + public init(_ stub: Stub, _ stubs: Stub...) { |
| 56 | + self.stubs = Array(stub, stubs) |
| 57 | + } |
| 58 | + |
| 59 | + internal init(_ stubs: [Stub]) { |
| 60 | + self.stubs = stubs |
| 61 | + } |
| 62 | + |
| 63 | + /// Call the DynamicResult, returning the next stub in the list of stubs. |
| 64 | + public func call(_ arguments: Arguments) -> Returning { |
| 65 | + lock.lock() |
| 66 | + defer { lock.unlock () } |
| 67 | + let value = nextStub().call(arguments) |
| 68 | + _stubHistory.append(value) |
| 69 | + return value |
| 70 | + } |
| 71 | + |
| 72 | + /// Call the DynamicResult, returning the next stub in the list of stubs. |
| 73 | + public func call() -> Returning where Arguments == Void { |
| 74 | + call(()) |
| 75 | + } |
| 76 | + |
| 77 | + /// Replace the stubs with the new static values |
| 78 | + public func replace(_ value: Returning, _ values: Returning...) { |
| 79 | + replace(Array(value, values)) |
| 80 | + } |
| 81 | + |
| 82 | + /// Replace the stubs with the new static values |
| 83 | + internal func replace(_ values: [Returning]) { |
| 84 | + lock.lock() |
| 85 | + defer { lock.unlock () } |
| 86 | + self.resolvePendables() |
| 87 | + self.stubs = values.map { .value($0) } |
| 88 | + } |
| 89 | + |
| 90 | + /// Replace the stubs with the new closure. |
| 91 | + public func replace(_ closure: @escaping @Sendable (Arguments) -> Returning) { |
| 92 | + lock.lock() |
| 93 | + defer { lock.unlock () } |
| 94 | + self.resolvePendables() |
| 95 | + self.stubs = [.closure(closure)] |
| 96 | + } |
| 97 | + |
| 98 | + /// Replace the stubs with the new list of stubs |
| 99 | + public func replace(_ stub: Stub, _ stubs: Stub...) { |
| 100 | + lock.lock() |
| 101 | + defer { lock.unlock () } |
| 102 | + self.resolvePendables() |
| 103 | + self.stubs = Array(stub, stubs) |
| 104 | + } |
| 105 | + |
| 106 | + /// Replace the stubs with the new list of stubs |
| 107 | + internal func replace(_ stubs: [Stub]) { |
| 108 | + lock.lock() |
| 109 | + defer { lock.unlock () } |
| 110 | + self.resolvePendables() |
| 111 | + self.stubs = stubs |
| 112 | + } |
| 113 | + |
| 114 | + /// Append the values to the list of stubs. |
| 115 | + public func append(_ value: Returning, _ values: Returning...) { |
| 116 | + append(Array(value, values)) |
| 117 | + } |
| 118 | + |
| 119 | + internal func append(_ values: [Returning]) { |
| 120 | + lock.lock() |
| 121 | + defer { lock.unlock () } |
| 122 | + stubs.append(contentsOf: values.map { .value($0) }) |
| 123 | + } |
| 124 | + |
| 125 | + /// Append the closure to the list of stubs. |
| 126 | + public func append(_ closure: @escaping @Sendable (Arguments) -> Returning) { |
| 127 | + lock.lock() |
| 128 | + defer { lock.unlock () } |
| 129 | + stubs.append(.closure(closure)) |
| 130 | + } |
| 131 | + |
| 132 | + /// Append the stubs to the list of stubs. |
| 133 | + public func append(_ stub: Stub, _ stubs: Stub...) { |
| 134 | + append(Array(stub, stubs)) |
| 135 | + } |
| 136 | + |
| 137 | + internal func append(_ stubs: [Stub]) { |
| 138 | + lock.lock() |
| 139 | + defer { lock.unlock () } |
| 140 | + self.stubs.append(contentsOf: stubs) |
| 141 | + } |
| 142 | + |
| 143 | + private func nextStub() -> Stub { |
| 144 | + guard let stub = stubs.first else { |
| 145 | + fatalError("Fakes: DynamicResult \(self) has 0 stubs. This should never happen. File a bug at https://github.com/Quick/swift-fakes/issues/new") |
| 146 | + } |
| 147 | + if stubs.count > 1 { |
| 148 | + stubs.removeFirst() |
| 149 | + } |
| 150 | + return stub |
| 151 | + } |
| 152 | + |
| 153 | + private func resolvePendables() { |
| 154 | + stubs.forEach { |
| 155 | + guard case .value(let value) = $0 else { return } |
| 156 | + if let resolvable = value as? ResolvableWithFallback { |
| 157 | + resolvable.resolveWithFallback() |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +extension DynamicResult: @unchecked Sendable where Arguments: Sendable, Returning: Sendable {} |
| 164 | + |
| 165 | +internal extension Array { |
| 166 | + init(_ value: Element, _ values: [Element]) { |
| 167 | + self = [value] + values |
| 168 | + } |
| 169 | + |
| 170 | + mutating func append(_ value: Element, _ values: [Element]) { |
| 171 | + self.append(contentsOf: Array(value, values)) |
| 172 | + } |
| 173 | +} |
0 commit comments