forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount_test.go
117 lines (105 loc) · 2.61 KB
/
count_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
// 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"
"testing"
"github.com/zulucmd/zflag/v2"
)
func TestCount(t *testing.T) {
tests := []struct {
name string
input []string
expectedErr string
expectedValue int
}{
{
name: "no flags",
input: []string{},
expectedValue: 0,
},
{
name: "single count",
input: []string{"-v"},
expectedValue: 1,
},
{
name: "multiple times",
input: []string{"-vvv"},
expectedValue: 3,
},
{
name: "multiple times separated",
input: []string{"-v", "-v", "-v"},
expectedValue: 3,
},
{
name: "multiple times interchanged and separated",
input: []string{"-v", "--verbose", "-v"},
expectedValue: 3,
},
{
name: "multiple times with value",
input: []string{"-v=3", "-v"},
expectedValue: 4,
},
{
name: "long opt with value",
input: []string{"--verbose=0"},
expectedValue: 0,
},
{
name: "single with value",
input: []string{"-v=0"},
expectedValue: 0,
},
{
name: "",
input: []string{"-v=a"},
expectedErr: `invalid argument "a" for "-v, --verbose" flag: strconv.ParseInt: parsing "a": invalid syntax`,
expectedValue: 0,
},
}
t.Parallel()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
var verbose int
f := zflag.NewFlagSet("test", zflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.CountVar(&verbose, "verbose", "usage", zflag.OptShorthand('v'))
err := f.Parse(test.input)
if test.expectedErr != "" {
assertErrMsg(t, test.expectedErr, err)
return
}
assertNoErr(t, err)
assertEqual(t, test.expectedValue, verbose)
getVerbose, err := f.GetCount("verbose")
assertNoErr(t, err)
assertEqual(t, test.expectedValue, getVerbose)
getVerboseGet, err := f.Get("verbose")
assertNoErr(t, err)
assertEqual(t, test.expectedValue, getVerboseGet)
defer assertNoPanic(t)()
mustBool := f.MustGetCount("verbose")
assertEqual(t, test.expectedValue, mustBool)
})
}
}
func TestCountErrors(t *testing.T) {
t.Parallel()
var s string
var count int
f := zflag.NewFlagSet("test", zflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.StringVar(&s, "s", "", "usage")
f.CountVar(&count, "count", "usage")
err := f.Parse([]string{})
assertNoErr(t, err)
_, err = f.GetBool("s")
assertErr(t, err)
defer assertPanic(t)()
_ = f.MustGetCount("s")
}