-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow indentation width to be set explicitly (#8)
This allows the user to circumvent the default indentation behavior. In particular, it can be used to avoid getting the default four-space indentation when expanding source that doesn’t contain any indentation to infer the indentation width from. Changes: * The `assertMacro` function, along with related functions, now accepts `indentationWidth` as a parameter (`Trivia`). * Introduces `IndentationWidthTests` to validate both the existing indentation behavior and the new explicit indentation width functionality.
- Loading branch information
Showing
2 changed files
with
171 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import MacroTesting | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
import XCTest | ||
|
||
private struct AddMemberMacro: MemberMacro { | ||
static func expansion( | ||
of node: AttributeSyntax, | ||
providingMembersOf declaration: some DeclGroupSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [DeclSyntax] { | ||
return ["let v: T"] | ||
} | ||
} | ||
|
||
final class IndentationWidthTests: XCTestCase { | ||
func testExpansionAddsMemberUsingDetectedIndentation() { | ||
assertMacro([AddMemberMacro.self]) { | ||
""" | ||
@AddMember | ||
struct S { | ||
let w: T | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
struct S { | ||
let w: T | ||
let v: T | ||
} | ||
""" | ||
} | ||
} | ||
|
||
func testExpansionAddsMemberToEmptyStructUsingDefaultIndentation() { | ||
assertMacro([AddMemberMacro.self]) { | ||
""" | ||
@AddMember | ||
struct S { | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
struct S { | ||
let v: T | ||
} | ||
""" | ||
} | ||
} | ||
|
||
func testExpansionAddsMemberToEmptyStructUsingTwoSpaceIndentation() { | ||
assertMacro( | ||
[AddMemberMacro.self], | ||
indentationWidth: .spaces(2) | ||
) { | ||
""" | ||
@AddMember | ||
struct S { | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
struct S { | ||
let v: T | ||
} | ||
""" | ||
} | ||
} | ||
|
||
func testExpansionAddsMemberToEmptyStructUsingTwoSpaceIndentation_withMacroTesting() { | ||
withMacroTesting( | ||
indentationWidth: .spaces(2), | ||
macros: [AddMemberMacro.self] | ||
) { | ||
assertMacro { | ||
""" | ||
@AddMember | ||
struct S { | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
struct S { | ||
let v: T | ||
} | ||
""" | ||
} | ||
} | ||
} | ||
|
||
func testExpansionAddsMemberUsingMistchedIndentation() { | ||
assertMacro( | ||
[AddMemberMacro.self], | ||
indentationWidth: .spaces(4) | ||
) { | ||
""" | ||
@AddMember | ||
struct S { | ||
let w: T | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
struct S { | ||
let w: T | ||
let v: T | ||
} | ||
""" | ||
} | ||
} | ||
} |