| 1 | // Copyright 2013 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 uint32Tests = []struct { |
| 14 | where string |
| 15 | in []string |
| 16 | i uint32 |
| 17 | uint32 uint32 |
| 18 | err string |
| 19 | }{ |
| 20 | { |
| 21 | loc(), |
| 22 | []string{}, |
| 23 | 17, 42, |
| 24 | "", |
| 25 | }, |
| 26 | { |
| 27 | loc(), |
| 28 | []string{"test", "-i", "1", "--uint32", "2"}, |
| 29 | 1, 2, |
| 30 | "", |
| 31 | }, |
| 32 | { |
| 33 | loc(), |
| 34 | []string{"test", "-i1", "--uint32=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 TestUint32(t *testing.T) { |
| 65 | for x, tt := range uint32Tests { |
| 66 | reset() |
| 67 | i := Uint32('i', 17) |
| 68 | opt := Uint32Long("uint32", 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.uint32; got != want { |
| 81 | t.Errorf("%s: got %v, want %v", tt.where, got, want) |
| 82 | } |
| 83 | } |
| 84 | } |