@@ -18,13 +18,30 @@ import SwiftSyntax
1818import XCTest
1919import _SwiftSyntaxTestSupport
2020
21+ fileprivate func assertFormatted< T: SyntaxProtocol > (
22+ tree: T ,
23+ expected: String ,
24+ using format: BasicFormat = BasicFormat ( indentationWidth: . spaces( 4 ) ) ,
25+ file: StaticString = #file,
26+ line: UInt = #line
27+ ) {
28+ assertStringsEqualWithDiff ( tree. formatted ( using: format) . description, expected, file: file, line: line)
29+ }
30+
2131fileprivate func assertFormatted(
2232 source: String ,
2333 expected: String ,
34+ using format: BasicFormat = BasicFormat ( indentationWidth: . spaces( 4 ) ) ,
2435 file: StaticString = #file,
2536 line: UInt = #line
2637) {
27- assertStringsEqualWithDiff ( Parser . parse ( source: source) . formatted ( ) . description, expected, file: file, line: line)
38+ assertFormatted (
39+ tree: Parser . parse ( source: source) ,
40+ expected: expected,
41+ using: format,
42+ file: file,
43+ line: line
44+ )
2845}
2946
3047final class BasicFormatTest : XCTestCase {
@@ -190,4 +207,57 @@ final class BasicFormatTest: XCTestCase {
190207 """
191208 )
192209 }
210+
211+ func testDontInsertTrailingWhitespaceIfNextTokenStartsWithLeadingWhitespace( ) {
212+ let tree = VariableDeclSyntax (
213+ bindingKeyword: . keyword( . var) ,
214+ bindings: PatternBindingListSyntax ( [
215+ PatternBindingSyntax (
216+ pattern: PatternSyntax ( IdentifierPatternSyntax ( identifier: . identifier( " x " ) ) ) ,
217+ typeAnnotation: TypeAnnotationSyntax (
218+ colon: . colonToken( trailingTrivia: . space) ,
219+ type: TypeSyntax ( SimpleTypeIdentifierSyntax ( name: . identifier( " Int " ) ) )
220+ ) ,
221+ accessor: PatternBindingSyntax . Accessor (
222+ AccessorBlockSyntax (
223+ leftBrace: . leftBraceToken( leadingTrivia: . space) ,
224+ accessors: AccessorListSyntax ( [ ] ) ,
225+ rightBrace: . rightBraceToken( leadingTrivia: . newline)
226+ )
227+ )
228+ )
229+ ] )
230+ )
231+ assertFormatted (
232+ tree: tree,
233+ expected: """
234+ var x: Int {
235+ }
236+ """
237+ )
238+ }
239+
240+ func testAccessor( ) {
241+ let source = """
242+ struct Point {
243+ var computed: Int {
244+ get { 0 }
245+ }
246+ }
247+ """
248+
249+ assertFormatted (
250+ source: source,
251+ expected: """
252+ struct Point {
253+ var computed: Int {
254+ get {
255+ 0
256+ }
257+ }
258+ }
259+ """ ,
260+ using: BasicFormat ( indentationWidth: . spaces( 2 ) )
261+ )
262+ }
193263}
0 commit comments