-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoption_stringifier_test.go
216 lines (176 loc) · 10 KB
/
option_stringifier_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
210
211
212
213
214
215
216
// Copyright (c) 2011, SoundCloud Ltd., Daniel Bornkessel
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/kesselborn/go-getopt
package getopt
import (
"testing"
)
func TestUsageOutput(t *testing.T) {
if got, expected := (Option{"method", "...", IsArg | Optional, ""}.Usage()),
"[<method>]"; got != expected {
t.Errorf("Error creating usage text (argument):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "...", IsArg, ""}.Usage()),
"<method>"; got != expected {
t.Errorf("Error creating usage text (argument):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "...", Required, ""}.Usage()),
"-m <method>"; got != expected {
t.Errorf("Error creating usage text (Required):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "...", Required, ""}.Usage()),
"--method=<method>"; got != expected {
t.Errorf("Error creating usage text (Required, no short):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "...", Optional, ""}.Usage()),
"[-m <method>]"; got != expected {
t.Errorf("Error creating usage text (Optional):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "...", Optional, ""}.Usage()),
"[--method=<method>]"; got != expected {
t.Errorf("Error creating usage text (Optional, no short):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "...", ExampleIsDefault, "defaultValue"}.Usage()),
"[-m <method>]"; got != expected {
t.Errorf("Error creating usage text (Optional):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "...", ExampleIsDefault, "defaultValue"}.Usage()),
"[--method=<method>]"; got != expected {
t.Errorf("Error creating usage text (Optional, no short):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "...", Flag | Optional, ""}.Usage()),
"[-m]"; got != expected {
t.Errorf("Error creating usage text (optional flag):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "...", Flag | Optional | NoLongOpt, ""}.Usage()),
"[-m]"; got != expected {
t.Errorf("Error creating usage text (optional flag):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "...", Flag | Optional, ""}.Usage()),
"[--method]"; got != expected {
t.Errorf("Error creating usage text (optional flag, no short):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "...", Flag, ""}.Usage()),
"-m"; got != expected {
t.Errorf("Error creating usage text (flag):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "...", Flag | NoLongOpt, ""}.Usage()),
"-m"; got != expected {
t.Errorf("Error creating usage text (flag):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "...", Flag, ""}.Usage()),
"--method"; got != expected {
t.Errorf("Error creating usage text (flag, no short):\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"configfile|c", "method", IsConfigFile | ExampleIsDefault, "/etc/cnf.conf"}.Usage()),
"[-c <configfile>]"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
}
func TestBasicOutput(t *testing.T) {
if got, expected := (Option{"method|m", "method: one of either 'heartbeat' or 'nagios'", Required | NoLongOpt, ""}.HelpText(20)),
" -m <method> method: one of either 'heartbeat' or 'nagios'"; got != expected {
t.Errorf("Error stringifying option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "method: one of either 'heartbeat' or 'nagios'", Required, ""}.HelpText(20)),
" --method=<method> method: one of either 'heartbeat' or 'nagios'"; got != expected {
t.Errorf("Error stringifying option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "method: one of either 'heartbeat' or 'nagios'", Required, ""}.HelpText(20)),
" -m, --method=<method> method: one of either 'heartbeat' or 'nagios'"; got != expected {
t.Errorf("Error stringifying option:\ngot: " + got + "\nexpected: " + expected)
}
}
func TestBasicOutputForFlags(t *testing.T) {
if got, expected := (Option{"method|m", "...", Flag, ""}.HelpText(20)),
" -m, --method ..."; got != expected {
t.Errorf("Error stringifying option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "...", Flag | NoLongOpt, ""}.HelpText(20)),
" -m ..."; got != expected {
t.Errorf("Error stringifying option:\ngot: " + got + "\nexpected: " + expected)
}
}
func TestBasicOutputWithExampleHelpTextValue(t *testing.T) {
if got, expected := (Option{"method|m", "method", Optional | NoLongOpt, "heartbeat"}.HelpText(20)),
" -m <method> method (e.g. heartbeat)"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "method", Optional, "heartbeat"}.HelpText(20)),
" --method=<method> method (e.g. heartbeat)"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "method", Optional, "heartbeat"}.HelpText(20)),
" -m, --method=<method> method (e.g. heartbeat)"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "method", Required | NoLongOpt, "heartbeat"}.HelpText(20)),
" -m <method> method (e.g. heartbeat)"; got != expected {
t.Errorf("Error stringifying required option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "method", Required, "heartbeat"}.HelpText(20)),
" --method=<method> method (e.g. heartbeat)"; got != expected {
t.Errorf("Error stringifying required option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "method", Required, "heartbeat"}.HelpText(20)),
" -m, --method=<method> method (e.g. heartbeat)"; got != expected {
t.Errorf("Error stringifying required option:\ngot: " + got + "\nexpected: " + expected)
}
}
func TestBasicOutputWithDefaultHelpText(t *testing.T) {
if got, expected := (Option{"method|m", "method", Optional | ExampleIsDefault | NoLongOpt, "heartbeat"}.HelpText(20)),
" -m <method> method (default: heartbeat)"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"configfile|c", "method", IsConfigFile | ExampleIsDefault, "/etc/cnf.conf"}.HelpText(20)),
" -c, --configfile=<configfile> method (default: /etc/cnf.conf)"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method", "method", Optional | ExampleIsDefault, "heartbeat"}.HelpText(20)),
" --method=<method> method (default: heartbeat)"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m", "method", Optional | ExampleIsDefault, "heartbeat"}.HelpText(20)),
" -m, --method=<method> method (default: heartbeat)"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
}
func TestOutputWithEnvVar(t *testing.T) {
if got, expected := (Option{"method|m|METHOD", "method", Optional | ExampleIsDefault | NoLongOpt, "heartbeat"}.HelpText(20)),
" -m <method> method (default: heartbeat); setable via $METHOD"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"method|m|METHOD", "method", Optional | ExampleIsDefault | NoEnvHelp, "heartbeat"}.HelpText(20)),
" -m, --method=<method> method (default: heartbeat)"; got != expected {
t.Errorf("Error stringifying optional option:\ngot: " + got + "\nexpected: " + expected)
}
}
func TestOutputArgument(t *testing.T) {
if got, expected := (Option{"logfile", "", IsArg, ""}.HelpText(20)),
""; got != expected {
t.Errorf("Error stringifying argument:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"logfile", "dump log into this file", IsArg, ""}.HelpText(20)),
" <logfile> dump log into this file"; got != expected {
t.Errorf("Error stringifying argument:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"logfile", "dump log into this file", IsArg, "/tmp/foo"}.HelpText(20)),
" <logfile> dump log into this file (e.g. /tmp/foo)"; got != expected {
t.Errorf("Error stringifying argument:\ngot: " + got + "\nexpected: " + expected)
}
}
func TestOutputPassThrough(t *testing.T) {
if got, expected := (Option{"command", "pass through arguments", IsPassThrough | Optional, ""}.Usage()),
"[<command>]"; got != expected {
t.Errorf("Error stringifying argument:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"command", "pass through arguments", IsPassThrough, ""}.Usage()),
"<command>"; got != expected {
t.Errorf("Error stringifying argument:\ngot: " + got + "\nexpected: " + expected)
}
if got, expected := (Option{"command", "pass through arguments", IsPassThrough, ""}.HelpText(20)),
" <command> pass through arguments"; got != expected {
t.Errorf("Error stringifying argument:\ngot: " + got + "\nexpected: " + expected)
}
}