| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package getopt |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
var boolTests = []struct { |
| 14 |
where string |
| 15 |
in []string |
| 16 |
f bool |
| 17 |
fc int |
| 18 |
opt bool |
| 19 |
optc int |
| 20 |
err string |
| 21 |
}{ |
| 22 |
{ |
| 23 |
loc(), |
| 24 |
[]string{}, |
| 25 |
false, 0, |
| 26 |
false, 0, |
| 27 |
"", |
| 28 |
}, |
| 29 |
{ |
| 30 |
loc(), |
| 31 |
[]string{"test", "-f", "--opt"}, |
| 32 |
true, 1, |
| 33 |
true, 1, |
| 34 |
"", |
| 35 |
}, |
| 36 |
{ |
| 37 |
loc(), |
| 38 |
[]string{"test", "--f", "--opt"}, |
| 39 |
true, 1, |
| 40 |
true, 1, |
| 41 |
"", |
| 42 |
}, |
| 43 |
{ |
| 44 |
loc(), |
| 45 |
[]string{"test", "-ff", "-f", "--opt", "--opt"}, |
| 46 |
true, 3, |
| 47 |
true, 2, |
| 48 |
"", |
| 49 |
}, |
| 50 |
{ |
| 51 |
loc(), |
| 52 |
[]string{"test", "--opt", "--opt=false"}, |
| 53 |
false, 0, |
| 54 |
false, 2, |
| 55 |
"", |
| 56 |
}, |
| 57 |
{ |
| 58 |
loc(), |
| 59 |
[]string{"test", "-f", "false"}, |
| 60 |
true, 1, |
| 61 |
false, 0, |
| 62 |
"", |
| 63 |
}, |
| 64 |
{ |
| 65 |
loc(), |
| 66 |
[]string{"test", "-f=false"}, |
| 67 |
true, 1, |
| 68 |
false, 0, |
| 69 |
"test: unknown option: -=\n", |
| 70 |
}, |
| 71 |
{ |
| 72 |
loc(), |
| 73 |
[]string{"test", "-f", "false"}, |
| 74 |
true, 1, |
| 75 |
false, 0, |
| 76 |
"", |
| 77 |
}, |
| 78 |
} |
| 79 |
|
| 80 |
func TestBool(t *testing.T) { |
| 81 |
for x, tt := range boolTests { |
| 82 |
reset() |
| 83 |
f := Bool('f') |
| 84 |
opt := BoolLong("opt", 0) |
| 85 |
if strings.Index(tt.where, ":-") > 0 { |
| 86 |
tt.where = fmt.Sprintf("#%d", x) |
| 87 |
} |
| 88 |
|
| 89 |
parse(tt.in) |
| 90 |
if s := checkError(tt.err); s != "" { |
| 91 |
t.Errorf("%s: %s", tt.where, s) |
| 92 |
} |
| 93 |
if got, want := *f, tt.f; got != want { |
| 94 |
t.Errorf("%s: got %v, want %v", tt.where, got, want) |
| 95 |
} |
| 96 |
if got, want := *opt, tt.opt; got != want { |
| 97 |
t.Errorf("%s: got %v, want %v", tt.where, got, want) |
| 98 |
} |
| 99 |
if got, want := GetCount('f'), tt.fc; got != want { |
| 100 |
t.Errorf("%s: got %v, want %v", tt.where, got, want) |
| 101 |
} |
| 102 |
if got, want := GetCount("opt"), tt.optc; got != want { |
| 103 |
t.Errorf("%s: got %v, want %v", tt.where, got, want) |
| 104 |
} |
| 105 |
} |
| 106 |
} |