| 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 signedNumberTests = []struct { |
| 14 | where string |
| 15 | in []string |
| 16 | l SignedLimit |
| 17 | out int64 |
| 18 | err string |
| 19 | }{ |
| 20 | { |
| 21 | where: loc(), |
| 22 | }, |
| 23 | { |
| 24 | loc(), |
| 25 | []string{"test", "-n", "1010"}, |
| 26 | SignedLimit{Base: 2, Bits: 5}, |
| 27 | 10, |
| 28 | "", |
| 29 | }, |
| 30 | { |
| 31 | loc(), |
| 32 | []string{"test", "-n", "1010"}, |
| 33 | SignedLimit{Base: 2, Bits: 4}, |
| 34 | 0, |
| 35 | "test: value out of range: 1010\n", |
| 36 | }, |
| 37 | { |
| 38 | loc(), |
| 39 | []string{"test", "-n", "-1000"}, |
| 40 | SignedLimit{Base: 2, Bits: 4}, |
| 41 | -8, |
| 42 | "", |
| 43 | }, |
| 44 | { |
| 45 | loc(), |
| 46 | []string{"test", "-n", "3"}, |
| 47 | SignedLimit{Min: 4, Max: 6}, |
| 48 | 0, |
| 49 | "test: value out of range (<4): 3\n", |
| 50 | }, |
| 51 | { |
| 52 | loc(), |
| 53 | []string{"test", "-n", "4"}, |
| 54 | SignedLimit{Min: 4, Max: 6}, |
| 55 | 4, |
| 56 | "", |
| 57 | }, |
| 58 | { |
| 59 | loc(), |
| 60 | []string{"test", "-n", "5"}, |
| 61 | SignedLimit{Min: 4, Max: 6}, |
| 62 | 5, |
| 63 | "", |
| 64 | }, |
| 65 | { |
| 66 | loc(), |
| 67 | []string{"test", "-n", "6"}, |
| 68 | SignedLimit{Min: 4, Max: 6}, |
| 69 | 6, |
| 70 | "", |
| 71 | }, |
| 72 | { |
| 73 | loc(), |
| 74 | []string{"test", "-n", "7"}, |
| 75 | SignedLimit{Min: 4, Max: 6}, |
| 76 | 0, |
| 77 | "test: value out of range (>6): 7\n", |
| 78 | }, |
| 79 | } |
| 80 | |
| 81 | func TestSigneds(t *testing.T) { |
| 82 | for x, tt := range signedNumberTests { |
| 83 | if strings.Index(tt.where, ":-") > 0 { |
| 84 | tt.where = fmt.Sprintf("#%d", x) |
| 85 | } |
| 86 | |
| 87 | reset() |
| 88 | n := Signed('n', 0, &tt.l) |
| 89 | parse(tt.in) |
| 90 | if s := checkError(tt.err); s != "" { |
| 91 | t.Errorf("%s: %s", tt.where, s) |
| 92 | } |
| 93 | if *n != tt.out { |
| 94 | t.Errorf("%s: got %v, want %v", tt.where, *n, tt.out) |
| 95 | } |
| 96 | } |
| 97 | } |