aboutsummaryrefslogtreecommitdiff
path: root/gopls/internal/lsp/template/parse_test.go
blob: 345f52347fa99d3510e348ac2c90a21b0ae14484 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package template

import (
	"strings"
	"testing"
)

type datum struct {
	buf  string
	cnt  int
	syms []string // the symbols in the parse of buf
}

var tmpl = []datum{{`
{{if (foo .X.Y)}}{{$A := "hi"}}{{.Z $A}}{{else}}
{{$A.X 12}}
{{foo (.X.Y) 23 ($A.Zü)}}
{{end}}`, 1, []string{"{7,3,foo,Function,false}", "{12,1,X,Method,false}",
	"{14,1,Y,Method,false}", "{21,2,$A,Variable,true}", "{26,2,,String,false}",
	"{35,1,Z,Method,false}", "{38,2,$A,Variable,false}",
	"{53,2,$A,Variable,false}", "{56,1,X,Method,false}", "{57,2,,Number,false}",
	"{64,3,foo,Function,false}", "{70,1,X,Method,false}",
	"{72,1,Y,Method,false}", "{75,2,,Number,false}", "{80,2,$A,Variable,false}",
	"{83,2,Zü,Method,false}", "{94,3,,Constant,false}"}},

	{`{{define "zzz"}}{{.}}{{end}}
{{template "zzz"}}`, 2, []string{"{10,3,zzz,Namespace,true}", "{18,1,dot,Variable,false}",
		"{41,3,zzz,Package,false}"}},

	{`{{block "aaa" foo}}b{{end}}`, 2, []string{"{9,3,aaa,Namespace,true}",
		"{9,3,aaa,Package,false}", "{14,3,foo,Function,false}", "{19,1,,Constant,false}"}},
	{"", 0, nil},
}

func TestSymbols(t *testing.T) {
	for i, x := range tmpl {
		got := parseBuffer([]byte(x.buf))
		if got.ParseErr != nil {
			t.Errorf("error:%v", got.ParseErr)
			continue
		}
		if len(got.named) != x.cnt {
			t.Errorf("%d: got %d, expected %d", i, len(got.named), x.cnt)
		}
		for n, s := range got.symbols {
			if s.String() != x.syms[n] {
				t.Errorf("%d: got %s, expected %s", i, s.String(), x.syms[n])
			}
		}
	}
}

func TestWordAt(t *testing.T) {
	want := []string{"", "", "$A", "$A", "", "", "", "", "", "",
		"", "", "", "if", "if", "", "$A", "$A", "", "",
		"B", "", "", "end", "end", "end", "", "", ""}
	p := parseBuffer([]byte("{{$A := .}}{{if $A}}B{{end}}"))
	for i := 0; i < len(p.buf); i++ {
		got := findWordAt(p, i)
		if got != want[i] {
			t.Errorf("for %d, got %q, wanted %q", i, got, want[i])
		}
	}
}

func TestNLS(t *testing.T) {
	buf := `{{if (foÜx .X.Y)}}{{$A := "hi"}}{{.Z $A}}{{else}}
	{{$A.X 12}}
	{{foo (.X.Y) 23 ($A.Z)}}
	{{end}}
	`
	p := parseBuffer([]byte(buf))
	if p.ParseErr != nil {
		t.Fatal(p.ParseErr)
	}
	// line 0 doesn't have a \n in front of it
	for i := 1; i < len(p.nls)-1; i++ {
		if buf[p.nls[i]] != '\n' {
			t.Errorf("line %d got %c", i, buf[p.nls[i]])
		}
	}
	// fake line at end of file
	if p.nls[len(p.nls)-1] != len(buf) {
		t.Errorf("got %d expected %d", p.nls[len(p.nls)-1], len(buf))
	}
}

func TestLineCol(t *testing.T) {
	buf := `{{if (foÜx .X.Y)}}{{$A := "hi"}}{{.Z $A}}{{else}}
	{{$A.X 12}}
	{{foo (.X.Y) 23 ($A.Z)}}
	{{end}}`
	if false {
		t.Error(buf)
	}
	for n, cx := range tmpl {
		buf := cx.buf
		p := parseBuffer([]byte(buf))
		if p.ParseErr != nil {
			t.Fatal(p.ParseErr)
		}
		type loc struct {
			offset int
			l, c   uint32
		}
		saved := []loc{}
		// forwards
		var lastl, lastc uint32
		for offset := range buf {
			l, c := p.LineCol(offset)
			saved = append(saved, loc{offset, l, c})
			if l > lastl {
				lastl = l
				if c != 0 {
					t.Errorf("line %d, got %d instead of 0", l, c)
				}
			}
			if c > lastc {
				lastc = c
			}
		}
		lines := strings.Split(buf, "\n")
		mxlen := -1
		for _, l := range lines {
			if len(l) > mxlen {
				mxlen = len(l)
			}
		}
		if int(lastl) != len(lines)-1 && int(lastc) != mxlen {
			// lastl is 0 if there is only 1 line(?)
			t.Errorf("expected %d, %d, got %d, %d for case %d", len(lines)-1, mxlen, lastl, lastc, n)
		}
		// backwards
		for j := len(saved) - 1; j >= 0; j-- {
			s := saved[j]
			xl, xc := p.LineCol(s.offset)
			if xl != s.l || xc != s.c {
				t.Errorf("at offset %d(%d), got (%d,%d), expected (%d,%d)", s.offset, j, xl, xc, s.l, s.c)
			}
		}
	}
}

func TestLineColNL(t *testing.T) {
	buf := "\n\n\n\n\n"
	p := parseBuffer([]byte(buf))
	if p.ParseErr != nil {
		t.Fatal(p.ParseErr)
	}
	for i := 0; i < len(buf); i++ {
		l, c := p.LineCol(i)
		if c != 0 || int(l) != i+1 {
			t.Errorf("got (%d,%d), expected (%d,0)", l, c, i)
		}
	}
}

func TestPos(t *testing.T) {
	buf := `
	{{if (foÜx .X.Y)}}{{$A := "hi"}}{{.Z $A}}{{else}}
	{{$A.X 12}}
	{{foo (.X.Y) 23 ($A.Z)}}
	{{end}}`
	p := parseBuffer([]byte(buf))
	if p.ParseErr != nil {
		t.Fatal(p.ParseErr)
	}
	for pos, r := range buf {
		if r == '\n' {
			continue
		}
		x := p.Position(pos)
		n := p.FromPosition(x)
		if n != pos {
			// once it's wrong, it will be wrong forever
			t.Fatalf("at pos %d (rune %c) got %d {%#v]", pos, r, n, x)
		}

	}
}
func TestLen(t *testing.T) {
	data := []struct {
		cnt int
		v   string
	}{{1, "a"}, {1, "膈"}, {4, "😆🥸"}, {7, "3😀4567"}}
	p := &Parsed{nonASCII: true}
	for _, d := range data {
		got := p.utf16len([]byte(d.v))
		if got != d.cnt {
			t.Errorf("%v, got %d wanted %d", d, got, d.cnt)
		}
	}
}

func TestUtf16(t *testing.T) {
	buf := `
	{{if (foÜx .X.Y)}}😀{{$A := "hi"}}{{.Z $A}}{{else}}
	{{$A.X 12}}
	{{foo (.X.Y) 23 ($A.Z)}}
	{{end}}`
	p := parseBuffer([]byte(buf))
	if p.nonASCII == false {
		t.Error("expected nonASCII to be true")
	}
}

type ttest struct {
	tmpl      string
	tokCnt    int
	elidedCnt int8
}

func TestQuotes(t *testing.T) {
	tsts := []ttest{
		{"{{- /*comment*/ -}}", 1, 0},
		{"{{/*`\ncomment\n`*/}}", 1, 0},
		//{"{{foo\nbar}}\n", 1, 0}, // this action spanning lines parses in 1.16
		{"{{\"{{foo}}{{\"}}", 1, 0},
		{"{{\n{{- when}}", 1, 1},          // corrected
		{"{{{{if .}}xx{{\n{{end}}", 2, 2}, // corrected
	}
	for _, s := range tsts {
		p := parseBuffer([]byte(s.tmpl))
		if len(p.tokens) != s.tokCnt {
			t.Errorf("%q: got %d tokens, expected %d", s, len(p.tokens), s.tokCnt)
		}
		if p.ParseErr != nil {
			t.Errorf("%q: %v", string(p.buf), p.ParseErr)
		}
		if len(p.elided) != int(s.elidedCnt) {
			t.Errorf("%q: elided %d, expected %d", s, len(p.elided), s.elidedCnt)
		}
	}
}