-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers_test.go
75 lines (63 loc) · 1.94 KB
/
helpers_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
package whatever
import (
"bytes"
"testing"
"text/template"
)
func TestSplitHelper(t *testing.T) {
tests := map[string]string{
`{{ "protocol.buffers" | lastPart "." }}`: "buffers",
`{{ lastPart "." ".mcpp.update.RotateKeyReq" }}`: "RotateKeyReq",
}
for templateData, expected := range tests {
funcMap := template.FuncMap{
"lastPart": lastPart,
}
tpl := template.Must(template.New("test").Funcs(funcMap).Parse(templateData))
var output bytes.Buffer
if err := tpl.Execute(&output, nil); err != nil {
t.Fatalf("%v", err)
}
if output.String() != expected {
t.Fatalf("Expected '%s' but got '%s'", expected, output.String())
}
}
}
func TestReplaceHelper(t *testing.T) {
tests := map[string]string{
`{{ "protocol.buffers" | replace "." "/" }}`: "protocol/buffers",
`{{ "goodbye.world" | replace "goodbye" "hello" | replace "." " " }}`: "hello world",
}
for templateData, expected := range tests {
funcMap := template.FuncMap{
"replace": replace,
}
tpl := template.Must(template.New("test").Funcs(funcMap).Parse(templateData))
var output bytes.Buffer
if err := tpl.Execute(&output, nil); err != nil {
t.Fatalf("%v", err)
}
if output.String() != expected {
t.Fatalf("Expected '%s' but got '%s'", expected, output.String())
}
}
}
func TestSplitCammelJoin(t *testing.T) {
tests := map[string]string{
`{{ "nick.gerakines" | splitCammelJoin "." }}`: "NickGerakines",
`{{ splitCammelJoin "." ".mcpp.update.RotateKeyReq" }}`: "McppUpdateRotateKeyReq",
}
for templateData, expected := range tests {
funcMap := template.FuncMap{
"splitCammelJoin": splitCammelJoin,
}
tpl := template.Must(template.New("test").Funcs(funcMap).Parse(templateData))
var output bytes.Buffer
if err := tpl.Execute(&output, nil); err != nil {
t.Fatalf("%v", err)
}
if output.String() != expected {
t.Fatalf("Expected '%s' but got '%s'", expected, output.String())
}
}
}