| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package template |
| 6 |
|
| 7 |
import ( |
| 8 |
"log" |
| 9 |
"sort" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"golang.org/x/tools/gopls/internal/protocol" |
| 14 |
) |
| 15 |
|
| 16 |
func init() { |
| 17 |
log.SetFlags(log.Lshortfile) |
| 18 |
} |
| 19 |
|
| 20 |
type tparse struct { |
| 21 |
marked string |
| 22 |
wanted []string |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func TestParsed(t *testing.T) { |
| 28 |
for _, test := range []tparse{ |
| 29 |
{"{{x}}{{12. xx^", nil}, |
| 30 |
{`<table class="chroma" data-new-comment-url="{{if $.PageIsPullFiles}}{{$.Issue.HTMLURL}}/files/reviews/new_comment{{else}}{{$.CommitHTML}}/new_comment^{{end}}">`, nil}, |
| 31 |
{"{{i^f}}", []string{"index", "if"}}, |
| 32 |
{"{{if .}}{{e^ {{end}}", []string{"eq", "end}}", "else", "end"}}, |
| 33 |
{"{{foo}}{{f^", []string{"foo"}}, |
| 34 |
{"{{$^}}", []string{"$"}}, |
| 35 |
{"{{$x:=4}}{{$^", []string{"$x"}}, |
| 36 |
{"{{$x:=4}}{{$ ^ ", []string{}}, |
| 37 |
{"{{len .Modified}}{{.^Mo", []string{"Modified"}}, |
| 38 |
{"{{len .Modified}}{{.mf^", []string{"Modified"}}, |
| 39 |
{"{{$^ }}", []string{"$"}}, |
| 40 |
{"{{$a =3}}{{$^", []string{"$a"}}, |
| 41 |
|
| 42 |
{`{{.Modified}}{{.^{{if $.one.two}}xxx{{end}}`, []string{"Modified", "one", "two"}}, |
| 43 |
{`{{.Modified}}{{.o^{{if $.one.two}}xxx{{end}}`, []string{"one"}}, |
| 44 |
{"{{.Modiifed}}{{.one.t^{{if $.one.two}}xxx{{end}}", []string{"two"}}, |
| 45 |
{`{{block "foo" .}}{{i^`, []string{"index", "if"}}, |
| 46 |
{"{{in^{{Internal}}", []string{"index", "Internal", "if"}}, |
| 47 |
|
| 48 |
{"{{4^e", []string{}}, |
| 49 |
|
| 50 |
{"{{`e^", []string{}}, |
| 51 |
{"{{`No i^", []string{}}, |
| 52 |
{"{{xavier}}{{12. x^", []string{"xavier"}}, |
| 53 |
} { |
| 54 |
t.Run("", func(t *testing.T) { |
| 55 |
var got []string |
| 56 |
if c := testCompleter(t, test); c != nil { |
| 57 |
ans, _ := c.complete() |
| 58 |
for _, a := range ans.Items { |
| 59 |
got = append(got, a.Label) |
| 60 |
} |
| 61 |
} |
| 62 |
if len(got) != len(test.wanted) { |
| 63 |
t.Fatalf("%q: got %q, wanted %q %d,%d", test.marked, got, test.wanted, len(got), len(test.wanted)) |
| 64 |
} |
| 65 |
sort.Strings(test.wanted) |
| 66 |
sort.Strings(got) |
| 67 |
for i := 0; i < len(got); i++ { |
| 68 |
if test.wanted[i] != got[i] { |
| 69 |
t.Fatalf("%q at %d: got %v, wanted %v", test.marked, i, got, test.wanted) |
| 70 |
} |
| 71 |
} |
| 72 |
}) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func testCompleter(t *testing.T, tx tparse) *completer { |
| 77 |
|
| 78 |
offset := strings.Index(tx.marked, "^") |
| 79 |
buf := strings.Replace(tx.marked, "^", "", 1) |
| 80 |
p := parseBuffer("", []byte(buf)) |
| 81 |
if p.parseErr != nil { |
| 82 |
t.Logf("%q: %v", tx.marked, p.parseErr) |
| 83 |
} |
| 84 |
pos, err := p.mapper.OffsetPosition(offset) |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
|
| 89 |
start, err := enclosingTokenStart(p, pos) |
| 90 |
if err != nil { |
| 91 |
if start == -1 { |
| 92 |
return nil |
| 93 |
} |
| 94 |
t.Fatal(err) |
| 95 |
} |
| 96 |
syms := make(map[string]symbol) |
| 97 |
filterSyms(syms, p.symbols) |
| 98 |
return &completer{ |
| 99 |
p: p, |
| 100 |
pos: pos, |
| 101 |
offset: start + len(lbraces), |
| 102 |
ctx: protocol.CompletionContext{TriggerKind: protocol.Invoked}, |
| 103 |
syms: syms, |
| 104 |
} |
| 105 |
} |