-
Notifications
You must be signed in to change notification settings - Fork 0
/
subcmd_test.go
207 lines (189 loc) · 4.79 KB
/
subcmd_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package subcmd_test
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/koron-go/subcmd"
)
func TestCommand(t *testing.T) {
var called bool
cmd := subcmd.DefineCommand("foo", t.Name(), func(context.Context, []string) error {
called = true
return nil
})
err := subcmd.Run(cmd)
if err != nil {
t.Fatal(err)
}
if !called {
t.Error("command func is not called")
}
}
func TestCommandNil(t *testing.T) {
cmd := subcmd.DefineCommand("foo", t.Name(), nil)
err := subcmd.Run(cmd)
if err == nil {
t.Fatal("unexpected succeed")
}
if d := cmp.Diff("no function declared for command: foo", err.Error()); d != "" {
t.Errorf("error unmatch: -want +got\n%s", d)
}
}
func TestSet(t *testing.T) {
var (
gotNames []string
gotArgs []string
)
record := func(ctx context.Context, args []string) error {
gotNames = subcmd.Names(ctx)
gotArgs = args
return nil
}
set := subcmd.DefineSet("set", "",
subcmd.DefineSet("user", "",
subcmd.DefineCommand("list", "", record),
subcmd.DefineCommand("add", "", record),
subcmd.DefineCommand("delete", "", record),
),
subcmd.DefineSet("post", "",
subcmd.DefineCommand("list", "", record),
subcmd.DefineCommand("add", "", record),
subcmd.DefineCommand("delete", "", record),
),
)
for i, c := range []struct {
args []string
wantNames []string
wantArgs []string
}{
{
[]string{"user", "list"},
[]string{"set", "user", "list"},
[]string{},
},
{
[]string{"user", "add", "-email", "[email protected]"},
[]string{"set", "user", "add"},
[]string{"-email", "[email protected]"},
},
{
[]string{"user", "delete", "-id", "123"},
[]string{"set", "user", "delete"},
[]string{"-id", "123"},
},
{
[]string{"post", "list"},
[]string{"set", "post", "list"},
[]string{},
},
{
[]string{"post", "add", "-title", "Brown fox..."},
[]string{"set", "post", "add"},
[]string{"-title", "Brown fox..."},
},
{
[]string{"post", "delete", "-id", "ABC"},
[]string{"set", "post", "delete"},
[]string{"-id", "ABC"},
},
} {
err := subcmd.Run(set, c.args...)
if err != nil {
t.Fatalf("failed for case#%d (%+v): %s", i, c, err)
continue
}
if d := cmp.Diff(c.wantNames, gotNames); d != "" {
t.Errorf("unexpected names on #%d: -want +got\n%s", i, d)
}
if d := cmp.Diff(c.wantArgs, gotArgs); d != "" {
t.Errorf("unexpected args on #%d: -want +got\n%s", i, d)
}
}
}
func TestSetFails(t *testing.T) {
rootSet := subcmd.DefineSet("fail", "",
subcmd.DefineCommand("list", "list all entries", nil),
subcmd.DefineCommand("add", "add a new entry", nil),
subcmd.DefineCommand("delete", "delete an entry", nil),
subcmd.DefineSet("item", "operate items"),
)
for i, c := range []struct {
args []string
want string
}{
{[]string{}, `no commands selected.
Available sub-commands are:
list list all entries
add add a new entry
delete delete an entry
item operate items`},
{[]string{"foo"}, `command not found.
Available sub-commands are:
list list all entries
add add a new entry
delete delete an entry
item operate items`},
} {
err := subcmd.Run(rootSet, c.args...)
if err == nil {
t.Fatalf("unexpected succeed at #%d %+v", i, c)
}
got := err.Error()
if d := cmp.Diff(c.want, got); d != "" {
t.Errorf("unexpected error at #%d: -want +got\n%s", i, d)
}
}
}
func TestAutoWidth(t *testing.T) {
rootSet := subcmd.DefineSet("faillong", "",
subcmd.DefineCommand("verylongname", "long name command", nil),
subcmd.DefineCommand("short", "short name command", nil),
)
err := subcmd.Run(rootSet)
if err == nil {
t.Fatal("unexpected succeed")
}
want := `no commands selected.
Available sub-commands are:
verylongname long name command
short short name command`
got := err.Error()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("unexpected error: -want +got\n%s", d)
}
}
func TestRootSet(t *testing.T) {
rootSet := subcmd.DefineRootSet()
if d := cmp.Diff("subcmd.test", rootSet.Name()); d != "" {
t.Errorf("unexpected name: -want +got\n%s", d)
}
}
func TestFlagSet(t *testing.T) {
t.Run("no name", func(t *testing.T) {
ctx := context.Background()
fs := subcmd.FlagSet(ctx)
got := fs.Name()
if got != "" {
t.Errorf("wrong name of FlagSet: got=%q", got)
}
})
t.Run("with names", func(t *testing.T) {
var gotName string
set := subcmd.DefineSet("first", "",
subcmd.DefineSet("second", "",
subcmd.DefineCommand("third", "", func(ctx context.Context, args []string) error {
fs := subcmd.FlagSet(ctx)
gotName = fs.Name()
return nil
}),
),
)
err := subcmd.Run(set, "second", "third")
if err != nil {
t.Fatalf("failed: %s", err)
}
if gotName != "first second third" {
t.Errorf("wrong name of FlagSet: got=%q", gotName)
}
})
}