Skip to content

Commit e24030d

Browse files
committed
Refactor a bit and add a unit test
1 parent 9be37f4 commit e24030d

File tree

3 files changed

+78
-18
lines changed

3 files changed

+78
-18
lines changed

Sources/TestingMacros/PragmaMacro.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,46 @@ public struct PragmaMacro: PeerMacro, Sendable {
3434
.disabled
3535
}
3636
}
37+
38+
/// Get all pragma attributes (`@__testing`) associated with a syntax node.
39+
///
40+
/// - Parameters:
41+
/// - node: The syntax node to inspect.
42+
///
43+
/// - Returns: The set of pragma attributes strings associated with `node`.
44+
///
45+
/// Attributes conditionally applied with `#if` are ignored.
46+
func pragmas(on node: some WithAttributesSyntax) -> [AttributeSyntax] {
47+
node.attributes
48+
.compactMap { attribute in
49+
if case let .attribute(attribute) = attribute {
50+
return attribute
51+
}
52+
return nil
53+
}.filter { attribute in
54+
attribute.attributeNameText == "__testing"
55+
}
56+
}
57+
58+
/// Get all "semantics" attributed to a syntax node using the
59+
/// `@__testing(semantics:)` attribute.
60+
///
61+
/// - Parameters:
62+
/// - node: The syntax node to inspect.
63+
///
64+
/// - Returns: The set of "semantics" strings associated with `node`.
65+
///
66+
/// Attributes conditionally applied with `#if` are ignored.
67+
func semantics(of node: some WithAttributesSyntax) -> [String] {
68+
pragmas(on: node)
69+
.compactMap { attribute in
70+
if case let .argumentList(arguments) = attribute.arguments {
71+
return arguments
72+
}
73+
return nil
74+
}.filter { arguments in
75+
arguments.first?.label?.textWithoutBackticks == "semantics"
76+
}.flatMap { argument in
77+
argument.compactMap { $0.expression.as(StringLiteralExprSyntax.self)?.representedLiteralValue }
78+
}
79+
}

Sources/TestingMacros/Support/Additions/MacroExpansionContextAdditions.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,13 @@ extension MacroExpansionContext {
9191
/// testing library. It may be modified or removed in a future update.
9292
var areWarningsSuppressed: Bool {
9393
#if DEBUG
94-
for lexicalContext in self.lexicalContext {
95-
guard let lexicalContext = lexicalContext.asProtocol((any WithAttributesSyntax).self) else {
96-
continue
97-
}
98-
for attribute in lexicalContext.attributes {
99-
if case let .attribute(attribute) = attribute,
100-
attribute.attributeNameText == "__testing",
101-
case let .argumentList(arguments) = attribute.arguments {
102-
return arguments.contains { argument in
103-
guard let argument = arguments.first?.expression.as(StringLiteralExprSyntax.self) else {
104-
return false
105-
}
106-
return argument.representedLiteralValue == "nomacrowarnings"
107-
}
108-
}
109-
}
110-
}
111-
#endif
94+
return lexicalContext
95+
.compactMap { $0.asProtocol((any WithAttributesSyntax).self) }
96+
.flatMap { semantics(of: $0) }
97+
.contains("nomacrowarnings")
98+
#else
11299
return false
100+
#endif
113101
}
114102

115103
/// Emit a diagnostic message.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// This source file is part of the Swift.org open source project
3+
//
4+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Licensed under Apache License v2.0 with Runtime Library Exception
6+
//
7+
// See https://swift.org/LICENSE.txt for license information
8+
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
//
10+
11+
import Testing
12+
@testable import TestingMacros
13+
14+
import SwiftParser
15+
import SwiftSyntax
16+
17+
@Suite("PragmaMacro Tests")
18+
struct PragmaMacroTests {
19+
@Test func findSemantics() throws {
20+
let node = """
21+
@__testing(semantics: "abc123")
22+
@__testing(semantics: "def456")
23+
let x = 0
24+
""" as DeclSyntax
25+
let nodeWithAttributes = try #require(node.asProtocol((any WithAttributesSyntax).self))
26+
let semantics = semantics(of: nodeWithAttributes)
27+
#expect(semantics == ["abc123", "def456"])
28+
}
29+
}

0 commit comments

Comments
 (0)