]> Cypherpunks repositories - gostls13.git/commitdiff
text/template/parse: print TextNodes using %s not %q
authorRob Pike <r@golang.org>
Wed, 31 Jul 2013 05:09:13 +0000 (15:09 +1000)
committerRob Pike <r@golang.org>
Wed, 31 Jul 2013 05:09:13 +0000 (15:09 +1000)
This means that printing a Node will produce output that can be used as valid input.
It won't be exactly the same - some spacing may be different - but it will mean the same.

Fixes #4593.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/12006047

src/pkg/text/template/multi_test.go
src/pkg/text/template/parse/node.go
src/pkg/text/template/parse/parse_test.go

index bd98bd047ec46b8cff57ea9f5f7634e4003cf215..1f6ed5d8e22d4b6ca304f7000effa7b81a9bc1ff 100644 (file)
@@ -33,10 +33,10 @@ var multiParseTests = []multiParseTest{
                nil},
        {"one", `{{define "foo"}} FOO {{end}}`, noError,
                []string{"foo"},
-               []string{`" FOO "`}},
+               []string{" FOO "}},
        {"two", `{{define "foo"}} FOO {{end}}{{define "bar"}} BAR {{end}}`, noError,
                []string{"foo", "bar"},
-               []string{`" FOO "`, `" BAR "`}},
+               []string{" FOO ", " BAR "}},
        // errors
        {"missing end", `{{define "foo"}} FOO `, hasError,
                nil,
index 9d0d09eb5fa644c34817bc2fe94324735bc10135..dc6a3bb929c76253d95c882bb0c41b2f201a5242 100644 (file)
@@ -13,6 +13,8 @@ import (
        "strings"
 )
 
+var textFormat = "%s" // Changed to "%q" in tests for better error messages.
+
 // A Node is an element in the parse tree. The interface is trivial.
 // The interface contains an unexported method so that only
 // types local to this package can satisfy it.
@@ -125,7 +127,7 @@ func newText(pos Pos, text string) *TextNode {
 }
 
 func (t *TextNode) String() string {
-       return fmt.Sprintf("%q", t.Text)
+       return fmt.Sprintf(textFormat, t.Text)
 }
 
 func (t *TextNode) Copy() Node {
index 695c76ebfe3649f43888558b80cca63b2012c889..0e5c1448c854e3fc0cf5e2cbb7c9c364eaa1da9c 100644 (file)
@@ -256,6 +256,8 @@ var builtins = map[string]interface{}{
 }
 
 func testParse(doCopy bool, t *testing.T) {
+       textFormat = "%q"
+       defer func() { textFormat = "%s" }()
        for _, test := range parseTests {
                tmpl, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree), builtins)
                switch {