| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package getopt |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestMandatory(t *testing.T) { |
| 12 |
for _, tt := range []struct { |
| 13 |
name string |
| 14 |
in []string |
| 15 |
err string |
| 16 |
}{ |
| 17 |
{ |
| 18 |
name: "required option present", |
| 19 |
in: []string{"test", "-r"}, |
| 20 |
}, |
| 21 |
{ |
| 22 |
name: "required option not present", |
| 23 |
in: []string{"test", "-o"}, |
| 24 |
err: "test: option -r is mandatory", |
| 25 |
}, |
| 26 |
{ |
| 27 |
name: "no options", |
| 28 |
in: []string{"test"}, |
| 29 |
err: "test: option -r is mandatory", |
| 30 |
}, |
| 31 |
} { |
| 32 |
reset() |
| 33 |
var val bool |
| 34 |
Flag(&val, 'o') |
| 35 |
Flag(&val, 'r').Mandatory() |
| 36 |
parse(tt.in) |
| 37 |
if s := checkError(tt.err); s != "" { |
| 38 |
t.Errorf("%s: %s", tt.name, s) |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestGroup(t *testing.T) { |
| 44 |
for _, tt := range []struct { |
| 45 |
name string |
| 46 |
in []string |
| 47 |
err string |
| 48 |
}{ |
| 49 |
{ |
| 50 |
name: "no args", |
| 51 |
in: []string{"test"}, |
| 52 |
err: "test: exactly one of the following options must be specified: -A, -B", |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "one of each", |
| 56 |
in: []string{"test", "-A", "-C"}, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "Two in group One", |
| 60 |
in: []string{"test", "-A", "-B"}, |
| 61 |
err: "test: options -A and -B are mutually exclusive", |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "Two in group Two", |
| 65 |
in: []string{"test", "-A", "-D", "-C"}, |
| 66 |
err: "test: options -C and -D are mutually exclusive", |
| 67 |
}, |
| 68 |
} { |
| 69 |
reset() |
| 70 |
var val bool |
| 71 |
Flag(&val, 'o') |
| 72 |
Flag(&val, 'A').SetGroup("One") |
| 73 |
Flag(&val, 'B').SetGroup("One") |
| 74 |
Flag(&val, 'C').SetGroup("Two") |
| 75 |
Flag(&val, 'D').SetGroup("Two") |
| 76 |
RequiredGroup("One") |
| 77 |
parse(tt.in) |
| 78 |
if s := checkError(tt.err); s != "" { |
| 79 |
t.Errorf("%s: %s", tt.name, s) |
| 80 |
} |
| 81 |
} |
| 82 |
} |