1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//==========================================================================//
// IMPORTANT: The macros defined in this file are intended to test the //
// behavior of MacroSystem. Many of them do not serve as good examples of //
// how macros should be written. In particular, they often lack error //
// handling because it is not needed in the few test cases in which these //
// macros are invoked. //
//==========================================================================//
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest
fileprivate struct DeclsFromStringsMacro: DeclarationMacro {
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
var strings: [String] = []
for arg in node.arguments {
guard let value = arg.expression.as(StringLiteralExprSyntax.self)?.representedLiteralValue else {
continue
}
strings.append(value)
}
return strings.map { "\(raw: $0)" }
}
}
final class CodeItemMacroTests: XCTestCase {
private let indentationWidth: Trivia = .spaces(2)
func testUnwrap() {
struct UnwrapMacro: CodeItemMacro {
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> [CodeBlockItemSyntax] {
guard !node.arguments.isEmpty else {
throw SwiftSyntaxMacros.MacroExpansionErrorMessage("'#unwrap' requires arguments")
}
let errorThrower = node.trailingClosure
let identifiers = try node.arguments.map { argument in
guard let declReferenceExpr = argument.expression.as(DeclReferenceExprSyntax.self) else {
throw SwiftSyntaxMacros.MacroExpansionErrorMessage("Arguments must be identifiers")
}
return declReferenceExpr.baseName
}
func elseBlock(_ token: TokenSyntax) -> CodeBlockSyntax {
let expr: ExprSyntax
if let errorThrower {
expr = """
\(errorThrower)("\(raw: token.text)")
"""
} else {
expr = """
fatalError("'\(raw: token.text)' is nil")
"""
}
return .init(
statements: .init([
.init(
leadingTrivia: " ",
item: .expr(expr),
trailingTrivia: " "
)
])
)
}
return identifiers.map { (identifier) -> CodeBlockItemSyntax in
"guard let \(raw: identifier.text) else \(elseBlock(identifier))"
}
}
}
assertMacroExpansion(
#"""
let x: Int? = 1
let y: Int? = nil
let z: Int? = 3
#unwrap(x, y, z)
#unwrap(x, y, z) {
fatalError("nil is \\($0)")
}
"""#,
expandedSource: #"""
let x: Int? = 1
let y: Int? = nil
let z: Int? = 3
guard let x else {
fatalError("'x' is nil")
}
guard let y else {
fatalError("'y' is nil")
}
guard let z else {
fatalError("'z' is nil")
}
guard let x else {
{
fatalError("nil is \\($0)")
}("x")
}
guard let y else {
{
fatalError("nil is \\($0)")
}("y")
}
guard let z else {
{
fatalError("nil is \\($0)")
}("z")
}
"""#,
macros: ["unwrap": UnwrapMacro.self],
indentationWidth: indentationWidth
)
}
func testEmpty() {
struct TestMacro: CodeItemMacro {
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> [CodeBlockItemSyntax] {
return []
}
}
assertMacroExpansion(
"#test",
expandedSource: "",
macros: [
"test": TestMacro.self
]
)
}
}
|