-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathhandlebars_test.go
290 lines (241 loc) · 7.98 KB
/
handlebars_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package handlebars
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/mickael-menu/zk/internal/adapter/handlebars/helpers"
"github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util"
"github.com/mickael-menu/zk/internal/util/fixtures"
"github.com/mickael-menu/zk/internal/util/paths"
"github.com/mickael-menu/zk/internal/util/test/assert"
)
func init() {
Init(true, &util.NullLogger)
}
// styler is a test double for core.Styler
// "hello", "red" -> "red(hello)"
type styler struct{}
func (s *styler) Style(text string, rules ...core.Style) (string, error) {
return s.MustStyle(text, rules...), nil
}
func (s *styler) MustStyle(text string, rules ...core.Style) string {
for _, rule := range rules {
text = fmt.Sprintf("%s(%s)", rule, text)
}
return text
}
func testString(t *testing.T, template string, context interface{}, expected string) {
sut := testLoader(LoaderOpts{})
templ, err := sut.LoadTemplate(template)
assert.Nil(t, err)
actual, err := templ.Render(context)
assert.Nil(t, err)
assert.Equal(t, actual, expected)
}
func testFile(t *testing.T, name string, context interface{}, expected string) {
sut := testLoader(LoaderOpts{})
templ, err := sut.LoadTemplateAt(fixtures.Path(name))
assert.Nil(t, err)
actual, err := templ.Render(context)
assert.Nil(t, err)
assert.Equal(t, actual, expected)
}
func TestLookupPaths(t *testing.T) {
root := fmt.Sprintf("/tmp/zk-test-%d", time.Now().Unix())
os.Remove(root)
path1 := filepath.Join(root, "1")
os.MkdirAll(path1, os.ModePerm)
path2 := filepath.Join(root, "1")
os.MkdirAll(filepath.Join(path2, "subdir"), os.ModePerm)
sut := testLoader(LoaderOpts{LookupPaths: []string{path1, path2}})
test := func(path string, expected string) {
tpl, err := sut.LoadTemplateAt(path)
assert.Nil(t, err)
res, err := tpl.Render(nil)
assert.Nil(t, err)
assert.Equal(t, res, expected)
}
test1 := filepath.Join(path1, "test1.tpl")
tpl1, err := sut.LoadTemplateAt(test1)
assert.Err(t, err, "cannot find template at "+test1)
assert.Nil(t, tpl1)
paths.WriteString(test1, "Test 1")
test(test1, "Test 1") // absolute
test("test1.tpl", "Test 1") // relative
test2 := filepath.Join(path2, "test2.tpl")
paths.WriteString(test2, "Test 2")
test(test2, "Test 2") // absolute
test("test2.tpl", "Test 2") // relative
test3 := filepath.Join(path2, "subdir/test3.tpl")
paths.WriteString(test3, "Test 3")
test(test3, "Test 3") // absolute
test("subdir/test3.tpl", "Test 3") // relative
}
func TestRenderString(t *testing.T) {
testString(t,
"Goodbye, {{name}}",
map[string]string{"name": "Ed"},
"Goodbye, Ed",
)
}
func TestRenderFile(t *testing.T) {
testFile(t,
"template.txt",
map[string]string{"name": "Thom"},
"Hello, Thom\n",
)
}
func TestUnknownVariable(t *testing.T) {
testString(t,
"Hi, {{unknown}}!",
nil,
"Hi, !",
)
}
func TestDoesntEscapeHTML(t *testing.T) {
testString(t,
"Salut, {{name}}!",
map[string]string{"name": "l'ami"},
"Salut, l'ami!",
)
testFile(t,
"unescape.txt",
map[string]string{"name": "l'ami"},
"Salut, l'ami!\n",
)
}
func TestConcatHelper(t *testing.T) {
testString(t, "{{concat '> ' 'A quote'}}", nil, "> A quote")
}
func TestSubstringHelper(t *testing.T) {
testString(t, "{{substring '' 2 4}}", nil, "")
testString(t, "{{substring 'A full quote' 2 4}}", nil, "full")
testString(t, "{{substring 'A full quote' 40 4}}", nil, "")
testString(t, "{{substring 'A full quote' -5 5}}", nil, "quote")
testString(t, "{{substring 'A full quote' -5 6}}", nil, "quote")
}
func TestJoinHelper(t *testing.T) {
test := func(items []string, expected string) {
context := map[string]interface{}{"items": items}
testString(t, "{{join items '-'}}", context, expected)
}
test([]string{}, "")
test([]string{"Item 1"}, "Item 1")
test([]string{"Item 1", "Item 2"}, "Item 1-Item 2")
test([]string{"Item 1", "Item 2", "Item 3"}, "Item 1-Item 2-Item 3")
}
type testJSONObject struct {
Foo string
Missing string `json:"missing,omitempty"`
List []string `json:"stringList"`
}
func TestJSONHelper(t *testing.T) {
test := func(value interface{}, expected string) {
context := map[string]interface{}{"value": value}
testString(t, "{{json value}}", context, expected)
}
test(`foo"bar"`, `"foo\"bar\""`)
test([]string{"foo", "bar"}, `["foo","bar"]`)
test(map[string]string{"foo": "bar"}, `{"foo":"bar"}`)
test(map[string]string{"foo": "bar"}, `{"foo":"bar"}`)
test(testJSONObject{
Foo: "baz",
List: []string{"foo", "bar"},
}, `{"Foo":"baz","stringList":["foo","bar"]}`)
}
func TestPrependHelper(t *testing.T) {
// inline
testString(t, "{{prepend '> ' 'A quote'}}", nil, "> A quote")
// block
testString(t, "{{#prepend '> '}}A quote{{/prepend}}", nil, "> A quote")
testString(t, "{{#prepend '> '}}A quote on\nseveral lines{{/prepend}}", nil, "> A quote on\n> several lines")
}
func TestListHelper(t *testing.T) {
test := func(items []string, expected string) {
context := map[string]interface{}{"items": items}
testString(t, "{{list items}}", context, expected)
}
test([]string{}, "")
test([]string{"Item 1"}, " ‣ Item 1\n")
test([]string{"Item 1", "Item 2"}, " ‣ Item 1\n ‣ Item 2\n")
test([]string{"Item 1", "Item 2", "Item 3"}, " ‣ Item 1\n ‣ Item 2\n ‣ Item 3\n")
test([]string{"An item\non several\nlines\n"}, " ‣ An item\n on several\n lines\n")
}
func TestLinkHelper(t *testing.T) {
sut := testLoader(LoaderOpts{})
templ, err := sut.LoadTemplate(`{{format-link "path/to note.md" "An interesting subject"}}`)
assert.Nil(t, err)
actual, err := templ.Render(map[string]interface{}{})
assert.Nil(t, err)
assert.Equal(t, actual, "path/to note.md - An interesting subject")
}
func TestSlugHelper(t *testing.T) {
// inline
testString(t,
`{{slug "This will be slugified!"}}`,
nil,
"this-will-be-slugified",
)
// block
testString(t,
"{{#slug}}This will be slugified!{{/slug}}",
nil,
"this-will-be-slugified",
)
}
func TestDateHelper(t *testing.T) {
context := map[string]interface{}{"now": time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)}
testString(t, "{{date now}}", context, "2009-11-17")
testString(t, "{{date now 'short'}}", context, "11/17/2009")
testString(t, "{{date now 'medium'}}", context, "Nov 17, 2009")
testString(t, "{{date now 'long'}}", context, "November 17, 2009")
testString(t, "{{date now 'full'}}", context, "Tuesday, November 17, 2009")
testString(t, "{{date now 'year'}}", context, "2009")
testString(t, "{{date now 'time'}}", context, "20:34")
testString(t, "{{date now 'timestamp'}}", context, "200911172034")
testString(t, "{{date now 'timestamp-unix'}}", context, "1258490098")
testString(t, "{{date now 'cust: %Y-%m'}}", context, "cust: 2009-11")
testString(t, "{{date now 'elapsed'}}", context, "13 years ago")
}
func TestShellHelper(t *testing.T) {
// block is passed as piped input
testString(t,
`{{#sh "tr '[a-z]' '[A-Z]'"}}Hello, world!{{/sh}}`,
nil,
"HELLO, WORLD!",
)
// inline
testString(t,
`{{sh "echo 'Hello, world!'"}}`,
nil,
"Hello, world!",
)
// using pipes
testString(t, `{{sh "echo hello | tr '[:lower:]' '[:upper:]'"}}`, nil, "HELLO")
}
func TestStyleHelper(t *testing.T) {
// inline
testString(t, "{{style 'single' 'Some text'}}", nil, "single(Some text)")
testString(t, "{{style 'red bold' 'Another text'}}", nil, "bold(red(Another text))")
// block
testString(t, "{{#style 'single'}}A multiline\ntext{{/style}}", nil, "single(A multiline\ntext)")
}
func testLoader(opts LoaderOpts) *Loader {
if opts.LookupPaths == nil {
opts.LookupPaths = []string{}
}
if opts.Styler == nil {
opts.Styler = &styler{}
}
loader := NewLoader(opts)
loader.RegisterHelper("style", helpers.NewStyleHelper(opts.Styler, &util.NullLogger))
loader.RegisterHelper("slug", helpers.NewSlugHelper("en", &util.NullLogger))
formatter := func(context core.LinkFormatterContext) (string, error) {
return context.Path + " - " + context.Title, nil
}
loader.RegisterHelper("format-link", helpers.NewLinkHelper(formatter, &util.NullLogger))
return loader
}