@@ -15,39 +15,46 @@ import SKTestSupport
15
15
import XCTest
16
16
import ISDBTestSupport
17
17
18
+
19
+ // Note that none of the indentation values choosen are equal to the default
20
+ // indentation level, which is two spaces.
18
21
final class FormattingTests : XCTestCase {
19
22
var workspace : SKTibsTestWorkspace ! = nil
20
23
21
24
func initialize( ) throws {
22
- workspace = try staticSourceKitTibsWorkspace ( name: " Formatting " ) !
25
+ workspace = try XCTUnwrap ( staticSourceKitTibsWorkspace ( name: " Formatting " ) )
23
26
try workspace. buildAndIndex ( )
24
- try workspace. openDocument ( workspace. testLoc ( " a.swift " ) . url, language: . swift)
27
+ try workspace. openDocument ( workspace. testLoc ( " Root " ) . url, language: . swift)
28
+ try workspace. openDocument ( workspace. testLoc ( " Directory " ) . url, language: . swift)
29
+ try workspace. openDocument ( workspace. testLoc ( " NestedWithConfig " ) . url, language: . swift)
30
+ try workspace. openDocument ( workspace. testLoc ( " NestedWithoutConfig " ) . url, language: . swift)
25
31
}
26
32
override func tearDown( ) {
27
33
workspace = nil
28
34
}
29
35
30
- func performFormattingRequest( file url: URL , options: FormattingOptions ) -> [ TextEdit ] ? {
36
+ func performFormattingRequest( file url: URL , options: FormattingOptions ) throws -> [ TextEdit ] ? {
31
37
let request = DocumentFormattingRequest (
32
38
textDocument: TextDocumentIdentifier ( url) ,
33
39
options: options
34
40
)
35
- return try ! workspace. sk. sendSync ( request) !
41
+ return try workspace. sk. sendSync ( request)
36
42
}
37
43
38
44
func testSpaces( ) throws {
39
45
XCTAssertNoThrow ( try initialize ( ) )
40
- let url = workspace. testLoc ( " a.swift " ) . url
46
+ let url = workspace. testLoc ( " Root " ) . url
41
47
let options = FormattingOptions ( tabSize: 3 , insertSpaces: true )
42
48
let edits = try XCTUnwrap ( performFormattingRequest ( file: url, options: options) )
43
49
XCTAssertEqual ( edits. count, 1 )
44
50
let firstEdit = try XCTUnwrap ( edits. first)
45
51
XCTAssertEqual ( firstEdit. range. lowerBound, Position ( line: 0 , utf16index: 0 ) )
46
52
XCTAssertEqual ( firstEdit. range. upperBound, Position ( line: 3 , utf16index: 1 ) )
47
53
// var bar needs to be indented with three spaces
54
+ // which is the value from lsp
48
55
XCTAssertEqual ( firstEdit. newText, #"""
49
- /*a.swift */
50
- struct Foo {
56
+ /*Root */
57
+ struct Root {
51
58
var bar = 123
52
59
}
53
60
@@ -56,20 +63,81 @@ final class FormattingTests: XCTestCase {
56
63
57
64
func testTabs( ) throws {
58
65
try initialize ( )
59
- let url = workspace. testLoc ( " a.swift " ) . url
66
+ let url = workspace. testLoc ( " Root " ) . url
60
67
let options = FormattingOptions ( tabSize: 3 , insertSpaces: false )
61
68
let edits = try XCTUnwrap ( performFormattingRequest ( file: url, options: options) )
62
69
XCTAssertEqual ( edits. count, 1 )
63
70
let firstEdit = try XCTUnwrap ( edits. first)
64
71
XCTAssertEqual ( firstEdit. range. lowerBound, Position ( line: 0 , utf16index: 0 ) )
65
72
XCTAssertEqual ( firstEdit. range. upperBound, Position ( line: 3 , utf16index: 1 ) )
66
73
// var bar needs to be indented with a tab
74
+ // which is the value from lsp
67
75
XCTAssertEqual ( firstEdit. newText, #"""
68
- /*a.swift */
69
- struct Foo {
76
+ /*Root */
77
+ struct Root {
70
78
\#tvar bar = 123
71
79
}
72
80
73
81
"""# )
74
82
}
83
+
84
+ func testConfigFile( ) throws {
85
+ XCTAssertNoThrow ( try initialize ( ) )
86
+ let url = workspace. testLoc ( " Directory " ) . url
87
+ let options = FormattingOptions ( tabSize: 3 , insertSpaces: true )
88
+ let edits = try XCTUnwrap ( performFormattingRequest ( file: url, options: options) )
89
+ XCTAssertEqual ( edits. count, 1 )
90
+ let firstEdit = try XCTUnwrap ( edits. first)
91
+ XCTAssertEqual ( firstEdit. range. lowerBound, Position ( line: 0 , utf16index: 0 ) )
92
+ XCTAssertEqual ( firstEdit. range. upperBound, Position ( line: 3 , utf16index: 1 ) )
93
+ // var bar needs to be indented with one space
94
+ // which is the value from ".swift-format" in "Directory"
95
+ XCTAssertEqual ( firstEdit. newText, #"""
96
+ /*Directory*/
97
+ struct Directory {
98
+ var bar = 123
99
+ }
100
+
101
+ """# )
102
+ }
103
+
104
+ func testConfigFileInParentDirectory( ) throws {
105
+ XCTAssertNoThrow ( try initialize ( ) )
106
+ let url = workspace. testLoc ( " NestedWithoutConfig " ) . url
107
+ let options = FormattingOptions ( tabSize: 3 , insertSpaces: true )
108
+ let edits = try XCTUnwrap ( performFormattingRequest ( file: url, options: options) )
109
+ XCTAssertEqual ( edits. count, 1 )
110
+ let firstEdit = try XCTUnwrap ( edits. first)
111
+ XCTAssertEqual ( firstEdit. range. lowerBound, Position ( line: 0 , utf16index: 0 ) )
112
+ XCTAssertEqual ( firstEdit. range. upperBound, Position ( line: 3 , utf16index: 1 ) )
113
+ // var bar needs to be indented with one space
114
+ // which is the value from ".swift-format" in "Directory"
115
+ XCTAssertEqual ( firstEdit. newText, #"""
116
+ /*NestedWithoutConfig*/
117
+ struct NestedWithoutConfig {
118
+ var bar = 123
119
+ }
120
+
121
+ """# )
122
+ }
123
+
124
+ func testConfigFileInNestedDirectory( ) throws {
125
+ XCTAssertNoThrow ( try initialize ( ) )
126
+ let url = workspace. testLoc ( " NestedWithConfig " ) . url
127
+ let options = FormattingOptions ( tabSize: 3 , insertSpaces: true )
128
+ let edits = try XCTUnwrap ( performFormattingRequest ( file: url, options: options) )
129
+ XCTAssertEqual ( edits. count, 1 )
130
+ let firstEdit = try XCTUnwrap ( edits. first)
131
+ XCTAssertEqual ( firstEdit. range. lowerBound, Position ( line: 0 , utf16index: 0 ) )
132
+ XCTAssertEqual ( firstEdit. range. upperBound, Position ( line: 3 , utf16index: 1 ) )
133
+ // var bar needs to be indented with four spaces
134
+ // which is the value from ".swift-format" in "NestedWithConfig"
135
+ XCTAssertEqual ( firstEdit. newText, #"""
136
+ /*NestedWithConfig*/
137
+ struct NestedWithConfig {
138
+ var bar = 123
139
+ }
140
+
141
+ """# )
142
+ }
75
143
}
0 commit comments