-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
157 lines (130 loc) · 3.64 KB
/
group_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
package cmdy
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/ArtProcessors/cmdy/arg"
"github.com/ArtProcessors/cmdy/internal/assert"
)
func strOutputBuilder(out string) func() Command {
return testCmdRunBuilder(func(ctx Context) error {
fmt.Println(out)
return nil
})
}
var (
newFooCommand = strOutputBuilder("foo")
newFoodCommand = strOutputBuilder("food")
newBaleCommand = strOutputBuilder("bale")
newBarkCommand = strOutputBuilder("bark")
)
func ExampleGroup_prefixMatcher() {
builders := Builders{
"foo": newFooCommand,
"food": newFoodCommand,
"bale": newBaleCommand,
"bark": newBarkCommand,
}
bldr := func() Command {
return NewGroup("group", builders, GroupPrefixMatcher(2))
}
Run(context.Background(), []string{"foo"}, bldr)
Run(context.Background(), []string{"food"}, bldr)
Run(context.Background(), []string{"bar"}, bldr)
Run(context.Background(), []string{"bal"}, bldr)
// Output:
// foo
// food
// bark
// bale
}
func TestGroup(t *testing.T) {
tt := assert.WrapTB(t)
var foo, bar = errors.New("foo"), errors.New("bar")
bldr := func() Command {
return NewGroup("set", Builders{
"foo": testCmdRunBuilder(func(c Context) error {
return foo
}),
"bar": testCmdRunBuilder(func(c Context) error {
return bar
}),
})
}
tt.MustEqual(foo, Run(context.Background(), []string{"foo"}, bldr))
tt.MustEqual(bar, Run(context.Background(), []string{"bar"}, bldr))
}
func TestGroup_SubcommandArgs(t *testing.T) {
tt := assert.WrapTB(t)
var p string
bldr := func() Command {
return NewGroup("set", Builders{
"foo": func() Command {
p = ""
as := arg.NewArgSet()
as.String(&p, "pants", "Usage...")
return &testCmd{args: as}
},
})
}
tt.MustOK(Run(context.Background(), []string{"foo", "yep"}, bldr))
tt.MustEqual("yep", p)
err := Run(context.Background(), []string{"foo", "yep", "nup"}, bldr)
tt.MustAssert(err != nil) // FIXME: check error
err = Run(context.Background(), []string{"foo"}, bldr)
tt.MustAssert(err != nil) // FIXME: check error
}
func TestGroup_SubcommandFlags(t *testing.T) {
tt := assert.WrapTB(t)
var p string
bldr := func() Command {
return NewGroup("set", Builders{
"foo": func() Command {
fs := NewFlagSet()
fs.StringVar(&p, "pants", "", "Usage...")
return &testCmd{flags: fs}
},
})
}
tt.MustOK(Run(context.Background(), []string{"foo", "-pants", "yep"}, bldr))
tt.MustEqual("yep", p)
err := Run(context.Background(), []string{"foo", "-pants"}, bldr)
tt.MustAssert(err != nil) // FIXME: check error
}
func TestGroup_Rewriter(t *testing.T) {
tt := assert.WrapTB(t)
var foo = errors.New("foo")
bldr := func() Command {
return NewGroup("set", Builders{},
GroupRewrite(func(g *Group, state GroupRunState) *GroupRunState {
state.Builder = testCmdRunBuilder(func(c Context) error {
return foo
})
return &state
}),
)
}
tt.MustEqual(foo, Run(context.Background(), []string{"foo"}, bldr))
}
func TestGroup_Hide(t *testing.T) {
tt := assert.WrapTB(t)
_ = tt
grp := NewGroup("set",
Builders{
"4GKwDcbp": func() Command { return &testCmd{synopsis: "4GKwDcbp"} },
"9rdjKX3j": func() Command { return &testCmd{synopsis: "9rdjKX3j"} },
"GM68tb0F": func() Command { return &testCmd{synopsis: "GM68tb0F"} },
"OZJpKePU": func() Command { return &testCmd{synopsis: "OZJpKePU"} },
},
GroupHide("9rdjKX3j", "OZJpKePU"),
)
var bld strings.Builder
tt.MustOK(grp.BuildHelp(&bld))
out := bld.String()
tt.MustAssert(!strings.Contains(out, "OZJpKePU"))
tt.MustAssert(!strings.Contains(out, "9rdjKX3j"))
tt.MustAssert(strings.Contains(out, "GM68tb0F"))
tt.MustAssert(strings.Contains(out, "4GKwDcbp"))
}