hq-tools 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/spec/hq/tools/getopt-spec.rb +341 -0
  2. metadata +4 -2
@@ -0,0 +1,341 @@
1
+ require "tempfile"
2
+
3
+ require "hq/tools/getopt"
4
+
5
+ module HQ
6
+ module Tools
7
+ describe Getopt do
8
+
9
+ def go expect, args, spec
10
+
11
+ ret, remain =
12
+ Getopt.process args, spec
13
+
14
+ ret.should == expect
15
+
16
+ end
17
+
18
+ def go_error args, spec, message
19
+
20
+ Tempfile.open "getopt-test" do |tmp|
21
+
22
+ $stderr.reopen tmp.path, "w"
23
+
24
+ expect {
25
+ Getopt.process args, spec
26
+ }.to raise_error GetoptError
27
+
28
+ $stderr.flush
29
+
30
+ File.read(tmp.path).chomp.should == "#{$0}: #{message}"
31
+
32
+ end
33
+
34
+ end
35
+
36
+ # ---------------------------------------- required
37
+
38
+ context "with a required option" do
39
+
40
+ before do
41
+ @spec = [ { name: :arg_0, required: true } ]
42
+ end
43
+
44
+ it "accepts a value" do
45
+ args = [ "--arg-0", "arg_0_value" ]
46
+ expect = { :arg_0 => "arg_0_value" }
47
+ go expect, args, @spec
48
+ end
49
+
50
+ it "errors if the option is not provided" do
51
+ args = [ ]
52
+ go_error args, @spec, "option '--arg-0' is required"
53
+ end
54
+
55
+ it "errors if the option has no value" do
56
+ args = [ "--arg-0" ]
57
+ go_error args, @spec, "option `--arg-0' requires an argument"
58
+ end
59
+
60
+ end
61
+
62
+ # ---------------------------------------- required with regex
63
+
64
+ context "with a required regex" do
65
+
66
+ before do
67
+ @spec = [ { name: :arg_0, required: true, regex: /[abc]{3}/ } ]
68
+ end
69
+
70
+ it "accepts a matching value" do
71
+ args = [ "--arg-0", "abc" ]
72
+ expect = { :arg_0 => "abc" }
73
+ go expect, args, @spec
74
+ end
75
+
76
+ it "errors if the option is not provided" do
77
+ args = [ ]
78
+ go_error args, @spec, "option '--arg-0' is required"
79
+ end
80
+
81
+ it "errors if the option has no value" do
82
+ args = [ "--arg-0" ]
83
+ go_error args, @spec, "option `--arg-0' requires an argument"
84
+ end
85
+
86
+ it "errors if the value does not match" do
87
+ args = [ "--arg-0", "abcd" ]
88
+ go_error args, @spec, "option '--arg-0' is invalid: abcd"
89
+ end
90
+
91
+ end
92
+
93
+ # ---------------------------------------- conversions
94
+
95
+ context "with a required integer conversion" do
96
+
97
+ before do
98
+ @spec = [
99
+ { name: :arg_0,
100
+ required: true,
101
+ regex: /[0-9]+/,
102
+ convert: :to_i }
103
+ ]
104
+ end
105
+
106
+ it "accepts a valid value" do
107
+ args = [ "--arg-0", "123" ]
108
+ expect = { :arg_0 => 123 }
109
+ go expect, args, @spec
110
+ end
111
+
112
+ end
113
+
114
+ context "with an optional integer conversion" do
115
+
116
+ before do
117
+ @spec = [ {
118
+ name: :arg_0,
119
+ regex: /[0-9]+/,
120
+ convert: :to_i,
121
+ } ]
122
+ end
123
+
124
+ it "accepts a valid value" do
125
+ args = [ "--arg-0", "123" ]
126
+ expect = { :arg_0 => 123 }
127
+ go expect, args, @spec
128
+ end
129
+
130
+ it "uses nil if the option is not provided" do
131
+ args = [ ]
132
+ expect = { :arg_0 => nil }
133
+ go expect, args, @spec
134
+ end
135
+
136
+ end
137
+
138
+ context "with an optional integer conversion with default" do
139
+
140
+ before do
141
+ @spec = [ {
142
+ name: :arg_0,
143
+ default: 10,
144
+ regex: /[0-9]+/,
145
+ convert: :to_i,
146
+ } ]
147
+ end
148
+
149
+ it "accepts a valid value" do
150
+ args = [ "--arg-0", "123" ]
151
+ expect = { :arg_0 => 123 }
152
+ go expect, args, @spec
153
+ end
154
+
155
+ it "uses the default if the option is not provided" do
156
+ args = [ ]
157
+ expect = { :arg_0 => 10 }
158
+ go expect, args, @spec
159
+ end
160
+
161
+ end
162
+
163
+ # ---------------------------------------- optional
164
+
165
+ context "optional without default" do
166
+
167
+ before do
168
+ @spec = [ {
169
+ name: :arg_0,
170
+ } ]
171
+ end
172
+
173
+ it "accepts if the option is provided" do
174
+ args = [ "--arg-0", "arg_0_value" ]
175
+ expect = { :arg_0 => "arg_0_value" }
176
+ go expect, args, @spec
177
+ end
178
+
179
+ it "uses nil if the option is not provided" do
180
+ args = [ ]
181
+ expect = { :arg_0 => nil }
182
+ go expect, args, @spec
183
+ end
184
+
185
+ end
186
+
187
+ context "optional with default" do
188
+
189
+ before do
190
+ @spec = [ {
191
+ name: :arg_0,
192
+ default: "default_0",
193
+ } ]
194
+ end
195
+
196
+ it "accepts if the option is provided" do
197
+ args = [ "--arg-0", "value_0" ]
198
+ expect = { :arg_0 => "value_0" }
199
+ go expect, args, @spec
200
+ end
201
+
202
+ it "uses the default if the option is not provided" do
203
+ args = [ ]
204
+ expect = { :arg_0 => "default_0" }
205
+ go expect, args, @spec
206
+ end
207
+
208
+ end
209
+
210
+ # ---------------------------------------- multi
211
+
212
+ context "multi optional" do
213
+
214
+ before do
215
+ @spec = [ {
216
+ name: :arg0,
217
+ multi: true,
218
+ } ]
219
+ end
220
+
221
+ it "accepts if the option is not provided" do
222
+ args = %W[ ]
223
+ expect = { :arg0 => [ ] }
224
+ go expect, args, @spec
225
+ end
226
+
227
+ it "accepts if the option is provided once" do
228
+ args = %W[ --arg0 arg0-value0 ]
229
+ expect = { :arg0 => [ "arg0-value0" ] }
230
+ go expect, args, @spec
231
+ end
232
+
233
+ it "accepts if the option is provided multiple times" do
234
+ args = %W[ --arg0 arg0-value0 --arg0 arg0-value1 ]
235
+ expect = { :arg0 => [ "arg0-value0", "arg0-value1" ] }
236
+ go expect, args, @spec
237
+ end
238
+
239
+ end
240
+
241
+ context "multi required" do
242
+
243
+ before do
244
+ @spec = [ {
245
+ name: :arg0,
246
+ multi: true,
247
+ required: true,
248
+ } ]
249
+ end
250
+
251
+ it "errors if the option is not provided" do
252
+ args = %W[ ]
253
+ expect = { :arg0 => [ "arg0-value0", "arg0-value1" ] }
254
+ go_error args, @spec, "option '--arg0' is required"
255
+ end
256
+
257
+ it "accepts if the option is provided" do
258
+ args = %W[ --arg0 arg0-value0 --arg0 arg0-value1 ]
259
+ expect = { :arg0 => [ "arg0-value0", "arg0-value1" ] }
260
+ go expect, args, @spec
261
+ end
262
+
263
+ end
264
+
265
+ # ---------------------------------------- boolean
266
+
267
+ context "boolean" do
268
+
269
+ before do
270
+ @spec = [ {
271
+ name: :arg0,
272
+ boolean: true,
273
+ } ]
274
+ end
275
+
276
+ it "uses false if the option is not provided" do
277
+ args = %W[ ]
278
+ expect = { :arg0 => false }
279
+ go expect, args, @spec
280
+ end
281
+
282
+ it "uses true if the option is provided" do
283
+ args = %W[ --arg0 ]
284
+ expect = { :arg0 => true }
285
+ go expect, args, @spec
286
+ end
287
+
288
+ end
289
+
290
+ # ---------------------------------------- switch
291
+
292
+ context "switch without default" do
293
+
294
+ before do
295
+ @spec = [ {
296
+ name: :arg0,
297
+ options: [ :opt0, :opt1 ],
298
+ } ]
299
+ end
300
+
301
+ it "accepts if the option is not provided" do
302
+ args = %W[ ]
303
+ expect = { :arg0 => nil }
304
+ go expect, args, @spec
305
+ end
306
+
307
+ it "accepts if the option is provided" do
308
+ args = %W[ --opt1 ]
309
+ expect = { :arg0 => :opt1 }
310
+ go expect, args, @spec
311
+ end
312
+
313
+ end
314
+
315
+ context "switch with default" do
316
+
317
+ before do
318
+ @spec = [ {
319
+ name: :arg0,
320
+ default: :opt0,
321
+ options: [ :opt1 ],
322
+ } ]
323
+ end
324
+
325
+ it "uses the default if the option is not provided" do
326
+ args = %W[ ]
327
+ expect = { :arg0 => :opt0 }
328
+ go expect, args, @spec
329
+ end
330
+
331
+ it "accepts if the option is provided" do
332
+ args = %W[ --opt1 ]
333
+ expect = { :arg0 => :opt1 }
334
+ go expect, args, @spec
335
+ end
336
+
337
+ end
338
+
339
+ end
340
+ end
341
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -99,6 +99,7 @@ extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
101
  - lib/hq/tools/getopt.rb
102
+ - spec/hq/tools/getopt-spec.rb
102
103
  homepage: https://github.com/jamespharaoh/hq-tools
103
104
  licenses: []
104
105
  post_install_message:
@@ -123,4 +124,5 @@ rubygems_version: 1.8.23
123
124
  signing_key:
124
125
  specification_version: 3
125
126
  summary: HQ tools
126
- test_files: []
127
+ test_files:
128
+ - spec/hq/tools/getopt-spec.rb