Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.13.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.14.0"),
],
targets: [
.target(
Expand Down
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func testStringify() {
"""
#stringify(a + b)
"""
} matches: {
} expansion: {
"""
(a + b, "a + b")
"""
Expand Down Expand Up @@ -86,14 +86,14 @@ assertMacro(["stringify": StringifyMacro.self], record: true) {
"""
#stringify(a + b)
"""
} matches: {
} expansion: {
"""
(a + b, "a + b")
"""
}
```

Now when you run the test again the freshest expanded macro will be written to the `matches`
Now when you run the test again the freshest expanded macro will be written to the `expansion`
trailing closure.

If you're writing many tests for a macro, you can avoid the repetitive work of specifying the macros
Expand All @@ -115,7 +115,7 @@ class StringifyMacroTests: XCTestCase {
"""
#stringify(a + b)
"""
} matches: {
} expansion: {
"""
(a + b, "a + b")
"""
Expand All @@ -142,27 +142,47 @@ override func invokeTest() {
Macro Testing can also test diagnostics, such as warnings, errors, notes, and fix-its. When a macro
expansion emits a diagnostic, it will render inline in the test. For example, a macro that adds
completion handler functions to async functions may emit an error and fix-it when it is applied to a
non-async function. The resulting macro test will fully capture this information:
non-async function. The resulting macro test will fully capture this information, including where
the diagnostics are emitted, how the fix-its are applied, and how the final macro expands:

```swift
func testNonAsyncFunctionDiagnostic() {
assertMacro {
"""
@AddCompletionHandler
func f(a: Int, for b: String, _ value: Double) -> String {
func f(a: Int, for b: String) -> String {
return b
}
}
"""
} matches: {
} diagnostics: {
"""
@AddCompletionHandler
func f(a: Int, for b: String, _ value: Double) -> String {
func f(a: Int, for b: String) -> String {
┬───
╰─ 🛑 can only add a completion-handler variant to an 'async' function
✏️ add 'async'
return b
}
"""
} fixes: {
"""
@AddCompletionHandler
func f(a: Int, for b: String) async -> String {
return b
}
"""
} expansion: {
"""
func f(a: Int, for b: String) async -> String {
return b
}

func f(a: Int, for b: String, completionHandler: @escaping (String) -> Void) {
Task {
completionHandler(await f(a: a, for: b, value))
}
}
"""
}
}
```
Expand Down
Loading