Skip to content

Commit 9d96073

Browse files
committed
add tests
1 parent dc54274 commit 9d96073

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

cmd_test.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,44 @@ import (
88
"testing"
99
)
1010

11-
func TestCommandSet_Add(t *testing.T) {
11+
type handlerFunc func(*Cmd) error
12+
13+
func (h handlerFunc) Handle(c *Cmd) error { return h(c) }
14+
15+
func TestCmdSet_Add(t *testing.T) {
1216
cmd := &CmdSet{}
13-
names := []string{"name", "andAnotherOne", "anotherName"}
14-
for _, v := range names {
15-
if c := cmd.Add("", flag.NewFlagSet(v, flag.ContinueOnError), nil, false); cmd.commands[v] != c {
16-
t.Errorf("%v has not been added to commandSet", v)
17+
18+
cmds := []Cmd{
19+
{
20+
"info for one",
21+
flag.NewFlagSet("one", flag.ContinueOnError),
22+
false,
23+
handlerFunc(func(c *Cmd) error { return nil }),
24+
},
25+
{
26+
"info for two",
27+
flag.NewFlagSet("two", flag.ContinueOnError),
28+
true,
29+
handlerFunc(func(c *Cmd) error { return nil }),
30+
},
31+
{
32+
"info for three",
33+
flag.NewFlagSet("three", flag.ContinueOnError),
34+
false,
35+
handlerFunc(func(c *Cmd) error { return nil }),
36+
},
37+
}
38+
for _, v := range cmds {
39+
c := cmd.Add(v.Info, v.FlagSet, v.Handler, v.AllowArgs)
40+
if cmd.commands[v.FlagSet.Name()] != c {
41+
t.Errorf("%v has not been added to commandSet", v.FlagSet.Name())
42+
}
43+
if c.AllowArgs != v.AllowArgs || c.FlagSet != v.FlagSet || c.Handler == nil || c.Info != v.Info {
44+
t.Errorf("added %v but got %v", v, *c)
1745
}
1846
}
1947

20-
expectedLen := len(names[1])
48+
expectedLen := len(cmds[2].FlagSet.Name())
2149
if cmd.cmdNameLength != expectedLen {
2250
t.Errorf("expected cmdNameLength length to be %v, got %v", expectedLen, cmd.cmdNameLength)
2351
}
@@ -28,22 +56,22 @@ func TestCommandSet_Add(t *testing.T) {
2856

2957
}
3058

31-
func TestCommandSet_AddEmptyName(t *testing.T) {
59+
func TestCmdSet_AddEmptyName(t *testing.T) {
3260
cmd := &CmdSet{}
3361
defer func() { recover() }()
3462
cmd.Add("", flag.NewFlagSet("", flag.ContinueOnError), nil, false)
3563
t.Errorf("expected empty name to panic")
3664
}
3765

38-
func TestCommandSet_AddExistingName(t *testing.T) {
66+
func TestCmdSet_AddExistingName(t *testing.T) {
3967
cmd := &CmdSet{}
4068
defer func() { recover() }()
4169
cmd.Add("name", flag.NewFlagSet("", flag.ContinueOnError), nil, false)
4270
cmd.Add("name", flag.NewFlagSet("", flag.ContinueOnError), nil, false)
4371
t.Errorf("expected empty name to panic")
4472
}
4573

46-
func TestCommandSet_Visit(t *testing.T) {
74+
func TestCmdSet_Visit(t *testing.T) {
4775
cmd := &CmdSet{}
4876
sets := map[*flag.FlagSet]bool{
4977
flag.NewFlagSet("a", flag.ContinueOnError): false,
@@ -63,7 +91,7 @@ func TestCommandSet_Visit(t *testing.T) {
6391

6492
}
6593

66-
func TestCommandSet_PrintUsage(t *testing.T) {
94+
func TestCmdSet_PrintUsage(t *testing.T) {
6795

6896
emptyCmd := &CmdSet{output: &strings.Builder{}}
6997

@@ -106,7 +134,7 @@ func TestCommandSet_PrintUsage(t *testing.T) {
106134
}
107135
}
108136

109-
func TestCommandSet_Parse(t *testing.T) {
137+
func TestCmdSet_Parse(t *testing.T) {
110138
cmd := &CmdSet{}
111139

112140
var av string
@@ -123,7 +151,7 @@ func TestCommandSet_Parse(t *testing.T) {
123151

124152
}
125153

126-
func TestCommandSet_ParseError(t *testing.T) {
154+
func TestCmdSet_ParseError(t *testing.T) {
127155
cmd := &CmdSet{}
128156
cmd.Add("", flag.NewFlagSet("a", flag.ContinueOnError), nil, false)
129157

@@ -158,7 +186,7 @@ func TestCommandSet_ParseError(t *testing.T) {
158186
t.Errorf("expected panic, got %v", err)
159187
}()
160188

161-
c := exec.Command(os.Args[0], "-test.run=TestCommandSet_ParseError")
189+
c := exec.Command(os.Args[0], "-test.run=TestCmdSet_ParseError")
162190
c.Env = append(os.Environ(), "EXIT="+tt.execFlag)
163191
err := c.Run()
164192
if e, ok := err.(*exec.ExitError); !ok || e.ExitCode() != 2 {
@@ -172,15 +200,15 @@ func TestCommandSet_ParseError(t *testing.T) {
172200
}
173201
}
174202

175-
func TestCommandSet_ParseHelp(t *testing.T) {
203+
func TestCmdSet_ParseHelp(t *testing.T) {
176204
cmd := &CmdSet{}
177205

178206
if os.Getenv("EXIT") == "HELP" {
179207
cmd.Parse([]string{"-HeLP"}, flag.ExitOnError)
180208
return
181209
}
182210

183-
c := exec.Command(os.Args[0], "-test.run=TestCommandSet_ParseHelp")
211+
c := exec.Command(os.Args[0], "-test.run=TestCmdSet_ParseHelp")
184212
c.Env = append(os.Environ(), "EXIT=HELP")
185213

186214
if err := c.Run(); err != nil {

0 commit comments

Comments
 (0)