]> Cypherpunks repositories - gostls13.git/commitdiff
exp/template: add a tree-walking example to the test.
authorRob Pike <r@golang.org>
Fri, 8 Jul 2011 06:49:06 +0000 (16:49 +1000)
committerRob Pike <r@golang.org>
Fri, 8 Jul 2011 06:49:06 +0000 (16:49 +1000)
Also fix a comment formatting glitch.

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/4672054

src/pkg/exp/template/exec.go
src/pkg/exp/template/exec_test.go

index 09bf8587e08a466892997467fca8744e4fef280f..42279c2e6b9861265f6f4da6975417a5b6b8cb78 100644 (file)
@@ -267,7 +267,7 @@ func isExported(name string) bool {
 // The 'final' argument represents the return value from the preceding
 // value of the pipeline, if any.
 // If we're in a chain, such as (.X.Y.Z), .X and .Y cannot be methods;
-//canBeMethod will be true only for the last element of such chains (here .Z).
+// canBeMethod will be true only for the last element of such chains (here .Z).
 // The isFirst argument tells whether this is the first element of a chain (here .X).
 // If true, evaluation is allowed to examine the parent to resolve the reference.
 func (s *state) evalField(data reflect.Value, fieldName string, args []node, final reflect.Value,
index db3e89f63d44c6e3d5363d30c90286611bc031c1..5d771a2b77500014433eabf4b86276be65916168 100644 (file)
@@ -346,3 +346,84 @@ func TestJSEscaping(t *testing.T) {
                }
        }
 }
+
+// A nice example: walk a binary tree.
+
+type Tree struct {
+       Val         int
+       Left, Right *Tree
+}
+
+const treeTemplate = `
+       {{define "tree"}}
+       [
+               {{.Val}}
+               {{with .Left}}
+                       {{template "tree" .}}
+               {{end}}
+               {{with .Right}}
+                       {{template "tree" .}}
+               {{end}}
+       ]
+       {{end}}
+`
+
+func TestTree(t *testing.T) {
+       var tree = &Tree{
+               1,
+               &Tree{
+                       2, &Tree{
+                               3,
+                               &Tree{
+                                       4, nil, nil,
+                               },
+                               nil,
+                       },
+                       &Tree{
+                               5,
+                               &Tree{
+                                       6, nil, nil,
+                               },
+                               nil,
+                       },
+               },
+               &Tree{
+                       7,
+                       &Tree{
+                               8,
+                               &Tree{
+                                       9, nil, nil,
+                               },
+                               nil,
+                       },
+                       &Tree{
+                               10,
+                               &Tree{
+                                       11, nil, nil,
+                               },
+                               nil,
+                       },
+               },
+       }
+       set := NewSet()
+       err := set.Parse(treeTemplate)
+       if err != nil {
+               t.Fatal("parse error:", err)
+       }
+       var b bytes.Buffer
+       err = set.Execute("tree", &b, tree)
+       if err != nil {
+               t.Fatal("exec error:", err)
+       }
+       stripSpace := func(r int) int {
+               if r == '\t' || r == '\n' {
+                       return -1
+               }
+               return r
+       }
+       result := strings.Map(stripSpace, b.String())
+       const expect = "[1[2[3[4]][5[6]]][7[8[9]][10[11]]]]"
+       if result != expect {
+               t.Errorf("expected %q got %q", expect, result)
+       }
+}