diff options
| author | Rob Pike <r@golang.org> | 2013-03-06 12:34:19 -0800 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2013-03-06 12:34:19 -0800 |
| commit | c76379954f57399b2e84528ac369f5cb07698acf (patch) | |
| tree | 0a0e225bdc3445ec50525e259ba862638d7fab42 /src/pkg/text/template/exec_test.go | |
| parent | 4f43201e51f36e7db909ff3c3a86104dada5161b (diff) | |
| download | go-c76379954f57399b2e84528ac369f5cb07698acf.tar.xz | |
text/template: improve error reporting for executing an empty template
Fixes #4522.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/7502044
Diffstat (limited to 'src/pkg/text/template/exec_test.go')
| -rw-r--r-- | src/pkg/text/template/exec_test.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/pkg/text/template/exec_test.go b/src/pkg/text/template/exec_test.go index 683e9ac76b..0f8beec5ed 100644 --- a/src/pkg/text/template/exec_test.go +++ b/src/pkg/text/template/exec_test.go @@ -816,3 +816,40 @@ func TestExecuteOnNewTemplate(t *testing.T) { // This is issue 3872. _ = New("Name").Templates() } + +const testTemplates = `{{define "one"}}one{{end}}{{define "two"}}two{{end}}` + +func TestMessageForExecuteEmpty(t *testing.T) { + // Test a truly empty template. + tmpl := New("empty") + var b bytes.Buffer + err := tmpl.Execute(&b, 0) + if err == nil { + t.Fatal("expected initial error") + } + got := err.Error() + want := `template: empty: "empty" is an incomplete or empty template` + if got != want { + t.Errorf("expected error %s got %s", want, got) + } + // Add a non-empty template to check that the error is helpful. + tests, err := New("").Parse(testTemplates) + if err != nil { + t.Fatal(err) + } + tmpl.AddParseTree("secondary", tests.Tree) + err = tmpl.Execute(&b, 0) + if err == nil { + t.Fatal("expected second error") + } + got = err.Error() + want = `template: empty: "empty" is an incomplete or empty template; defined templates are: "secondary"` + if got != want { + t.Errorf("expected error %s got %s", want, got) + } + // Make sure we can execute the secondary. + err = tmpl.ExecuteTemplate(&b, "secondary", 0) + if err != nil { + t.Fatal(err) + } +} |
