-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guard EntryMacro tests with
SwiftSyntax600
availability check
Tests cover new lexical context feature of SwiftSyntaxMacros.
- Loading branch information
Showing
2 changed files
with
69 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,54 @@ | ||
import MacroTesting | ||
import XCTest | ||
#if canImport(SwiftSyntax600) | ||
import MacroTesting | ||
import XCTest | ||
|
||
final class EntryMacroTests: BaseTestCase { | ||
override func invokeTest() { | ||
withMacroTesting( | ||
macros: [ | ||
EntryMacro.self | ||
] | ||
) { | ||
super.invokeTest() | ||
final class EntryMacroTests: BaseTestCase { | ||
override func invokeTest() { | ||
withMacroTesting( | ||
macros: [ | ||
EntryMacro.self | ||
] | ||
) { | ||
super.invokeTest() | ||
} | ||
} | ||
} | ||
|
||
func testWithinEnvironmentValues() { | ||
assertMacro { | ||
""" | ||
extension EnvironmentValues { | ||
@Entry var x: String = "" | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
extension EnvironmentValues { | ||
var x: String { | ||
get { | ||
fatalError() | ||
func testWithinEnvironmentValues() { | ||
assertMacro { | ||
""" | ||
extension EnvironmentValues { | ||
@Entry var x: String = "" | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
extension EnvironmentValues { | ||
var x: String { | ||
get { | ||
fatalError() | ||
} | ||
} | ||
} | ||
""" | ||
} | ||
""" | ||
} | ||
} | ||
|
||
func testNotWithinEnvironmentValues() { | ||
assertMacro { | ||
""" | ||
extension String { | ||
@Entry var x: String = "" | ||
} | ||
""" | ||
} diagnostics: { | ||
""" | ||
extension String { | ||
@Entry var x: String = "" | ||
┬───── | ||
╰─ 🛑 '@Entry' macro can only attach to var declarations inside extensions of EnvironmentValues | ||
func testNotWithinEnvironmentValues() { | ||
assertMacro { | ||
""" | ||
extension String { | ||
@Entry var x: String = "" | ||
} | ||
""" | ||
} diagnostics: { | ||
""" | ||
extension String { | ||
@Entry var x: String = "" | ||
┬───── | ||
╰─ 🛑 '@Entry' macro can only attach to var declarations inside extensions of EnvironmentValues | ||
} | ||
""" | ||
} | ||
""" | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,30 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
#if canImport(SwiftSyntax600) | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
|
||
// Not complete, just enough to unit test lexical context. | ||
public struct EntryMacro: AccessorMacro { | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingAccessorsOf declaration: some DeclSyntaxProtocol, | ||
in context: some MacroExpansionContext | ||
) throws -> [AccessorDeclSyntax] { | ||
let isInEnvironmentValues = context.lexicalContext.contains { lexicalContext in | ||
lexicalContext.as(ExtensionDeclSyntax.self)?.extendedType.trimmedDescription | ||
== "EnvironmentValues" | ||
} | ||
|
||
guard isInEnvironmentValues else { | ||
throw MacroExpansionErrorMessage( | ||
"'@Entry' macro can only attach to var declarations inside extensions of EnvironmentValues") | ||
} | ||
// Not complete, just enough to unit test lexical context. | ||
public struct EntryMacro: AccessorMacro { | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingAccessorsOf declaration: some DeclSyntaxProtocol, | ||
in context: some MacroExpansionContext | ||
) throws -> [AccessorDeclSyntax] { | ||
let isInEnvironmentValues = context.lexicalContext.contains { lexicalContext in | ||
lexicalContext.as(ExtensionDeclSyntax.self)?.extendedType.trimmedDescription | ||
== "EnvironmentValues" | ||
} | ||
|
||
return [ | ||
AccessorDeclSyntax(accessorSpecifier: .keyword(.get)) { | ||
"fatalError()" | ||
guard isInEnvironmentValues else { | ||
throw MacroExpansionErrorMessage( | ||
"'@Entry' macro can only attach to var declarations inside extensions of EnvironmentValues" | ||
) | ||
} | ||
] | ||
|
||
return [ | ||
AccessorDeclSyntax(accessorSpecifier: .keyword(.get)) { | ||
"fatalError()" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
#endif |