-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs_test.go
209 lines (182 loc) · 6.32 KB
/
args_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package opt
import (
"reflect"
"strings"
"testing"
)
// TestParse tests parse method.
func TestParse(t *testing.T) {
split := func(str string) []string { return strings.Split(str, ":") }
flags := map[string]int{"d": 1, "U": 1, "g": 1, "verbose": 1}
expected := map[string][]string{
"0": {"./app"}, "1": {"5"}, "2": {"10"}, "3": {"15"},
"U": {"Jan,Bob"}, "d": {"true"}, "g": {"Hello, world"},
"verbose": {"false"},
}
tests := [][]string{
split("./app:-dU:Jan,Bob:--no-verbose:-g:Hello, world:5:10:15"),
split("./app:-d:-U:Jan,Bob:--no-verbose:-g:Hello, world:--:5:10:15"),
split("./app:-dU:Jan,Bob:--verbose:false:-g:Hello, world:5:10:15"),
split("./app:-dUJan,Bob:--verbose=false:-gHello, world:5:10:15"),
split("./app:5:10:15:-dUJan,Bob:--verbose:false:-gHello, world"),
split("./app:5:10:15:-UJan,Bob:--verbose:false:-gHello, world:-d:true"),
split("./app:5:-UJan,Bob:--verbose:false:-gHello, world:-d:--:10:15"),
}
am := argMap{}
for _, test := range tests {
if err := am.parse(test, flags); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(am.asFlat(), expected) {
t.Errorf("expected %v but %v", expected, am)
}
}
}
// TestParseMultiflags tests parse method with multiflags.
func TestParseMultiflags(t *testing.T) {
split := func(str string) []string { return strings.Split(str, ":") }
flags := map[string]int{"U": 1, "verbose": 1}
expected := map[string][]string{
"0": {"./app"}, "1": {"5"}, "2": {"10"}, "3": {"15"},
"U": {"Jan", "Bob", "Smit"}, "verbose": {"false"},
}
tests := [][]string{
split("./app:-U:Jan:-UBob:-U:Smit:--no-verbose:5:10:15"),
split("./app:5:10:-UJan:--no-verbose:-U:Bob:-U:Smit:15"),
}
am := argMap{}
for _, test := range tests {
if err := am.parse(test, flags); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(am.asFlat(), expected) {
t.Errorf("expected %v but %v", expected, am)
}
}
}
// TestParseErrors tests parse methods with errors.
func TestParseErrors(t *testing.T) {
split := func(str string) []string { return strings.Split(str, ":") }
flags := map[string]int{"U": 1, "verbose": 1}
tests := [][]string{
split("./app:-dU:Jan:-UBob:-U:Smit:--no-verbose"), // -d
split("./app:-UJan:--users=Bob,Smit"), // --users
split("./app:-UJan:--no-users=Bob,Smit"), // --no-users
}
am := argMap{}
for _, test := range tests {
if err := am.parse(test, flags); err == nil {
t.Error("an error on a non-existent flag is expected")
}
}
}
// TestPositional tests positional method.
func TestPositional(t *testing.T) {
split := func(str string) []string { return strings.Split(str, ":") }
flags := map[string]int{"d": 1, "U": 1, "g": 1, "verbose": 1}
expected := []string{"5", "10", "15"} // ignore app name "./app"
tests := [][]string{
split("./app:-dU:Jan,Bob:--no-verbose:-g:Hello, world:5:10:15"),
split("./app:-d:-U:Jan,Bob:--no-verbose:-g:Hello, world:--:5:10:15"),
split("./app:-dU:Jan,Bob:--verbose:false:-g:Hello, world:5:10:15"),
split("./app:-dUJan,Bob:--verbose=false:-gHello, world:5:10:15"),
split("./app:5:10:15:-dUJan,Bob:--verbose:false:-gHello, world"),
split("./app:5:10:15:-UJan,Bob:--no-verbose:-gHello, world:-d:true"),
split("./app:5:-UJan,Bob:--verbose:false:-gHello, world:-d:--:10:15"),
}
for i, test := range tests {
am := argMap{}
if err := am.parse(test, flags); err != nil {
t.Error(err)
}
// Check positional arguments without app name.
if r := am.posValues(); !reflect.DeepEqual(r, expected) {
t.Errorf("%d test, expected %v but %v", i, expected, r)
}
// Check app name.
value, _ := am.flagValue("0", "", "", "")
if r := strings.Join(value, ""); r != tests[0][0] {
t.Errorf("%d test, expected %v but %v", i, tests[0][0], r)
}
}
}
// TestValue tests value method.
func TestValue(t *testing.T) {
split := func(str string) []string { return strings.Split(str, ":") }
flags := map[string]int{"d": 1, "U": 1, "g": 1, "verbose": 1, "users": 1}
expected := []string{"Jan,Bob"}
tests := [][]string{
// Short flag for users.
split("./app:-dU:Jan,Bob:--no-verbose:-g:Hello, world:5:10:15"),
split("./app:-d:-U:Jan,Bob:--no-verbose:-g:Hello, world:--:5:10:15"),
split("./app:-dU:Jan,Bob:--verbose:false:-g:Hello, world:5:10:15"),
// Long flag for users.
split("./app:--users=Jan,Bob:--verbose=false:-gHello, world:5:10:15"),
split("./app:5:10:15:--users:Jan,Bob:--verbose:false:-gHello, world"),
// Default value.
split("./app:5:10:15:--verbose:false:-gHello, world:-d:true"),
split("./app:5:--verbose:false:-gHello, world:-d:--:10:15"),
}
am := argMap{}
for _, test := range tests {
if err := am.parse(test, flags); err != nil {
t.Error(err)
}
r, _ := am.flagValue("U", "users", "Jan,Bob", "")
if !reflect.DeepEqual(r, expected) {
t.Errorf("expected %v but %v", expected, r)
}
}
}
// TestSplit tests split method.
func TestSplit(t *testing.T) {
split := func(str string) []string { return strings.Split(str, ":") }
equal := func(a, b []string) bool {
am := make(map[string]bool, len(a))
for _, key := range a {
am[key] = true
}
bm := make(map[string]bool, len(b))
for _, key := range b {
bm[key] = true
}
return reflect.DeepEqual(am, bm)
}
flags := map[string]int{"d": 1, "U": 1, "g": 1, "verbose": 1, "users": 1}
expected := []string{"Jan", "Bob", "Roy"}
tests := [][]string{
split("./app:-U:Jan:--users:Bob:--users=Roy"),
split("./app:--users:Jan:-U:Bob:-URoy"),
}
am := argMap{}
for i, test := range tests {
if err := am.parse(test, flags); err != nil {
t.Error(err)
}
r, _ := am.flagValue("U", "users", "Jan,Bob,Roy", "")
if !equal(r, expected) {
t.Errorf("test %d, expected %v but %v", i, expected, r)
}
}
}
// TestSplitFolding tests split method with folding data.
func TestSplitFolding(t *testing.T) {
split := func(str string) []string { return strings.Split(str, ":") }
flags := map[string]int{"d": 1, "U": 1, "g": 1, "verbose": 1, "users": 1}
expected := []string{"Jan", "John,Bob", "Roy"}
tests := [][]string{
split("./app:-U:Jan:-U:John,Bob:-URoy"),
split("./app:--users:Jan:-UJohn,Bob:-URoy"),
split("./app:--users=Jan:-U:John,Bob:-URoy"),
}
am := argMap{}
for i, test := range tests {
if err := am.parse(test, flags); err != nil {
t.Error(err)
}
r, _ := am.flagValue("U", "users", "Jan,Bob,Roy", "")
if !reflect.DeepEqual(r, expected) {
t.Errorf("test %d, expected %v but %v", i, expected, r)
}
}
}