forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_to_int64_test.go
164 lines (147 loc) · 4.85 KB
/
string_to_int64_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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag_test
import (
"io/ioutil"
"sort"
"strings"
"testing"
"github.com/zulucmd/zflag/v2"
)
func TestStringToInt64(t *testing.T) {
tests := []struct {
name string
flagDefault map[string]int64
input []string
expectedErr string
expectedValues map[string]int64
expectedStrValues []string
visitor func(f *zflag.Flag)
}{
{
name: "no value passed",
input: []string{},
flagDefault: map[string]int64{},
expectedErr: "",
expectedValues: map[string]int64{},
expectedStrValues: []string{},
},
{
name: "empty value passed",
input: []string{""},
flagDefault: map[string]int64{},
expectedErr: `invalid argument "" for "--s2i64" flag: must be formatted as key=value`,
},
{
name: "invalid int64",
input: []string{"blabla"},
flagDefault: map[string]int64{},
expectedErr: `invalid argument "blabla" for "--s2i64" flag: blabla must be formatted as key=value`,
},
{
name: "no csv",
input: []string{"test=1,5"},
flagDefault: map[string]int64{},
expectedErr: `invalid argument "test=1,5" for "--s2i64" flag: strconv.ParseInt: parsing "1,5": invalid syntax`,
},
{
name: "single key value pair per arg",
input: []string{"test=1=1"},
flagDefault: map[string]int64{},
expectedErr: `invalid argument "test=1=1" for "--s2i64" flag: strconv.ParseInt: parsing "1=1": invalid syntax`,
},
{
name: "overrides multiple calls",
input: []string{"test=1", "test=5"},
flagDefault: map[string]int64{},
expectedValues: map[string]int64{"test": 5},
expectedStrValues: []string{"test=5"},
},
{
name: "empty defaults",
input: []string{"test=1", "test2=5"},
flagDefault: map[string]int64{},
expectedValues: map[string]int64{"test": 1, "test2": 5},
expectedStrValues: []string{"test=1", "test2=5"},
},
{
name: "overrides default values",
input: []string{"test=1", "test2=5"},
flagDefault: map[string]int64{"test2": 1, "test": 5},
expectedValues: map[string]int64{"test": 1, "test2": 5},
expectedStrValues: []string{"test=1", "test2=5"},
},
{
name: "returns default values",
input: []string{},
flagDefault: map[string]int64{"test2": 1, "test": 5},
expectedValues: map[string]int64{"test2": 1, "test": 5},
expectedStrValues: []string{"test2=1", "test=5"},
},
{
name: "trims input",
input: []string{"test= 1", "test2=5 ", "test3= 9 "},
flagDefault: map[string]int64{},
expectedValues: map[string]int64{"test": 1, "test2": 5, "test3": 9},
expectedStrValues: []string{"test=1", "test2=5", "test3=9"},
},
}
t.Parallel()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
var s2i64 map[string]int64
f := zflag.NewFlagSet("test", zflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.StringToInt64Var(&s2i64, "s2i64", test.flagDefault, "usage")
err := f.Parse(repeatFlag("--s2i64", test.input...))
if test.expectedErr != "" {
assertErr(t, err)
assertEqualf(t, test.expectedErr, err.Error(), "expected error to equal %q, but was: %s", test.expectedErr, err)
return
}
assertNoErr(t, err)
if test.visitor != nil {
f.VisitAll(test.visitor)
}
assertDeepEqual(t, test.expectedValues, s2i64)
s2i64GetS2I64, err := f.GetStringToInt64("s2i64")
assertNoErr(t, err)
assertDeepEqual(t, test.expectedValues, s2i64GetS2I64)
s2i64Get, err := f.Get("s2i64")
assertNoErr(t, err)
assertDeepEqual(t, test.expectedValues, s2i64Get)
flag := f.Lookup("s2i64")
strVal := flag.Value.String()
if len(test.expectedStrValues) == 0 {
assertEqual(t, "[]", strVal)
} else {
assertEqual(t, '[', rune(strVal[0]))
assertEqual(t, ']', rune(strVal[len(strVal)-1]))
strVals := strings.Split(strVal[1:len(strVal)-1], " ")
sort.Strings(strVals)
sort.Strings(test.expectedStrValues)
assertDeepEqual(t, test.expectedStrValues, strVals)
}
defer assertNoPanic(t)()
mustStringToInt64 := f.MustGetStringToInt64("s2i64")
assertDeepEqual(t, test.expectedValues, mustStringToInt64)
})
}
}
func TestStringToInt64Errors(t *testing.T) {
t.Parallel()
var s string
var s2i64 map[string]int64
f := zflag.NewFlagSet("test", zflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.StringVar(&s, "s", "", "usage")
f.StringToInt64Var(&s2i64, "s2i64", map[string]int64{}, "usage")
err := f.Parse([]string{})
assertNoErr(t, err)
_, err = f.GetStringToInt64("s")
assertErr(t, err)
defer assertPanic(t)()
_ = f.MustGetStringToInt64("s")
}