-
Notifications
You must be signed in to change notification settings - Fork 0
/
definitions_test.go
288 lines (263 loc) · 7.41 KB
/
definitions_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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package clo
import (
"github.com/boggydigital/testo"
"github.com/boggydigital/wits"
"strconv"
"strings"
"testing"
)
func mockDefinedArg(_, arg string) (string, error) {
return arg, nil
}
func mockDefinitions() *definitions {
return &definitions{
Cmd: map[string][]string{
"command1" + defaultAttr: {"argument1" + defaultAttr + requiredAttr + envAttr, "argument2" + multipleAttr, "abbr-arg"},
"command2": {"argument2" + multipleAttr, "xyz"},
"abc": {"argval" + argValuesSep + "value1,value2" + defaultAttr + ",abbr-val", "defarg" + defaultAttr},
},
Help: map[string]string{
"command1": "command1 help",
"command1:argument1": "command1 argument1 help",
"command1:argument2": "command1 argument2 help",
"command1:abbr-arg": "command1 abbr_arg help",
"command2": "command2 help",
"command2:argument2": "command2 argument2 help",
"command2:xyz": "command2 xyz help",
"abc": "abc help",
"abc:argval": "abc argval help",
"abc:defarg": "abc defarg help",
},
}
}
var valueDelegates = map[string]func() []string{
"arguments": func() []string { return []string{"a1", "a2"} },
"values": func() []string { return []string{"v1", "v2"} },
}
func mockDefinitionsReplace() *definitions {
return &definitions{
Cmd: map[string][]string{
"c1": {"{arguments}"},
"c2": {"arg" + argValuesSep + "{values}"},
},
}
}
func mockDefinitionsNoDefaults() *definitions {
return &definitions{
Cmd: map[string][]string{
"command1": {"argument1" + argValuesSep + "value1,value2", "argument2" + argValuesSep + "value3,value4"},
},
}
}
func TestDefinitionsLoad(t *testing.T) {
commandsWriter := &strings.Builder{}
md := mockDefinitions()
err := wits.KeyValues(md.Cmd).Write(commandsWriter)
testo.Error(t, err, false)
tests := []struct {
content string
expNil bool
expErr bool
}{
{commandsWriter.String(), false, false},
}
for ii, tt := range tests {
t.Run(strconv.Itoa(ii), func(t *testing.T) {
r := strings.NewReader(tt.content)
defs, err := Load(r, nil, nil)
testo.Nil(t, defs, tt.expNil)
testo.Error(t, err, tt.expErr)
// check that Load adds help command
if defs != nil {
helpCmd, err := defs.definedCmd("help")
testo.Error(t, err, false)
testo.UnequalValues(t, helpCmd, "")
}
})
}
}
type valueDelegatesTest struct {
cmd string
expArgs int
placeholders bool
}
func TestDefinitionsLoadReplace(t *testing.T) {
commandsWriter := &strings.Builder{}
md := mockDefinitionsReplace()
err := wits.KeyValues(md.Cmd).Write(commandsWriter)
testo.Error(t, err, false)
testsNoValuesDelegates := []valueDelegatesTest{
{"c1", 1, true},
{"c2", 1, true},
}
testsValuesDelegates := []valueDelegatesTest{
{"c1", 2, false},
{"c2", 1, false},
}
valueDelegatesTests := []struct {
name string
delegates map[string]func() []string
valueDelegatesTests []valueDelegatesTest
expError bool
}{
{"no-vd-", nil, testsNoValuesDelegates, false},
{"empty-vd-", map[string]func() []string{}, nil, true},
{"vd-", valueDelegates, testsValuesDelegates, false},
}
for _, vdt := range valueDelegatesTests {
def, err := Load(strings.NewReader(commandsWriter.String()), nil, vdt.delegates)
testo.Error(t, err, vdt.expError)
for _, tt := range vdt.valueDelegatesTests {
t.Run(vdt.name+tt.cmd, func(t *testing.T) {
testo.EqualValues(t, len(def.Cmd[tt.cmd]), tt.expArgs)
testo.EqualValues(t, strings.Contains(def.Cmd[tt.cmd][0], placeholderPrefix), tt.placeholders)
testo.EqualValues(t, strings.Contains(def.Cmd[tt.cmd][0], placeholderSuffix), tt.placeholders)
})
}
}
}
func TestDefinitionsDefinedCmd(t *testing.T) {
tests := []struct {
cmd string
expCmd string
expErr bool
}{
{"cmd-that-doesnt-exist", "", false}, // used to test defs == nil
{"cmd-that-doesnt-exist", "", false},
{"command1", "command1" + defaultAttr, false},
{"a", "abc", false},
{"ab", "abc", false},
{"abc", "abc", false},
{"c", "", true},
{"command", "", true},
}
for _, tt := range tests {
t.Run(tt.cmd, func(t *testing.T) {
defs := mockDefinitions()
dc, err := defs.definedCmd(tt.cmd)
testo.Error(t, err, tt.expErr)
testo.EqualValues(t, dc, tt.expCmd)
})
}
}
func TestDefinitionsDefinedCmdArg(t *testing.T) {
tests := []struct {
cmd, arg string
expArg string
}{
{"cmd-that-doesnt-exist", "arg-that-doesnt-exist", ""}, // used to test defs == nil
{"cmd-that-doesnt-exist", "arg-that-doesnt-exist", ""},
{"command1", "argument1", "argument1" + defaultAttr + requiredAttr + envAttr},
{"command1", "argument-that-doesnt-exist", ""},
}
for _, tt := range tests {
t.Run(tt.cmd+tt.arg, func(t *testing.T) {
defs := mockDefinitions()
da, err := defs.definedArg(tt.cmd, tt.arg)
testo.Error(t, err, false)
testo.EqualValues(t, da, tt.expArg)
})
}
}
func TestDefinitionsDefinedCmdArgVal(t *testing.T) {
tests := []struct {
cmd, arg, val string
expVal string
}{
{"cmd-that-doesnt-exist", "arg-that-doesnt-exist", "value1", ""},
{"cmd-that-doesnt-exist", "arg-that-doesnt-exist", "value1", ""},
{"command1", "argument1", "", ""},
{"abc", "argval", "value1", "value1"},
}
for _, tt := range tests {
t.Run(tt.cmd+tt.arg+tt.val, func(t *testing.T) {
defs := mockDefinitions()
dv, err := defs.definedVal(tt.cmd, tt.arg, tt.val)
testo.Error(t, err, false)
testo.EqualValues(t, dv, tt.expVal)
})
}
}
func TestDefinitionsDefaultCommand(t *testing.T) {
tests := []struct {
defs *definitions
expCmd string
}{
{nil, ""},
{mockDefinitions(), "command1" + defaultAttr},
{&definitions{}, ""},
}
for _, tt := range tests {
t.Run(tt.expCmd, func(t *testing.T) {
dc := tt.defs.defaultCommand()
testo.EqualValues(t, dc, tt.expCmd)
})
}
}
func TestDefinitionsDefaultArgument(t *testing.T) {
tests := []struct {
cmd string
expArg string
}{
{"command1", "argument1" + defaultAttr + requiredAttr + envAttr},
{"cmd-that-doesnt-exist", ""},
{"command2", ""},
}
for _, tt := range tests {
defs := mockDefinitions()
da, err := defs.defaultArgument(tt.cmd)
testo.Error(t, err, false)
testo.EqualValues(t, da, tt.expArg)
}
}
func TestDefinitionsDefaultArgumentValues(t *testing.T) {
tests := []struct {
req *request
expReq *request
expErr bool
}{
{nil, nil, true},
{&request{}, &request{}, false},
{&request{Command: "command1"}, &request{Command: "command1", Arguments: map[string][]string{}}, false},
{&request{Command: "abc"}, &request{
Command: "abc",
Arguments: map[string][]string{
"argval": {"value2"},
},
}, false},
{&request{
Command: "abc",
Arguments: map[string][]string{
"argval": {"value1"}},
},
&request{
Command: "abc",
Arguments: map[string][]string{
"argval": {"value1"}},
}, false},
}
defs := mockDefinitions()
for ii, tt := range tests {
t.Run(strconv.Itoa(ii), func(t *testing.T) {
testo.Error(t, defs.defaultArgValues(tt.req), tt.expErr)
testo.DeepEqual(t, tt.req, tt.expReq)
})
}
}
func TestDefaultArgByNameNotValues(t *testing.T) {
tests := []struct {
cmd string
extArg string
expErr bool
}{
{"abc", "defarg" + defaultAttr, false},
}
defs := mockDefinitions()
for _, tt := range tests {
t.Run(tt.cmd+tt.extArg, func(t *testing.T) {
defArg, err := defs.defaultArgument(tt.cmd)
testo.Error(t, err, tt.expErr)
testo.EqualValues(t, defArg, tt.extArg)
})
}
}