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