mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Add TestSortedFlags
This commit is contained in:
parent
e135867f96
commit
f58a8d6bd3
1 changed files with 37 additions and 1 deletions
|
@ -6,6 +6,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
// test to ensure hidden commands run as intended
|
// test to ensure hidden commands run as intended
|
||||||
|
@ -200,7 +202,6 @@ func TestSetOutput(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFlagErrorFunc(t *testing.T) {
|
func TestFlagErrorFunc(t *testing.T) {
|
||||||
|
|
||||||
cmd := &Command{
|
cmd := &Command{
|
||||||
Use: "print",
|
Use: "print",
|
||||||
RunE: func(cmd *Command, args []string) error {
|
RunE: func(cmd *Command, args []string) error {
|
||||||
|
@ -222,3 +223,38 @@ func TestFlagErrorFunc(t *testing.T) {
|
||||||
t.Errorf("expected %v, got %v", expected, err.Error())
|
t.Errorf("expected %v, got %v", expected, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestSortedFlags checks,
|
||||||
|
// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
|
||||||
|
// https://github.com/spf13/cobra/issues/404
|
||||||
|
func TestSortedFlags(t *testing.T) {
|
||||||
|
cmd := &Command{}
|
||||||
|
cmd.Flags().SortFlags = false
|
||||||
|
names := []string{"C", "B", "A", "D"}
|
||||||
|
for _, name := range names {
|
||||||
|
cmd.Flags().Bool(name, false, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {
|
||||||
|
if i == len(names) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if contains(f.Name, names) {
|
||||||
|
if names[i] != f.Name {
|
||||||
|
t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// contains checks, if s is in ss.
|
||||||
|
func contains(s string, ss []string) bool {
|
||||||
|
for _, v := range ss {
|
||||||
|
if v == s {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue