-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_test.go
65 lines (59 loc) · 1.19 KB
/
option_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package rcli
import "testing"
type options struct {
Verbose *Opt
Action *Opt
}
func (cmd *options) Run(args []string) int {
return 0
}
func Test_optionsOf(t *testing.T) {
cmd := &options{
Verbose: &Opt{
flags: []string{"-v", "--verbose"},
},
Action: &Opt{
flags: []string{"-a", "--action"},
},
}
opts := optionsOf(cmd)
if len(opts) != 4 {
t.Error("optionsOf failed")
}
if v, found := opts["-v"]; !found || v != cmd.Verbose {
t.Error("optionsOf failed")
}
if v, found := opts["--verbose"]; !found || v != cmd.Verbose {
t.Error("optionsOf failed")
}
if v, found := opts["-a"]; !found || v != cmd.Action {
t.Error("optionsOf failed")
}
if v, found := opts["--action"]; !found || v != cmd.Action {
t.Error("optionsOf failed")
}
}
func Test_validateFlag(t *testing.T) {
cases := []struct {
in string
panic bool
}{
{"", true},
{"-", true},
{"--", true},
{"ab", true},
{"-ab", true},
{"--ab", false},
{"-a", false},
}
for _, c := range cases {
t.Run(c.in, func(t *testing.T) {
defer func() {
if r := recover(); (r != nil) != c.panic {
t.Errorf("validateFlag failed")
}
}()
validateOption(&Opt{flags: []string{c.in}})
})
}
}