Skip to content

Commit dd88f82

Browse files
authored
Merge pull request #1 from twof/6Upgrade
6 upgrade
2 parents 9365bf9 + 81d1be4 commit dd88f82

File tree

9 files changed

+125
-328
lines changed

9 files changed

+125
-328
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.vscode
12
.DS_Store
23
/.build
34
/Packages

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.0-DEVELOPMENT-SNAPSHOT-2024-06-22-a

Package.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// swift-tools-version: 5.10
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
1+
// swift-tools-version: 6.0
32

43
import PackageDescription
54

@@ -10,18 +9,16 @@ let package = Package(
109
.iOS(.v17)
1110
],
1211
products: [
13-
// Products define the executables and libraries a package produces, making them visible to other packages.
1412
.library(
1513
name: "FunctionSpy",
1614
targets: ["FunctionSpy"]),
1715
],
1816
targets: [
19-
// Targets are the basic building blocks of a package, defining a module or a test suite.
20-
// Targets can depend on other targets in this package and products from dependencies.
2117
.target(
2218
name: "FunctionSpy"),
2319
.testTarget(
2420
name: "FunctionSpyTests",
2521
dependencies: ["FunctionSpy"]),
26-
]
22+
],
23+
swiftLanguageModes: [.v6]
2724
)

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,3 @@ public extension SpyProtocol {
6161
```
6262

6363
Feel free to open a PR if you'd like to add a helper function to the library!
64-
65-
## Note from the developer
66-
When I started writing this, I thought I'd be able to use parameter packs, but they're very buggy as of Swift 5.9. (example [one](https://github.com/apple/swift/issues/69317), [two](https://github.com/apple/swift/issues/69313), [three](https://github.com/apple/swift/issues/69028)) Because of that, this library only supports functions with up to 4 parameters. If you need more, open an issue or a PR, and I'll be happy to add more overloads. For now I'm assuming [YAGNI](https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it).
Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,14 @@
11
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing but return `result`.
2-
public func emptyClosure<A, Result>(_ closure: (A) -> Result, result: Result) -> @Sendable (A) -> Result {
3-
{ (_: A) in result }
4-
}
5-
6-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing but return `result`.
7-
public func emptyClosure<A, B, Result>(_ closure: (A, B) -> Result, result: Result) -> @Sendable (A, B) -> Result {
8-
{ (_: A, _: B) in result }
9-
}
10-
11-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing but return `result`.
12-
public func emptyClosure<A, B, C, Result>(_ closure: (A, B, C) -> Result, result: Result) -> @Sendable (A, B, C) -> Result {
13-
{ (_: A, _: B, _: C) in result }
14-
}
15-
16-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing but return `result`.
17-
public func emptyClosure<A, B, C, D, Result>(_ closure: (A, B, C, D) -> Result, result: Result) -> @Sendable (A, B, C, D) -> Result {
18-
{ (_: A, _: B, _: C, _: D) in result }
2+
public func emptyClosure<each A, Result: Sendable>(_ closure: @Sendable (repeat each A) -> Result, result: Result) -> @Sendable (repeat each A) -> Result {
3+
{ (_: repeat each A) in result }
194
}
205

216
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing.
22-
public func emptyClosure<A>(_ closure: (A) -> Void) -> @Sendable (A) -> Void {
23-
{ (_: A) in }
24-
}
25-
26-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing.
27-
public func emptyClosure<A, B>(_ closure: (A, B) -> Void) -> @Sendable (A, B) -> Void {
28-
{ (_: A, _: B) in }
29-
}
30-
31-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing.
32-
public func emptyClosure<A, B, C>(_ closure: (A, B, C) -> Void) -> @Sendable (A, B, C) -> Void {
33-
{ (_: A, _: B, _: C) in }
34-
}
35-
36-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing.
37-
public func emptyClosure<A, B, C, D>(_ closure: (A, B, C, D) -> Void) -> @Sendable (A, B, C, D) -> Void {
38-
{ (_: A, _: B, _: C, _: D) in }
39-
}
40-
41-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing but return `result`.
42-
public func emptyClosure<A, Result>(_ closure: (A) async throws -> Result, result: Result) -> @Sendable (A) async throws -> Result {
43-
{ (_: A) in result }
44-
}
45-
46-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing but return `result`.
47-
public func emptyClosure<A, B, Result>(_ closure: (A, B) async throws -> Result, result: Result) -> @Sendable (A, B) async throws -> Result {
48-
{ (_: A, _: B) in result }
49-
}
50-
51-
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing but return `result`.
52-
public func emptyClosure<A, B, C, Result>(_ closure: (A, B, C) async throws -> Result, result: Result) async throws -> @Sendable (A, B, C) -> Result {
53-
{ (_: A, _: B, _: C) in result }
7+
public func emptyClosure<each A>(_ closure: @Sendable (repeat each A) -> Void) -> @Sendable (repeat each A) -> Void {
8+
{ (_: repeat each A) in }
549
}
5510

5611
/// Produces an empty closure with the same signature as the passed function. Resulting closure does nothing but return `result`.
57-
public func emptyClosure<A, B, C, D, Result>(_ closure: (A, B, C, D) async throws -> Result, result: Result) -> @Sendable (A, B, C, D) async throws -> Result {
58-
{ (_: A, _: B, _: C, _: D) in result }
12+
public func emptyClosure<each A, Result: Sendable>(_ closure: @Sendable (repeat each A) async throws -> Result, result: Result) -> @Sendable (repeat each A) async throws -> Result {
13+
{ (_: repeat each A) in result }
5914
}
Lines changed: 32 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,210 +1,60 @@
11
/// Wraps a function in a spy
22
/// - Parameter closure: The function to wrap
33
/// - Returns: The spy and and the wrapped function
4-
public func spy<Result>(
5-
_ closure: @escaping @Sendable () -> Result
6-
) -> (Spy, @Sendable () -> Result) {
7-
let spy = Spy()
8-
return (spy, {
4+
public func spy<each A, Result>(
5+
_ closure: @escaping @Sendable (repeat each A) -> Result
6+
) -> (Spy<repeat each A>, @Sendable (repeat each A) -> Result) {
7+
let spy = Spy<repeat each A>()
8+
9+
return (spy, { (a: repeat each A) -> Result in
910
spy.increment()
10-
spy.recordCall()
11-
return closure()
11+
spy.recordCall(repeat each a)
12+
return closure(repeat each a)
1213
})
1314
}
1415

1516
/// Wraps a function in a spy
1617
/// - Parameter closure: The function to wrap
1718
/// - Returns: The spy and and the wrapped function
18-
public func spy<A, Result>(
19-
_ closure: @escaping @Sendable (A) -> Result
20-
) -> (Spy1<A>, @Sendable (A) -> Result) {
21-
let spy = Spy1<A>()
22-
return (spy, { a in
19+
public func spy<each A, Result>(
20+
_ closure: @escaping @Sendable (repeat each A) throws -> Result
21+
) -> (Spy<repeat each A>, @Sendable (repeat each A) throws -> Result) {
22+
let spy = Spy<repeat each A>()
23+
24+
return (spy, { (a: repeat each A) throws -> Result in
2325
spy.increment()
24-
spy.recordCall(a)
25-
return closure(a)
26+
spy.recordCall(repeat each a)
27+
return try closure(repeat each a)
2628
})
2729
}
2830

2931
/// Wraps a function in a spy
3032
/// - Parameter closure: The function to wrap
3133
/// - Returns: The spy and and the wrapped function
32-
public func spy<A, B, Result>(
33-
_ closure: @escaping @Sendable (A, B) -> Result
34-
) -> (Spy2<A, B>, @Sendable (A, B) -> Result) {
35-
let spy = Spy2<A, B>()
36-
return (spy, { a, b in
34+
public func spy<each A, Result>(
35+
_ closure: @escaping @Sendable (repeat each A) async -> Result
36+
) -> (Spy<repeat each A>, @Sendable (repeat each A) async -> Result) {
37+
let spy = Spy<repeat each A>()
38+
39+
return (spy, { (a: repeat each A) async -> Result in
3740
spy.increment()
38-
spy.recordCall(a, b)
39-
return closure(a, b)
41+
spy.recordCall(repeat each a)
42+
return await closure(repeat each a)
4043
})
4144
}
4245

43-
/// Wraps a function in a spy
44-
/// - Parameter closure: The function to wrap
45-
/// - Returns: The spy and and the wrapped function
46-
public func spy<A, B, C, Result>(
47-
_ closure: @escaping @Sendable (A, B, C) -> Result
48-
) -> (Spy3<A, B, C>, @Sendable (A, B, C) -> Result) {
49-
let spy = Spy3<A, B, C>()
50-
return (spy, { a, b, c in
51-
spy.increment()
52-
spy.recordCall(a, b, c)
53-
return closure(a, b, c)
54-
})
55-
}
56-
57-
/// Wraps a function in a spy
58-
/// - Parameter closure: The function to wrap
59-
/// - Returns: The spy and and the wrapped function
60-
public func spy<A, B, C, D, Result>(
61-
_ closure: @escaping @Sendable (A, B, C, D) -> Result
62-
) -> (Spy4<A, B, C, D>, @Sendable (A, B, C, D) -> Result) {
63-
let spy = Spy4<A, B, C, D>()
64-
return (spy, { a, b, c, d in
65-
spy.increment()
66-
spy.recordCall(a, b, c, d)
67-
return closure(a, b, c, d)
68-
})
69-
}
70-
71-
/// Wraps a function in a spy
72-
/// - Parameter closure: The function to wrap
73-
/// - Returns: The spy and and the wrapped function
74-
public func spy<Result>(
75-
_ closure: @escaping @Sendable () throws -> Result
76-
) -> (Spy, @Sendable () throws -> Result) {
77-
let spy = Spy()
78-
return (spy, {
79-
spy.increment()
80-
spy.recordCall()
81-
return try closure()
82-
})
83-
}
8446

8547
/// Wraps a function in a spy
8648
/// - Parameter closure: The function to wrap
8749
/// - Returns: The spy and and the wrapped function
88-
public func spy<A, Result>(
89-
_ closure: @escaping @Sendable (A) throws -> Result
90-
) -> (Spy1<A>, @Sendable (A) throws -> Result) {
91-
let spy = Spy1<A>()
92-
return (spy, { a in
50+
public func spy<each A, Result>(
51+
_ closure: @escaping @Sendable (repeat each A) async throws -> Result
52+
) -> (Spy<repeat each A>, @Sendable (repeat each A) async throws -> Result) {
53+
let spy = Spy<repeat each A>()
54+
55+
return (spy, { (a: repeat each A) async throws -> Result in
9356
spy.increment()
94-
spy.recordCall(a)
95-
return try closure(a)
57+
spy.recordCall(repeat each a)
58+
return try await closure(repeat each a)
9659
})
9760
}
98-
99-
/// Wraps a function in a spy
100-
/// - Parameter closure: The function to wrap
101-
/// - Returns: The spy and and the wrapped function
102-
public func spy<A, B, Result>(
103-
_ closure: @escaping @Sendable (A, B) throws -> Result
104-
) -> (Spy2<A, B>, @Sendable (A, B) throws -> Result) {
105-
let spy = Spy2<A, B>()
106-
return (spy, { a, b in
107-
spy.increment()
108-
spy.recordCall(a, b)
109-
return try closure(a, b)
110-
})
111-
}
112-
113-
/// Wraps a function in a spy
114-
/// - Parameter closure: The function to wrap
115-
/// - Returns: The spy and and the wrapped function
116-
public func spy<A, B, C, Result>(
117-
_ closure: @escaping @Sendable (A, B, C) throws -> Result
118-
) -> (Spy3<A, B, C>, @Sendable (A, B, C) throws -> Result) {
119-
let spy = Spy3<A, B, C>()
120-
return (spy, { a, b, c in
121-
spy.increment()
122-
spy.recordCall(a, b, c)
123-
return try closure(a, b, c)
124-
})
125-
}
126-
127-
/// Wraps a function in a spy
128-
/// - Parameter closure: The function to wrap
129-
/// - Returns: The spy and and the wrapped function
130-
public func spy<A, B, C, D, Result>(
131-
_ closure: @escaping @Sendable (A, B, C, D) throws -> Result
132-
) -> (Spy4<A, B, C, D>, @Sendable (A, B, C, D) throws -> Result) {
133-
let spy = Spy4<A, B, C, D>()
134-
return (spy, { a, b, c, d in
135-
spy.increment()
136-
spy.recordCall(a, b, c, d)
137-
return try closure(a, b, c, d)
138-
})
139-
}
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)