From f58a8d6bd383c1594da70068e4c236136adc5496 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Wed, 19 Apr 2017 13:59:20 +0200 Subject: [PATCH] Add TestSortedFlags --- command_test.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/command_test.go b/command_test.go index 8b78ee3..3921a0b 100644 --- a/command_test.go +++ b/command_test.go @@ -6,6 +6,8 @@ import ( "os" "reflect" "testing" + + "github.com/spf13/pflag" ) // test to ensure hidden commands run as intended @@ -200,7 +202,6 @@ func TestSetOutput(t *testing.T) { } func TestFlagErrorFunc(t *testing.T) { - cmd := &Command{ Use: "print", 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()) } } + +// 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 +}