summaryrefslogtreecommitdiff
path: root/vendor/github.com/pborman/getopt/v2/generic_test.go
blob: 9a247ec3d89b0a88c5781063fc0aef1ef9beb9e3 (plain)
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
// Copyright 2017 Google Inc.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package getopt

import (
	"bytes"
	"fmt"
	"os"
	"reflect"
	"runtime"
	"strings"
	"testing"
	"time"
)

func TestGeneric(t *testing.T) {
	const (
		shortTest = iota
		longTest
		bothTest
	)
	for _, tt := range []struct {
		where string
		kind  int
		val   interface{}
		str   string
		def   interface{}
		in    []string
		err   string
	}{
		// Do all four tests for string, the rest can mostly just use
		// shortTest (the 0 value).
		{
			where: loc(),
			kind:  shortTest,
			val:   "42",
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			kind:  longTest,
			val:   "42",
			str:   "42",
			in:    []string{"test", "--long", "42"},
		},
		{
			where: loc(),
			kind:  bothTest,
			val:   "42",
			str:   "42",
			in:    []string{"test", "--both", "42"},
		},
		{
			where: loc(),
			kind:  bothTest,
			val:   "42",
			str:   "42",
			in:    []string{"test", "-b", "42"},
		},
		{
			where: loc(),
			val:   "42",
			def:   "42",
			str:   "42",
			in:    []string{"test"},
		},
		{
			where: loc(),
			val:   "42",
			def:   "43",
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},

		{
			where: loc(),
			val:   true,
			str:   "true",
			in:    []string{"test", "-s"},
		},
		{
			where: loc(),
			val:   true,
			def:   true,
			str:   "true",
			in:    []string{"test"},
		},
		{
			where: loc(),
			kind:  longTest,
			val:   false,
			str:   "false",
			in:    []string{"test", "--long=false"},
		},
		{
			where: loc(),
			kind:  longTest,
			val:   false,
			def:   true,
			str:   "false",
			in:    []string{"test", "--long=false"},
		},

		{
			where: loc(),
			val:   int(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			val:   int8(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			val:   int16(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			val:   int32(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			val:   int64(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},

		{
			where: loc(),
			val:   uint(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			val:   uint8(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			val:   uint16(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			val:   uint32(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},
		{
			where: loc(),
			val:   uint64(42),
			str:   "42",
			in:    []string{"test", "-s", "42"},
		},

		{
			where: loc(),
			val:   float32(4.2),
			str:   "4.2",
			in:    []string{"test", "-s", "4.2"},
		},
		{
			where: loc(),
			val:   float64(4.2),
			str:   "4.2",
			in:    []string{"test", "-s", "4.2"},
		},

		{
			where: loc(),
			val:   time.Duration(time.Second * 42),
			def:   time.Second * 2,
			str:   "42s",
			in:    []string{"test", "-s", "42s"},
		},
		{
			where: loc(),
			val:   time.Duration(time.Second * 42),
			def:   time.Second * 2,
			str:   "42s",
			in:    []string{"test", "-s42s"},
		},
		{
			where: loc(),
			val:   time.Duration(time.Second * 2),
			def:   time.Second * 2,
			in:    []string{"test", "-s42"},
			str:   "2s",
			err:   "test: time: missing unit in duration 42",
		},

		{
			where: loc(),
			val:   []string{"42", "."},
			str:   "42,.",
			def:   []string{"one", "two", "three"},
			in:    []string{"test", "-s42", "-s."},
		},
		{
			where: loc(),
			val:   []string{"42", "."},
			str:   "42,.",
			def:   []string{"one", "two", "three"},
			in:    []string{"test", "-s42,."},
		},
		{
			where: loc(),
			val:   []string{"one", "two", "three"},
			def:   []string{"one", "two", "three"},
			str:   "one,two,three",
			in:    []string{"test"},
		},
	} {
		reset()
		var opt Option
		val := reflect.New(reflect.TypeOf(tt.val)).Interface()
		if tt.def != nil {
			reflect.ValueOf(val).Elem().Set(reflect.ValueOf(tt.def))
		}
		switch tt.kind {
		case shortTest:
			opt = Flag(val, 's')
		case longTest:
			opt = FlagLong(val, "long", 0)
		case bothTest:
			opt = FlagLong(val, "both", 'b')
		}
		_ = opt
		parse(tt.in)
		if s := checkError(tt.err); s != "" {
			t.Errorf("%s: %s", tt.where, s)
			continue
		}
		got := reflect.ValueOf(val).Elem().Interface()
		want := reflect.ValueOf(tt.val).Interface()
		if !reflect.DeepEqual(got, want) {
			t.Errorf("%s: got %v, want %v", tt.where, got, want)
		}
		if str := opt.String(); str != tt.str {
			t.Errorf("%s: got string %q, want %q", tt.where, str, tt.str)
		}
	}
}

func TestGenericDup(t *testing.T) {
	defer func() {
		stderr = os.Stderr
		exit = os.Exit
	}()

	reset()
	var v1, v2 string
	type myPanic struct{}
	var errbuf bytes.Buffer
	stderr = &errbuf
	_, file, line, _ := runtime.Caller(0)
	Flag(&v1, 's')
	line++ // line is now the line number of the first call to Flag.

	exit = func(i int) { panic(myPanic{}) }
	defer func() {
		p := recover()
		if _, ok := p.(myPanic); ok {
			err := errbuf.String()
			if !strings.Contains(err, "-s already declared") || !strings.Contains(err, fmt.Sprintf("%s:%d", file, line)) {
				t.Errorf("unexpected error: %q\nshould contain \"-s already declared\" and \"%s:%d\"", err, file, line)
			}
		} else if p == nil {
			t.Errorf("Second call to Flag did not fail")
		} else {
			t.Errorf("panic %v", p)
		}
	}()
	Flag(&v2, 's')
}

func TestGenericDupNested(t *testing.T) {
	defer func() {
		stderr = os.Stderr
		exit = os.Exit
	}()

	reset()
	type myPanic struct{}
	var errbuf bytes.Buffer
	stderr = &errbuf
	_, file, line, _ := runtime.Caller(0)
	String('s', "default")
	line++ // line is now the line number of the first call to Flag.

	exit = func(i int) { panic(myPanic{}) }
	defer func() {
		p := recover()
		if _, ok := p.(myPanic); ok {
			err := errbuf.String()
			if !strings.Contains(err, "-s already declared") || !strings.Contains(err, fmt.Sprintf("%s:%d", file, line)) {
				t.Errorf("unexpected error: %q\nshould contain \"-s already declared\" and \"%s:%d\"", err, file, line)
			}
		} else if p == nil {
			t.Errorf("Second call to Flag did not fail")
		} else {
			t.Errorf("panic %v", p)
		}
	}()
	String('s', "default")
}