-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag_test.go
More file actions
210 lines (178 loc) · 4.46 KB
/
flag_test.go
File metadata and controls
210 lines (178 loc) · 4.46 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package flagx
import (
"errors"
"flag"
"fmt"
"reflect"
"strings"
"testing"
)
func Test_AliasedFlagSet(t *testing.T) {
var (
str1 string
str2 string
int1 int
int2 int
bool1 bool
bool2 bool
int641 int64
int642 int64
float641 float64
float642 float64
)
fs := flag.NewFlagSet("test", flag.ExitOnError)
fs.StringVar(&str1, "str1", "", "usage str1")
fs.IntVar(&int1, "int1", 0, "usage int1")
AliasedStringVar(fs, &str2, "str2,s2", "valstr2", "usage str2")
AliasedIntVar(fs, &int2, "int2,i2", 10, "usage int2")
AliasedBoolVar(fs, &bool2, "bool2,b2,alias-bool2", true, "usage bool2")
fs.BoolVar(&bool1, "bool1", false, "usage bool1")
fs.Int64Var(&int641, "int641", 0, "usage int641")
AliasedInt64Var(fs, &int642, "int642,i642", 99999999999, "usage int642")
fs.Float64Var(&float641, "float641", 0, "usage flat641")
AliasedFloat64Var(fs, &float642, "float642,f642", 1234, "usage float642")
var buf strings.Builder
fs.SetOutput(&buf)
fs.PrintDefaults()
got := buf.String()
for _, want := range []string{"-str1 string", "-int1 int", "-str2 string", "-s2 string",
"-int2 int", "-i2 int", "-bool2", "-b2", "-alias-bool2", "-bool1",
"-int641 int", "-int642 int", "-i642 int",
"-float641 float", "-float642 float", "-f642 float"} {
if !strings.Contains(got, want) {
t.Errorf("PrintDefaults(): got %v, want substring %v", got, want)
}
}
}
func Test_AliasedStringsVar(t *testing.T) {
tests := []struct {
name string
args string
wantErr error
wantErrMsg string
wantOutput string
wantStrings []string
}{
{
name: "empty command line",
args: "",
},
{
name: "one arg with one string",
args: "-a s1",
wantStrings: []string{"s1"},
},
{
name: "one arg with two strings",
args: "--astr s1,s2 ",
wantStrings: []string{"s1", "s2"},
},
{
name: "two args with one string",
args: "-a s1 --astr s2 ",
wantStrings: []string{"s1", "s2"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var astr []string
var out strings.Builder
fs := flag.NewFlagSet("test", flag.ExitOnError)
fs.SetOutput(&out)
AliasedStringsVar(fs, &astr, "a,astr", "usage astr")
args := splitTrimSpace(tt.args, " ")
err := fs.Parse(args)
if err != nil {
if (tt.wantErr == nil) && (tt.wantErrMsg == "") {
t.Errorf("error: got %q, want nil", err)
return
}
}
if tt.wantErr != nil {
if err == nil {
t.Errorf("error: got nil, want %q", tt.wantErrMsg)
return
}
if !errors.Is(err, tt.wantErr) {
t.Errorf("error: got %q, want %q", err, tt.wantErr)
return
}
}
if tt.wantStrings != nil {
if !reflect.DeepEqual(astr, tt.wantStrings) {
t.Errorf("strings: got %v, want %v", astr, tt.wantStrings)
}
}
})
}
}
func Test_IsPassed_Int(t *testing.T) {
const defValue = 99
tests := []struct {
name string
args string
flagNames string
wantPassed bool
wantValue int
}{
{
name: "empty command line",
args: "",
flagNames: "i,int",
wantPassed: false,
wantValue: defValue,
},
{
name: "arg --other",
args: "--other 12",
flagNames: "i,int",
wantPassed: false,
wantValue: defValue,
},
{
name: "arg --int",
args: fmt.Sprintf("--int %v", defValue),
flagNames: "i,int",
wantPassed: true,
wantValue: defValue,
},
{
name: "arg -i",
args: "-i 12",
flagNames: "int,i",
wantPassed: true,
wantValue: 12,
},
{
name: "multiple win the last",
args: "-i 12 -o 13 --int 14 --other 15",
flagNames: "int,i",
wantPassed: true,
wantValue: 14,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var value int
var other int
var out strings.Builder
fs := flag.NewFlagSet("test", flag.ExitOnError)
fs.SetOutput(&out)
AliasedIntVar(fs, &value, "i,int", defValue, "usage int")
AliasedIntVar(fs, &other, "o,other", defValue, "usage other")
args := splitTrimSpace(tt.args, " ")
err := fs.Parse(args)
if err != nil {
t.Errorf("error: got %q, want nil", err)
return
}
gotPassed := IsPassed(fs, tt.flagNames)
if gotPassed != tt.wantPassed {
t.Errorf("IsFlagPassed(%q): got %v, want %v", tt.flagNames, gotPassed, tt.wantPassed)
}
if value != tt.wantValue {
t.Errorf("Value(%q): got %v, want %v", tt.flagNames, value, tt.wantValue)
}
})
}
}