Testing
Use servertest to exercise handlers without launching an editor.
Basic Test
func TestHover(t *testing.T) {
h := servertest.New(t, handler.New())
h.DidOpen("file:///test.txt", "plaintext", "hello world")
hover, err := h.Hover("file:///test.txt", 0, 5)
if err != nil {
t.Fatal(err)
}
if hover == nil {
t.Fatal("expected hover result")
}
}
Document Operations
h.DidOpen("file:///main.go", "go", source)
h.DidChange("file:///main.go", 2, updated)
h.DidSave("file:///main.go")
h.DidClose("file:///main.go")
Diagnostics
h.DidOpen("file:///test.txt", "plaintext", "TODO fix this")
h.DidSave("file:///test.txt")
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
diags, err := h.WaitForDiagnostics(ctx, "file:///test.txt")
More Helpers
The harness includes typed helpers for common LSP requests including completion resolve, signature help, declaration, definition, references, document highlights, code actions, code lenses, document links, colors, formatting, rename, folding ranges, selection ranges, linked editing, monikers, inlay hints, pull diagnostics, semantic tokens, call/type hierarchy, file operations, and execute command.
Use CallAsync and CancelRequest to test cancellation. Use SetClientResponse, SetClientError, ClientRequests, and WaitForClientRequest to test handlers that make server-to-client requests through server.Client.
Repository Test Commands
make test # go test -v ./...
make test-race # go test -race ./...
make test-cover # go test -cover ./...
make test-fuzz-document # short document fuzz run
Fuzz tests live in normal package test files. Ordinary go test ./... runs only their seed cases; use the fuzz target for active document fuzzing.
Options
h := servertest.New(t, handler.New(),
servertest.WithInitializeParams(&lsp.InitializeParams{
Capabilities: lsp.ClientCapabilities{},
}),
servertest.WithServerOptions(
server.WithCompletionOptions(lsp.CompletionOptions{
TriggerCharacters: []string{"."},
}),
),
)