|
| 1 | +package server_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/docker/docker-language-server/internal/tliron/glsp/protocol" |
| 10 | + "github.com/sourcegraph/jsonrpc2" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestPrepareRename(t *testing.T) { |
| 15 | + s := startServer() |
| 16 | + |
| 17 | + client := bytes.NewBuffer(make([]byte, 0, 1024)) |
| 18 | + server := bytes.NewBuffer(make([]byte, 0, 1024)) |
| 19 | + serverStream := &TestStream{incoming: server, outgoing: client, closed: false} |
| 20 | + defer serverStream.Close() |
| 21 | + go s.ServeStream(serverStream) |
| 22 | + |
| 23 | + clientStream := jsonrpc2.NewBufferedStream(&TestStream{incoming: client, outgoing: server, closed: false}, jsonrpc2.VSCodeObjectCodec{}) |
| 24 | + defer clientStream.Close() |
| 25 | + conn := jsonrpc2.NewConn(context.Background(), clientStream, &ConfigurationHandler{t: t}) |
| 26 | + initialize(t, conn, protocol.InitializeParams{}) |
| 27 | + |
| 28 | + homedir, err := os.UserHomeDir() |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + testCases := []struct { |
| 32 | + name string |
| 33 | + content string |
| 34 | + position protocol.Position |
| 35 | + result *protocol.Range |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "rename dependent service", |
| 39 | + content: ` |
| 40 | +services: |
| 41 | + test: |
| 42 | + depends_on: |
| 43 | + - test2 |
| 44 | + test2:`, |
| 45 | + position: protocol.Position{Line: 4, Character: 11}, |
| 46 | + result: &protocol.Range{ |
| 47 | + Start: protocol.Position{Line: 4, Character: 8}, |
| 48 | + End: protocol.Position{Line: 4, Character: 13}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tc := range testCases { |
| 54 | + t.Run(tc.name, func(t *testing.T) { |
| 55 | + didOpen := createDidOpenTextDocumentParams(homedir, t.Name()+".yaml", tc.content, "dockercompose") |
| 56 | + err := conn.Notify(context.Background(), protocol.MethodTextDocumentDidOpen, didOpen) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + var result *protocol.Range |
| 60 | + err = conn.Call(context.Background(), protocol.MethodTextDocumentPrepareRename, protocol.PrepareRenameParams{ |
| 61 | + TextDocumentPositionParams: protocol.TextDocumentPositionParams{ |
| 62 | + TextDocument: protocol.TextDocumentIdentifier{URI: didOpen.TextDocument.URI}, |
| 63 | + Position: tc.position, |
| 64 | + }, |
| 65 | + }, &result) |
| 66 | + require.NoError(t, err) |
| 67 | + require.Equal(t, tc.result, result) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
0 commit comments