Skip to content

Commit 2754f76

Browse files
authored
Update MacroTesting example tests (#5)
This brings in the latest versions of Apple's macro demos and tests from SwiftSyntax.
1 parent 0d03f14 commit 2754f76

39 files changed

+1203
-444
lines changed

Tests/MacroTestingTests/AddAsyncCompletionHandlerTests.swift

Lines changed: 0 additions & 134 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import MacroTesting
2+
import XCTest
3+
4+
final class AddAsyncMacroTests: BaseTestCase {
5+
override func invokeTest() {
6+
withMacroTesting(macros: [AddAsyncMacro.self]) {
7+
super.invokeTest()
8+
}
9+
}
10+
11+
func testExpansionTransformsFunctionWithResultCompletionToAsyncThrows() {
12+
assertMacro {
13+
#"""
14+
@AddAsync
15+
func c(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Result<String, Error>) -> Void) -> Void {
16+
completionBlock(.success("a: \(a), b: \(b), value: \(value)"))
17+
}
18+
"""#
19+
} expansion: {
20+
#"""
21+
func c(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Result<String, Error>) -> Void) -> Void {
22+
completionBlock(.success("a: \(a), b: \(b), value: \(value)"))
23+
}
24+
25+
func c(a: Int, for b: String, _ value: Double) async throws -> String {
26+
try await withCheckedThrowingContinuation { continuation in
27+
c(a: a, for: b, value) { returnValue in
28+
29+
switch returnValue {
30+
case .success(let value):
31+
continuation.resume(returning: value)
32+
case .failure(let error):
33+
continuation.resume(throwing: error)
34+
}
35+
}
36+
}
37+
}
38+
"""#
39+
}
40+
}
41+
42+
func testExpansionTransformsFunctionWithBoolCompletionToAsync() {
43+
assertMacro {
44+
"""
45+
@AddAsync
46+
func d(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Bool) -> Void) -> Void {
47+
completionBlock(true)
48+
}
49+
"""
50+
} expansion: {
51+
"""
52+
func d(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Bool) -> Void) -> Void {
53+
completionBlock(true)
54+
}
55+
56+
func d(a: Int, for b: String, _ value: Double) async -> Bool {
57+
await withCheckedContinuation { continuation in
58+
d(a: a, for: b, value) { returnValue in
59+
60+
continuation.resume(returning: returnValue)
61+
}
62+
}
63+
}
64+
"""
65+
}
66+
}
67+
68+
func testExpansionOnStoredPropertyEmitsError() {
69+
assertMacro {
70+
"""
71+
struct Test {
72+
@AddAsync
73+
var name: String
74+
}
75+
"""
76+
} diagnostics: {
77+
"""
78+
struct Test {
79+
@AddAsync
80+
┬────────
81+
╰─ 🛑 @addAsync only works on functions
82+
var name: String
83+
}
84+
"""
85+
}
86+
}
87+
88+
func testExpansionOnAsyncFunctionEmitsError() {
89+
assertMacro {
90+
"""
91+
struct Test {
92+
@AddAsync
93+
async func sayHello() {
94+
}
95+
}
96+
"""
97+
} diagnostics: {
98+
"""
99+
struct Test {
100+
@AddAsync
101+
┬────────
102+
╰─ 🛑 @addAsync requires an function that returns void
103+
async func sayHello() {
104+
}
105+
}
106+
"""
107+
}
108+
}
109+
}

Tests/MacroTestingTests/AddBlockerTests.swift

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,37 @@ final class AddBlockerTests: BaseTestCase {
88
}
99
}
1010

11-
func testAddBlocker() {
11+
func testExpansionTransformsAdditionToSubtractionAndEmitsWarning() {
1212
assertMacro {
1313
"""
14-
let x = 1
15-
let y = 2
16-
let z = 3
1714
#addBlocker(x * y + z)
1815
"""
1916
} diagnostics: {
2017
"""
21-
let x = 1
22-
let y = 2
23-
let z = 3
2418
#addBlocker(x * y + z)
2519
───── ┬ ─
2620
╰─ ⚠️ blocked an add; did you mean to subtract?
2721
✏️ use '-'
2822
"""
2923
} fixes: {
3024
"""
31-
let x = 1
32-
let y = 2
33-
let z = 3
3425
#addBlocker(x * y - z)
3526
"""
3627
} expansion: {
3728
"""
38-
let x = 1
39-
let y = 2
40-
let z = 3
4129
x * y - z
4230
"""
4331
}
4432
}
4533

46-
func testAddBlocker_Inline() {
34+
func testExpansionPreservesSubtraction() {
4735
assertMacro {
4836
"""
49-
#addBlocker(1 * 2 + 3)
50-
"""
51-
} diagnostics: {
52-
"""
53-
#addBlocker(1 * 2 + 3)
54-
───── ┬ ─
55-
╰─ ⚠️ blocked an add; did you mean to subtract?
56-
✏️ use '-'
57-
"""
58-
} fixes: {
59-
"""
60-
#addBlocker(1 * 2 - 3)
37+
#addBlocker(x * y - z)
6138
"""
6239
} expansion: {
6340
"""
64-
1 * 2 - 3
41+
x * y - z
6542
"""
6643
}
6744
}

0 commit comments

Comments
 (0)