Commit cae23f03 authored by Scott Lawrence's avatar Scott Lawrence Committed by Rob Pike

template: fix error checking on execute without parse

Fixed error checking in exec.go to give a sensible error message when
execution is attempted before a successful parse (rather than an
outright panic).

R=r
CC=golang-dev
https://golang.org/cl/5306065
parent 92926f54
...@@ -1549,8 +1549,8 @@ func TestEnsurePipelineContains(t *testing.T) { ...@@ -1549,8 +1549,8 @@ func TestEnsurePipelineContains(t *testing.T) {
} }
} }
func expectExecuteFailure(t *testing.T, b *bytes.Buffer) { func expectExecuteFailure(t *testing.T, b *bytes.Buffer, err os.Error) {
if x := recover(); x != nil { if err != nil {
if b.Len() != 0 { if b.Len() != 0 {
t.Errorf("output on buffer: %q", b.String()) t.Errorf("output on buffer: %q", b.String())
} }
...@@ -1563,8 +1563,8 @@ func TestEscapeErrorsNotIgnorable(t *testing.T) { ...@@ -1563,8 +1563,8 @@ func TestEscapeErrorsNotIgnorable(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
tmpl := template.Must(template.New("dangerous").Parse("<a")) tmpl := template.Must(template.New("dangerous").Parse("<a"))
Escape(tmpl) Escape(tmpl)
defer expectExecuteFailure(t, &b) err := tmpl.Execute(&b, nil)
tmpl.Execute(&b, nil) expectExecuteFailure(t, &b, err)
} }
func TestEscapeSetErrorsNotIgnorable(t *testing.T) { func TestEscapeSetErrorsNotIgnorable(t *testing.T) {
...@@ -1574,8 +1574,8 @@ func TestEscapeSetErrorsNotIgnorable(t *testing.T) { ...@@ -1574,8 +1574,8 @@ func TestEscapeSetErrorsNotIgnorable(t *testing.T) {
} }
EscapeSet(s, "t") EscapeSet(s, "t")
var b bytes.Buffer var b bytes.Buffer
defer expectExecuteFailure(t, &b) err = s.Execute(&b, "t", nil)
s.Execute(&b, "t", nil) expectExecuteFailure(t, &b, err)
} }
func TestRedundantFuncs(t *testing.T) { func TestRedundantFuncs(t *testing.T) {
......
...@@ -97,7 +97,7 @@ func (t *Template) Execute(wr io.Writer, data interface{}) (err os.Error) { ...@@ -97,7 +97,7 @@ func (t *Template) Execute(wr io.Writer, data interface{}) (err os.Error) {
line: 1, line: 1,
vars: []variable{{"$", value}}, vars: []variable{{"$", value}},
} }
if t.Root == nil { if t.Tree == nil || t.Root == nil {
state.errorf("must be parsed before execution") state.errorf("must be parsed before execution")
} }
state.walk(value, t.Root) state.walk(value, t.Root)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment