diff options
| author | Rob Pike <r@golang.org> | 2012-09-24 13:23:15 +1000 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2012-09-24 13:23:15 +1000 |
| commit | 9050550c12e2d09cf8f0c22a270cfa90120cdf6d (patch) | |
| tree | 014cb848c742805de43d7d3f5293e830af2baef6 /src/pkg/text/template/exec_test.go | |
| parent | edce6349639e321c3b1a34036a8fbc08ad363cd3 (diff) | |
| download | go-9050550c12e2d09cf8f0c22a270cfa90120cdf6d.tar.xz | |
text/template: allow .Field access to parenthesized expressions
Change the grammar so that field access is a proper operator.
This introduces a new node, ChainNode, into the public (but
actually internal) API of text/template/parse. For
compatibility, we only use the new node type for the specific
construct, which was not parseable before. Therefore this
should be backward-compatible.
Before, .X.Y was a token in the lexer; this CL breaks it out
into .Y applied to .X. But for compatibility we mush them
back together before delivering. One day we might remove
that hack; it's the simple TODO in parse.go/operand.
This change also provides grammatical distinction between
f
and
(f)
which might permit function values later, but not now.
Fixes #3999.
R=golang-dev, dsymonds, gri, rsc, mikesamuel
CC=golang-dev
https://golang.org/cl/6494119
Diffstat (limited to 'src/pkg/text/template/exec_test.go')
| -rw-r--r-- | src/pkg/text/template/exec_test.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/pkg/text/template/exec_test.go b/src/pkg/text/template/exec_test.go index 7f60dcafa5..0835d31f79 100644 --- a/src/pkg/text/template/exec_test.go +++ b/src/pkg/text/template/exec_test.go @@ -340,6 +340,12 @@ var execTests = []execTest{ // Parenthesized expressions {"parens in pipeline", "{{printf `%d %d %d` (1) (2 | add 3) (add 4 (add 5 6))}}", "1 5 15", tVal, true}, + // Parenthesized expressions with field accesses + {"parens: $ in paren", "{{($).X}}", "x", tVal, true}, + {"parens: $.GetU in paren", "{{($.GetU).V}}", "v", tVal, true}, + {"parens: $ in paren in pipe", "{{($ | echo).X}}", "x", tVal, true}, + {"parens: spaces and args", `{{(makemap "up" "down" "left" "right").left}}`, "right", tVal, true}, + // If. {"if true", "{{if true}}TRUE{{end}}", "TRUE", tVal, true}, {"if false", "{{if false}}TRUE{{else}}FALSE{{end}}", "FALSE", tVal, true}, @@ -535,6 +541,21 @@ func add(args ...int) int { return sum } +func echo(arg interface{}) interface{} { + return arg +} + +func makemap(arg ...string) map[string]string { + if len(arg)%2 != 0 { + panic("bad makemap") + } + m := make(map[string]string) + for i := 0; i < len(arg); i += 2 { + m[arg[i]] = arg[i+1] + } + return m +} + func stringer(s fmt.Stringer) string { return s.String() } @@ -545,6 +566,8 @@ func testExecute(execTests []execTest, template *Template, t *testing.T) { "add": add, "count": count, "dddArg": dddArg, + "echo": echo, + "makemap": makemap, "oneArg": oneArg, "typeOf": typeOf, "vfunc": vfunc, |
