cobra_test.go 547 B

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