Skip to content
Myr

Add TestSortedFlags

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent e135867 · Browse files · 1 file+37 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+37 added−1 removed

  • other:+37 added−1 removed

1 file+37 added−1 removed

modified:command_test.go+37 added-1 removed
4 unmodified lines
5 "fmt"6 "os"7 "reflect"8 "testing"910 "github.com/spf13/pflag"11)1213// test to ensure hidden commands run as intended14func TestHiddenCommandExecutes(t *testing.T) {
186 unmodified lines
201 }202}203204func TestFlagErrorFunc(t *testing.T) {203205 cmd := &Command{206 Use: "print",207 RunE: func(cmd *Command, args []string) error {208 return nil
13 unmodified lines
222 if err.Error() != expected {223 t.Errorf("expected %v, got %v", expected, err.Error())224 }225}226227// TestSortedFlags checks,228// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.229// https://github.com/spf13/cobra/issues/404230func TestSortedFlags(t *testing.T) {231 cmd := &Command{}232 cmd.Flags().SortFlags = false233 names := []string{"C", "B", "A", "D"}234 for _, name := range names {235 cmd.Flags().Bool(name, false, "")236 }237238 i := 0239 cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {240 if i == len(names) {241 return242 }243 if contains(f.Name, names) {244 if names[i] != f.Name {245 t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)246 }247 i++248 }249 })250}251252// contains checks, if s is in ss.253func contains(s string, ss []string) bool {254 for _, v := range ss {255 if v == s {256 return true257 }258 }259 return false260}