| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package template |
| 6 |
|
| 7 |
import "testing" |
| 8 |
|
| 9 |
func TestSymbols(t *testing.T) { |
| 10 |
for i, test := range []struct { |
| 11 |
buf string |
| 12 |
wantNamed int |
| 13 |
syms []string |
| 14 |
}{ |
| 15 |
{` |
| 16 |
{{if (foo .X.Y)}}{{$A := "hi"}}{{.Z $A}}{{else}} |
| 17 |
{{$A.X 12}} |
| 18 |
{{foo (.X.Y) 23 ($A.Zü)}} |
| 19 |
{{end}}`, 1, []string{ |
| 20 |
"{7,3,foo,Function,false}", |
| 21 |
"{12,1,X,Method,false}", |
| 22 |
"{14,1,Y,Method,false}", |
| 23 |
"{21,2,$A,Variable,true}", |
| 24 |
"{26,4,,String,false}", |
| 25 |
"{35,1,Z,Method,false}", |
| 26 |
"{38,2,$A,Variable,false}", |
| 27 |
"{53,2,$A,Variable,false}", |
| 28 |
"{56,1,X,Method,false}", |
| 29 |
"{57,2,,Number,false}", |
| 30 |
"{64,3,foo,Function,false}", |
| 31 |
"{70,1,X,Method,false}", |
| 32 |
"{72,1,Y,Method,false}", |
| 33 |
"{75,2,,Number,false}", |
| 34 |
"{80,2,$A,Variable,false}", |
| 35 |
"{83,3,Zü,Method,false}", |
| 36 |
"{94,3,,Constant,false}", |
| 37 |
}}, |
| 38 |
{`{{define "zzz"}}{{.}}{{end}} |
| 39 |
{{template "zzz"}}`, 2, []string{ |
| 40 |
"{10,3,zzz,Namespace,true}", |
| 41 |
"{18,1,dot,Variable,false}", |
| 42 |
"{41,3,zzz,Package,false}", |
| 43 |
}}, |
| 44 |
{`{{block "aaa" foo}}b{{end}}`, 2, []string{ |
| 45 |
"{9,3,aaa,Namespace,true}", |
| 46 |
"{9,3,aaa,Package,false}", |
| 47 |
"{14,3,foo,Function,false}", |
| 48 |
"{19,1,,Constant,false}", |
| 49 |
}}, |
| 50 |
{"", 0, nil}, |
| 51 |
{`{{/* this is |
| 52 |
a comment */}}`, 1, nil}, |
| 53 |
} { |
| 54 |
got := parseBuffer("", []byte(test.buf)) |
| 55 |
if got.parseErr != nil { |
| 56 |
t.Error(got.parseErr) |
| 57 |
continue |
| 58 |
} |
| 59 |
if len(got.named) != test.wantNamed { |
| 60 |
t.Errorf("%d: got %d, expected %d", i, len(got.named), test.wantNamed) |
| 61 |
} |
| 62 |
for n, s := range got.symbols { |
| 63 |
if s.String() != test.syms[n] { |
| 64 |
t.Errorf("%d: got %s, expected %s", i, s.String(), test.syms[n]) |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestWordAt(t *testing.T) { |
| 71 |
want := []string{"", "", "$A", "$A", "", "", "", "", "", "", |
| 72 |
"", "", "", "if", "if", "", "$A", "$A", "", "", |
| 73 |
"B", "", "", "end", "end", "end", "", "", ""} |
| 74 |
buf := []byte("{{$A := .}}{{if $A}}B{{end}}") |
| 75 |
for i := range buf { |
| 76 |
got := wordAt(buf, i) |
| 77 |
if got != want[i] { |
| 78 |
t.Errorf("for %d, got %q, wanted %q", i, got, want[i]) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestQuotes(t *testing.T) { |
| 84 |
for _, s := range []struct { |
| 85 |
tmpl string |
| 86 |
tokCnt int |
| 87 |
elidedCnt int8 |
| 88 |
}{ |
| 89 |
{"{{- /*comment*/ -}}", 1, 0}, |
| 90 |
{"{{/*`\ncomment\n`*/}}", 1, 0}, |
| 91 |
|
| 92 |
{"{{\"{{foo}}{{\"}}", 1, 0}, |
| 93 |
{"{{\n{{- when}}", 1, 1}, |
| 94 |
{"{{{{if .}}xx{{\n{{end}}", 2, 2}, |
| 95 |
} { |
| 96 |
p := parseBuffer("", []byte(s.tmpl)) |
| 97 |
if len(p.tokens) != s.tokCnt { |
| 98 |
t.Errorf("%#v: got %d tokens, expected %d", s, len(p.tokens), s.tokCnt) |
| 99 |
} |
| 100 |
if p.parseErr != nil { |
| 101 |
t.Errorf("%q: %v", string(p.buf), p.parseErr) |
| 102 |
} |
| 103 |
if len(p.elided) != int(s.elidedCnt) { |
| 104 |
t.Errorf("%#v: elided %d, expected %d", s, len(p.elided), s.elidedCnt) |
| 105 |
} |
| 106 |
} |
| 107 |
} |