forked from flowchartsman/handlebars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_test.go
166 lines (129 loc) · 3.28 KB
/
template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package handlebars
import (
"fmt"
"testing"
)
var sourceBasic = `<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>`
var basicAST = `CONTENT[ '<div class="entry">
<h1>' ]
{{ PATH:title [] }}
CONTENT[ '</h1>
<div class="body">
' ]
{{ PATH:body [] }}
CONTENT[ '
</div>
</div>' ]
`
func TestNewTemplate(t *testing.T) {
t.Parallel()
tpl := newTemplate(sourceBasic)
if tpl.source != sourceBasic {
t.Errorf("Failed to instantiate template")
}
}
func TestParse(t *testing.T) {
t.Parallel()
tpl, err := Parse(sourceBasic)
if err != nil || (tpl.source != sourceBasic) {
t.Errorf("Failed to parse template")
}
if str := tpl.PrintAST(); str != basicAST {
t.Errorf("Template parsing incorrect: %s", str)
}
}
func TestClone(t *testing.T) {
t.Parallel()
sourcePartial := `I am a {{wat}} partial`
sourcePartial2 := `Partial for the {{wat}}`
tpl := MustParse(sourceBasic)
tpl.RegisterPartial("p", sourcePartial)
if (len(tpl.partials) != 1) || (tpl.partials["p"] == nil) {
t.Errorf("What?")
}
cloned := tpl.Clone()
if (len(cloned.partials) != 1) || (cloned.partials["p"] == nil) {
t.Errorf("Template partials must be cloned")
}
cloned.RegisterPartial("p2", sourcePartial2)
if (len(cloned.partials) != 2) || (cloned.partials["p"] == nil) || (cloned.partials["p2"] == nil) {
t.Errorf("Failed to register a partial on cloned template")
}
if (len(tpl.partials) != 1) || (tpl.partials["p"] == nil) {
t.Errorf("Modification of a cloned template MUST NOT affect original template")
}
}
func ExampleTemplate_Exec() {
source := "<h1>{{title}}</h1><p>{{body.content}}</p>"
ctx := map[string]interface{}{
"title": "foo",
"body": map[string]string{"content": "bar"},
}
// parse template
tpl := MustParse(source)
// evaluate template with context
output, err := tpl.Exec(ctx)
if err != nil {
panic(err)
}
fmt.Print(output)
// Output: <h1>foo</h1><p>bar</p>
}
func ExampleTemplate_MustExec() {
source := "<h1>{{title}}</h1><p>{{body.content}}</p>"
ctx := map[string]interface{}{
"title": "foo",
"body": map[string]string{"content": "bar"},
}
// parse template
tpl := MustParse(source)
// evaluate template with context
output := tpl.MustExec(ctx)
fmt.Print(output)
// Output: <h1>foo</h1><p>bar</p>
}
func ExampleTemplate_ExecWith() {
source := "<h1>{{title}}</h1><p>{{#body}}{{content}} and {{@baz.bat}}{{/body}}</p>"
ctx := map[string]interface{}{
"title": "foo",
"body": map[string]string{"content": "bar"},
}
// parse template
tpl := MustParse(source)
// computes private data frame
frame := NewDataFrame()
frame.Set("baz", map[string]string{"bat": "unicorns"})
// evaluate template
output, err := tpl.ExecWith(ctx, frame)
if err != nil {
panic(err)
}
fmt.Print(output)
// Output: <h1>foo</h1><p>bar and unicorns</p>
}
func ExampleTemplate_PrintAST() {
source := "<h1>{{title}}</h1><p>{{#body}}{{content}} and {{@baz.bat}}{{/body}}</p>"
// parse template
tpl := MustParse(source)
// print AST
output := tpl.PrintAST()
fmt.Print(output)
// Output: CONTENT[ '<h1>' ]
// {{ PATH:title [] }}
// CONTENT[ '</h1><p>' ]
// BLOCK:
// PATH:body []
// PROGRAM:
// {{ PATH:content []
// }}
// CONTENT[ ' and ' ]
// {{ @PATH:baz/bat []
// }}
// CONTENT[ '</p>' ]
//
}