Commit 1b4dff0f authored by Rob Pike's avatar Rob Pike

exp/template: simplify the helper functions

- create a pair of "Must" functions, one for templates, one for sets
- regularize the return values of the parsers so they can be wrapped by Must
- delete all the old Must functions and methods.

R=golang-dev, gri, r
CC=golang-dev
https://golang.org/cl/4826052
parent 28a23675
......@@ -72,7 +72,7 @@ var tVal = &T{
Empty4: &U{"UinEmpty"},
PI: newInt(23),
PSI: newIntSlice(21, 22, 23),
Tmpl: New("x").MustParse("test template"), // "x" is the value of .X
Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X
}
// Helpers for creation.
......@@ -359,7 +359,7 @@ func testExecute(execTests []execTest, set *Set, t *testing.T) {
funcs := FuncMap{"zeroArgs": zeroArgs, "oneArg": oneArg, "typeOf": typeOf}
for _, test := range execTests {
tmpl := New(test.name).Funcs(funcs)
err := tmpl.ParseInSet(test.input, set)
_, err := tmpl.ParseInSet(test.input, set)
if err != nil {
t.Errorf("%s: parse error: %s", test.name, err)
continue
......@@ -394,7 +394,7 @@ func TestExecute(t *testing.T) {
func TestExecuteError(t *testing.T) {
b := new(bytes.Buffer)
tmpl := New("error")
err := tmpl.Parse("{{.EPERM true}}")
_, err := tmpl.Parse("{{.EPERM true}}")
if err != nil {
t.Fatalf("parse error: %s", err)
}
......@@ -487,7 +487,7 @@ func TestTree(t *testing.T) {
},
}
set := new(Set)
err := set.Parse(treeTemplate)
_, err := set.Parse(treeTemplate)
if err != nil {
t.Fatal("parse error:", err)
}
......
This diff is collapsed.
......@@ -610,25 +610,25 @@ func (t *Template) atEOF() bool {
// Parse parses the template definition string to construct an internal
// representation of the template for execution.
func (t *Template) Parse(s string) (err os.Error) {
func (t *Template) Parse(s string) (tmpl *Template, err os.Error) {
defer t.recover(&err)
t.startParse(t.set, lex(t.name, s))
t.parse(true)
t.stopParse()
return
return t, nil
}
// ParseInSet parses the template definition string to construct an internal
// representation of the template for execution. It also adds the template
// to the set.
// Function bindings are checked against those in the set.
func (t *Template) ParseInSet(s string, set *Set) (err os.Error) {
func (t *Template) ParseInSet(s string, set *Set) (tmpl *Template, err os.Error) {
defer t.recover(&err)
t.startParse(set, lex(t.name, s))
t.parse(true)
t.stopParse()
t.addToSet(set)
return nil
return t, nil
}
// addToSet adds the template to the set, verifying it's not being double-assigned.
......
......@@ -230,8 +230,7 @@ var parseTests = []parseTest{
func TestParse(t *testing.T) {
for _, test := range parseTests {
tmpl := New(test.name)
err := tmpl.Parse(test.input)
tmpl, err := New(test.name).Parse(test.input)
switch {
case err == nil && !test.ok:
t.Errorf("%q: expected error; got none", test.name)
......
......@@ -99,7 +99,8 @@ func (s *Set) recover(errp *os.Error) {
// multiple times for a given set, adding the templates defined in the string
// to the set. If a template is redefined, the element in the set is
// overwritten with the new definition.
func (s *Set) Parse(text string) (err os.Error) {
func (s *Set) Parse(text string) (set *Set, err os.Error) {
set = s
s.init()
defer s.recover(&err)
lex := lex("set", text)
......@@ -130,5 +131,5 @@ func (s *Set) Parse(text string) (err os.Error) {
t.addToSet(s)
s.tmpl[t.name] = t
}
return nil
return s, nil
}
......@@ -38,8 +38,7 @@ var setParseTests = []setParseTest{
func TestSetParse(t *testing.T) {
for _, test := range setParseTests {
set := new(Set)
err := set.Parse(test.input)
set, err := new(Set).Parse(test.input)
switch {
case err == nil && !test.ok:
t.Errorf("%q: expected error; got none", test.name)
......@@ -54,6 +53,9 @@ func TestSetParse(t *testing.T) {
}
continue
}
if set == nil {
continue
}
if len(set.tmpl) != len(test.names) {
t.Errorf("%s: wrong number of templates; wanted %d got %d", test.name, len(test.names), len(set.tmpl))
continue
......@@ -101,12 +103,11 @@ const setText2 = `
func TestSetExecute(t *testing.T) {
// Declare a set with a couple of templates first.
set := new(Set)
err := set.Parse(setText1)
set, err := new(Set).Parse(setText1)
if err != nil {
t.Fatalf("error parsing set: %s", err)
}
err = set.Parse(setText2)
_, err = set.Parse(setText2)
if err != nil {
t.Fatalf("error parsing set: %s", err)
}
......@@ -114,12 +115,11 @@ func TestSetExecute(t *testing.T) {
}
func TestSetParseFile(t *testing.T) {
set := new(Set)
err := set.ParseFile("DOES NOT EXIST")
set, err := new(Set).ParseFile("DOES NOT EXIST")
if err == nil {
t.Error("expected error for non-existent file; got none")
}
err = set.ParseFile("testdata/file1.tmpl", "testdata/file2.tmpl")
_, err = set.ParseFile("testdata/file1.tmpl", "testdata/file2.tmpl")
if err != nil {
t.Fatalf("error parsing files: %v", err)
}
......@@ -139,16 +139,15 @@ func TestParseSetFile(t *testing.T) {
}
func TestSetParseFiles(t *testing.T) {
set := new(Set)
err := set.ParseFiles("DOES NOT EXIST")
set, err := new(Set).ParseFiles("DOES NOT EXIST")
if err == nil {
t.Error("expected error for non-existent file; got none")
}
err = set.ParseFiles("[x")
_, err = set.ParseFiles("[x")
if err == nil {
t.Error("expected error for bad pattern; got none")
}
err = set.ParseFiles("testdata/file*.tmpl")
_, err = set.ParseFiles("testdata/file*.tmpl")
if err != nil {
t.Fatalf("error parsing files: %v", err)
}
......@@ -172,16 +171,15 @@ func TestParseSetFiles(t *testing.T) {
}
var templateFileExecTests = []execTest{
{"teset", `{{template "tmpl1.tmpl"}}{{template "tmpl2.tmpl"}}`, "template1\ntemplate2\n", 0, true},
{"test", `{{template "tmpl1.tmpl"}}{{template "tmpl2.tmpl"}}`, "template1\ntemplate2\n", 0, true},
}
func TestSetParseTemplateFile(t *testing.T) {
set := new(Set)
err := set.ParseTemplateFile("DOES NOT EXIST")
set, err := ParseTemplateFile("DOES NOT EXIST")
if err == nil {
t.Error("expected error for non-existent file; got none")
}
err = set.ParseTemplateFile("testdata/tmpl1.tmpl", "testdata/tmpl2.tmpl")
_, err = set.ParseTemplateFile("testdata/tmpl1.tmpl", "testdata/tmpl2.tmpl")
if err != nil {
t.Fatalf("error parsing files: %v", err)
}
......@@ -201,16 +199,15 @@ func TestParseTemplateFile(t *testing.T) {
}
func TestSetParseTemplateFiles(t *testing.T) {
set := new(Set)
err := set.ParseTemplateFiles("DOES NOT EXIST")
set, err := ParseTemplateFiles("DOES NOT EXIST")
if err == nil {
t.Error("expected error for non-existent file; got none")
}
err = set.ParseTemplateFiles("[x")
_, err = set.ParseTemplateFiles("[x")
if err == nil {
t.Error("expected error for bad pattern; got none")
}
err = set.ParseTemplateFiles("testdata/tmpl*.tmpl")
_, err = set.ParseTemplateFiles("testdata/tmpl*.tmpl")
if err != nil {
t.Fatalf("error parsing files: %v", err)
}
......
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