Skip to content

Commit

Permalink
Add tests for command.VisibleFlagCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Oct 7, 2022
1 parent e62a087 commit 8227be1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
4 changes: 3 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ func (a *App) Setup() {
a.flagCategories = newFlagCategories()
for _, fl := range a.Flags {
if cf, ok := fl.(CategorizableFlag); ok {
a.flagCategories.AddFlag(cf.GetCategory(), cf)
if cf.GetCategory() != "" {
a.flagCategories.AddFlag(cf.GetCategory(), cf)
}
}
}

Expand Down
31 changes: 27 additions & 4 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func ExampleApp_Run_appHelp() {
// help, h Shows a list of commands or help for one command
//
// GLOBAL OPTIONS:
// --help, -h show help (default: false)
// --name value a name to say (default: "bob")
// --help, -h show help (default: false)
// --version, -v print the version (default: false)
}

Expand Down Expand Up @@ -2242,10 +2242,33 @@ func TestApp_VisibleCategories(t *testing.T) {
}

func TestApp_VisibleFlagCategories(t *testing.T) {
app := &App{}
app := &App{
Flags: []Flag{
&StringFlag{
Name: "strd", // no category set
},
&Int64Flag{
Name: "intd",
Aliases: []string{"altd1", "altd2"},
Category: "cat1",
},
},
}
app.Setup()
vfc := app.VisibleFlagCategories()
if len(vfc) != 0 {
t.Errorf("unexpected visible flag categories %+v", vfc)
if len(vfc) != 1 {
t.Fatalf("unexpected visible flag categories %+v", vfc)
}
if vfc[0].Name() != "cat1" {
t.Errorf("expected category name cat1 got %s", vfc[0].Name())
}
if len(vfc[0].Flags()) != 1 {
t.Fatalf("expected flag category to have just one flag got %+v", vfc[0].Flags())
}

fl := vfc[0].Flags()[0]
if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) {
t.Errorf("unexpected flag %+v", fl.Names())
}
}

Expand Down
35 changes: 35 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -449,3 +450,37 @@ func TestCommand_VisibleSubcCommands(t *testing.T) {

expect(t, c.VisibleCommands(), []*Command{subc1, subc3})
}

func TestCommand_VisibleFlagCategories(t *testing.T) {

c := &Command{
Name: "bar",
Usage: "this is for testing",
Flags: []Flag{
&StringFlag{
Name: "strd", // no category set
},
&Int64Flag{
Name: "intd",
Aliases: []string{"altd1", "altd2"},
Category: "cat1",
},
},
}

vfc := c.VisibleFlagCategories()
if len(vfc) != 1 {
t.Fatalf("unexpected visible flag categories %+v", vfc)
}
if vfc[0].Name() != "cat1" {
t.Errorf("expected category name cat1 got %s", vfc[0].Name())
}
if len(vfc[0].Flags()) != 1 {
t.Fatalf("expected flag category to have just one flag got %+v", vfc[0].Flags())
}

fl := vfc[0].Flags()[0]
if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) {
t.Errorf("unexpected flag %+v", fl.Names())
}
}

0 comments on commit 8227be1

Please sign in to comment.