Debian Codegraph @

v2/int_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 "fmt"
9 "strings"
10 "testing"
11 )
12
13 var intTests = []struct {
14 where string
15 in []string
16 i int
17 int int
18 err string
19 }{
20 {
21 loc(),
22 []string{},
23 17, 42,
24 "",
25 },
26 {
27 loc(),
28 []string{"test", "-i", "1", "--int", "2"},
29 1, 2,
30 "",
31 },
32 {
33 loc(),
34 []string{"test", "-i1", "--int=2"},
35 1, 2,
36 "",
37 },
38 {
39 loc(),
40 []string{"test", "-i1", "-i2"},
41 2, 42,
42 "",
43 },
44 {
45 loc(),
46 []string{"test", "-i=1"},
47 17, 42,
48 "test: not a valid number: =1\n",
49 },
50 {
51 loc(),
52 []string{"test", "-i0x20"},
53 0x20, 42,
54 "",
55 },
56 {
57 loc(),
58 []string{"test", "-i010"},
59 8, 42,
60 "",
61 },
62 }
63
64 func TestInt(t *testing.T) {
65 for x, tt := range intTests {
66 reset()
67 i := Int('i', 17)
68 opt := IntLong("int", 0, 42)
69 if strings.Index(tt.where, ":-") > 0 {
70 tt.where = fmt.Sprintf("#%d", x)
71 }
72
73 parse(tt.in)
74 if s := checkError(tt.err); s != "" {
75 t.Errorf("%s: %s", tt.where, s)
76 }
77 if got, want := *i, tt.i; got != want {
78 t.Errorf("%s: got %v, want %v", tt.where, got, want)
79 }
80 if got, want := *opt, tt.int; got != want {
81 t.Errorf("%s: got %v, want %v", tt.where, got, want)
82 }
83 }
84 }
85
86 var int16Tests = []struct {
87 where string
88 in []string
89 i int16
90 int16 int16
91 err string
92 }{
93 {
94 loc(),
95 []string{},
96 17, 42,
97 "",
98 },
99 {
100 loc(),
101 []string{"test", "-i", "1", "--int16", "2"},
102 1, 2,
103 "",
104 },
105 {
106 loc(),
107 []string{"test", "-i1", "--int16=2"},
108 1, 2,
109 "",
110 },
111 {
112 loc(),
113 []string{"test", "-i1", "-i2"},
114 2, 42,
115 "",
116 },
117 {
118 loc(),
119 []string{"test", "-i=1"},
120 17, 42,
121 "test: not a valid number: =1\n",
122 },
123 {
124 loc(),
125 []string{"test", "-i0x20"},
126 0x20, 42,
127 "",
128 },
129 {
130 loc(),
131 []string{"test", "-i010"},
132 8, 42,
133 "",
134 },
135 }
136
137 func TestInt16(t *testing.T) {
138 for x, tt := range int16Tests {
139 reset()
140 i := Int16('i', 17)
141 opt := Int16Long("int16", 0, 42)
142 if strings.Index(tt.where, ":-") > 0 {
143 tt.where = fmt.Sprintf("#%d", x)
144 }
145
146 parse(tt.in)
147 if s := checkError(tt.err); s != "" {
148 t.Errorf("%s: %s", tt.where, s)
149 }
150 if got, want := *i, tt.i; got != want {
151 t.Errorf("%s: got %v, want %v", tt.where, got, want)
152 }
153 if got, want := *opt, tt.int16; got != want {
154 t.Errorf("%s: got %v, want %v", tt.where, got, want)
155 }
156 }
157 }
158
159 var int32Tests = []struct {
160 where string
161 in []string
162 i int32
163 int32 int32
164 err string
165 }{
166 {
167 loc(),
168 []string{},
169 17, 42,
170 "",
171 },
172 {
173 loc(),
174 []string{"test", "-i", "1", "--int32", "2"},
175 1, 2,
176 "",
177 },
178 {
179 loc(),
180 []string{"test", "-i1", "--int32=2"},
181 1, 2,
182 "",
183 },
184 {
185 loc(),
186 []string{"test", "-i1", "-i2"},
187 2, 42,
188 "",
189 },
190 {
191 loc(),
192 []string{"test", "-i=1"},
193 17, 42,
194 "test: not a valid number: =1\n",
195 },
196 {
197 loc(),
198 []string{"test", "-i0x20"},
199 0x20, 42,
200 "",
201 },
202 {
203 loc(),
204 []string{"test", "-i010"},
205 8, 42,
206 "",
207 },
208 }
209
210 func TestInt32(t *testing.T) {
211 for x, tt := range int32Tests {
212 reset()
213 i := Int32('i', 17)
214 opt := Int32Long("int32", 0, 42)
215 if strings.Index(tt.where, ":-") > 0 {
216 tt.where = fmt.Sprintf("#%d", x)
217 }
218
219 parse(tt.in)
220 if s := checkError(tt.err); s != "" {
221 t.Errorf("%s: %s", tt.where, s)
222 }
223 if got, want := *i, tt.i; got != want {
224 t.Errorf("%s: got %v, want %v", tt.where, got, want)
225 }
226 if got, want := *opt, tt.int32; got != want {
227 t.Errorf("%s: got %v, want %v", tt.where, got, want)
228 }
229 }
230 }
231
232 var int64Tests = []struct {
233 where string
234 in []string
235 i int64
236 int64 int64
237 err string
238 }{
239 {
240 loc(),
241 []string{},
242 17, 42,
243 "",
244 },
245 {
246 loc(),
247 []string{"test", "-i", "1", "--int64", "2"},
248 1, 2,
249 "",
250 },
251 {
252 loc(),
253 []string{"test", "-i1", "--int64=2"},
254 1, 2,
255 "",
256 },
257 {
258 loc(),
259 []string{"test", "-i1", "-i2"},
260 2, 42,
261 "",
262 },
263 {
264 loc(),
265 []string{"test", "-i=1"},
266 17, 42,
267 "test: not a valid number: =1\n",
268 },
269 {
270 loc(),
271 []string{"test", "-i0x20"},
272 0x20, 42,
273 "",
274 },
275 {
276 loc(),
277 []string{"test", "-i010"},
278 8, 42,
279 "",
280 },
281 }
282
283 func TestInt64(t *testing.T) {
284 for x, tt := range int64Tests {
285 reset()
286 i := Int64('i', 17)
287 opt := Int64Long("int64", 0, 42)
288 if strings.Index(tt.where, ":-") > 0 {
289 tt.where = fmt.Sprintf("#%d", x)
290 }
291
292 parse(tt.in)
293 if s := checkError(tt.err); s != "" {
294 t.Errorf("%s: %s", tt.where, s)
295 }
296 if got, want := *i, tt.i; got != want {
297 t.Errorf("%s: got %v, want %v", tt.where, got, want)
298 }
299 if got, want := *opt, tt.int64; got != want {
300 t.Errorf("%s: got %v, want %v", tt.where, got, want)
301 }
302 }
303 }
304
305 var uintTests = []struct {
306 where string
307 in []string
308 i uint
309 uint uint
310 err string
311 }{
312 {
313 loc(),
314 []string{},
315 17, 42,
316 "",
317 },
318 {
319 loc(),
320 []string{"test", "-i", "1", "--uint", "2"},
321 1, 2,
322 "",
323 },
324 {
325 loc(),
326 []string{"test", "-i1", "--uint=2"},
327 1, 2,
328 "",
329 },
330 {
331 loc(),
332 []string{"test", "-i1", "-i2"},
333 2, 42,
334 "",
335 },
336 {
337 loc(),
338 []string{"test", "-i=1"},
339 17, 42,
340 "test: not a valid number: =1\n",
341 },
342 {
343 loc(),
344 []string{"test", "-i0x20"},
345 0x20, 42,
346 "",
347 },
348 {
349 loc(),
350 []string{"test", "-i010"},
351 8, 42,
352 "",
353 },
354 }
355
356 func TestUint(t *testing.T) {
357 for x, tt := range uintTests {
358 reset()
359 i := Uint('i', 17)
360 opt := UintLong("uint", 0, 42)
361 if strings.Index(tt.where, ":-") > 0 {
362 tt.where = fmt.Sprintf("#%d", x)
363 }
364
365 parse(tt.in)
366 if s := checkError(tt.err); s != "" {
367 t.Errorf("%s: %s", tt.where, s)
368 }
369 if got, want := *i, tt.i; got != want {
370 t.Errorf("%s: got %v, want %v", tt.where, got, want)
371 }
372 if got, want := *opt, tt.uint; got != want {
373 t.Errorf("%s: got %v, want %v", tt.where, got, want)
374 }
375 }
376 }
377
378 var uint16Tests = []struct {
379 where string
380 in []string
381 i uint16
382 uint16 uint16
383 err string
384 }{
385 {
386 loc(),
387 []string{},
388 17, 42,
389 "",
390 },
391 {
392 loc(),
393 []string{"test", "-i", "1", "--uint16", "2"},
394 1, 2,
395 "",
396 },
397 {
398 loc(),
399 []string{"test", "-i1", "--uint16=2"},
400 1, 2,
401 "",
402 },
403 {
404 loc(),
405 []string{"test", "-i1", "-i2"},
406 2, 42,
407 "",
408 },
409 {
410 loc(),
411 []string{"test", "-i=1"},
412 17, 42,
413 "test: not a valid number: =1\n",
414 },
415 {
416 loc(),
417 []string{"test", "-i0x20"},
418 0x20, 42,
419 "",
420 },
421 {
422 loc(),
423 []string{"test", "-i010"},
424 8, 42,
425 "",
426 },
427 }
428
429 func TestUint16(t *testing.T) {
430 for x, tt := range uint16Tests {
431 reset()
432 i := Uint16('i', 17)
433 opt := Uint16Long("uint16", 0, 42)
434 if strings.Index(tt.where, ":-") > 0 {
435 tt.where = fmt.Sprintf("#%d", x)
436 }
437
438 parse(tt.in)
439 if s := checkError(tt.err); s != "" {
440 t.Errorf("%s: %s", tt.where, s)
441 }
442 if got, want := *i, tt.i; got != want {
443 t.Errorf("%s: got %v, want %v", tt.where, got, want)
444 }
445 if got, want := *opt, tt.uint16; got != want {
446 t.Errorf("%s: got %v, want %v", tt.where, got, want)
447 }
448 }
449 }
450
451 var uint32Tests = []struct {
452 where string
453 in []string
454 i uint32
455 uint32 uint32
456 err string
457 }{
458 {
459 loc(),
460 []string{},
461 17, 42,
462 "",
463 },
464 {
465 loc(),
466 []string{"test", "-i", "1", "--uint32", "2"},
467 1, 2,
468 "",
469 },
470 {
471 loc(),
472 []string{"test", "-i1", "--uint32=2"},
473 1, 2,
474 "",
475 },
476 {
477 loc(),
478 []string{"test", "-i1", "-i2"},
479 2, 42,
480 "",
481 },
482 {
483 loc(),
484 []string{"test", "-i=1"},
485 17, 42,
486 "test: not a valid number: =1\n",
487 },
488 {
489 loc(),
490 []string{"test", "-i0x20"},
491 0x20, 42,
492 "",
493 },
494 {
495 loc(),
496 []string{"test", "-i010"},
497 8, 42,
498 "",
499 },
500 }
501
502 func TestUint32(t *testing.T) {
503 for x, tt := range uint32Tests {
504 reset()
505 i := Uint32('i', 17)
506 opt := Uint32Long("uint32", 0, 42)
507 if strings.Index(tt.where, ":-") > 0 {
508 tt.where = fmt.Sprintf("#%d", x)
509 }
510
511 parse(tt.in)
512 if s := checkError(tt.err); s != "" {
513 t.Errorf("%s: %s", tt.where, s)
514 }
515 if got, want := *i, tt.i; got != want {
516 t.Errorf("%s: got %v, want %v", tt.where, got, want)
517 }
518 if got, want := *opt, tt.uint32; got != want {
519 t.Errorf("%s: got %v, want %v", tt.where, got, want)
520 }
521 }
522 }
523
524 var uint64Tests = []struct {
525 where string
526 in []string
527 i uint64
528 uint64 uint64
529 err string
530 }{
531 {
532 loc(),
533 []string{},
534 17, 42,
535 "",
536 },
537 {
538 loc(),
539 []string{"test", "-i", "1", "--uint64", "2"},
540 1, 2,
541 "",
542 },
543 {
544 loc(),
545 []string{"test", "-i1", "--uint64=2"},
546 1, 2,
547 "",
548 },
549 {
550 loc(),
551 []string{"test", "-i1", "-i2"},
552 2, 42,
553 "",
554 },
555 {
556 loc(),
557 []string{"test", "-i=1"},
558 17, 42,
559 "test: not a valid number: =1\n",
560 },
561 {
562 loc(),
563 []string{"test", "-i0x20"},
564 0x20, 42,
565 "",
566 },
567 {
568 loc(),
569 []string{"test", "-i010"},
570 8, 42,
571 "",
572 },
573 }
574
575 func TestUint64(t *testing.T) {
576 for x, tt := range uint64Tests {
577 reset()
578 i := Uint64('i', 17)
579 opt := Uint64Long("uint64", 0, 42)
580 if strings.Index(tt.where, ":-") > 0 {
581 tt.where = fmt.Sprintf("#%d", x)
582 }
583
584 parse(tt.in)
585 if s := checkError(tt.err); s != "" {
586 t.Errorf("%s: %s", tt.where, s)
587 }
588 if got, want := *i, tt.i; got != want {
589 t.Errorf("%s: got %v, want %v", tt.where, got, want)
590 }
591 if got, want := *opt, tt.uint64; got != want {
592 t.Errorf("%s: got %v, want %v", tt.where, got, want)
593 }
594 }
595 }