Skip to content

Commit 016a0b1

Browse files
authored
Merge pull request #2283 from natecook1000/formatting-tests
2 parents 37aafd3 + 7acb34e commit 016a0b1

File tree

3 files changed

+81
-63
lines changed

3 files changed

+81
-63
lines changed

Tests/SourceKitLSPTests/FormattingTests.swift

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ final class FormattingTests: XCTestCase {
2525
let testClient = try await TestSourceKitLSPClient()
2626
let uri = DocumentURI(for: .swift)
2727

28-
let positions = testClient.openDocument(
29-
"""
28+
let source = """
3029
struct S {
31-
1️⃣var foo: 2️⃣ 3️⃣Int
32-
4️⃣var bar: Int
33-
}5️⃣
34-
""",
35-
uri: uri
36-
)
30+
var foo: Int
31+
var bar: Int
32+
}
33+
"""
34+
testClient.openDocument(source, uri: uri)
3735

3836
let response = try await testClient.send(
3937
DocumentFormattingRequest(
@@ -43,14 +41,18 @@ final class FormattingTests: XCTestCase {
4341
)
4442

4543
let edits = try XCTUnwrap(response)
44+
let formattedSource = apply(edits: edits, to: source)
45+
46+
XCTAssert(edits.allSatisfy { $0.newText.allSatisfy(\.isWhitespace) })
4647
XCTAssertEqual(
47-
edits,
48-
[
49-
TextEdit(range: Range(positions["1️⃣"]), newText: " "),
50-
TextEdit(range: positions["2️⃣"]..<positions["3️⃣"], newText: ""),
51-
TextEdit(range: Range(positions["4️⃣"]), newText: " "),
52-
TextEdit(range: Range(positions["5️⃣"]), newText: "\n"),
53-
]
48+
formattedSource,
49+
"""
50+
struct S {
51+
var foo: Int
52+
var bar: Int
53+
}
54+
55+
"""
5456
)
5557
}
5658

@@ -217,15 +219,13 @@ final class FormattingTests: XCTestCase {
217219
let testClient = try await TestSourceKitLSPClient()
218220
let uri = DocumentURI(for: .swift)
219221

220-
let positions = testClient.openDocument(
221-
"""
222-
1️⃣public 2️⃣extension Example {
223-
3️⃣func function() {}
222+
let source = """
223+
public extension Example {
224+
func function() {}
224225
}
225226
226-
""",
227-
uri: uri
228-
)
227+
"""
228+
testClient.openDocument(source, uri: uri)
229229

230230
let response = try await testClient.send(
231231
DocumentFormattingRequest(
@@ -235,42 +235,44 @@ final class FormattingTests: XCTestCase {
235235
)
236236

237237
let edits = try XCTUnwrap(response)
238+
let formattedSource = apply(edits: edits, to: source)
239+
238240
XCTAssertEqual(
239-
edits,
240-
[
241-
TextEdit(range: positions["1️⃣"]..<positions["2️⃣"], newText: ""),
242-
TextEdit(range: Range(positions["3️⃣"]), newText: "public "),
243-
]
241+
formattedSource,
242+
"""
243+
extension Example {
244+
public func function() {}
245+
}
246+
247+
"""
244248
)
245249
}
246250

247251
func testMultiLineStringInsertion() async throws {
248252
let testClient = try await TestSourceKitLSPClient()
249253
let uri = DocumentURI(for: .swift)
250254

251-
let positions = testClient.openDocument(
252-
#"""
255+
let source = #"""
253256
_ = [
254257
Node(
255258
documentation: """
256-
1️⃣A
257-
2️⃣B
258-
3️⃣C
259-
4️⃣""",
259+
A
260+
B
261+
C
262+
""",
260263
children: [
261264
Child(
262265
documentation: """
263-
5️⃣A
264-
6️⃣ 7️⃣\#("")
265-
8️⃣ 9️⃣ 🔟"""
266+
A
267+
\#("")
268+
"""
266269
)
267270
]
268271
)
269272
]
270273
271-
"""#,
272-
uri: uri
273-
)
274+
"""#
275+
testClient.openDocument(source, uri: uri)
274276

275277
let response = try await testClient.send(
276278
DocumentFormattingRequest(
@@ -280,18 +282,31 @@ final class FormattingTests: XCTestCase {
280282
)
281283

282284
let edits = try XCTUnwrap(response)
285+
let formattedSource = apply(edits: edits, to: source)
286+
287+
XCTAssert(edits.allSatisfy { $0.newText.allSatisfy(\.isWhitespace) })
283288
XCTAssertEqual(
284-
edits,
285-
[
286-
TextEdit(range: Range(positions["1️⃣"]), newText: " "),
287-
TextEdit(range: Range(positions["2️⃣"]), newText: " "),
288-
TextEdit(range: Range(positions["3️⃣"]), newText: " "),
289-
TextEdit(range: Range(positions["4️⃣"]), newText: " "),
290-
TextEdit(range: Range(positions["5️⃣"]), newText: " "),
291-
TextEdit(range: Range(positions["6️⃣"]), newText: "\n"),
292-
TextEdit(range: positions["7️⃣"]..<positions["8️⃣"], newText: ""),
293-
TextEdit(range: positions["9️⃣"]..<positions["🔟"], newText: ""),
289+
formattedSource,
290+
#"""
291+
_ = [
292+
Node(
293+
documentation: """
294+
A
295+
B
296+
C
297+
""",
298+
children: [
299+
Child(
300+
documentation: """
301+
A
302+
303+
"""
304+
)
305+
]
306+
)
294307
]
308+
309+
"""#
295310
)
296311
}
297312

Tests/SourceKitLSPTests/OnTypeFormattingTests.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,36 @@ final class OnTypeFormattingTests: XCTestCase {
5454
let testClient = try await TestSourceKitLSPClient()
5555
let uri = DocumentURI(for: .swift)
5656

57-
let positions = testClient.openDocument(
58-
"""
57+
let source = """
5958
func foo() {
60-
1️⃣if let SomeReallyLongVar = 2️⃣ 3️⃣Some.More.Stuff(), let a = 4️⃣ 5️⃣myfunc() 6️⃣{
59+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() 1️⃣{
6160
}
6261
}
63-
""",
64-
uri: uri
65-
)
62+
"""
63+
let positions = testClient.openDocument(source, uri: uri)
6664

6765
let response = try await testClient.send(
6866
DocumentOnTypeFormattingRequest(
6967
textDocument: TextDocumentIdentifier(uri),
70-
position: positions["6️⃣"],
68+
position: positions["1️⃣"],
7169
ch: "{",
7270
options: FormattingOptions(tabSize: 4, insertSpaces: true)
7371
)
7472
)
7573

7674
let edits = try XCTUnwrap(response)
75+
let (_, unmarkedSource) = extractMarkers(source)
76+
let formattedSource = apply(edits: edits, to: unmarkedSource)
77+
78+
XCTAssert(edits.allSatisfy { $0.newText.allSatisfy(\.isWhitespace) })
7779
XCTAssertEqual(
78-
edits,
79-
[
80-
TextEdit(range: Range(positions["1️⃣"]), newText: " "),
81-
TextEdit(range: positions["2️⃣"]..<positions["3️⃣"], newText: ""),
82-
TextEdit(range: positions["4️⃣"]..<positions["5️⃣"], newText: ""),
83-
]
80+
formattedSource,
81+
"""
82+
func foo() {
83+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
84+
}
85+
}
86+
"""
8487
)
8588
}
8689

Tests/SourceKitLSPTests/RenameAssertions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SKTestSupport
1515
import SKUtilities
1616
import XCTest
1717

18-
private func apply(edits: [TextEdit], to source: String) -> String {
18+
internal func apply(edits: [TextEdit], to source: String) -> String {
1919
var lineTable = LineTable(source)
2020
let edits = edits.sorted(by: { $0.range.lowerBound < $1.range.lowerBound })
2121
for edit in edits.reversed() {

0 commit comments

Comments
 (0)