-
Notifications
You must be signed in to change notification settings - Fork 196
/
command_test.go
96 lines (82 loc) · 2.52 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package ishell_test
import (
"testing"
"github.com/abiosoft/ishell/v2"
"github.com/stretchr/testify/assert"
)
func newCmd(name string, help string) *ishell.Cmd {
return &ishell.Cmd{
Name: name,
Help: help,
}
}
func TestAddCommand(t *testing.T) {
cmd := newCmd("root", "")
assert.Equal(t, len(cmd.Children()), 0, "should be empty")
cmd.AddCmd(newCmd("child", ""))
assert.Equal(t, len(cmd.Children()), 1, "should include one child command")
}
func TestDeleteCommand(t *testing.T) {
cmd := newCmd("root", "")
cmd.AddCmd(newCmd("child", ""))
assert.Equal(t, len(cmd.Children()), 1, "should include one child command")
cmd.DeleteCmd("child")
assert.Equal(t, len(cmd.Children()), 0, "should be empty")
}
func TestFindCmd(t *testing.T) {
cmd := newCmd("root", "")
cmd.AddCmd(newCmd("child1", ""))
cmd.AddCmd(newCmd("child2", ""))
res, err := cmd.FindCmd([]string{"child1"})
if err != nil {
t.Fatal("finding should work")
}
assert.Equal(t, res.Name, "child1")
res, err = cmd.FindCmd([]string{"child2"})
if err != nil {
t.Fatal("finding should work")
}
assert.Equal(t, res.Name, "child2")
res, err = cmd.FindCmd([]string{"child3"})
if err == nil {
t.Fatal("should not find this child!")
}
assert.Nil(t, res)
}
func TestFindAlias(t *testing.T) {
cmd := newCmd("root", "")
subcmd := newCmd("child1", "")
subcmd.Aliases = []string{"alias1", "alias2"}
cmd.AddCmd(subcmd)
res, err := cmd.FindCmd([]string{"alias1"})
if err != nil {
t.Fatal("finding alias should work")
}
assert.Equal(t, res.Name, "child1")
res, err = cmd.FindCmd([]string{"alias2"})
if err != nil {
t.Fatal("finding alias should work")
}
assert.Equal(t, res.Name, "child1")
res, err = cmd.FindCmd([]string{"alias3"})
if err == nil {
t.Fatal("should not find this child!")
}
assert.Nil(t, res)
}
func TestHelpText(t *testing.T) {
cmd := newCmd("root", "help for root command")
cmd.AddCmd(newCmd("child1", "help for child1 command"))
cmd.AddCmd(newCmd("child2", "help for child2 command"))
res := cmd.HelpText()
expected := "\nhelp for root command\n\nCommands:\n child1 help for child1 command\n child2 help for child2 command\n\n"
assert.Equal(t, res, expected)
}
func TestChildrenSortedAlphabetically(t *testing.T) {
cmd := newCmd("root", "help for root command")
cmd.AddCmd(newCmd("child2", "help for child1 command"))
cmd.AddCmd(newCmd("child1", "help for child2 command"))
children := cmd.Children()
assert.Equal(t, children[0].Name, "child1", "must be first")
assert.Equal(t, children[1].Name, "child2", "must be second")
}