Skip to content

Commit

Permalink
Merge pull request #83 from TTOzzi/fix-behindPreprocessorFlag-bug
Browse files Browse the repository at this point in the history
Fix to search for 'Spyable' in AttributeListSyntax when checking behindPreprocessorFlag.
  • Loading branch information
Matejkob authored Feb 1, 2024
2 parents d4fb93e + 64c6561 commit 4e093c3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/SpyableMacro/Macro/SpyableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public enum SpyableMacro: PeerMacro {
extension DeclSyntaxProtocol {
/// - Returns: The preprocessor `flag` parameter that can be optionally provided via `@Spyable(flag:)`.
fileprivate var preprocessorFlag: String? {
self.as(ProtocolDeclSyntax.self)?.attributes.first?
self
.as(ProtocolDeclSyntax.self)?.attributes.first {
$0.as(AttributeSyntax.self)?.attributeName
.as(IdentifierTypeSyntax.self)?.name.text == "Spyable"
}?
.as(AttributeSyntax.self)?.arguments?
.as(LabeledExprListSyntax.self)?.first {
$0.label?.text == "behindPreprocessorFlag"
Expand Down
29 changes: 29 additions & 0 deletions Tests/SpyableMacroTests/Macro/UT_SpyableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,33 @@ final class UT_SpyableMacro: XCTestCase {
macros: sut
)
}

func testMacroWithFlagMultipleAttributes() {
let protocolDeclaration = """
public protocol ServiceProtocol {
var variable: Bool? { get set }
}
"""
assertMacroExpansion(
"""
@MainActor
@Spyable(behindPreprocessorFlag: "CUSTOM")
@available(*, deprecated)
\(protocolDeclaration)
""",
expandedSource: """
@MainActor
@available(*, deprecated)
\(protocolDeclaration)
#if CUSTOM
class ServiceProtocolSpy: ServiceProtocol {
var variable: Bool?
}
#endif
""",
macros: sut
)
}
}

0 comments on commit 4e093c3

Please sign in to comment.