|
| 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