2023-03-06 05:28:31 +03:00
|
|
|
// Copyright 2013-2023 The Cobra Authors
|
2022-09-16 14:55:56 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-09-25 01:04:00 +04:00
|
|
|
package cobra
|
2013-09-04 02:54:51 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-09-01 06:36:55 +03:00
|
|
|
"text/template"
|
2013-09-04 02:54:51 +04:00
|
|
|
)
|
|
|
|
|
2021-02-08 03:08:50 +03:00
|
|
|
func assertNoErr(t *testing.T, e error) {
|
|
|
|
if e != nil {
|
|
|
|
t.Error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 06:36:55 +03:00
|
|
|
func TestAddTemplateFunctions(t *testing.T) {
|
|
|
|
AddTemplateFunc("t", func() bool { return true })
|
|
|
|
AddTemplateFuncs(template.FuncMap{
|
2015-09-04 23:34:51 +03:00
|
|
|
"f": func() bool { return false },
|
|
|
|
"h": func() string { return "Hello," },
|
2015-09-01 06:36:55 +03:00
|
|
|
"w": func() string { return "world." }})
|
|
|
|
|
|
|
|
c := &Command{}
|
|
|
|
c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)
|
2015-09-04 23:34:51 +03:00
|
|
|
|
2017-10-31 21:58:37 +03:00
|
|
|
const expected = "Hello, world."
|
|
|
|
if got := c.UsageString(); got != expected {
|
|
|
|
t.Errorf("Expected UsageString: %v\nGot: %v", expected, got)
|
2017-04-05 19:44:50 +03:00
|
|
|
}
|
|
|
|
}
|