1package cobra 2 3import ( 4 "testing" 5 "text/template" 6) 7 8func TestAddTemplateFunctions(t *testing.T) { 9 AddTemplateFunc("t", func() bool { return true }) 10 AddTemplateFuncs(template.FuncMap{ 11 "f": func() bool { return false }, 12 "h": func() string { return "Hello," }, 13 "w": func() string { return "world." }}) 14 15 c := &Command{} 16 c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`) 17 18 const expected = "Hello, world." 19 if got := c.UsageString(); got != expected { 20 t.Errorf("Expected UsageString: %v\nGot: %v", expected, got) 21 } 22} 23