-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcommand_test.go
63 lines (49 loc) · 1.59 KB
/
command_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cli_test
import (
"github.com/stretchr/testify/assert"
"github.com/hidevopsio/hiboot/pkg/app/cli"
"testing"
)
func TestCommand(t *testing.T) {
t.Run("should add child command and found the child", func(t *testing.T) {
barCmd := newBarCommand()
bazCmd := newBazCommand()
fooCmd := newFooCommand(barCmd, bazCmd)
fooCmd.SetName("foo")
assert.Equal(t, "foo", fooCmd.FullName())
fooCmd.SetFullName("foo command")
assert.Equal(t, "foo command", fooCmd.FullName())
assert.Equal(t, true, fooCmd.HasChild())
assert.Equal(t, barCmd, fooCmd.Children()[0])
assert.Equal(t, fooCmd.GetName(), barCmd.Parent().GetName())
foundCmd, err := fooCmd.Find("bar")
assert.Equal(t, nil, err)
assert.Equal(t, foundCmd.GetName(), barCmd.GetName())
})
t.Run("should found the command directly", func(t *testing.T) {
fooCmd := new(fooCommand)
fooCmd.SetName("foo")
_, err := fooCmd.Find("foo")
assert.Equal(t, nil, err)
})
t.Run("should not found the non-exist command", func(t *testing.T) {
fooCmd := new(fooCommand)
fooCmd.SetName("foo")
_, err := fooCmd.Find("bar")
assert.Equal(t, cli.ErrCommandNotFound, err)
})
t.Run("should run command handler", func(t *testing.T) {
cmd := new(cli.SubCommand)
err := cmd.Run(nil)
assert.Equal(t, nil, err)
})
t.Run("should run secondary command handler", func(t *testing.T) {
cmd := new(fooCommand)
res, err := cli.Dispatch(cmd, []string{"daz"})
assert.Equal(t, nil, err)
assert.Equal(t, false, res.(bool))
res, err = cli.Dispatch(cmd, []string{"buzz"})
assert.Equal(t, nil, err)
assert.Equal(t, true, res.(bool))
})
}