From cd981c6e3b7f360eb134ae311d9bbeb9a62fdd93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Galen=20O=E2=80=99Hanlon?= Date: Mon, 30 Sep 2024 16:15:59 -0700 Subject: [PATCH] Guard EntryMacro tests with `SwiftSyntax600` availability check Tests cover new lexical context feature of SwiftSyntaxMacros. --- Tests/MacroTestingTests/EntryMacroTests.swift | 84 ++++++++++--------- .../MacroExamples/EntryMacro.swift | 49 ++++++----- 2 files changed, 69 insertions(+), 64 deletions(-) diff --git a/Tests/MacroTestingTests/EntryMacroTests.swift b/Tests/MacroTestingTests/EntryMacroTests.swift index 057ac35..e441d9b 100644 --- a/Tests/MacroTestingTests/EntryMacroTests.swift +++ b/Tests/MacroTestingTests/EntryMacroTests.swift @@ -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 diff --git a/Tests/MacroTestingTests/MacroExamples/EntryMacro.swift b/Tests/MacroTestingTests/MacroExamples/EntryMacro.swift index 17c94fc..a1f12e9 100644 --- a/Tests/MacroTestingTests/MacroExamples/EntryMacro.swift +++ b/Tests/MacroTestingTests/MacroExamples/EntryMacro.swift @@ -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