diff options
Diffstat (limited to 'src/pkg/template/exec_test.go')
| -rw-r--r-- | src/pkg/template/exec_test.go | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/pkg/template/exec_test.go b/src/pkg/template/exec_test.go index 82f56e13c..8e1894ea0 100644 --- a/src/pkg/template/exec_test.go +++ b/src/pkg/template/exec_test.go @@ -391,6 +391,8 @@ var execTests = []execTest{ {"range $x $y MSIone", "{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}", "<one=1>", tVal, true}, {"range $x PSI", "{{range $x := .PSI}}<{{$x}}>{{end}}", "<21><22><23>", tVal, true}, {"declare in range", "{{range $x := .PSI}}<{{$foo:=$x}}{{$x}}>{{end}}", "<21><22><23>", tVal, true}, + {"range count", `{{range $i, $x := count 5}}[{{$i}}]{{$x}}{{end}}`, "[0]a[1]b[2]c[3]d[4]e", tVal, true}, + {"range nil count", `{{range $i, $x := count 0}}{{else}}empty{{end}}`, "empty", tVal, true}, // Cute examples. {"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true}, @@ -414,6 +416,11 @@ var execTests = []execTest{ {"bug4", "{{if .Empty0}}non-nil{{else}}nil{{end}}", "nil", tVal, true}, // Stringer. {"bug5", "{{.Str}}", "foozle", tVal, true}, + // Args need to be indirected and dereferenced sometimes. + {"bug6a", "{{vfunc .V0 .V1}}", "vfunc", tVal, true}, + {"bug6b", "{{vfunc .V0 .V0}}", "vfunc", tVal, true}, + {"bug6c", "{{vfunc .V1 .V0}}", "vfunc", tVal, true}, + {"bug6d", "{{vfunc .V1 .V1}}", "vfunc", tVal, true}, } func zeroArgs() string { @@ -424,9 +431,35 @@ func oneArg(a string) string { return "oneArg=" + a } +// count returns a channel that will deliver n sequential 1-letter strings starting at "a" +func count(n int) chan string { + if n == 0 { + return nil + } + c := make(chan string) + go func() { + for i := 0; i < n; i++ { + c <- "abcdefghijklmnop"[i : i+1] + } + close(c) + }() + return c +} + +// vfunc takes a *V and a V +func vfunc(V, *V) string { + return "vfunc" +} + func testExecute(execTests []execTest, set *Set, t *testing.T) { b := new(bytes.Buffer) - funcs := FuncMap{"zeroArgs": zeroArgs, "oneArg": oneArg, "typeOf": typeOf} + funcs := FuncMap{ + "count": count, + "oneArg": oneArg, + "typeOf": typeOf, + "vfunc": vfunc, + "zeroArgs": zeroArgs, + } for _, test := range execTests { tmpl := New(test.name).Funcs(funcs) _, err := tmpl.ParseInSet(test.input, set) |
