|
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package template |
| 6 | + |
| 7 | +import ( |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "golang.org/x/tools/gopls/internal/hooks" |
| 12 | + "golang.org/x/tools/internal/lsp/protocol" |
| 13 | + . "golang.org/x/tools/internal/lsp/regtest" |
| 14 | +) |
| 15 | + |
| 16 | +func TestMain(m *testing.M) { |
| 17 | + Main(m, hooks.Options) |
| 18 | +} |
| 19 | + |
| 20 | +func TestTemplatesFromExtensions(t *testing.T) { |
| 21 | + const files = ` |
| 22 | +-- go.mod -- |
| 23 | +module mod.com |
| 24 | +
|
| 25 | +go 1.12 |
| 26 | +-- hello.tmpl -- |
| 27 | +{{range .Planets}} |
| 28 | +Hello {{}} <-- missing body |
| 29 | +{{end}} |
| 30 | +` |
| 31 | + |
| 32 | + WithOptions( |
| 33 | + EditorConfig{ |
| 34 | + Settings: map[string]interface{}{ |
| 35 | + "templateExtensions": []string{"tmpl"}, |
| 36 | + }, |
| 37 | + }, |
| 38 | + ).Run(t, files, func(t *testing.T, env *Env) { |
| 39 | + // TODO: can we move this diagnostic onto {{}}? |
| 40 | + env.Await(env.DiagnosticAtRegexp("hello.tmpl", "()Hello {{}}")) |
| 41 | + env.WriteWorkspaceFile("hello.tmpl", "{{range .Planets}}\nHello {{.}}\n{{end}}") |
| 42 | + env.Await(EmptyDiagnostics("hello.tmpl")) |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +func TestTemplatesFromLangID(t *testing.T) { |
| 47 | + const files = ` |
| 48 | +-- go.mod -- |
| 49 | +module mod.com |
| 50 | +
|
| 51 | +go 1.12 |
| 52 | +` |
| 53 | + |
| 54 | + Run(t, files, func(t *testing.T, env *Env) { |
| 55 | + env.CreateBuffer("hello.tmpl", "") |
| 56 | + env.Await( |
| 57 | + OnceMet( |
| 58 | + env.DoneWithOpen(), |
| 59 | + NoDiagnostics("hello.tmpl"), // Don't get spurious errors for empty templates. |
| 60 | + ), |
| 61 | + ) |
| 62 | + env.SetBufferContent("hello.tmpl", "{{range .Planets}}\nHello {{}}\n{{end}}") |
| 63 | + env.Await(env.DiagnosticAtRegexp("hello.tmpl", "()Hello {{}}")) |
| 64 | + env.RegexpReplace("hello.tmpl", "{{}}", "{{.}}") |
| 65 | + env.Await(EmptyOrNoDiagnostics("hello.tmpl")) |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +func TestClosingTemplatesMakesDiagnosticsDisappear(t *testing.T) { |
| 70 | + const files = ` |
| 71 | +-- go.mod -- |
| 72 | +module mod.com |
| 73 | +
|
| 74 | +go 1.12 |
| 75 | +-- hello.tmpl -- |
| 76 | +{{range .Planets}} |
| 77 | +Hello {{}} <-- missing body |
| 78 | +{{end}} |
| 79 | +` |
| 80 | + |
| 81 | + Run(t, files, func(t *testing.T, env *Env) { |
| 82 | + env.OpenFile("hello.tmpl") |
| 83 | + env.Await(env.DiagnosticAtRegexp("hello.tmpl", "()Hello {{}}")) |
| 84 | + // Since we don't have templateExtensions configured, closing hello.tmpl |
| 85 | + // should make its diagnostics disappear. |
| 86 | + env.CloseBuffer("hello.tmpl") |
| 87 | + env.Await(EmptyDiagnostics("hello.tmpl")) |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func TestMultipleSuffixes(t *testing.T) { |
| 92 | + const files = ` |
| 93 | +-- go.mod -- |
| 94 | +module mod.com |
| 95 | +
|
| 96 | +go 1.12 |
| 97 | +-- b.gotmpl -- |
| 98 | +{{define "A"}}goo{{end}} |
| 99 | +-- a.tmpl -- |
| 100 | +{{template "A"}} |
| 101 | +` |
| 102 | + |
| 103 | + WithOptions( |
| 104 | + EditorConfig{ |
| 105 | + Settings: map[string]interface{}{ |
| 106 | + "templateExtensions": []string{"tmpl", "gotmpl"}, |
| 107 | + }, |
| 108 | + }, |
| 109 | + ).Run(t, files, func(t *testing.T, env *Env) { |
| 110 | + env.OpenFile("a.tmpl") |
| 111 | + x := env.RegexpSearch("a.tmpl", `A`) |
| 112 | + file, pos := env.GoToDefinition("a.tmpl", x) |
| 113 | + refs := env.References(file, pos) |
| 114 | + if len(refs) != 2 { |
| 115 | + t.Fatalf("got %v reference(s), want 2", len(refs)) |
| 116 | + } |
| 117 | + // make sure we got one from b.gotmpl |
| 118 | + want := env.Sandbox.Workdir.URI("b.gotmpl") |
| 119 | + if refs[0].URI != want && refs[1].URI != want { |
| 120 | + t.Errorf("failed to find reference to %s", shorten(want)) |
| 121 | + for i, r := range refs { |
| 122 | + t.Logf("%d: URI:%s %v", i, shorten(r.URI), r.Range) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + content, npos := env.Hover(file, pos) |
| 127 | + if pos != npos { |
| 128 | + t.Errorf("pos? got %v, wanted %v", npos, pos) |
| 129 | + } |
| 130 | + if content.Value != "template A defined" { |
| 131 | + t.Errorf("got %s, wanted 'template A defined", content.Value) |
| 132 | + } |
| 133 | + }) |
| 134 | +} |
| 135 | + |
| 136 | +// shorten long URIs |
| 137 | +func shorten(fn protocol.DocumentURI) string { |
| 138 | + if len(fn) <= 20 { |
| 139 | + return string(fn) |
| 140 | + } |
| 141 | + pieces := strings.Split(string(fn), "/") |
| 142 | + if len(pieces) < 2 { |
| 143 | + return string(fn) |
| 144 | + } |
| 145 | + j := len(pieces) |
| 146 | + return pieces[j-2] + "/" + pieces[j-1] |
| 147 | +} |
| 148 | + |
| 149 | +// Hover, SemTok, Diagnose with errors |
| 150 | +// and better coverage |
0 commit comments