Skip to content

Commit 9365bf9

Browse files
committed
add async throws variants to spy
1 parent 5e321d5 commit 9365bf9

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Sources/FunctionSpy/FunctionSpy.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,74 @@ public func spy<A, B, C, D, Result>(
137137
return try closure(a, b, c, d)
138138
})
139139
}
140+
141+
/// Wraps a function in a spy
142+
/// - Parameter closure: The function to wrap
143+
/// - Returns: The spy and and the wrapped function
144+
public func spy<Result>(
145+
_ closure: @escaping @Sendable () async throws -> Result
146+
) -> (Spy, @Sendable () async throws -> Result) {
147+
let spy = Spy()
148+
return (spy, {
149+
spy.increment()
150+
spy.recordCall()
151+
return try await closure()
152+
})
153+
}
154+
155+
/// Wraps a function in a spy
156+
/// - Parameter closure: The function to wrap
157+
/// - Returns: The spy and and the wrapped function
158+
public func spy<A, Result>(
159+
_ closure: @escaping @Sendable (A) async throws -> Result
160+
) -> (Spy1<A>, @Sendable (A) async throws -> Result) {
161+
let spy = Spy1<A>()
162+
return (spy, { a in
163+
spy.increment()
164+
spy.recordCall(a)
165+
return try await closure(a)
166+
})
167+
}
168+
169+
/// Wraps a function in a spy
170+
/// - Parameter closure: The function to wrap
171+
/// - Returns: The spy and and the wrapped function
172+
public func spy<A, B, Result>(
173+
_ closure: @escaping @Sendable (A, B) async throws -> Result
174+
) -> (Spy2<A, B>, @Sendable (A, B) async throws -> Result) {
175+
let spy = Spy2<A, B>()
176+
return (spy, { a, b in
177+
spy.increment()
178+
spy.recordCall(a, b)
179+
return try await closure(a, b)
180+
})
181+
}
182+
183+
/// Wraps a function in a spy
184+
/// - Parameter closure: The function to wrap
185+
/// - Returns: The spy and and the wrapped function
186+
public func spy<A, B, C, Result>(
187+
_ closure: @escaping @Sendable (A, B, C) async throws -> Result
188+
) -> (Spy3<A, B, C>, @Sendable (A, B, C) async throws -> Result) {
189+
let spy = Spy3<A, B, C>()
190+
return (spy, { a, b, c in
191+
spy.increment()
192+
spy.recordCall(a, b, c)
193+
return try await closure(a, b, c)
194+
})
195+
}
196+
197+
/// Wraps a function in a spy
198+
/// - Parameter closure: The function to wrap
199+
/// - Returns: The spy and and the wrapped function
200+
public func spy<A, B, C, D, Result>(
201+
_ closure: @escaping @Sendable (A, B, C, D) async throws -> Result
202+
) -> (Spy4<A, B, C, D>, @Sendable (A, B, C, D) async throws -> Result) {
203+
let spy = Spy4<A, B, C, D>()
204+
return (spy, { a, b, c, d in
205+
spy.increment()
206+
spy.recordCall(a, b, c, d)
207+
return try await closure(a, b, c, d)
208+
})
209+
}
210+

0 commit comments

Comments
 (0)