|
| 1 | +# Development |
| 2 | + |
| 3 | +This document contains notes about development and testing of SourceKit-LSP. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +* [Writing Tests](#writing-tests) |
| 8 | +* [Tibs, the "Test Index Build System"](#tibs) |
| 9 | + |
| 10 | +## Writing Tests |
| 11 | + |
| 12 | +As much as is practical, all code should be covered by tests. New tests can be added under the `Tests` directory and should use `XCTest`. The rest of this section will describe the additional tools available in the `SKTestSupport` module to make it easier to write good and efficient tests. |
| 13 | + |
| 14 | +### Test Projects (Fixtures) |
| 15 | + |
| 16 | +SourceKit test projects should be put in the `Tests/INPUTS` directory. Generally, they should use the [Tibs](#tibs) build system to define their sources and targets. An exception is for tests that need to specifically test the interaction with the Swift Package Manager. An example Tibs test project might look like: |
| 17 | + |
| 18 | +``` |
| 19 | +Tests/ |
| 20 | + Inputs/ |
| 21 | + MyTestProj/ |
| 22 | + a.swift |
| 23 | + b.swift |
| 24 | + c.cpp |
| 25 | +``` |
| 26 | + |
| 27 | +Where `project.json` describes the project's targets, for example |
| 28 | + |
| 29 | +``` |
| 30 | +{ "sources": ["a.swift", "b.swift", "c.cpp"] } |
| 31 | +``` |
| 32 | + |
| 33 | +Tibs supports more advanced project configurations, such as multiple swift modules with dependencies, etc. For much more information about Tibs, including what features it supports and how it works, see [Tibs](#tibs). |
| 34 | + |
| 35 | +### SKTibsTestWorkspace |
| 36 | + |
| 37 | +The `SKTibsTestWorkspace` pulls together the various pieces needed for working with tests, including opening a connection to the language server, building the project to produce index data, loading source code into open documents, etc. |
| 38 | + |
| 39 | +To create a `SKTibsTestWorkspace`, use the `staticSourceKitTibsWorkspace` method (the intent is to provide a `mutableSourceKitTibsWorkspace` method in the future for tests that mutate source code). |
| 40 | + |
| 41 | +```swift |
| 42 | +func testFoo() { |
| 43 | + // Create the workspace, including opening a connection to the TestServer. |
| 44 | + guard let ws = try staticSourceKitTibsWorkspace(name: "MyTestProj") else { return } |
| 45 | + let loc = ws.testLoc("myLocation") |
| 46 | + |
| 47 | + // Build the project and populate the index. |
| 48 | + try ws.buildAndIndex() |
| 49 | + |
| 50 | + // Open a document from the test project sources. |
| 51 | + try ws.openDocument(loc.url, language: .swift) |
| 52 | + |
| 53 | + // Send requests to the server. |
| 54 | + let response = try ws.sk.sendSync(...) |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +#### Source Locations |
| 59 | + |
| 60 | +It is common to want to refer to specific locations in the source code of a test project. This is supported using inline comment syntax. |
| 61 | + |
| 62 | +```swift |
| 63 | +Test.swift: |
| 64 | +func /*myFuncDef*/myFunc() { |
| 65 | + /*myFuncCall*/myFunc() |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +In a test, these locations can be referenced by name. The named location is immediately after the comment. |
| 70 | + |
| 71 | +```swift |
| 72 | + |
| 73 | +let loc = ws.testLoc("myFuncDef") |
| 74 | +// TestLocation(url: ..., line: 1, column: 19) |
| 75 | +``` |
| 76 | + |
| 77 | +`TestLocation`s can be easily converted to LSP `Location` and `Position`s. |
| 78 | + |
| 79 | +```swift |
| 80 | +Location(ws.testLoc("aaa:call")) |
| 81 | +Position(ws.testLoc("aaa:call")) |
| 82 | +``` |
| 83 | + |
| 84 | +## Tibs |
| 85 | + |
| 86 | +We use Tibs, the "Test Index Build System" from the IndexStoreDB project to provide build system support for test projects, including getting compiler arguments and building an index. |
| 87 | +For much more information about Tibs, see [IndexStoreDB/Documentation/Tibs.md](https://github.com/apple/indexstore-db/blob/master/Documentation/Tibs.md). |
0 commit comments