|
| 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 | +import SwiftDiagnostics |
| 14 | +import SwiftSyntax |
| 15 | +import SwiftSyntaxMacros |
| 16 | +import SwiftSyntaxMacrosTestSupport |
| 17 | +import XCTest |
| 18 | + |
| 19 | +final class AssertionsTests: XCTestCase { |
| 20 | + struct OnlyStruct: DiagnosticMessage { |
| 21 | + var message = "'@NoStruct' cannot be applied to struct types" |
| 22 | + var diagnosticID = MessageID(domain: "\(AssertionsTests.self)", id: "\(OnlyStruct.self)") |
| 23 | + var severity = DiagnosticSeverity.error |
| 24 | + } |
| 25 | + |
| 26 | + struct NoStruct: MemberMacro { |
| 27 | + static func expansion( |
| 28 | + of node: AttributeSyntax, |
| 29 | + providingMembersOf declaration: some DeclGroupSyntax, |
| 30 | + conformingTo protocols: [TypeSyntax], |
| 31 | + in context: some MacroExpansionContext |
| 32 | + ) throws -> [DeclSyntax] { |
| 33 | + if let structDecl = declaration.as(StructDeclSyntax.self) { |
| 34 | + context.diagnose( |
| 35 | + .init( |
| 36 | + node: structDecl.structKeyword, |
| 37 | + message: OnlyStruct() |
| 38 | + ) |
| 39 | + ) |
| 40 | + } |
| 41 | + return [] |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + struct NoStructMultiHighlight: MemberMacro { |
| 46 | + static func expansion( |
| 47 | + of node: AttributeSyntax, |
| 48 | + providingMembersOf declaration: some DeclGroupSyntax, |
| 49 | + conformingTo protocols: [TypeSyntax], |
| 50 | + in context: some MacroExpansionContext |
| 51 | + ) throws -> [DeclSyntax] { |
| 52 | + if let structDecl = declaration.as(StructDeclSyntax.self) { |
| 53 | + context.diagnose( |
| 54 | + .init( |
| 55 | + node: structDecl.structKeyword, |
| 56 | + message: OnlyStruct(), |
| 57 | + highlights: [Syntax(structDecl.structKeyword), Syntax(structDecl.name)] |
| 58 | + ) |
| 59 | + ) |
| 60 | + } |
| 61 | + return [] |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + func testAssertMacroExpansionIgnoresHighlightMatchingIfNil() { |
| 66 | + assertMacroExpansion( |
| 67 | + "@NoStruct struct S { }", |
| 68 | + expandedSource: "struct S { }", |
| 69 | + diagnostics: [ |
| 70 | + .init(message: OnlyStruct().message, line: 1, column: 11) |
| 71 | + ], |
| 72 | + macros: ["NoStruct": NoStruct.self] |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + func testAssertMacroExpansionMatchesSingleStringHighlight() { |
| 77 | + assertMacroExpansion( |
| 78 | + "@NoStruct struct S { }", |
| 79 | + expandedSource: "struct S { }", |
| 80 | + diagnostics: [ |
| 81 | + .init(message: OnlyStruct().message, line: 1, column: 11, highlight: "struct") |
| 82 | + ], |
| 83 | + macros: ["NoStruct": NoStruct.self] |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + func testAssertMacroExpansionMatchesArrayHighlight() { |
| 88 | + assertMacroExpansion( |
| 89 | + "@NoStruct struct S { }", |
| 90 | + expandedSource: "struct S { }", |
| 91 | + diagnostics: [ |
| 92 | + .init(message: OnlyStruct().message, line: 1, column: 11, highlight: "struct") |
| 93 | + ], |
| 94 | + macros: ["NoStruct": NoStruct.self] |
| 95 | + ) |
| 96 | + |
| 97 | + assertMacroExpansion( |
| 98 | + "@NoStruct struct S { }", |
| 99 | + expandedSource: "struct S { }", |
| 100 | + diagnostics: [ |
| 101 | + .init(message: OnlyStruct().message, line: 1, column: 11, highlights: ["struct", "S"]) |
| 102 | + ], |
| 103 | + macros: ["NoStruct": NoStructMultiHighlight.self] |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
0 commit comments