OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [html/] [template/] [clone_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package template
6
 
7
import (
8
        "bytes"
9
        "testing"
10
)
11
 
12
func TestClone(t *testing.T) {
13
        tests := []struct {
14
                input, want, wantClone string
15
        }{
16
                {
17
                        `Hello, {{if true}}{{""}}{{end}}!`,
18
                        "Hello, !",
19
                        "Hello, <World>!",
20
                },
21
                {
22
                        `Hello, {{if false}}{{.X}}{{else}}{{""}}{{end}}!`,
23
                        "Hello, !",
24
                        "Hello, <World>!",
25
                },
26
                {
27
                        `Hello, {{with ""}}{{.}}{{end}}!`,
28
                        "Hello, !",
29
                        "Hello, <World>!",
30
                },
31
                {
32
                        `{{range .}}

{{.}}

{{end}}`,
33
                        "

foo

baz

",
34
                        "

foo

<bar>

baz

",
35
                },
36
                {
37
                        `Hello, {{"" | html}}!`,
38
                        "Hello, <World>!",
39
                        "Hello, <World>!",
40
                },
41
                {
42
                        `Hello{{if 1}}, World{{else}}{{template "d"}}{{end}}!`,
43
                        "Hello, World!",
44
                        "Hello, World!",
45
                },
46
        }
47
 
48
        for _, test := range tests {
49
                s, err := New("s").Parse(test.input)
50
                if err != nil {
51
                        t.Errorf("input=%q: unexpected parse error %v", test.input, err)
52
                }
53
 
54
                d, _ := New("d").Parse(test.input)
55
                // Hack: just replace the root of the tree.
56
                d.text.Root = cloneList(s.text.Root)
57
 
58
                if want, got := s.text.Root.String(), d.text.Root.String(); want != got {
59
                        t.Errorf("want %q, got %q", want, got)
60
                }
61
 
62
                err = escapeTemplates(d, "d")
63
                if err != nil {
64
                        t.Errorf("%q: failed to escape: %s", test.input, err)
65
                        continue
66
                }
67
 
68
                if want, got := "s", s.Name(); want != got {
69
                        t.Errorf("want %q, got %q", want, got)
70
                        continue
71
                }
72
                if want, got := "d", d.Name(); want != got {
73
                        t.Errorf("want %q, got %q", want, got)
74
                        continue
75
                }
76
 
77
                data := []string{"foo", "", "baz"}
78
 
79
                var b bytes.Buffer
80
                d.Execute(&b, data)
81
                if got := b.String(); got != test.wantClone {
82
                        t.Errorf("input=%q: want %q, got %q", test.input, test.wantClone, got)
83
                }
84
 
85
                // Make sure escaping d did not affect s.
86
                b.Reset()
87
                s.text.Execute(&b, data)
88
                if got := b.String(); got != test.want {
89
                        t.Errorf("input=%q: want %q, got %q", test.input, test.want, got)
90
                }
91
        }
92
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.