{"$.U.V", "{{$.U.V}}", "v", tVal, true},
{"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true},
{"simple assignment", "{{$x := 2}}{{$x = 3}}{{$x}}", "3", tVal, true},
- {
- "nested assignment",
+ {"nested assignment",
"{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{$x}}",
- "3", tVal, true,
- },
- {
- "nested assignment changes the last declaration",
+ "3", tVal, true},
+ {"nested assignment changes the last declaration",
"{{$x := 1}}{{if true}}{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{end}}{{$x}}",
- "1", tVal, true,
- },
+ "1", tVal, true},
// Type with String method.
{"V{6666}.String()", "-{{.V0}}-", "-<6666>-", tVal, true},
{".Method3(nil constant)", "-{{.Method3 nil}}-", "-Method3: <nil>-", tVal, true},
{".Method3(nil value)", "-{{.Method3 .MXI.unset}}-", "-Method3: <nil>-", tVal, true},
{"method on var", "{{if $x := .}}-{{$x.Method2 .U16 $x.X}}{{end}}-", "-Method2: 16 x-", tVal, true},
- {
- "method on chained var",
+ {"method on chained var",
"{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
- "true", tVal, true,
- },
- {
- "chained method",
+ "true", tVal, true},
+ {"chained method",
"{{range .MSIone}}{{if $.GetU.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
- "true", tVal, true,
- },
- {
- "chained method on variable",
+ "true", tVal, true},
+ {"chained method on variable",
"{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}",
- "true", tVal, true,
- },
+ "true", tVal, true},
{".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true},
{".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true},
{"method on nil value from slice", "-{{range .}}{{.Method1 1234}}{{end}}-", "-1234-", tSliceOfNil, true},
{"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) M0", tVal, true},
// HTML.
- {
- "html", `{{html "<script>alert(\"XSS\");</script>"}}`,
- "<script>alert("XSS");</script>", nil, true,
- },
- {
- "html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
- "<script>alert("XSS");</script>", nil, true,
- },
+ {"html", `{{html "<script>alert(\"XSS\");</script>"}}`,
+ "<script>alert("XSS");</script>", nil, true},
+ {"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
+ "<script>alert("XSS");</script>", nil, true},
{"html", `{{html .PS}}`, "a string", tVal, true},
{"html typed nil", `{{html .NIL}}`, "<nil>", tVal, true},
{"html untyped nil", `{{html .Empty0}}`, "<no value>", tVal, true},
func TestDelims(t *testing.T) {
const hello = "Hello, world"
- value := struct{ Str string }{hello}
+ var value = struct{ Str string }{hello}
for i := 0; i < len(delimPairs); i += 2 {
text := ".Str"
left := delimPairs[i+0]
if err != nil {
t.Fatalf("delim %q text %q parse err %s", left, text, err)
}
- b := new(bytes.Buffer)
+ var b = new(bytes.Buffer)
err = tmpl.Execute(b, value)
if err != nil {
t.Fatalf("delim %q exec err %s", left, err)
`
func TestTree(t *testing.T) {
- tree := &Tree{
+ var tree = &Tree{
1,
&Tree{
2, &Tree{
func TestComparison(t *testing.T) {
b := new(bytes.Buffer)
- cmpStruct := struct {
+ var cmpStruct = struct {
Uthree, Ufour uint
NegOne, Three int
Ptr, NilPtr *int
)
var parseTests = []parseTest{
- {
- "empty", "", noError,
- ``,
- },
- {
- "comment", "{{/*\n\n\n*/}}", noError,
- ``,
- },
- {
- "spaces", " \t\n", noError,
- `" \t\n"`,
- },
- {
- "text", "some text", noError,
- `"some text"`,
- },
- {
- "emptyAction", "{{}}", hasError,
- `{{}}`,
- },
- {
- "field", "{{.X}}", noError,
- `{{.X}}`,
- },
- {
- "simple command", "{{printf}}", noError,
- `{{printf}}`,
- },
- {
- "$ invocation", "{{$}}", noError,
- "{{$}}",
- },
- {
- "variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError,
- "{{with $x := 3}}{{$x 23}}{{end}}",
- },
- {
- "variable with fields", "{{$.I}}", noError,
- "{{$.I}}",
- },
- {
- "multi-word command", "{{printf `%d` 23}}", noError,
- "{{printf `%d` 23}}",
- },
- {
- "pipeline", "{{.X|.Y}}", noError,
- `{{.X | .Y}}`,
- },
- {
- "pipeline with decl", "{{$x := .X|.Y}}", noError,
- `{{$x := .X | .Y}}`,
- },
- {
- "nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError,
- `{{.X (.Y .Z) (.A | .B .C) (.E)}}`,
- },
- {
- "field applied to parentheses", "{{(.Y .Z).Field}}", noError,
- `{{(.Y .Z).Field}}`,
- },
- {
- "simple if", "{{if .X}}hello{{end}}", noError,
- `{{if .X}}"hello"{{end}}`,
- },
- {
- "if with else", "{{if .X}}true{{else}}false{{end}}", noError,
- `{{if .X}}"true"{{else}}"false"{{end}}`,
- },
- {
- "if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError,
- `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`,
- },
- {
- "if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError,
- `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`,
- },
- {
- "simple range", "{{range .X}}hello{{end}}", noError,
- `{{range .X}}"hello"{{end}}`,
- },
- {
- "chained field range", "{{range .X.Y.Z}}hello{{end}}", noError,
- `{{range .X.Y.Z}}"hello"{{end}}`,
- },
- {
- "nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError,
- `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`,
- },
- {
- "range with else", "{{range .X}}true{{else}}false{{end}}", noError,
- `{{range .X}}"true"{{else}}"false"{{end}}`,
- },
- {
- "range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError,
- `{{range .X | .M}}"true"{{else}}"false"{{end}}`,
- },
- {
- "range []int", "{{range .SI}}{{.}}{{end}}", noError,
- `{{range .SI}}{{.}}{{end}}`,
- },
- {
- "range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError,
- `{{range $x := .SI}}{{.}}{{end}}`,
- },
- {
- "range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError,
- `{{range $x, $y := .SI}}{{.}}{{end}}`,
- },
- {
- "constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError,
- `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`,
- },
- {
- "template", "{{template `x`}}", noError,
- `{{template "x"}}`,
- },
- {
- "template with arg", "{{template `x` .Y}}", noError,
- `{{template "x" .Y}}`,
- },
- {
- "with", "{{with .X}}hello{{end}}", noError,
- `{{with .X}}"hello"{{end}}`,
- },
- {
- "with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,
- `{{with .X}}"hello"{{else}}"goodbye"{{end}}`,
- },
+ {"empty", "", noError,
+ ``},
+ {"comment", "{{/*\n\n\n*/}}", noError,
+ ``},
+ {"spaces", " \t\n", noError,
+ `" \t\n"`},
+ {"text", "some text", noError,
+ `"some text"`},
+ {"emptyAction", "{{}}", hasError,
+ `{{}}`},
+ {"field", "{{.X}}", noError,
+ `{{.X}}`},
+ {"simple command", "{{printf}}", noError,
+ `{{printf}}`},
+ {"$ invocation", "{{$}}", noError,
+ "{{$}}"},
+ {"variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError,
+ "{{with $x := 3}}{{$x 23}}{{end}}"},
+ {"variable with fields", "{{$.I}}", noError,
+ "{{$.I}}"},
+ {"multi-word command", "{{printf `%d` 23}}", noError,
+ "{{printf `%d` 23}}"},
+ {"pipeline", "{{.X|.Y}}", noError,
+ `{{.X | .Y}}`},
+ {"pipeline with decl", "{{$x := .X|.Y}}", noError,
+ `{{$x := .X | .Y}}`},
+ {"nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError,
+ `{{.X (.Y .Z) (.A | .B .C) (.E)}}`},
+ {"field applied to parentheses", "{{(.Y .Z).Field}}", noError,
+ `{{(.Y .Z).Field}}`},
+ {"simple if", "{{if .X}}hello{{end}}", noError,
+ `{{if .X}}"hello"{{end}}`},
+ {"if with else", "{{if .X}}true{{else}}false{{end}}", noError,
+ `{{if .X}}"true"{{else}}"false"{{end}}`},
+ {"if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError,
+ `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`},
+ {"if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError,
+ `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`},
+ {"simple range", "{{range .X}}hello{{end}}", noError,
+ `{{range .X}}"hello"{{end}}`},
+ {"chained field range", "{{range .X.Y.Z}}hello{{end}}", noError,
+ `{{range .X.Y.Z}}"hello"{{end}}`},
+ {"nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError,
+ `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`},
+ {"range with else", "{{range .X}}true{{else}}false{{end}}", noError,
+ `{{range .X}}"true"{{else}}"false"{{end}}`},
+ {"range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError,
+ `{{range .X | .M}}"true"{{else}}"false"{{end}}`},
+ {"range []int", "{{range .SI}}{{.}}{{end}}", noError,
+ `{{range .SI}}{{.}}{{end}}`},
+ {"range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError,
+ `{{range $x := .SI}}{{.}}{{end}}`},
+ {"range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError,
+ `{{range $x, $y := .SI}}{{.}}{{end}}`},
+ {"constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError,
+ `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`},
+ {"template", "{{template `x`}}", noError,
+ `{{template "x"}}`},
+ {"template with arg", "{{template `x` .Y}}", noError,
+ `{{template "x" .Y}}`},
+ {"with", "{{with .X}}hello{{end}}", noError,
+ `{{with .X}}"hello"{{end}}`},
+ {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,
+ `{{with .X}}"hello"{{else}}"goodbye"{{end}}`},
// Trimming spaces.
{"trim left", "x \r\n\t{{- 3}}", noError, `"x"{{3}}`},
{"trim right", "{{3 -}}\n\n\ty", noError, `{{3}}"y"`},
{"comment trim left", "x \r\n\t{{- /* hi */}}", noError, `"x"`},
{"comment trim right", "{{/* hi */ -}}\n\n\ty", noError, `"y"`},
{"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x""y"`},
- {
- "block definition", `{{block "foo" .}}hello{{end}}`, noError,
- `{{template "foo" .}}`,
- },
+ {"block definition", `{{block "foo" .}}hello{{end}}`, noError,
+ `{{template "foo" .}}`},
// Errors.
{"unclosed action", "hello{{range", hasError, ""},
{"unmatched end", "{{end}}", hasError, ""},
// All failures, and the result is a string that must appear in the error message.
var errorTests = []parseTest{
// Check line numbers are accurate.
- {
- "unclosed1",
+ {"unclosed1",
"line1\n{{",
- hasError, `unclosed1:2: unexpected unclosed action in command`,
- },
- {
- "unclosed2",
+ hasError, `unclosed1:2: unexpected unclosed action in command`},
+ {"unclosed2",
"line1\n{{define `x`}}line2\n{{",
- hasError, `unclosed2:3: unexpected unclosed action in command`,
- },
+ hasError, `unclosed2:3: unexpected unclosed action in command`},
// Specific errors.
- {
- "function",
+ {"function",
"{{foo}}",
- hasError, `function "foo" not defined`,
- },
- {
- "comment",
+ hasError, `function "foo" not defined`},
+ {"comment",
"{{/*}}",
- hasError, `unclosed comment`,
- },
- {
- "lparen",
+ hasError, `unclosed comment`},
+ {"lparen",
"{{.X (1 2 3}}",
- hasError, `unclosed left paren`,
- },
- {
- "rparen",
+ hasError, `unclosed left paren`},
+ {"rparen",
"{{.X 1 2 3)}}",
- hasError, `unexpected ")"`,
- },
- {
- "space",
+ hasError, `unexpected ")"`},
+ {"space",
"{{`x`3}}",
- hasError, `in operand`,
- },
- {
- "idchar",
+ hasError, `in operand`},
+ {"idchar",
"{{a#}}",
- hasError, `'#'`,
- },
- {
- "charconst",
+ hasError, `'#'`},
+ {"charconst",
"{{'a}}",
- hasError, `unterminated character constant`,
- },
- {
- "stringconst",
+ hasError, `unterminated character constant`},
+ {"stringconst",
`{{"a}}`,
- hasError, `unterminated quoted string`,
- },
- {
- "rawstringconst",
+ hasError, `unterminated quoted string`},
+ {"rawstringconst",
"{{`a}}",
- hasError, `unterminated raw quoted string`,
- },
- {
- "number",
+ hasError, `unterminated raw quoted string`},
+ {"number",
"{{0xi}}",
- hasError, `number syntax`,
- },
- {
- "multidefine",
+ hasError, `number syntax`},
+ {"multidefine",
"{{define `a`}}a{{end}}{{define `a`}}b{{end}}",
- hasError, `multiple definition of template`,
- },
- {
- "eof",
+ hasError, `multiple definition of template`},
+ {"eof",
"{{range .X}}",
- hasError, `unexpected EOF`,
- },
- {
- "variable",
+ hasError, `unexpected EOF`},
+ {"variable",
// Declare $x so it's defined, to avoid that error, and then check we don't parse a declaration.
"{{$x := 23}}{{with $x.y := 3}}{{$x 23}}{{end}}",
- hasError, `unexpected ":="`,
- },
- {
- "multidecl",
+ hasError, `unexpected ":="`},
+ {"multidecl",
"{{$a,$b,$c := 23}}",
- hasError, `too many declarations`,
- },
- {
- "undefvar",
+ hasError, `too many declarations`},
+ {"undefvar",
"{{$a}}",
- hasError, `undefined variable`,
- },
- {
- "wrongdot",
+ hasError, `undefined variable`},
+ {"wrongdot",
"{{true.any}}",
- hasError, `unexpected . after term`,
- },
- {
- "wrongpipeline",
+ hasError, `unexpected . after term`},
+ {"wrongpipeline",
"{{12|false}}",
- hasError, `non executable command in pipeline`,
- },
- {
- "emptypipeline",
+ hasError, `non executable command in pipeline`},
+ {"emptypipeline",
`{{ ( ) }}`,
- hasError, `missing value for parenthesized pipeline`,
- },
- {
- "multilinerawstring",
+ hasError, `missing value for parenthesized pipeline`},
+ {"multilinerawstring",
"{{ $v := `\n` }} {{",
- hasError, `multilinerawstring:2: unexpected unclosed action`,
- },
- {
- "rangeundefvar",
+ hasError, `multilinerawstring:2: unexpected unclosed action`},
+ {"rangeundefvar",
"{{range $k}}{{end}}",
- hasError, `undefined variable`,
- },
- {
- "rangeundefvars",
+ hasError, `undefined variable`},
+ {"rangeundefvars",
"{{range $k, $v}}{{end}}",
- hasError, `undefined variable`,
- },
- {
- "rangemissingvalue1",
+ hasError, `undefined variable`},
+ {"rangemissingvalue1",
"{{range $k,}}{{end}}",
- hasError, `missing value for range`,
- },
- {
- "rangemissingvalue2",
+ hasError, `missing value for range`},
+ {"rangemissingvalue2",
"{{range $k, $v := }}{{end}}",
- hasError, `missing value for range`,
- },
- {
- "rangenotvariable1",
+ hasError, `missing value for range`},
+ {"rangenotvariable1",
"{{range $k, .}}{{end}}",
- hasError, `range can only initialize variables`,
- },
- {
- "rangenotvariable2",
+ hasError, `range can only initialize variables`},
+ {"rangenotvariable2",
"{{range $k, 123 := .}}{{end}}",
- hasError, `range can only initialize variables`,
- },
+ hasError, `range can only initialize variables`},
}
func TestErrors(t *testing.T) {