Repository navigation
Expand file tree
/
Copy pathdispatch_test.go
More file actions
184 lines (172 loc) · 6.16 KB
/
Copy pathdispatch_test.go
File metadata and controls
184 lines (172 loc) · 6.16 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2024 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"fmt"
"strings"
"testing"
)
// TestCommandOrCodeActionDispatch covers the textDocument/codeAction result
// union, where a CodeAction may carry an OPTIONAL `command` object whose key
// collides with Command's required `command` string. Required-key probing alone
// mis-routes; the decode-error gate must reject Command for an object command.
func TestCommandOrCodeActionDispatch(t *testing.T) {
tests := map[string]struct {
json string
wantType string // "*protocol.Command" or "*protocol.CodeAction"
}{
"success: plain command (command is a string)": {
json: `{"title":"x","command":"do.it"}`,
wantType: "*protocol.Command",
},
"success: code action carrying a command object": {
json: `{"title":"Fix","command":{"title":"y","command":"do.it"}}`,
wantType: "*protocol.CodeAction",
},
"success: code action with edit but no command": {
json: `{"title":"Fix","kind":"quickfix"}`,
wantType: "*protocol.CodeAction",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
var v CommandOrCodeAction
if err := Unmarshal([]byte(tt.json), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got := typeName(v); got != tt.wantType {
t.Fatalf("got %s, want %s", got, tt.wantType)
}
out, err := Marshal(v)
if err != nil {
t.Fatalf("marshal: %v", err)
}
if got, want := canon(t, out), canon(t, []byte(tt.json)); got != want {
t.Errorf("round-trip mismatch:\n got=%s\nwant=%s", got, want)
}
})
}
// The actual response shape: an array mixing both arms.
t.Run("success: []CommandOrCodeAction mixed", func(t *testing.T) {
in := `[{"title":"a","command":"run"},{"title":"Fix","command":{"title":"y","command":"run"}}]`
var v []CommandOrCodeAction
if err := Unmarshal([]byte(in), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if len(v) != 2 {
t.Fatalf("len = %d, want 2", len(v))
}
if _, ok := v[0].(*Command); !ok {
t.Errorf("v[0] = %T, want *Command", v[0])
}
if _, ok := v[1].(*CodeAction); !ok {
t.Errorf("v[1] = %T, want *CodeAction", v[1])
}
})
}
// TestRegistrationOptionsSupersetNotLost covers Options|RegistrationOptions
// unions where the RegistrationOptions arm adds an optional `id`
// (StaticRegistrationOptions). The superset arm must win when `id` is present so
// it is not dropped.
func TestRegistrationOptionsSupersetNotLost(t *testing.T) {
t.Run("success: notebook sync keeps id", func(t *testing.T) {
in := `{"notebookSelector":[],"id":"reg-1"}`
var v NotebookDocumentSync
if err := Unmarshal([]byte(in), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if _, ok := v.(*NotebookDocumentSyncRegistrationOptions); !ok {
t.Fatalf("got %T, want *NotebookDocumentSyncRegistrationOptions", v)
}
out, _ := Marshal(v)
if !strings.Contains(string(out), `"id":"reg-1"`) {
t.Errorf("id dropped: %s", out)
}
})
t.Run("success: text document content keeps id", func(t *testing.T) {
in := `{"schemes":["file"],"id":"reg-123"}`
var v WorkspaceOptionsTextDocumentContent
if err := Unmarshal([]byte(in), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if _, ok := v.(*TextDocumentContentRegistrationOptions); !ok {
t.Fatalf("got %T, want *TextDocumentContentRegistrationOptions", v)
}
out, _ := Marshal(v)
if !strings.Contains(string(out), `"id":"reg-123"`) {
t.Errorf("id dropped: %s", out)
}
})
}
// TestDocumentFilterNotebookNotShadowed ensures a NotebookCellTextDocumentFilter
// is not captured by the flattened TextDocumentFilter language probe.
func TestDocumentFilterNotebookNotShadowed(t *testing.T) {
in := `{"notebook":"jupyter-notebook","language":"python"}`
var v DocumentFilter
if err := Unmarshal([]byte(in), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if _, ok := v.(*NotebookCellTextDocumentFilter); !ok {
t.Fatalf("got %T, want *NotebookCellTextDocumentFilter", v)
}
out, _ := Marshal(v)
if !strings.Contains(string(out), `"notebook"`) {
t.Errorf("notebook dropped: %s", out)
}
}
// TestTextDocumentFilterByRequiredKey ensures arms with identical full key sets
// but different required keys still dispatch correctly.
func TestTextDocumentFilterByRequiredKey(t *testing.T) {
tests := map[string]struct {
json string
wantType string
}{
"success: language-required arm": {`{"language":"go"}`, "*protocol.TextDocumentFilterLanguage"},
"success: scheme-required arm": {`{"scheme":"file"}`, "*protocol.TextDocumentFilterScheme"},
"success: pattern-required arm": {`{"pattern":"**/*.go"}`, "*protocol.TextDocumentFilterPattern"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
var v TextDocumentFilter
if err := Unmarshal([]byte(tt.json), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got := typeName(v); got != tt.wantType {
t.Errorf("got %s, want %s", got, tt.wantType)
}
})
}
}
func TestDuplicateKindDiscriminatorUsesLastValue(t *testing.T) {
t.Parallel()
in := `{"kind":"full","items":[],"kind":"unchanged","resultId":"r"}`
var v DocumentDiagnosticReport
if err := Unmarshal([]byte(in), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
got, ok := v.(*RelatedUnchangedDocumentDiagnosticReport)
if !ok {
t.Fatalf("got %T, want *RelatedUnchangedDocumentDiagnosticReport", v)
}
if got.Kind != "unchanged" || got.ResultID != "r" {
t.Fatalf("decoded report = %+v, want kind=unchanged resultId=r", got)
}
}
// TestSymbolArrayDistinguishedByData ensures a WorkspaceSymbol[] carrying the
// WorkspaceSymbol-only `data` field decodes to WorkspaceSymbolSlice (not
// SymbolInformationSlice) and does not lose `data`.
func TestSymbolArrayDistinguishedByData(t *testing.T) {
in := `[{"name":"Foo","kind":5,"location":{"uri":"file:///x.go"},"data":{"k":"v"}}]`
var v WorkspaceSymbolResult
if err := Unmarshal([]byte(in), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if _, ok := v.(WorkspaceSymbolSlice); !ok {
t.Fatalf("got %T, want WorkspaceSymbolSlice", v)
}
out, _ := Marshal(v)
if !strings.Contains(string(out), `"data"`) {
t.Errorf("data dropped: %s", out)
}
}
func typeName(v any) string { return fmt.Sprintf("%T", v) }