-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs_test.go
201 lines (180 loc) · 4.95 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
package args
import (
. "testing"
)
func argsEqual(a1, a2 []string) bool {
if len(a1) != len(a2) {
return false
}
for i, v := range(a1) {
if v != a2[i] {
return false
}
}
return true
}
func TestParam(t *T) {
args, param, ok := Param([]string{"Foo", "Bar", "-F", "--Foobar"})
if !argsEqual(args, []string{"Bar", "-F", "--Foobar"}) {
t.Errorf("The first argument should have been consumed.")
}
if param != "Foo" {
t.Errorf("The first argument should have been returned.")
}
if !ok {
t.Errorf("Should have returned true.")
}
args, param, ok = Param(args)
if !argsEqual(args, []string{"-F", "--Foobar"}) {
t.Errorf("The first argument should have been consumed (again).")
}
if param != "Bar" {
t.Errorf("The first argument should have been returned.")
}
if !ok {
t.Errorf("Should have returned true.")
}
args, param, ok = Param(args)
if !argsEqual(args, []string{"--Foobar"}) {
t.Errorf("The first argument should have been consumed (again).")
}
if param != "-F" {
t.Errorf("The first argument should have been returned.")
}
if !ok {
t.Errorf("Should have returned true.")
}
args, param, ok = Param(args)
if !argsEqual(args, []string{}) {
t.Errorf("The final argument should have been consumed.")
}
if param != "--Foobar" {
t.Errorf("The final argument should have been returned.")
}
if !ok {
t.Errorf("Should have returned true.")
}
args, param, ok = Param(args)
if !argsEqual(args, []string{}) {
t.Errorf("Arguments should be unchanged.")
}
if ok {
t.Errorf("Should have returned false.")
}
}
func TestFlag(t *T) {
args, flag := Flag([]string{"Foo", "--bar", "-f", "what", "--what"}, "bar", "f")
if !argsEqual(args, []string{"Foo", "-f", "what", "--what"}) {
t.Errorf("Should have consumed the first matching flag.")
}
if !flag {
t.Errorf("Should have found the flag.")
}
args, flag = Flag(args, "w", "what")
if !argsEqual(args, []string{"Foo", "-f", "what"}) {
t.Errorf("Should have consumed the flag (with leading hyphens).")
}
if !flag {
t.Errorf("Should have found the flag.")
}
args, flag = Flag(args, "w", "what")
if !argsEqual(args, []string{"Foo", "-f", "what"}) {
t.Errorf("Should not have consumed anything.")
}
if flag {
t.Errorf("Should not have found the flag.")
}
args, flag = Flag([]string{}, "foo")
if !argsEqual(args, []string{}) {
t.Errorf("Should not have...invented new arguments?")
}
if flag {
t.Errorf("Should not have found the flag.")
}
}
func TestOption(t *T) {
args := []string{"Foo", "--bar", "fizz", "-b", "que", "-w"}
args, value, ok, err := Option(args, "b", "bar")
if !argsEqual(args, []string{"Foo", "-b", "que", "-w"}) {
t.Errorf("Should have consumed the first matching option and its value.")
}
if value != "fizz" {
t.Errorf("Should have gotten the value immediately after the first matching option.")
}
if !ok {
t.Errorf("Should have found the option.")
}
if err != nil {
t.Errorf("Should not have returned an error.")
}
args, value, ok, err = Option(args, "Foo", "f")
if !argsEqual(args, []string{"Foo", "-b", "que", "-w"}) {
t.Errorf("Should not have consumed anything.")
}
if ok {
t.Errorf("Should not have found the option.")
}
if err != nil {
t.Errorf("Should not have returned an error.")
}
args, value, ok, err = Option(args, "w")
if !argsEqual(args, []string{"Foo", "-b", "que", "-w"}) {
t.Errorf("Should not have consumed anything.")
}
if !ok {
t.Errorf("Should have found the option.")
}
if e, ok := err.(OptionMissingValue); !ok {
t.Errorf("Should have returned an OptionMissingValue error.")
} else if string(e) != "-w" {
t.Errorf("Should have returned an error with the name of the argument found.")
}
}
func TestOptionInt(t *T) {
args, val, ok, err := OptionInt([]string{"foo", "42"}, "foo")
if !argsEqual(args, []string{"foo", "42"}) {
t.Errorf("Should not have consumed anything.")
}
if ok {
t.Errorf("Should not have found the option.")
}
if err != nil {
t.Errorf("Should not have returned an error.")
}
args, val, ok, err = OptionInt([]string{"--foo", "42"}, "foo")
if !argsEqual(args, []string{}) {
t.Errorf("Should have consumed the option and its value.")
}
if val != 42 {
t.Errorf("Should have returned the parsed value.")
}
if !ok {
t.Errorf("Should have found the option.")
}
if err != nil {
t.Errorf("Should not have returned an error.")
}
args, val, ok, err = OptionInt([]string{"--foo", "-98765"}, "foo")
if !argsEqual(args, []string{}) {
t.Errorf("Should have consumed the option and its value.")
}
if val != -98765 {
t.Errorf("Should have returned the parsed value.")
}
if !ok {
t.Errorf("Should have found the option.")
}
if err != nil {
t.Errorf("Should not have returned an error.")
}
args, val, ok, err = OptionInt([]string{"--foo", "what"}, "foo")
if !argsEqual(args, []string{"--foo", "what"}) {
t.Errorf("Should not have consumed anything.")
}
if !ok {
t.Errorf("Should have found the option.")
}
if err == nil {
t.Errorf("Should have returned an error.")
}
}