-
Notifications
You must be signed in to change notification settings - Fork 6
/
pointer_test.go
164 lines (123 loc) · 3.12 KB
/
pointer_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package flags
import (
"testing"
)
func TestPointerBool(t *testing.T) {
var opts = struct {
Value *bool `short:"v"`
}{}
ret := assertParseSuccess(t, &opts, "-v")
assertStringArray(t, ret, []string{})
if !*opts.Value {
t.Errorf("Expected Value to be true")
}
}
func TestPointerString(t *testing.T) {
var opts = struct {
Value *string `short:"v"`
}{}
ret := assertParseSuccess(t, &opts, "-v", "value")
assertStringArray(t, ret, []string{})
assertString(t, *opts.Value, "value")
}
func TestPointerSlice(t *testing.T) {
var opts = struct {
Value *[]string `short:"v"`
}{}
ret := assertParseSuccess(t, &opts, "-v", "value1", "-v", "value2")
assertStringArray(t, ret, []string{})
assertStringArray(t, *opts.Value, []string{"value1", "value2"})
}
func TestPointerMap(t *testing.T) {
var opts = struct {
Value *map[string]int `short:"v"`
}{}
ret := assertParseSuccess(t, &opts, "-v", "k1:2", "-v", "k2:-5")
assertStringArray(t, ret, []string{})
if v, ok := (*opts.Value)["k1"]; !ok {
t.Errorf("Expected key \"k1\" to exist")
} else if v != 2 {
t.Errorf("Expected \"k1\" to be 2, but got %#v", v)
}
if v, ok := (*opts.Value)["k2"]; !ok {
t.Errorf("Expected key \"k2\" to exist")
} else if v != -5 {
t.Errorf("Expected \"k2\" to be -5, but got %#v", v)
}
}
type marshalledString string
func (m *marshalledString) UnmarshalFlag(value string) error {
*m = marshalledString(value)
return nil
}
func (m marshalledString) MarshalFlag() (string, error) {
return string(m), nil
}
func TestPointerStringMarshalled(t *testing.T) {
var opts = struct {
Value *marshalledString `short:"v"`
}{}
ret := assertParseSuccess(t, &opts, "-v", "value")
assertStringArray(t, ret, []string{})
if opts.Value == nil {
t.Error("Expected value not to be nil")
return
}
assertString(t, string(*opts.Value), "value")
}
type marshalledStruct struct {
Value string
}
func (m *marshalledStruct) UnmarshalFlag(value string) error {
m.Value = value
return nil
}
func (m marshalledStruct) MarshalFlag() (string, error) {
return m.Value, nil
}
func TestPointerStructMarshalled(t *testing.T) {
var opts = struct {
Value *marshalledStruct `short:"v"`
}{}
ret := assertParseSuccess(t, &opts, "-v", "value")
assertStringArray(t, ret, []string{})
if opts.Value == nil {
t.Error("Expected value not to be nil")
return
}
assertString(t, opts.Value.Value, "value")
}
type PointerGroup struct {
Value bool `short:"v"`
}
func TestPointerGroup(t *testing.T) {
var opts = struct {
Group *PointerGroup `group:"Group Options"`
}{}
ret := assertParseSuccess(t, &opts, "-v")
assertStringArray(t, ret, []string{})
if !opts.Group.Value {
t.Errorf("Expected Group.Value to be true")
}
}
func TestDoNotChangeNonTaggedFields(t *testing.T) {
var opts struct {
A struct {
Pointer *int
}
B *struct {
Pointer *int
}
}
ret := assertParseSuccess(t, &opts)
assertStringArray(t, ret, []string{})
if opts.A.Pointer != nil {
t.Error("Expected A.Pointer to be nil")
}
if opts.B != nil {
t.Error("Expected B to be nil")
}
if opts.B != nil && opts.B.Pointer != nil {
t.Error("Expected B.Pointer to be nil")
}
}