-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_test.go
189 lines (146 loc) · 4.05 KB
/
flag_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package command_test
import (
"flag"
"net"
"strings"
"testing"
"time"
"github.com/Thiht/go-command"
)
type stringList []string
var _ flag.Getter = (*stringList)(nil)
func (sl *stringList) Set(value string) error {
*sl = strings.Split(value, ",")
return nil
}
func (sl *stringList) String() string {
return strings.Join(*sl, ",")
}
func (sl *stringList) Get() any {
return *sl
}
type ipVar struct {
IP net.IP
}
var _ flag.Getter = &ipVar{}
func (v *ipVar) Get() any {
return v.IP
}
func (v *ipVar) String() string {
return v.IP.String()
}
func (v *ipVar) Set(s string) error {
v.IP = net.ParseIP(s)
return nil
}
func TestLookup(t *testing.T) {
t.Parallel()
fs := flag.NewFlagSet("test", flag.ExitOnError)
fs.Bool("bool", false, "")
fs.Int("int", 0, "")
fs.Int64("int64", 0, "")
fs.Uint("uint", 0, "")
fs.Uint64("uint64", 0, "")
fs.String("string", "info", "")
fs.Float64("float64", 0, "")
fs.Duration("duration", 0, "")
var strings stringList
fs.Var(&strings, "strings", "")
fs.Var(&ipVar{}, "ip", "")
if err := fs.Parse([]string{
"-bool",
"-int=1",
"-int64=1",
"-uint=1",
"-uint64=1",
"-string=debug",
"-float64=3.14",
"-duration=1s",
"-strings=foo,bar,baz",
"-ip=127.0.0.1",
}); err != nil {
t.Fatal(err)
}
t.Run("lookup bool", func(t *testing.T) {
t.Parallel()
if boolValue := command.Lookup[bool](fs, "bool"); !boolValue {
t.Errorf("boolValue = %v, want %v", boolValue, true)
}
})
t.Run("lookup int", func(t *testing.T) {
t.Parallel()
if intValue := command.Lookup[int](fs, "int"); intValue != 1 {
t.Errorf("intValue = %v, want %v", intValue, 1)
}
})
t.Run("lookup int64", func(t *testing.T) {
t.Parallel()
if int64Value := command.Lookup[int64](fs, "int64"); int64Value != 1 {
t.Errorf("int64Value = %v, want %v", int64Value, 1)
}
})
t.Run("lookup uint", func(t *testing.T) {
t.Parallel()
if uintValue := command.Lookup[uint](fs, "uint"); uintValue != 1 {
t.Errorf("uintValue = %v, want %v", uintValue, 1)
}
})
t.Run("lookup uint64", func(t *testing.T) {
t.Parallel()
if uint64Value := command.Lookup[uint64](fs, "uint64"); uint64Value != 1 {
t.Errorf("uint64Value = %v, want %v", uint64Value, 1)
}
})
t.Run("lookup string", func(t *testing.T) {
t.Parallel()
if stringValue := command.Lookup[string](fs, "string"); stringValue != "debug" {
t.Errorf("stringValue = %v, want %v", stringValue, "debug")
}
})
t.Run("lookup float64", func(t *testing.T) {
t.Parallel()
if float64Value := command.Lookup[float64](fs, "float64"); float64Value != 3.14 {
t.Errorf("float64Value = %v, want %v", float64Value, 3.14)
}
})
t.Run("lookup duration", func(t *testing.T) {
t.Parallel()
if durationValue := command.Lookup[time.Duration](fs, "duration"); durationValue != time.Second {
t.Errorf("durationValue = %v, want %v", durationValue, time.Second)
}
})
t.Run("lookup custom type stringList", func(t *testing.T) {
t.Parallel()
if stringsValue := command.Lookup[stringList](fs, "strings"); len(stringsValue) != 3 || stringsValue[0] != "foo" || stringsValue[1] != "bar" || stringsValue[2] != "baz" {
t.Errorf("stringsValue = %v, want %v", stringsValue, []string{"foo", "bar", "baz"})
}
})
t.Run("lookup ip", func(t *testing.T) {
t.Parallel()
if ipValue := command.Lookup[net.IP](fs, "ip"); ipValue.String() != "127.0.0.1" {
t.Errorf("ipValue = %v, want %v", ipValue.String(), "127.0.0.1")
}
})
t.Run("lookup non-existent flag", func(t *testing.T) {
t.Parallel()
defer func() {
if recover() == nil {
t.Error("lookup of a non-existent flag should panic")
}
}()
if stringValue := command.Lookup[string](fs, "non-existent"); stringValue != "" {
t.Errorf("stringValue = %v, want %v", stringValue, "")
}
})
t.Run("lookup flag with wrong type", func(t *testing.T) {
t.Parallel()
defer func() {
if recover() == nil {
t.Error("lookup of a flag with wrong type should panic")
}
}()
if stringValue := command.Lookup[string](fs, "bool"); stringValue != "" {
t.Errorf("stringValue = %v, want %v", stringValue, "")
}
})
}