Skip to content

Commit cd73ad2

Browse files
committed
Add simple test coverage
1 parent 3115f96 commit cd73ad2

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ let package = Package(
208208
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftParser", "SwiftSyntaxMacros", "SwiftSyntaxMacroExpansion"]
209209
),
210210

211+
.testTarget(
212+
name: "SwiftSyntaxMacrosTestSupportTests",
213+
dependencies: ["SwiftDiagnostics", "SwiftSyntax", "SwiftSyntaxMacros", "SwiftSyntaxMacrosTestSupport"]
214+
),
215+
211216
// MARK: SwiftParser
212217

213218
.target(

Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public struct DiagnosticSpec {
128128
/// The expected severity of the diagnostic
129129
public let severity: DiagnosticSeverity
130130

131-
/// If not `nil`, the text the diagnostic is expected to highlight
131+
/// If not `nil`, the text fragments the diagnostic is expected to highlight
132132
public let highlights: [String]?
133133

134134
/// The notes that are expected to be attached to the diagnostic
@@ -237,7 +237,7 @@ func assertDiagnostic(
237237
XCTFail(
238238
"""
239239
Expected \(highlights.count) highlights but received \(diag.highlights.count):
240-
\(diag.highlights.map(\.debugDescription).joined(separator: "\n"))
240+
\(diag.highlights.map(\.trimmedDescription).joined(separator: "\n"))
241241
""",
242242
file: spec.originatorFile,
243243
line: spec.originatorLine
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)