spf13--cobra/cobra_test.go
2023-11-17 14:59:07 +01:00

84 lines
1.8 KiB
Go

// Copyright 2013-2023 The Cobra Authors
//
// 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.
package cobra
import (
"errors"
"testing"
"text/template"
)
func assertNoErr(t *testing.T, e error) {
if e != nil {
t.Error(e)
}
}
func TestAddTemplateFunctions(t *testing.T) {
AddTemplateFunc("t", func() bool { return true })
AddTemplateFuncs(template.FuncMap{
"f": func() bool { return false },
"h": func() string { return "Hello," },
"w": func() string { return "world." }})
c := &Command{}
c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)
const expected = "Hello, world."
if got := c.UsageString(); got != expected {
t.Errorf("Expected UsageString: %v\nGot: %v", expected, got)
}
}
func TestCheckErr(t *testing.T) {
tests := []struct {
name string
msg interface{}
panic bool
}{
{
name: "no error",
msg: nil,
panic: false,
},
{
name: "panic string",
msg: "test",
panic: true,
},
{
name: "panic error",
msg: errors.New("test error"),
panic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if r != nil {
if !tt.panic {
t.Error("Didn't expect panic")
}
} else {
if tt.panic {
t.Error("Expected to panic")
}
}
}()
CheckErr(tt.msg)
})
}
}