-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_test.go
More file actions
62 lines (57 loc) · 1.73 KB
/
struct_test.go
File metadata and controls
62 lines (57 loc) · 1.73 KB
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
package struct2pflag_test
import (
"testing"
"github.com/goark/struct2pflag"
"github.com/spf13/pflag"
)
type subStruct1a struct {
Boolean1a bool `pflag:"bool1a,b,Enable boolean 1a mode"`
}
type subStruct1b struct {
Boolean1b bool `pflag:"bool1b,B,Enable boolean 1b mode"`
}
type subStruct2a struct {
Boolean2a bool `flag:"bool2a,Enable boolean 2a mode"`
}
type subStruct2b struct {
Boolean2b bool `flag:"bool2b,Enable boolean 2b mode"`
}
type tsStruct struct {
Sub0a subStruct1a // no tag, should be ignored
Sub0b *subStruct1a `flag:""` // nil pointer, should be ignored
Sub1a subStruct1a `flag:""` // empty tag, should be processed
Sub1b subStruct1b `pflag:""` // empty tag, should be processed
Sub2a *subStruct2a `flag:""` // pointer to struct, should be processed
Sub2b subStruct2b `pflag:""` // empty tag, should be processed
}
func TestPflagBindStruct(t *testing.T) {
ts := &tsStruct{Sub2a: &subStruct2a{}} // initialize pointer field
flagSet := pflag.NewFlagSet("", pflag.ContinueOnError)
struct2pflag.Bind(flagSet, ts)
err := flagSet.Parse(
[]string{
"--bool1a",
"--bool1b",
"--bool2a",
"--bool2b",
},
)
if err != nil {
t.Fatal(err)
}
if expect := false; ts.Sub0a.Boolean1a != expect {
t.Errorf("expect %#v,but %#v", ts.Sub0a.Boolean1a, expect)
}
if expect := true; ts.Sub1a.Boolean1a != expect {
t.Errorf("expect %#v,but %#v", ts.Sub1a.Boolean1a, expect)
}
if expect := true; ts.Sub1b.Boolean1b != expect {
t.Errorf("expect %#v,but %#v", ts.Sub1b.Boolean1b, expect)
}
if expect := true; ts.Sub2a.Boolean2a != expect {
t.Errorf("expect %#v,but %#v", ts.Sub2a.Boolean2a, expect)
}
if expect := true; ts.Sub2b.Boolean2b != expect {
t.Errorf("expect %#v,but %#v", ts.Sub2b.Boolean2b, expect)
}
}