| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package template |
| 6 |
|
| 7 |
import ( |
| 8 |
"os" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"golang.org/x/tools/gopls/internal/protocol" |
| 13 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 14 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 15 |
) |
| 16 |
|
| 17 |
func TestMain(m *testing.M) { |
| 18 |
bug.PanicOnBugs = true |
| 19 |
os.Exit(Main(m)) |
| 20 |
} |
| 21 |
|
| 22 |
func TestMultilineTokens(t *testing.T) { |
| 23 |
|
| 24 |
const files = ` |
| 25 |
-- go.mod -- |
| 26 |
module mod.com |
| 27 |
|
| 28 |
go 1.17 |
| 29 |
-- hi.tmpl -- |
| 30 |
{{if (foÜx .X.Y)}}😀{{$A := |
| 31 |
"hi" |
| 32 |
}}{{.Z $A}}{{else}} |
| 33 |
{{$A.X 12}} |
| 34 |
{{foo (.X.Y) 23 ($A.Z)}} |
| 35 |
{{end}} |
| 36 |
` |
| 37 |
WithOptions( |
| 38 |
Settings{ |
| 39 |
"templateExtensions": []string{"tmpl"}, |
| 40 |
"semanticTokens": true, |
| 41 |
}, |
| 42 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 43 |
var p protocol.SemanticTokensParams |
| 44 |
p.TextDocument.URI = env.Sandbox.Workdir.URI("hi.tmpl") |
| 45 |
toks, err := env.Editor.Server.SemanticTokensFull(env.Ctx, &p) |
| 46 |
if err != nil { |
| 47 |
t.Errorf("semantic token failed: %v", err) |
| 48 |
} |
| 49 |
if toks == nil || len(toks.Data) == 0 { |
| 50 |
t.Errorf("got no semantic tokens") |
| 51 |
} |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
func TestMultilineTokensAgain(t *testing.T) { |
| 56 |
|
| 57 |
const files = ` |
| 58 |
-- go.mod -- |
| 59 |
module mod.com |
| 60 |
|
| 61 |
go 1.17 |
| 62 |
-- hi.tmpl -- |
| 63 |
{{/* this is |
| 64 |
a comment */}} |
| 65 |
` |
| 66 |
WithOptions( |
| 67 |
Settings{ |
| 68 |
"templateExtensions": []string{"tmpl"}, |
| 69 |
"semanticTokens": true, |
| 70 |
}, |
| 71 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 72 |
var p protocol.SemanticTokensParams |
| 73 |
p.TextDocument.URI = env.Sandbox.Workdir.URI("hi.tmpl") |
| 74 |
toks, err := env.Editor.Server.SemanticTokensFull(env.Ctx, &p) |
| 75 |
if err != nil { |
| 76 |
t.Errorf("semantic token failed: %v", err) |
| 77 |
} |
| 78 |
if toks == nil || len(toks.Data) == 0 { |
| 79 |
t.Errorf("got no semantic tokens") |
| 80 |
} |
| 81 |
}) |
| 82 |
} |
| 83 |
|
| 84 |
func TestTemplatesFromExtensions(t *testing.T) { |
| 85 |
const files = ` |
| 86 |
-- go.mod -- |
| 87 |
module mod.com |
| 88 |
|
| 89 |
go 1.12 |
| 90 |
-- hello.tmpl -- |
| 91 |
{{range .Planets}} |
| 92 |
Hello {{}} <-- missing body |
| 93 |
{{end}} |
| 94 |
` |
| 95 |
WithOptions( |
| 96 |
Settings{ |
| 97 |
"templateExtensions": []string{"tmpl"}, |
| 98 |
"semanticTokens": true, |
| 99 |
}, |
| 100 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 101 |
|
| 102 |
var diags protocol.PublishDiagnosticsParams |
| 103 |
env.OnceMet( |
| 104 |
InitialWorkspaceLoad, |
| 105 |
Diagnostics(env.AtRegexp("hello.tmpl", "()Hello {{}}")), |
| 106 |
ReadDiagnostics("hello.tmpl", &diags), |
| 107 |
) |
| 108 |
d := diags.Diagnostics |
| 109 |
if len(d) != 1 { |
| 110 |
t.Errorf("expected 1 diagnostic, got %d", len(d)) |
| 111 |
return |
| 112 |
} |
| 113 |
if d[0].Source != "template" { |
| 114 |
t.Errorf("expected Source 'template', got %q", d[0].Source) |
| 115 |
} |
| 116 |
|
| 117 |
var p protocol.SemanticTokensParams |
| 118 |
p.TextDocument.URI = env.Sandbox.Workdir.URI("hello.tmpl") |
| 119 |
toks, err := env.Editor.Server.SemanticTokensFull(env.Ctx, &p) |
| 120 |
if err != nil { |
| 121 |
t.Errorf("semantic token failed: %v", err) |
| 122 |
} |
| 123 |
if toks == nil || len(toks.Data) == 0 { |
| 124 |
t.Errorf("got no semantic tokens") |
| 125 |
} |
| 126 |
|
| 127 |
env.WriteWorkspaceFile("hello.tmpl", "{{range .Planets}}\nHello {{.}}\n{{end}}") |
| 128 |
env.AfterChange(NoDiagnostics(ForFile("hello.tmpl"))) |
| 129 |
}) |
| 130 |
} |
| 131 |
|
| 132 |
func TestTemplatesObserveDirectoryFilters(t *testing.T) { |
| 133 |
const files = ` |
| 134 |
-- go.mod -- |
| 135 |
module mod.com |
| 136 |
|
| 137 |
go 1.12 |
| 138 |
-- a/a.tmpl -- |
| 139 |
A {{}} <-- missing body |
| 140 |
-- b/b.tmpl -- |
| 141 |
B {{}} <-- missing body |
| 142 |
` |
| 143 |
|
| 144 |
WithOptions( |
| 145 |
Settings{ |
| 146 |
"directoryFilters": []string{"-b"}, |
| 147 |
"templateExtensions": []string{"tmpl"}, |
| 148 |
}, |
| 149 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 150 |
env.OnceMet( |
| 151 |
InitialWorkspaceLoad, |
| 152 |
Diagnostics(env.AtRegexp("a/a.tmpl", "()A")), |
| 153 |
NoDiagnostics(ForFile("b/b.tmpl")), |
| 154 |
) |
| 155 |
}) |
| 156 |
} |
| 157 |
|
| 158 |
func TestTemplatesFromLangID(t *testing.T) { |
| 159 |
const files = ` |
| 160 |
-- go.mod -- |
| 161 |
module mod.com |
| 162 |
|
| 163 |
go 1.12 |
| 164 |
` |
| 165 |
|
| 166 |
Run(t, files, func(t *testing.T, env *Env) { |
| 167 |
env.CreateBuffer("hello.tmpl", "") |
| 168 |
env.AfterChange( |
| 169 |
NoDiagnostics(ForFile("hello.tmpl")), |
| 170 |
) |
| 171 |
env.SetBufferContent("hello.tmpl", "{{range .Planets}}\nHello {{}}\n{{end}}") |
| 172 |
env.Await(Diagnostics(env.AtRegexp("hello.tmpl", "()Hello {{}}"))) |
| 173 |
env.RegexpReplace("hello.tmpl", "{{}}", "{{.}}") |
| 174 |
env.Await(NoDiagnostics(ForFile("hello.tmpl"))) |
| 175 |
}) |
| 176 |
} |
| 177 |
|
| 178 |
func TestClosingTemplatesMakesDiagnosticsDisappear(t *testing.T) { |
| 179 |
const files = ` |
| 180 |
-- go.mod -- |
| 181 |
module mod.com |
| 182 |
|
| 183 |
go 1.12 |
| 184 |
-- hello.tmpl -- |
| 185 |
{{range .Planets}} |
| 186 |
Hello {{}} <-- missing body |
| 187 |
{{end}} |
| 188 |
` |
| 189 |
|
| 190 |
Run(t, files, func(t *testing.T, env *Env) { |
| 191 |
env.OpenFile("hello.tmpl") |
| 192 |
env.AfterChange( |
| 193 |
Diagnostics(env.AtRegexp("hello.tmpl", "()Hello {{}}")), |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
env.CloseBuffer("hello.tmpl") |
| 198 |
env.AfterChange( |
| 199 |
NoDiagnostics(ForFile("hello.tmpl")), |
| 200 |
) |
| 201 |
}) |
| 202 |
} |
| 203 |
|
| 204 |
func TestMultipleSuffixes(t *testing.T) { |
| 205 |
const files = ` |
| 206 |
-- go.mod -- |
| 207 |
module mod.com |
| 208 |
|
| 209 |
go 1.12 |
| 210 |
-- b.gotmpl -- |
| 211 |
{{define "A"}}goo{{end}} |
| 212 |
-- a.tmpl -- |
| 213 |
{{template "A"}} |
| 214 |
` |
| 215 |
|
| 216 |
WithOptions( |
| 217 |
Settings{ |
| 218 |
"templateExtensions": []string{"tmpl", "gotmpl"}, |
| 219 |
}, |
| 220 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 221 |
env.OpenFile("a.tmpl") |
| 222 |
x := env.RegexpSearch("a.tmpl", `()A`) |
| 223 |
loc := env.FirstDefinition(x) |
| 224 |
env.OpenFile(env.Sandbox.Workdir.URIToPath(loc.URI)) |
| 225 |
refs := env.References(loc) |
| 226 |
if len(refs) != 2 { |
| 227 |
t.Fatalf("got %v reference(s), want 2", len(refs)) |
| 228 |
} |
| 229 |
|
| 230 |
want := env.Sandbox.Workdir.URI("b.gotmpl") |
| 231 |
if refs[0].URI != want && refs[1].URI != want { |
| 232 |
t.Errorf("failed to find reference to %s", shorten(want)) |
| 233 |
for i, r := range refs { |
| 234 |
t.Logf("%d: URI:%s %v", i, shorten(r.URI), r.Range) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
content, nloc := env.Hover(loc) |
| 239 |
if loc != nloc { |
| 240 |
t.Errorf("loc? got %v, wanted %v", nloc, loc) |
| 241 |
} |
| 242 |
if content.Value != "template A defined" { |
| 243 |
t.Errorf("got %s, wanted 'template A defined", content.Value) |
| 244 |
} |
| 245 |
}) |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
func shorten(fn protocol.DocumentURI) string { |
| 250 |
if len(fn) <= 20 { |
| 251 |
return string(fn) |
| 252 |
} |
| 253 |
pieces := strings.Split(string(fn), "/") |
| 254 |
if len(pieces) < 2 { |
| 255 |
return string(fn) |
| 256 |
} |
| 257 |
j := len(pieces) |
| 258 |
return pieces[j-2] + "/" + pieces[j-1] |
| 259 |
} |
| 260 |
|
| 261 |
func TestCompletionPanic_Issue57621(t *testing.T) { |
| 262 |
const src = ` |
| 263 |
-- go.mod -- |
| 264 |
module mod.com |
| 265 |
|
| 266 |
go 1.12 |
| 267 |
-- hello.tmpl -- |
| 268 |
{{range .Planets}} |
| 269 |
Hello {{ |
| 270 |
{{end}} |
| 271 |
` |
| 272 |
Run(t, src, func(t *testing.T, env *Env) { |
| 273 |
env.OpenFile("hello.tmpl") |
| 274 |
|
| 275 |
env.Completion(env.RegexpSearch("hello.tmpl", `Hello ()\{\{`)) |
| 276 |
env.Completion(env.RegexpSearch("hello.tmpl", `Hello \{()\{`)) |
| 277 |
env.Completion(env.RegexpSearch("hello.tmpl", `Hello \{\{()`)) |
| 278 |
env.Completion(env.RegexpSearch("hello.tmpl", `()\{\{range`)) |
| 279 |
env.Completion(env.RegexpSearch("hello.tmpl", `\{()\{range`)) |
| 280 |
env.Completion(env.RegexpSearch("hello.tmpl", `\{\{()range`)) |
| 281 |
env.Completion(env.RegexpSearch("hello.tmpl", `Planets()}}`)) |
| 282 |
env.Completion(env.RegexpSearch("hello.tmpl", `Planets}()}`)) |
| 283 |
env.Completion(env.RegexpSearch("hello.tmpl", `Planets}}()`)) |
| 284 |
}) |
| 285 |
} |
| 286 |
|
| 287 |
|