Skip to content

Commit 4c39fbb

Browse files
Add test cases that document formatting picks up .swift-format configuration files from the workspace
Co-Authored-By: cukr <[email protected]>
1 parent 8666a8d commit 4c39fbb

File tree

9 files changed

+173
-0
lines changed

9 files changed

+173
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This file is intentionally an invalid swift-format configuration file. It is used to test
2+
the behavior when program cannot find a valid file. We cannot just left this directory blank,
3+
because swift-format searches for the configuration in parent directories, and it's possible
4+
that user has another configuration file somewhere outside the Tests.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 1,
3+
"indentation": {
4+
"spaces": 1
5+
},
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*Directory*/
2+
struct Directory {
3+
var bar = 123
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 1,
3+
"indentation": {
4+
"spaces": 4
5+
},
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*NestedWithConfig*/
2+
struct NestedWithConfig {
3+
var bar = 123
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*NestedWithoutConfig*/
2+
struct NestedWithoutConfig {
3+
var bar = 123
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*Root*/
2+
struct Root {
3+
var bar = 123
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"sources": [
3+
"Root.swift",
4+
"Directory/Directory.swift",
5+
"Directory/NestedWithConfig/NestedWithConfig.swift",
6+
"Directory/NestedWithoutConfig/NestedWithoutConfig.swift"
7+
]
8+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LanguageServerProtocol
14+
import SKTestSupport
15+
import XCTest
16+
import ISDBTestSupport
17+
18+
// Note that none of the indentation values choosen are equal to the default
19+
// indentation level, which is two spaces.
20+
final class WorkspaceFormattingTests: XCTestCase {
21+
var workspace: SKTibsTestWorkspace! = nil
22+
23+
override func setUp() {
24+
workspace = try! XCTUnwrap(staticSourceKitTibsWorkspace(name: "Formatting"))
25+
try! workspace.buildAndIndex()
26+
try! workspace.openDocument(workspace.testLoc("Root").url, language: .swift)
27+
try! workspace.openDocument(workspace.testLoc("Directory").url, language: .swift)
28+
try! workspace.openDocument(workspace.testLoc("NestedWithConfig").url, language: .swift)
29+
try! workspace.openDocument(workspace.testLoc("NestedWithoutConfig").url, language: .swift)
30+
}
31+
32+
override func tearDown() {
33+
workspace = nil
34+
}
35+
36+
func performFormattingRequest(file url: URL, options: FormattingOptions) throws -> [TextEdit]? {
37+
let request = DocumentFormattingRequest(
38+
textDocument: TextDocumentIdentifier(url),
39+
options: options
40+
)
41+
return try workspace.sk.sendSync(request)
42+
}
43+
44+
func testSpaces() throws {
45+
let url = workspace.testLoc("Root").url
46+
let options = FormattingOptions(tabSize: 3, insertSpaces: true)
47+
let edits = try XCTUnwrap(performFormattingRequest(file: url, options: options))
48+
XCTAssertEqual(edits.count, 1)
49+
let edit = try XCTUnwrap(edits.first)
50+
XCTAssertEqual(edit.range, Position(line: 0, utf16index: 0)..<Position(line: 3, utf16index: 1))
51+
// var bar needs to be indented with three spaces
52+
// which is the value from lsp
53+
XCTAssertEqual(edit.newText, """
54+
/*Root*/
55+
struct Root {
56+
var bar = 123
57+
}
58+
59+
""")
60+
}
61+
62+
func testTabs() throws {
63+
let url = workspace.testLoc("Root").url
64+
let options = FormattingOptions(tabSize: 3, insertSpaces: false)
65+
let edits = try XCTUnwrap(performFormattingRequest(file: url, options: options))
66+
XCTAssertEqual(edits.count, 1)
67+
let edit = try XCTUnwrap(edits.first)
68+
XCTAssertEqual(edit.range, Position(line: 0, utf16index: 0)..<Position(line: 3, utf16index: 1))
69+
// var bar needs to be indented with a tab
70+
// which is the value from lsp
71+
XCTAssertEqual(edit.newText, """
72+
/*Root*/
73+
struct Root {
74+
\tvar bar = 123
75+
}
76+
77+
""")
78+
}
79+
80+
func testConfigFile() throws {
81+
let url = workspace.testLoc("Directory").url
82+
let options = FormattingOptions(tabSize: 3, insertSpaces: true)
83+
let edits = try XCTUnwrap(performFormattingRequest(file: url, options: options))
84+
XCTAssertEqual(edits.count, 1)
85+
let edit = try XCTUnwrap(edits.first)
86+
XCTAssertEqual(edit.range, Position(line: 0, utf16index: 0)..<Position(line: 3, utf16index: 1))
87+
// var bar needs to be indented with one space
88+
// which is the value from ".swift-format" in "Directory"
89+
XCTAssertEqual(edit.newText, """
90+
/*Directory*/
91+
struct Directory {
92+
var bar = 123
93+
}
94+
95+
""")
96+
}
97+
98+
func testConfigFileInParentDirectory() throws {
99+
let url = workspace.testLoc("NestedWithoutConfig").url
100+
let options = FormattingOptions(tabSize: 3, insertSpaces: true)
101+
let edits = try XCTUnwrap(performFormattingRequest(file: url, options: options))
102+
XCTAssertEqual(edits.count, 1)
103+
let edit = try XCTUnwrap(edits.first)
104+
XCTAssertEqual(edit.range, Position(line: 0, utf16index: 0)..<Position(line: 3, utf16index: 1))
105+
// var bar needs to be indented with one space
106+
// which is the value from ".swift-format" in "Directory"
107+
XCTAssertEqual(edit.newText, """
108+
/*NestedWithoutConfig*/
109+
struct NestedWithoutConfig {
110+
var bar = 123
111+
}
112+
113+
""")
114+
}
115+
116+
func testConfigFileInNestedDirectory() throws {
117+
let url = workspace.testLoc("NestedWithConfig").url
118+
let options = FormattingOptions(tabSize: 3, insertSpaces: true)
119+
let edits = try XCTUnwrap(performFormattingRequest(file: url, options: options))
120+
XCTAssertEqual(edits.count, 1)
121+
let edit = try XCTUnwrap(edits.first)
122+
XCTAssertEqual(edit.range, Position(line: 0, utf16index: 0)..<Position(line: 3, utf16index: 1))
123+
// var bar needs to be indented with four spaces
124+
// which is the value from ".swift-format" in "NestedWithConfig"
125+
XCTAssertEqual(edit.newText, """
126+
/*NestedWithConfig*/
127+
struct NestedWithConfig {
128+
var bar = 123
129+
}
130+
131+
""")
132+
}
133+
}

0 commit comments

Comments
 (0)