Debian Codegraph @

v2/generic_test.go

raw symbols SCIP
1 // Copyright 2017 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package getopt
6
7 import (
8 "bytes"
9 "fmt"
10 "os"
11 "reflect"
12 "runtime"
13 "strings"
14 "testing"
15 "time"
16 )
17
18 func TestGeneric(t *testing.T) {
19 const (
20 shortTest = iota
21 longTest
22 bothTest
23 )
24 for _, tt := range []struct {
25 where string
26 kind int
27 val interface{}
28 str string
29 def interface{}
30 in []string
31 err string
32 }{
33 // Do all four tests for string, the rest can mostly just use
34 // shortTest (the 0 value).
35 {
36 where: loc(),
37 kind: shortTest,
38 val: "42",
39 str: "42",
40 in: []string{"test", "-s", "42"},
41 },
42 {
43 where: loc(),
44 kind: longTest,
45 val: "42",
46 str: "42",
47 in: []string{"test", "--long", "42"},
48 },
49 {
50 where: loc(),
51 kind: bothTest,
52 val: "42",
53 str: "42",
54 in: []string{"test", "--both", "42"},
55 },
56 {
57 where: loc(),
58 kind: bothTest,
59 val: "42",
60 str: "42",
61 in: []string{"test", "-b", "42"},
62 },
63 {
64 where: loc(),
65 val: "42",
66 def: "42",
67 str: "42",
68 in: []string{"test"},
69 },
70 {
71 where: loc(),
72 val: "42",
73 def: "43",
74 str: "42",
75 in: []string{"test", "-s", "42"},
76 },
77
78 {
79 where: loc(),
80 val: true,
81 str: "true",
82 in: []string{"test", "-s"},
83 },
84 {
85 where: loc(),
86 val: true,
87 def: true,
88 str: "true",
89 in: []string{"test"},
90 },
91 {
92 where: loc(),
93 kind: longTest,
94 val: false,
95 str: "false",
96 in: []string{"test", "--long=false"},
97 },
98 {
99 where: loc(),
100 kind: longTest,
101 val: false,
102 def: true,
103 str: "false",
104 in: []string{"test", "--long=false"},
105 },
106
107 {
108 where: loc(),
109 val: int(42),
110 str: "42",
111 in: []string{"test", "-s", "42"},
112 },
113 {
114 where: loc(),
115 val: int8(42),
116 str: "42",
117 in: []string{"test", "-s", "42"},
118 },
119 {
120 where: loc(),
121 val: int16(42),
122 str: "42",
123 in: []string{"test", "-s", "42"},
124 },
125 {
126 where: loc(),
127 val: int32(42),
128 str: "42",
129 in: []string{"test", "-s", "42"},
130 },
131 {
132 where: loc(),
133 val: int64(42),
134 str: "42",
135 in: []string{"test", "-s", "42"},
136 },
137
138 {
139 where: loc(),
140 val: uint(42),
141 str: "42",
142 in: []string{"test", "-s", "42"},
143 },
144 {
145 where: loc(),
146 val: uint8(42),
147 str: "42",
148 in: []string{"test", "-s", "42"},
149 },
150 {
151 where: loc(),
152 val: uint16(42),
153 str: "42",
154 in: []string{"test", "-s", "42"},
155 },
156 {
157 where: loc(),
158 val: uint32(42),
159 str: "42",
160 in: []string{"test", "-s", "42"},
161 },
162 {
163 where: loc(),
164 val: uint64(42),
165 str: "42",
166 in: []string{"test", "-s", "42"},
167 },
168
169 {
170 where: loc(),
171 val: float32(4.2),
172 str: "4.2",
173 in: []string{"test", "-s", "4.2"},
174 },
175 {
176 where: loc(),
177 val: float64(4.2),
178 str: "4.2",
179 in: []string{"test", "-s", "4.2"},
180 },
181
182 {
183 where: loc(),
184 val: time.Duration(time.Second * 42),
185 def: time.Second * 2,
186 str: "42s",
187 in: []string{"test", "-s", "42s"},
188 },
189 {
190 where: loc(),
191 val: time.Duration(time.Second * 42),
192 def: time.Second * 2,
193 str: "42s",
194 in: []string{"test", "-s42s"},
195 },
196 {
197 where: loc(),
198 val: time.Duration(time.Second * 2),
199 def: time.Second * 2,
200 in: []string{"test", "-s42"},
201 str: "2s",
202 err: "test: time: missing unit in duration",
203 },
204
205 {
206 where: loc(),
207 val: []string{"42", "."},
208 str: "42,.",
209 def: []string{"one", "two", "three"},
210 in: []string{"test", "-s42", "-s."},
211 },
212 {
213 where: loc(),
214 val: []string{"42", "."},
215 str: "42,.",
216 def: []string{"one", "two", "three"},
217 in: []string{"test", "-s42,."},
218 },
219 {
220 where: loc(),
221 val: []string{"one", "two", "three"},
222 def: []string{"one", "two", "three"},
223 str: "one,two,three",
224 in: []string{"test"},
225 },
226 } {
227 reset()
228 var opt Option
229 val := reflect.New(reflect.TypeOf(tt.val)).Interface()
230 if tt.def != nil {
231 reflect.ValueOf(val).Elem().Set(reflect.ValueOf(tt.def))
232 }
233 switch tt.kind {
234 case shortTest:
235 opt = Flag(val, 's')
236 case longTest:
237 opt = FlagLong(val, "long", 0)
238 case bothTest:
239 opt = FlagLong(val, "both", 'b')
240 }
241 _ = opt
242 parse(tt.in)
243 if s := checkError(tt.err); s != "" {
244 t.Errorf("%s: %s", tt.where, s)
245 continue
246 }
247 got := reflect.ValueOf(val).Elem().Interface()
248 want := reflect.ValueOf(tt.val).Interface()
249 if !reflect.DeepEqual(got, want) {
250 t.Errorf("%s: got %v, want %v", tt.where, got, want)
251 }
252 if str := opt.String(); str != tt.str {
253 t.Errorf("%s: got string %q, want %q", tt.where, str, tt.str)
254 }
255 }
256 }
257
258 func TestGenericDup(t *testing.T) {
259 defer func() {
260 stderr = os.Stderr
261 exit = os.Exit
262 }()
263
264 reset()
265 var v1, v2 string
266 type myPanic struct{}
267 var errbuf bytes.Buffer
268 stderr = &errbuf
269 _, file, line, _ := runtime.Caller(0)
270 Flag(&v1, 's')
271 line++ // line is now the line number of the first call to Flag.
272
273 exit = func(i int) { panic(myPanic{}) }
274 defer func() {
275 p := recover()
276 if _, ok := p.(myPanic); ok {
277 err := errbuf.String()
278 if !strings.Contains(err, "-s already declared") || !strings.Contains(err, fmt.Sprintf("%s:%d", file, line)) {
279 t.Errorf("unexpected error: %q\nshould contain \"-s already declared\" and \"%s:%d\"", err, file, line)
280 }
281 } else if p == nil {
282 t.Errorf("Second call to Flag did not fail")
283 } else {
284 t.Errorf("panic %v", p)
285 }
286 }()
287 Flag(&v2, 's')
288 }
289
290 func TestGenericDupNested(t *testing.T) {
291 defer func() {
292 stderr = os.Stderr
293 exit = os.Exit
294 }()
295
296 reset()
297 type myPanic struct{}
298 var errbuf bytes.Buffer
299 stderr = &errbuf
300 _, file, line, _ := runtime.Caller(0)
301 String('s', "default")
302 line++ // line is now the line number of the first call to Flag.
303
304 exit = func(i int) { panic(myPanic{}) }
305 defer func() {
306 p := recover()
307 if _, ok := p.(myPanic); ok {
308 err := errbuf.String()
309 if !strings.Contains(err, "-s already declared") || !strings.Contains(err, fmt.Sprintf("%s:%d", file, line)) {
310 t.Errorf("unexpected error: %q\nshould contain \"-s already declared\" and \"%s:%d\"", err, file, line)
311 }
312 } else if p == nil {
313 t.Errorf("Second call to Flag did not fail")
314 } else {
315 t.Errorf("panic %v", p)
316 }
317 }()
318 String('s', "default")
319 }