File: getopt_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (320 lines) | stat: -rw-r--r-- 8,571 bytes parent folder | download | duplicates (2)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package getopt

import (
	"errors"
	"reflect"
	"testing"

	"src.elv.sh/pkg/errutil"
)

var (
	vSpec = &OptionSpec{'v', "verbose", NoArgument}
	nSpec = &OptionSpec{'n', "dry-run", NoArgument}
	fSpec = &OptionSpec{'f', "file", RequiredArgument}
	iSpec = &OptionSpec{'i', "in-place", OptionalArgument}
	specs = []*OptionSpec{vSpec, nSpec, fSpec, iSpec}
)

var parseTests = []struct {
	name     string
	cfg      Config
	args     []string
	wantOpts []*Option
	wantArgs []string
	wantErr  error
}{
	{
		name:     "short option",
		args:     []string{"-v"},
		wantOpts: []*Option{{Spec: vSpec}},
	},
	{
		name:     "short option with required argument",
		args:     []string{"-fname"},
		wantOpts: []*Option{{Spec: fSpec, Argument: "name"}},
	},
	{
		name:     "short option with required argument in separate argument",
		args:     []string{"-f", "name"},
		wantOpts: []*Option{{Spec: fSpec, Argument: "name"}},
	},
	{
		name:     "short option with optional argument",
		args:     []string{"-i.bak"},
		wantOpts: []*Option{{Spec: iSpec, Argument: ".bak"}},
	},
	{
		name:     "short option with optional argument omitted",
		args:     []string{"-i", ".bak"},
		wantOpts: []*Option{{Spec: iSpec}},
		wantArgs: []string{".bak"},
	},
	{
		name:     "short option chaining",
		args:     []string{"-vn"},
		wantOpts: []*Option{{Spec: vSpec}, {Spec: nSpec}},
	},
	{
		name:     "short option chaining with argument",
		args:     []string{"-vfname"},
		wantOpts: []*Option{{Spec: vSpec}, {Spec: fSpec, Argument: "name"}},
	},
	{
		name:     "short option chaining with argument in separate argument",
		args:     []string{"-vf", "name"},
		wantOpts: []*Option{{Spec: vSpec}, {Spec: fSpec, Argument: "name"}},
	},

	{
		name:     "long option",
		args:     []string{"--verbose"},
		wantOpts: []*Option{{Spec: vSpec, Long: true}},
	},
	{
		name:     "long option with required argument",
		args:     []string{"--file=name"},
		wantOpts: []*Option{{Spec: fSpec, Long: true, Argument: "name"}},
	},
	{
		name:     "long option with required argument in separate argument",
		args:     []string{"--file", "name"},
		wantOpts: []*Option{{Spec: fSpec, Long: true, Argument: "name"}},
	},
	{
		name:     "long option with optional argument",
		args:     []string{"--in-place=.bak"},
		wantOpts: []*Option{{Spec: iSpec, Long: true, Argument: ".bak"}},
	},
	{
		name:     "long option with optional argument omitted",
		args:     []string{"--in-place", ".bak"},
		wantOpts: []*Option{{Spec: iSpec, Long: true}},
		wantArgs: []string{".bak"},
	},

	{
		name:     "long option, LongOnly mode",
		args:     []string{"-verbose"},
		cfg:      LongOnly,
		wantOpts: []*Option{{Spec: vSpec, Long: true}},
	},
	{
		name:     "long option with required argument, LongOnly mode",
		args:     []string{"-file", "name"},
		cfg:      LongOnly,
		wantOpts: []*Option{{Spec: fSpec, Long: true, Argument: "name"}},
	},

	{
		name:     "StopAfterDoubleDash off",
		args:     []string{"-v", "--", "-n"},
		wantOpts: []*Option{{Spec: vSpec}, {Spec: nSpec}},
		wantArgs: []string{"--"},
	},
	{
		name:     "StopAfterDoubleDash on",
		args:     []string{"-v", "--", "-n"},
		cfg:      StopAfterDoubleDash,
		wantOpts: []*Option{{Spec: vSpec}},
		wantArgs: []string{"-n"},
	},

	{
		name:     "StopBeforeFirstNonOption off",
		args:     []string{"-v", "foo", "-n"},
		wantOpts: []*Option{{Spec: vSpec}, {Spec: nSpec}},
		wantArgs: []string{"foo"},
	},
	{
		name:     "StopBeforeFirstNonOption on",
		args:     []string{"-v", "foo", "-n"},
		cfg:      StopBeforeFirstNonOption,
		wantOpts: []*Option{{Spec: vSpec}},
		wantArgs: []string{"foo", "-n"},
	},

	{
		name:     "single dash is not an option",
		args:     []string{"-"},
		wantArgs: []string{"-"},
	},
	{
		name:     "single dash is not an option, LongOnly mode",
		args:     []string{"-"},
		cfg:      LongOnly,
		wantArgs: []string{"-"},
	},

	{
		name:    "short option with required argument missing",
		args:    []string{"-f"},
		wantErr: errors.New("missing argument for -f"),
	},
	{
		name:    "long option with required argument missing",
		args:    []string{"--file"},
		wantErr: errors.New("missing argument for --file"),
	},
	{
		name: "unknown short option",
		args: []string{"-b"},
		wantOpts: []*Option{
			{Spec: &OptionSpec{Short: 'b', Arity: OptionalArgument}, Unknown: true}},
		wantErr: errors.New("unknown option -b"),
	},
	{
		name: "unknown short option with argument",
		args: []string{"-bfoo"},
		wantOpts: []*Option{
			{Spec: &OptionSpec{Short: 'b', Arity: OptionalArgument}, Unknown: true, Argument: "foo"}},
		wantErr: errors.New("unknown option -b"),
	},
	{
		name: "unknown long option",
		args: []string{"--bad"},
		wantOpts: []*Option{
			{Spec: &OptionSpec{Long: "bad", Arity: OptionalArgument}, Long: true, Unknown: true}},
		wantErr: errors.New("unknown option --bad"),
	},
	{
		name: "unknown long option with argument",
		args: []string{"--bad=foo"},
		wantOpts: []*Option{
			{Spec: &OptionSpec{Long: "bad", Arity: OptionalArgument}, Long: true, Unknown: true, Argument: "foo"}},
		wantErr: errors.New("unknown option --bad"),
	},
	{
		name: "multiple errors",
		args: []string{"-b", "-f"},
		wantOpts: []*Option{
			{Spec: &OptionSpec{Short: 'b', Arity: OptionalArgument}, Unknown: true}},
		wantErr: errutil.Multi(
			errors.New("missing argument for -f"), errors.New("unknown option -b")),
	},
}

func TestParse(t *testing.T) {
	for _, tc := range parseTests {
		t.Run(tc.name, func(t *testing.T) {
			opts, args, err := Parse(tc.args, specs, tc.cfg)
			check := func(name string, got, want any) {
				if !reflect.DeepEqual(got, want) {
					t.Errorf("Parse(%#v) (config = %v)\ngot %s = %v, want %v",
						tc.args, tc.cfg, name, got, want)
				}
			}
			check("opts", opts, tc.wantOpts)
			check("args", args, tc.wantArgs)
			check("err", err, tc.wantErr)
		})
	}
}

var completeTests = []struct {
	name     string
	cfg      Config
	args     []string
	wantOpts []*Option
	wantArgs []string
	wantCtx  Context
}{
	{
		name:    "NewOptionOrArgument",
		args:    []string{""},
		wantCtx: Context{Type: OptionOrArgument},
	},
	{
		name:    "NewOption",
		args:    []string{"-"},
		wantCtx: Context{Type: AnyOption},
	},
	{
		name:    "LongOption",
		args:    []string{"--f"},
		wantCtx: Context{Type: LongOption, Text: "f"},
	},
	{
		name:    "LongOption with LongOnly",
		args:    []string{"-f"},
		cfg:     LongOnly,
		wantCtx: Context{Type: LongOption, Text: "f"},
	},
	{
		name:     "ChainShortOption",
		args:     []string{"-v"},
		wantOpts: []*Option{{Spec: vSpec}},
		wantCtx:  Context{Type: ChainShortOption},
	},
	{
		name: "OptionArgument of short option, separate argument",
		args: []string{"-f", "foo"},
		wantCtx: Context{
			Type:   OptionArgument,
			Option: &Option{Spec: fSpec, Argument: "foo"}},
	},
	{
		name: "OptionArgument of short option, same argument",
		args: []string{"-ffoo"},
		wantCtx: Context{
			Type:   OptionArgument,
			Option: &Option{Spec: fSpec, Argument: "foo"}},
	},
	{
		name: "OptionArgument of long option, separate argument",
		args: []string{"--file", "foo"},
		wantCtx: Context{
			Type:   OptionArgument,
			Option: &Option{Spec: fSpec, Long: true, Argument: "foo"}},
	},
	{
		name: "OptionArgument of long option, same argument",
		args: []string{"--file=foo"},
		wantCtx: Context{
			Type:   OptionArgument,
			Option: &Option{Spec: fSpec, Long: true, Argument: "foo"}},
	},
	{
		name: "OptionArgument of long option with LongOnly, same argument",
		args: []string{"-file=foo"},
		cfg:  LongOnly,
		wantCtx: Context{
			Type:   OptionArgument,
			Option: &Option{Spec: fSpec, Long: true, Argument: "foo"}},
	},
	{
		name:    "Argument",
		args:    []string{"foo"},
		wantCtx: Context{Type: Argument, Text: "foo"},
	},
	{
		name:    "Argument after --",
		args:    []string{"--", "foo"},
		cfg:     StopAfterDoubleDash,
		wantCtx: Context{Type: Argument, Text: "foo"},
	},
	{
		name:     "Argument after first non-option argument",
		args:     []string{"bar", "foo"},
		cfg:      StopBeforeFirstNonOption,
		wantArgs: []string{"bar"},
		wantCtx:  Context{Type: Argument, Text: "foo"},
	},
}

func TestComplete(t *testing.T) {
	for _, tc := range completeTests {
		t.Run(tc.name, func(t *testing.T) {
			opts, args, ctx := Complete(tc.args, specs, tc.cfg)
			check := func(name string, got, want any) {
				if !reflect.DeepEqual(got, want) {
					t.Errorf("Parse(%#v) (config = %v)\ngot %s = %v, want %v",
						tc.args, tc.cfg, name, got, want)
				}
			}
			check("opts", opts, tc.wantOpts)
			check("args", args, tc.wantArgs)
			check("ctx", ctx, tc.wantCtx)
		})
	}
}