-
Notifications
You must be signed in to change notification settings - Fork 2
/
comp_arguments_test.go
227 lines (201 loc) · 6.51 KB
/
comp_arguments_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package mcli
import (
"bytes"
"flag"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestArgCompletionContext(t *testing.T) {
resetDefaultApp()
addTestCompletionCommands()
type globalFlags struct {
G1 bool `cli:"-g, --global-1"`
G2 string `cli:"-G, --global-2"`
}
type testArgs struct {
A []string `cli:"-a, --a-flag, description a flag"`
A1 bool `cli:"-1, --1-flag"`
S1 bool `cli:"-s1, --s1-flag"`
S2 bool `cli:"-s2, --s2-flag"`
V1 string `cli:"value1"`
V2 int64 `cli:"value2"`
}
argCompFuncs := map[string]ArgCompletionFunc{
"-a": func(ctx ArgCompletionContext) []CompletionItem {
args := ctx.CommandArgs().(*testArgs)
if args.S1 {
return nil
}
return []CompletionItem{
{"abc", "abc description"},
{"def", "def description"},
}
},
"value1": func(ctx ArgCompletionContext) []CompletionItem {
gf := ctx.GlobalFlags().(*globalFlags)
if gf.G1 {
return []CompletionItem{
{"Tom", "Tom who"},
{"John", "John Smith"},
}
}
if gf.G2 == "hello" {
return []CompletionItem{
{"world", "Hello world!"},
{"there", "Hello there!"},
}
}
return []CompletionItem{
{"dummy-value-1", ""},
}
},
"value2": func(ctx ArgCompletionContext) []CompletionItem {
fs := ctx.FlagSet()
prefix := ctx.ArgPrefix()
compItems := []CompletionItem{
{"abc-1", "abc 1 description"},
{"abc-2", "abc 2 description"},
{"def-3", "def 3 description"},
{"def-4", "def 4 description"},
}
if fs.Lookup("s2").Value.(flag.Getter).Get() == true {
var result []CompletionItem
for _, x := range compItems {
if strings.HasPrefix(x.Value, prefix) {
result = append(result, x)
}
}
return result
}
return compItems
},
}
cmdFunc := func(ctx *Context, args *testArgs) {
// pass
}
testCmd := NewCommand(cmdFunc, WithArgCompFuncs(argCompFuncs))
defaultApp.SetGlobalFlags(&globalFlags{})
Add("group1 cmdv", testCmd, "A group1 cmdv description")
var buf bytes.Buffer
defaultApp.completionCtx.out = &buf
reset := func() {
buf.Reset()
defaultApp.resetParsingContext()
defaultApp.resetCompletionCtx()
defaultApp.SetGlobalFlags(&globalFlags{})
}
t.Run("suggest flags", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-s1=1", "-", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "-a:description a flag\n")
assert.Contains(t, got1, "-1\n")
assert.NotContains(t, got1, "s1-flag")
assert.Contains(t, got1, "--s2:--s2-flag\n")
})
t.Run("suggest flag value / s1 true", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-s1", "-a", "", completionFlag, "zsh")
got1 := buf.String()
assert.Equal(t, got1, "")
})
t.Run("suggest flag value / s1 false", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-a", "", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "abc:abc description\n")
assert.Contains(t, got1, "def:def description\n")
})
t.Run("suggest value1 / g1 true", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-g", "", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "Tom:Tom who\n")
assert.Contains(t, got1, "John:John Smith\n")
})
t.Run("suggest value1 / g2 hello", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-G", "hello", "", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "world:Hello world!\n")
assert.Contains(t, got1, "there:Hello there!\n")
})
t.Run("suggest value1 / default", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-g=0", "-G", "there", "", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "dummy-value-1\n")
})
t.Run("suggest value2 / s2 true", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-s2=1", "value1 value", "", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "abc-1:abc 1 description\n")
assert.Contains(t, got1, "abc-2:abc 2 description\n")
assert.Contains(t, got1, "def-3:def 3 description\n")
assert.Contains(t, got1, "def-4:def 4 description\n")
reset()
Run("group1", "cmdv", "-s2", "value1 value", "abc", completionFlag, "zsh")
got2 := buf.String()
assert.Contains(t, got2, "abc-1:abc 1 description\n")
assert.Contains(t, got2, "abc-2:abc 2 description\n")
assert.NotContains(t, got2, "def-3")
assert.NotContains(t, got2, "def-4")
reset()
Run("group1", "cmdv", "-s2", "value1 value", "abd", completionFlag, "zsh")
got3 := buf.String()
assert.Equal(t, got3, "")
})
t.Run("suggest value2 / s2 false", func(t *testing.T) {
reset()
Run("group1", "cmdv", "value1 value", "", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "abc-1:abc 1 description\n")
assert.Contains(t, got1, "abc-2:abc 2 description\n")
assert.Contains(t, got1, "def-3:def 3 description\n")
assert.Contains(t, got1, "def-4:def 4 description\n")
reset()
Run("group1", "cmdv", "value1 value", "abc", completionFlag, "zsh")
got2 := buf.String()
assert.Contains(t, got2, "abc-1:abc 1 description\n")
assert.Contains(t, got2, "abc-2:abc 2 description\n")
assert.Contains(t, got2, "def-3:def 3 description\n")
assert.Contains(t, got2, "def-4:def 4 description\n")
})
t.Run("coverage / last word is a flag which wants a value", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-1", "-a=123", "-a=", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "abc:abc description")
assert.Contains(t, got1, "def:def description")
})
t.Run("coverage / second last word is a flag which has its value", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-g", "-a=123", "xxx", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "Tom:Tom who")
assert.Contains(t, got1, "John:John Smith")
})
t.Run("coverage / second last word is a boolean flag", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-g", "xxx", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "Tom:Tom who")
assert.Contains(t, got1, "John:John Smith")
})
t.Run("coverage / second last word is a non-boolean flag", func(t *testing.T) {
reset()
Run("group1", "cmdv", "-a", "xxx", completionFlag, "zsh")
got1 := buf.String()
assert.Contains(t, got1, "abc:abc description")
assert.Contains(t, got1, "def:def description")
})
t.Run("coverage / last word is complete and second last word is a flag which has its value",
func(t *testing.T) {
reset()
Run("group1", "cmdv", "-a=xxx", "", completionFlag, "zsh")
got1 := buf.String()
assert.Equal(t, "dummy-value-1\n", got1)
})
}