-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmpl_test.go
419 lines (317 loc) · 11.5 KB
/
tmpl_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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package tmpl_test
import (
"fmt"
"github.com/kanamone/tmpl"
"strings"
"testing"
)
func TestTemplate(t *testing.T) {
tmp := tmpl.NewTemplate("/*<slot name>*/content/*</slot>*/")
if tmp == nil {
t.Errorf("Expected template, got nil")
return
}
}
func TestGetSlotMatchesSingle(t *testing.T) {
tmp := tmpl.NewTemplate("/*<slot name>*/content/*</slot>*/")
if tmp == nil {
t.Errorf("Expected template, got nil")
return
}
matches := tmp.GetSlotMatches()
if len(matches) == 0 {
t.Errorf("Expected matches, got empty")
return
}
if matches[0].StartIndex != 0 {
t.Errorf("Expected startByte 0, got %d", matches[0].StartIndex)
}
if matches[0].EndIndex != 33 {
t.Errorf("Expected endByte 8, got %d", matches[0].EndIndex)
}
if matches[0].Identifier != "name" {
t.Errorf("Expected identifier name, got %s", matches[0].Identifier)
}
if matches[0].InnerContent != "content" {
t.Errorf("Expected innerContent content, got %s", matches[0].InnerContent)
}
if matches[0].OuterContent != "/*<slot name>*/content/*</slot>*/" {
t.Errorf("Expected outerContent /*<slot name>*/content/*</slot>*/, got %s", matches[0].OuterContent)
}
}
// TestGetSlotMatchesSingleMultiple tests that the GetSlotMatches function
// e.g. /*<slot a>*/content1/*</slot>*/ /*<slot b>*/content2/*</slot>*/
func TestGetSlotMatchesMultiple(t *testing.T) {
tmp := tmpl.NewTemplate("/*<slot a>*/content1/*</slot>*/ /*<slot b>*/content2/*</slot>*/")
if tmp == nil {
t.Errorf("Expected template, got nil")
return
}
matches := tmp.GetSlotMatches()
if len(matches) == 0 {
t.Errorf("Expected matches, got empty")
return
}
if matches[0].StartIndex != 0 {
t.Errorf("Expected startByte 0, got %d", matches[0].StartIndex)
}
if matches[0].EndIndex != 31 {
t.Errorf("Expected endByte 31, got %d", matches[0].EndIndex)
}
if matches[0].Identifier != "a" {
t.Errorf("Expected identifier a, got %s", matches[0].Identifier)
}
if matches[0].InnerContent != "content1" {
t.Errorf("Expected innerContent content1, got %s", matches[0].InnerContent)
}
if matches[0].OuterContent != "/*<slot a>*/content1/*</slot>*/" {
t.Errorf("Expected outerContent /*<slot a>*/content1/*</slot>*/, got %s", matches[0].OuterContent)
}
if matches[1].StartIndex != 32 {
t.Errorf("Expected startByte 32, got %d", matches[1].StartIndex)
}
if matches[1].EndIndex != 63 {
t.Errorf("Expected endByte 63, got %d", matches[1].EndIndex)
}
if matches[1].Identifier != "b" {
t.Errorf("Expected identifier b, got %s", matches[1].Identifier)
}
if matches[1].InnerContent != "content2" {
t.Errorf("Expected innerContent content2, got %s", matches[1].InnerContent)
}
if matches[1].OuterContent != "/*<slot b>*/content2/*</slot>*/" {
t.Errorf("Expected outerContent /*<slot b>*/content2/*</slot>*/, got %s", matches[1].OuterContent)
}
}
func TestGetSlotMatchesMultilinePreserveTags(t *testing.T) {
tmp := tmpl.NewTemplate("/*<slot name>*/\ncontent\n/*</slot>*/")
if tmp == nil {
t.Errorf("Expected template, got nil")
return
}
matches := tmp.GetSlotMatches()
if len(matches) == 0 {
t.Errorf("Expected matches, got empty")
return
}
if matches[0].StartIndex != 0 {
t.Errorf("Expected startIndex 0, got %d", matches[0].StartIndex)
}
if matches[0].EndIndex != 35 {
t.Errorf("Expected endIndex 8, got %d", matches[0].EndIndex)
}
if matches[0].Identifier != "name" {
t.Errorf("Expected identifier name, got %s", matches[0].Identifier)
}
if matches[0].InnerContent != "\ncontent\n" {
t.Errorf("Expected innerContent content, got %s", matches[0].InnerContent)
}
if matches[0].OuterContent != "/*<slot name>*/\ncontent\n/*</slot>*/" {
t.Errorf("Expected outerContent /*<slot name>*/content/*</slot>*/, got %s", matches[0].OuterContent)
}
}
// TestGetSlotMatchesMultilineOuter tests that the GetSlotMatches function
// correctly returns the start and end indices of the slot delimiters when the
// inner content spans multiple lines. e.g. "Big\n/*<slot name>*/Band/*</slot>*/\nBeat"
func TestGetSlotMatchesMultilineOuter(t *testing.T) {
tmp := tmpl.NewTemplate("Big\n/*<slot name>*/Band/*</slot>*/\nBeat")
if tmp == nil {
t.Errorf("Expected template, got nil")
return
}
matches := tmp.GetSlotMatches()
if len(matches) == 0 {
t.Errorf("Expected matches, got empty")
return
}
if matches[0].StartIndex != 4 {
t.Errorf("Expected startIndex 4, got %d", matches[0].StartIndex)
}
if matches[0].EndIndex != 34 {
t.Errorf("Expected endIndex 34, got %d", matches[0].EndIndex)
}
if matches[0].Identifier != "name" {
t.Errorf("Expected identifier name, got %s", matches[0].Identifier)
}
if matches[0].InnerContent != "Band" {
t.Errorf("Expected innerContent Band, got %s", matches[0].InnerContent)
}
if matches[0].OuterContent != "/*<slot name>*/Band/*</slot>*/" {
t.Errorf("Expected outerContent /*<slot name>*/Band/*</slot>*/, got %s", matches[0].OuterContent)
}
}
func TestGetSlotMatchesUnicode(t *testing.T) {
tmp := tmpl.NewTemplate("/*<slot name>*/🐱🐶🐭🐹🐰/*</slot>*/")
if tmp == nil {
t.Errorf("Expected template, got nil")
return
}
matches := tmp.GetSlotMatches()
if len(matches) == 0 {
t.Errorf("Expected matches, got empty")
return
}
if matches[0].Identifier != "name" {
t.Errorf("Expected identifier name, got %s", matches[0].Identifier)
}
if matches[0].InnerContent != "🐱🐶🐭🐹🐰" {
t.Errorf("Expected innerContent content, got %s", matches[0].InnerContent)
}
if matches[0].OuterContent != "/*<slot name>*/🐱🐶🐭🐹🐰/*</slot>*/" {
t.Errorf("Expected outerContent /*<slot name>*/content/*</slot>*/, got %s", matches[0].OuterContent)
}
if matches[0].StartIndex != 0 {
t.Errorf("Expected startIndex 0, got %d", matches[0].StartIndex)
}
if matches[0].EndIndex != 46 {
t.Errorf("Expected endIndex 46, got %d", matches[0].EndIndex)
}
}
func TestGetSlotMatchesJapanese(t *testing.T) {
tmp := tmpl.NewTemplate("/*<slot name>*/こんにちは/*</slot>*/")
if tmp == nil {
t.Errorf("Expected template, got nil")
return
}
matches := tmp.GetSlotMatches()
if len(matches) == 0 {
t.Errorf("Expected matches, got empty")
return
}
if matches[0].Identifier != "name" {
t.Errorf("Expected identifier name, got %s", matches[0].Identifier)
}
if matches[0].InnerContent != "こんにちは" {
t.Errorf("Expected innerContent content, got %s", matches[0].InnerContent)
}
if matches[0].OuterContent != "/*<slot name>*/こんにちは/*</slot>*/" {
t.Errorf("Expected outerContent /*<slot name>*/content/*</slot>*/, got %s", matches[0].OuterContent)
}
if matches[0].StartIndex != 0 {
t.Errorf("Expected startIndex 0, got %d", matches[0].StartIndex)
}
if matches[0].EndIndex != 41 {
t.Errorf("Expected endIndex 41, got %d", matches[0].EndIndex)
}
}
func TestGetSlotByIdentifier(t *testing.T) {
tmp := tmpl.NewTemplate("/*<slot name>*/content/*</slot>*/")
if tmp == nil {
t.Errorf("Expected template, got nil")
return
}
match, err := tmp.GetSlotMatch("name")
if err != nil {
t.Errorf("Expected nil, got %s", err.Error())
return
}
if match == nil {
t.Errorf("Expected match, got nil")
return
}
if match.Identifier != "name" {
t.Errorf("Expected identifier name, got %s", match.Identifier)
}
if match.InnerContent != "content" {
t.Errorf("Expected innerContent content, got %s", match.InnerContent)
}
if match.OuterContent != "/*<slot name>*/content/*</slot>*/" {
t.Errorf("Expected outerContent /*<slot name>*/content/*</slot>*/, got %s", match.OuterContent)
}
}
func TestReplaceSlot(t *testing.T) {
tmp := tmpl.NewTemplate("Hello, my name is /*<slot name>*/John Doe/*</slot>*/")
replaced, err := tmp.ReplaceSlot("name", "Jane Doe")
if err != nil {
t.Errorf("Expected nil, got %s", err.Error())
return
}
if replaced.GetContent() != "Hello, my name is Jane Doe" {
t.Errorf("Expected replaced template, got %s", replaced.GetContent())
}
}
func TestReplaceSlotAllOccurrence(t *testing.T) {
tmp := tmpl.NewTemplate("Hello, my name is /*<slot name>*/John Doe/*</slot>*/ and I am a /*<slot name>*/developer/*</slot>*/")
replaced, err := tmp.ReplaceSlot("name", "Jane Doe", tmpl.ReplaceSlotOptionAll)
if err != nil {
t.Errorf("Expected nil, got %s", err.Error())
return
}
if replaced.GetContent() != "Hello, my name is Jane Doe and I am a Jane Doe" {
t.Errorf("Expected replaced template, got '%s'", replaced.GetContent())
}
}
func TestReplaceSlotMapSingle(t *testing.T) {
tmp := tmpl.NewTemplate("Hello, my name is /*<slot name>*/John Doe/*</slot>*/ and I am a /*<slot role>*/developer/*</slot>*/")
replacements := map[string]string{
"name": "Jane Doe",
"role": "designer",
}
replaced, err := tmp.ReplaceSlotMap(replacements)
if err != nil {
t.Errorf("Expected nil, got %s", err.Error())
return
}
if replaced.GetContent() != "Hello, my name is Jane Doe and I am a designer" {
t.Errorf("Expected replaced template, got '%s'", replaced.GetContent())
}
}
func TestReplaceSlotMapMultiple(t *testing.T) {
tmp := tmpl.NewTemplate(strings.Join([]string{
"Hello, my name is /*<slot name>*/John Doe/*</slot>*/ and I am a /*<slot role>*/developer/*</slot>*/",
"Hello, my name is /*<slot name>*/Claire Doe/*</slot>*/ and I am a /*<slot role>*/designer/*</slot>*/",
}, "\n"))
replacements := map[string]string{
"name": "Dane Doe",
"role": "architect",
}
replaced, err := tmp.ReplaceSlotMap(replacements)
if err != nil {
t.Errorf("Expected nil, got %s", err.Error())
return
}
if replaced.GetContent() != strings.Join([]string{
"Hello, my name is Dane Doe and I am a architect",
"Hello, my name is Dane Doe and I am a architect",
}, "\n") {
t.Errorf("Expected replaced template, got '%s'", replaced.GetContent())
}
}
func TestReplaceSlotMapSinglePreserveTags(t *testing.T) {
tmp := tmpl.NewTemplate("Hello, my name is /*<slot name>*/John Doe/*</slot>*/ and I am a /*<slot role>*/developer/*</slot>*/")
replacements := map[string]string{
"name": "Jane Doe",
"role": "designer",
}
replaced, err := tmp.ReplaceSlotMap(replacements, tmpl.ReplaceSlotOptionPreserveTags)
if err != nil {
t.Errorf("Expected nil, got %s", err.Error())
return
}
if replaced.GetContent() != "Hello, my name is /*<slot name>*/Jane Doe/*</slot>*/ and I am a /*<slot role>*/designer/*</slot>*/" {
t.Errorf("Expected replaced template, got '%s'", replaced.GetContent())
}
}
func TestReplaceSlotAny(t *testing.T) {
tmp := tmpl.NewTemplate("Hello, my name is /*<slot name>*/John Doe/*</slot>*/ and I am a /*<slot name>*/developer/*</slot>*/")
replaced := tmp.ReplaceSlotAny("Jane Doe")
if replaced.GetContent() != "Hello, my name is Jane Doe and I am a Jane Doe" {
t.Errorf("Expected replaced template, got '%s'", replaced.GetContent())
}
}
func TestReplaceSlotAnyPreserveTags(t *testing.T) {
tmp := tmpl.NewTemplate("Hello, my name is /*<slot name>*/John Doe/*</slot>*/ and I am a /*<slot name>*/developer/*</slot>*/")
replaced := tmp.ReplaceSlotAny("Jane Doe", tmpl.ReplaceSlotOptionPreserveTags)
if replaced.GetContent() != "Hello, my name is /*<slot name>*/Jane Doe/*</slot>*/ and I am a /*<slot name>*/Jane Doe/*</slot>*/" {
t.Errorf("Expected replaced template, got '%s'", replaced.GetContent())
}
}
func TestReplaceSlotFunc(t *testing.T) {
tmp := tmpl.NewTemplate("Hello, my name is /*<slot name>*/John Doe/*</slot>*/ and I am a /*<slot name>*/developer/*</slot>*/")
replaced := tmp.ReplaceSlotFunc(func(identifier, innerContent, outerContent string) string {
return fmt.Sprintf("[%s=%s]", identifier, innerContent)
})
if replaced.GetContent() != "Hello, my name is [name=John Doe] and I am a [name=developer]" {
t.Errorf("Expected replaced template, got '%s'", replaced.GetContent())
}
}