...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package cobra
16
17 import (
18 "testing"
19 "text/template"
20 )
21
22 func assertNoErr(t *testing.T, e error) {
23 if e != nil {
24 t.Error(e)
25 }
26 }
27
28 func TestAddTemplateFunctions(t *testing.T) {
29 AddTemplateFunc("t", func() bool { return true })
30 AddTemplateFuncs(template.FuncMap{
31 "f": func() bool { return false },
32 "h": func() string { return "Hello," },
33 "w": func() string { return "world." }})
34
35 c := &Command{}
36 c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)
37
38 const expected = "Hello, world."
39 if got := c.UsageString(); got != expected {
40 t.Errorf("Expected UsageString: %v\nGot: %v", expected, got)
41 }
42 }
43
View as plain text