| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package getopt |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
var counterTests = []struct { |
| 14 |
where string |
| 15 |
in []string |
| 16 |
c int |
| 17 |
cnt int |
| 18 |
myint int |
| 19 |
err string |
| 20 |
}{ |
| 21 |
{ |
| 22 |
loc(), |
| 23 |
[]string{}, |
| 24 |
0, |
| 25 |
0, |
| 26 |
0, |
| 27 |
"", |
| 28 |
}, |
| 29 |
{ |
| 30 |
loc(), |
| 31 |
[]string{"test", "-c", "--cnt"}, |
| 32 |
1, |
| 33 |
1, |
| 34 |
0, |
| 35 |
"", |
| 36 |
}, |
| 37 |
{ |
| 38 |
loc(), |
| 39 |
[]string{"test", "-cc", "-c", "--cnt", "--cnt", "-mmmm", "--myint"}, |
| 40 |
3, |
| 41 |
2, |
| 42 |
5, |
| 43 |
"", |
| 44 |
}, |
| 45 |
{ |
| 46 |
loc(), |
| 47 |
[]string{"test", "--c=17", "--cnt=42", "--m=13"}, |
| 48 |
17, |
| 49 |
42, |
| 50 |
13, |
| 51 |
"", |
| 52 |
}, |
| 53 |
{ |
| 54 |
loc(), |
| 55 |
[]string{"test", "--cnt=false"}, |
| 56 |
0, 0, 0, |
| 57 |
"test: not a valid number: false\n", |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
func TestCounter(t *testing.T) { |
| 62 |
for x, tt := range counterTests { |
| 63 |
reset() |
| 64 |
var myint int |
| 65 |
c := Counter('c') |
| 66 |
cnt := CounterLong("cnt", 0) |
| 67 |
CounterLongVar(&myint, "myint", 'm') |
| 68 |
if strings.Index(tt.where, ":-") > 0 { |
| 69 |
tt.where = fmt.Sprintf("#%d", x) |
| 70 |
} |
| 71 |
|
| 72 |
parse(tt.in) |
| 73 |
if s := checkError(tt.err); s != "" { |
| 74 |
t.Errorf("%s: %s", tt.where, s) |
| 75 |
} |
| 76 |
if got, want := *c, tt.c; got != want { |
| 77 |
t.Errorf("%s: got %v, want %v", tt.where, got, want) |
| 78 |
} |
| 79 |
if got, want := *cnt, tt.cnt; got != want { |
| 80 |
t.Errorf("%s: got %v, want %v", tt.where, got, want) |
| 81 |
} |
| 82 |
if got, want := myint, tt.myint; got != want { |
| 83 |
t.Errorf("%s: got %v, want %v", tt.where, got, want) |
| 84 |
} |
| 85 |
} |
| 86 |
} |