Skip to content

Commit

Permalink
Guard EntryMacro tests with SwiftSyntax600 availability check
Browse files Browse the repository at this point in the history
Tests cover new lexical context feature of SwiftSyntaxMacros.
  • Loading branch information
gohanlon committed Sep 30, 2024
1 parent a78f06f commit cd981c6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 64 deletions.
84 changes: 43 additions & 41 deletions Tests/MacroTestingTests/EntryMacroTests.swift
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
49 changes: 26 additions & 23 deletions Tests/MacroTestingTests/MacroExamples/EntryMacro.swift
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

0 comments on commit cd981c6

Please sign in to comment.