-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformatting_test.go
69 lines (59 loc) · 1.97 KB
/
formatting_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package server_test
import (
"bytes"
"context"
"os"
"testing"
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
"github.com/sourcegraph/jsonrpc2"
"github.com/stretchr/testify/require"
)
func TestFormatting(t *testing.T) {
s := startServer()
client := bytes.NewBuffer(make([]byte, 0, 1024))
server := bytes.NewBuffer(make([]byte, 0, 1024))
serverStream := &TestStream{incoming: server, outgoing: client, closed: false}
defer serverStream.Close()
go s.ServeStream(serverStream)
clientStream := jsonrpc2.NewBufferedStream(&TestStream{incoming: client, outgoing: server, closed: false}, jsonrpc2.VSCodeObjectCodec{})
defer clientStream.Close()
conn := jsonrpc2.NewConn(context.Background(), clientStream, &ConfigurationHandler{t: t})
initialize(t, conn, protocol.InitializeParams{})
homedir, err := os.UserHomeDir()
require.NoError(t, err)
testCases := []struct {
name string
content string
edits []protocol.TextEdit
}{
{
name: "truncate whitespace before a block type",
content: " target t {\n}",
edits: []protocol.TextEdit{
{
NewText: "",
Range: protocol.Range{
Start: protocol.Position{Line: 0, Character: 0},
End: protocol.Position{Line: 0, Character: 1},
},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
didOpen := createDidOpenTextDocumentParams(homedir, t.Name()+".hcl", tc.content, "dockerbake")
err := conn.Notify(context.Background(), protocol.MethodTextDocumentDidOpen, didOpen)
require.NoError(t, err)
var edits []protocol.TextEdit
err = conn.Call(context.Background(), protocol.MethodTextDocumentFormatting, protocol.HoverParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{URI: didOpen.TextDocument.URI},
Position: protocol.Position{Line: 0, Character: 3},
},
}, &edits)
require.NoError(t, err)
require.Equal(t, tc.edits, edits)
})
}
}