-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_example_test.go
More file actions
158 lines (131 loc) · 3.72 KB
/
Copy pathplugin_example_test.go
File metadata and controls
158 lines (131 loc) · 3.72 KB
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
package petra_test
import (
"bytes"
"fmt"
"html/template"
"testing"
"testing/fstest"
"github.com/illyabusigin/petra"
)
type badgePlugin struct{}
func (badgePlugin) Funcs() (template.FuncMap, error) {
return template.FuncMap{
"badge": func(label string) template.HTML {
return template.HTML(`<span class="badge">` + template.HTMLEscapeString(label) + `</span>`)
},
}, nil
}
func (badgePlugin) Apply(*template.Template) error {
return nil
}
func ExamplePlugin() {
files := fstest.MapFS{
"templates/layout.html": {Data: []byte(`{{block "content" .}}{{end}}`)},
"templates/index.html": {Data: []byte(`{{define "content"}}{{badge .Role}}{{end}}`)},
}
tmpl := petra.NewWithOptions(petra.Options{
Plugins: petra.Plugins{badgePlugin{}},
})
if err := tmpl.ParseFS(files, "templates"); err != nil {
panic(err)
}
var out bytes.Buffer
if err := tmpl.ExecuteTemplate(&out, "index", map[string]string{
"Role": "Admin <Owner>",
}); err != nil {
panic(err)
}
fmt.Println(out.String())
// Output:
// <span class="badge">Admin <Owner></span>
}
type labelPlugin string
func (p labelPlugin) Funcs() (template.FuncMap, error) {
return template.FuncMap{
"label": func() string {
return string(p)
},
}, nil
}
func (labelPlugin) Apply(*template.Template) error {
return nil
}
func TestPluginFunctionsOverrideFuncMap(t *testing.T) {
got := renderPluginFixture(t, template.FuncMap{
"label": func() string {
return "funcmap"
},
}, petra.Plugins{labelPlugin("plugin")})
if got != "plugin" {
t.Fatalf("rendered label = %q, want plugin", got)
}
}
func TestLaterPluginFunctionsOverrideEarlierPlugins(t *testing.T) {
got := renderPluginFixture(t, nil, petra.Plugins{
labelPlugin("first"),
labelPlugin("second"),
})
if got != "second" {
t.Fatalf("rendered label = %q, want second", got)
}
}
type cachedPlugin struct{}
func (cachedPlugin) Funcs() (template.FuncMap, error) {
calls := 0
return template.FuncMap{
"cached": func() string {
calls++
return fmt.Sprintf("hit-%d", calls)
},
}, nil
}
func (cachedPlugin) Apply(*template.Template) error {
return nil
}
func TestPluginFunctionClosuresResetAfterReparse(t *testing.T) {
files := fstest.MapFS{
"templates/layout.html": {Data: []byte(`{{block "content" .}}{{end}}`)},
"templates/index.html": {Data: []byte(`{{define "content"}}{{cached}}{{end}}`)},
}
tmpl := petra.NewWithOptions(petra.Options{
Plugins: petra.Plugins{cachedPlugin{}},
})
if err := tmpl.ParseFS(files, "templates"); err != nil {
t.Fatalf("ParseFS() error = %v", err)
}
if got := executePluginFixture(t, tmpl); got != "hit-1" {
t.Fatalf("first render = %q, want hit-1", got)
}
if got := executePluginFixture(t, tmpl); got != "hit-2" {
t.Fatalf("second render = %q, want hit-2", got)
}
if err := tmpl.ParseFS(files, "templates"); err != nil {
t.Fatalf("second ParseFS() error = %v", err)
}
if got := executePluginFixture(t, tmpl); got != "hit-1" {
t.Fatalf("render after reparse = %q, want hit-1", got)
}
}
func renderPluginFixture(t *testing.T, funcs template.FuncMap, plugins petra.Plugins) string {
t.Helper()
files := fstest.MapFS{
"templates/layout.html": {Data: []byte(`{{block "content" .}}{{end}}`)},
"templates/index.html": {Data: []byte(`{{define "content"}}{{label}}{{end}}`)},
}
tmpl := petra.NewWithOptions(petra.Options{
FuncMap: funcs,
Plugins: plugins,
})
if err := tmpl.ParseFS(files, "templates"); err != nil {
t.Fatalf("ParseFS() error = %v", err)
}
return executePluginFixture(t, tmpl)
}
func executePluginFixture(t *testing.T, tmpl *petra.Template) string {
t.Helper()
var out bytes.Buffer
if err := tmpl.ExecuteTemplate(&out, "index", nil); err != nil {
t.Fatalf("ExecuteTemplate() error = %v", err)
}
return out.String()
}