git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/cobra/cobra_test.go (about) 1 // Copyright 2013-2022 The Cobra Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 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 }