|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +//==========================================================================// |
| 14 | +// IMPORTANT: The macros defined in this file are intended to test the // |
| 15 | +// behavior of MacroSystem. Many of them do not serve as good examples of // |
| 16 | +// how macros should be written. In particular, they often lack error // |
| 17 | +// handling because it is not needed in the few test cases in which these // |
| 18 | +// macros are invoked. // |
| 19 | +//==========================================================================// |
| 20 | + |
| 21 | +import SwiftSyntax |
| 22 | +import SwiftSyntaxMacros |
| 23 | +import SwiftSyntaxMacrosTestSupport |
| 24 | +import XCTest |
| 25 | + |
| 26 | +fileprivate struct ConstantOneGetter: AccessorMacro { |
| 27 | + static func expansion( |
| 28 | + of node: AttributeSyntax, |
| 29 | + providingAccessorsOf declaration: some DeclSyntaxProtocol, |
| 30 | + in context: some MacroExpansionContext |
| 31 | + ) throws -> [AccessorDeclSyntax] { |
| 32 | + return [ |
| 33 | + """ |
| 34 | + get { |
| 35 | + return 1 |
| 36 | + } |
| 37 | + """ |
| 38 | + ] |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +final class AccessorMacroTests: XCTestCase { |
| 43 | + private let indentationWidth: Trivia = .spaces(2) |
| 44 | + |
| 45 | + func testAccessorOnVariableDeclWithExistingGetter() { |
| 46 | + assertMacroExpansion( |
| 47 | + """ |
| 48 | + @constantOne |
| 49 | + var x: Int { |
| 50 | + return 42 |
| 51 | + } |
| 52 | + """, |
| 53 | + expandedSource: """ |
| 54 | + var x: Int { |
| 55 | + get { |
| 56 | + return 42 |
| 57 | + } |
| 58 | + get { |
| 59 | + return 1 |
| 60 | + } |
| 61 | + } |
| 62 | + """, |
| 63 | + macros: ["constantOne": ConstantOneGetter.self], |
| 64 | + indentationWidth: indentationWidth |
| 65 | + ) |
| 66 | + |
| 67 | + assertMacroExpansion( |
| 68 | + """ |
| 69 | + struct Foo { |
| 70 | + @constantOne |
| 71 | + var x: Int { |
| 72 | + return 42 |
| 73 | + } |
| 74 | + } |
| 75 | + """, |
| 76 | + expandedSource: """ |
| 77 | + struct Foo { |
| 78 | + var x: Int { |
| 79 | + get { |
| 80 | + return 42 |
| 81 | + } |
| 82 | + get { |
| 83 | + return 1 |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + """, |
| 88 | + macros: ["constantOne": ConstantOneGetter.self], |
| 89 | + indentationWidth: indentationWidth |
| 90 | + ) |
| 91 | + |
| 92 | + assertMacroExpansion( |
| 93 | + """ |
| 94 | + @constantOne |
| 95 | + var x: Int { |
| 96 | + get { |
| 97 | + return 42 |
| 98 | + } |
| 99 | + } |
| 100 | + """, |
| 101 | + expandedSource: """ |
| 102 | + var x: Int { |
| 103 | + get { |
| 104 | + return 42 |
| 105 | + } |
| 106 | + get { |
| 107 | + return 1 |
| 108 | + } |
| 109 | + } |
| 110 | + """, |
| 111 | + macros: ["constantOne": ConstantOneGetter.self], |
| 112 | + indentationWidth: indentationWidth |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + func testAccessorOnSubscript() { |
| 117 | + // Adding an accessor to a subscript without an accessor isn't supported by |
| 118 | + // the compiler (it complains that the subscript should have a body) but we |
| 119 | + // can stil make the most reasonable syntactic expansion. |
| 120 | + assertMacroExpansion( |
| 121 | + """ |
| 122 | + struct Foo { |
| 123 | + @constantOne |
| 124 | + subscript() -> Int |
| 125 | + } |
| 126 | + """, |
| 127 | + expandedSource: """ |
| 128 | + struct Foo { |
| 129 | + subscript() -> Int { |
| 130 | + get { |
| 131 | + return 1 |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + """, |
| 136 | + macros: ["constantOne": ConstantOneGetter.self], |
| 137 | + indentationWidth: indentationWidth |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + func testAccessorOnSubscriptDeclWithExistingGetter() { |
| 142 | + assertMacroExpansion( |
| 143 | + """ |
| 144 | + struct Foo { |
| 145 | + @constantOne |
| 146 | + subscript() -> Int { |
| 147 | + return 42 |
| 148 | + } |
| 149 | + } |
| 150 | + """, |
| 151 | + expandedSource: """ |
| 152 | + struct Foo { |
| 153 | + subscript() -> Int { |
| 154 | + get { |
| 155 | + return 42 |
| 156 | + } |
| 157 | + get { |
| 158 | + return 1 |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + """, |
| 163 | + macros: ["constantOne": ConstantOneGetter.self], |
| 164 | + indentationWidth: indentationWidth |
| 165 | + ) |
| 166 | + |
| 167 | + assertMacroExpansion( |
| 168 | + """ |
| 169 | + struct Foo { |
| 170 | + @constantOne |
| 171 | + subscript() -> Int { |
| 172 | + return 42 |
| 173 | + } |
| 174 | + } |
| 175 | + """, |
| 176 | + expandedSource: """ |
| 177 | + struct Foo { |
| 178 | + subscript() -> Int { |
| 179 | + get { |
| 180 | + return 42 |
| 181 | + } |
| 182 | + get { |
| 183 | + return 1 |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + """, |
| 188 | + macros: ["constantOne": ConstantOneGetter.self], |
| 189 | + indentationWidth: indentationWidth |
| 190 | + ) |
| 191 | + |
| 192 | + assertMacroExpansion( |
| 193 | + """ |
| 194 | + struct Foo { |
| 195 | + @constantOne |
| 196 | + subscript() -> Int { |
| 197 | + get { |
| 198 | + return 42 |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + """, |
| 203 | + expandedSource: """ |
| 204 | + struct Foo { |
| 205 | + subscript() -> Int { |
| 206 | + get { |
| 207 | + return 42 |
| 208 | + } |
| 209 | + get { |
| 210 | + return 1 |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + """, |
| 215 | + macros: ["constantOne": ConstantOneGetter.self], |
| 216 | + indentationWidth: indentationWidth |
| 217 | + ) |
| 218 | + } |
| 219 | + |
| 220 | + func testAccessorOnVariableDeclWithMultipleBindings() { |
| 221 | + assertMacroExpansion( |
| 222 | + """ |
| 223 | + @constantOneGetter |
| 224 | + var x: Int, y: Int |
| 225 | + """, |
| 226 | + expandedSource: """ |
| 227 | + var x: Int, y: Int |
| 228 | + """, |
| 229 | + diagnostics: [ |
| 230 | + DiagnosticSpec( |
| 231 | + message: |
| 232 | + "swift-syntax applies macros syntactically and there is no way to represent a variable declaration with multiple bindings that have accessors syntactically. While the compiler allows this expansion, swift-syntax cannot represent it and thus disallows it.", |
| 233 | + line: 1, |
| 234 | + column: 1, |
| 235 | + severity: .error |
| 236 | + ) |
| 237 | + ], |
| 238 | + macros: ["constantOneGetter": ConstantOneGetter.self], |
| 239 | + indentationWidth: indentationWidth |
| 240 | + ) |
| 241 | + } |
| 242 | + |
| 243 | + func testMultipleAccessorMacros() { |
| 244 | + assertMacroExpansion( |
| 245 | + """ |
| 246 | + @constantOne |
| 247 | + @constantOne |
| 248 | + var x: Int |
| 249 | + """, |
| 250 | + expandedSource: """ |
| 251 | + var x: Int { |
| 252 | + get { |
| 253 | + return 1 |
| 254 | + } |
| 255 | + get { |
| 256 | + return 1 |
| 257 | + } |
| 258 | + } |
| 259 | + """, |
| 260 | + macros: ["constantOne": ConstantOneGetter.self], |
| 261 | + indentationWidth: indentationWidth |
| 262 | + ) |
| 263 | + } |
| 264 | +} |
0 commit comments