Skip to content

Commit 5e67ed6

Browse files
committed
Corrected Shards#validate.
1 parent adf1228 commit 5e67ed6

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

lib/masscan/command.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,13 @@ def format(value)
303303
#
304304
class Shards < CommandMapper::Types::Str
305305

306+
# Regular expression for validating `--shards` values.
307+
REGEXP = %r{\A\d+/\d+\z}
308+
306309
#
307310
# Validates a shards value.
308311
#
309-
# @param [Array, Object] value
312+
# @param [Array, Rational, String, #to_s] value
310313
# The shards value to validate.
311314
#
312315
# @return [true, (false, String)]
@@ -316,20 +319,38 @@ class Shards < CommandMapper::Types::Str
316319
def validate(value)
317320
case value
318321
when Array
319-
if value.length > 2
320-
return [false, "cannot contain more tha two elements (#{value.inspect})"]
322+
unless value.length == 2
323+
return [false, "must contain two elements (#{value.inspect})"]
324+
end
325+
326+
unless (value[0].kind_of?(Integer) && value[1].kind_of?(Integer))
327+
return [false, "shard values must be Integers (#{value.inspect})"]
321328
end
322329

330+
return true
331+
when Rational
323332
return true
324333
else
325-
super(value)
334+
valid, message = super(value)
335+
336+
unless valid
337+
return [valid, message]
338+
end
339+
340+
string = value.to_s
341+
342+
unless string =~ REGEXP
343+
return [false, "invalid shards value (#{value.inspect})"]
344+
end
345+
346+
return true
326347
end
327348
end
328349

329350
#
330351
# Formats a shards value into a String.
331352
#
332-
# @param [(#to_s, #to_s), #to_s] value
353+
# @param [(Integer, Integer), Rational, #to_s] value
333354
# The shards value to format.
334355
#
335356
# @return [String]

spec/command_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,42 @@
367367
expect(subject.validate(value)).to be(true)
368368
end
369369

370+
context "but the Array length is 1" do
371+
let(:value) { [1] }
372+
373+
it "must return a validation error" do
374+
expect(subject.validate(value)).to eq(
375+
[false, "must contain two elements (#{value.inspect})"]
376+
)
377+
end
378+
end
379+
370380
context "but the Array length is > 2" do
371381
let(:value) { [1,2,3] }
372382

373383
it "must return a validation error" do
374384
expect(subject.validate(value)).to eq(
375-
[false, "cannot contain more tha two elements (#{value.inspect})"]
385+
[false, "must contain two elements (#{value.inspect})"]
386+
)
387+
end
388+
end
389+
end
390+
391+
context "when given a String" do
392+
context "and it matches X/Y" do
393+
let(:value) { "1/2" }
394+
395+
it "must return true" do
396+
expect(subject.validate(value)).to be(true)
397+
end
398+
end
399+
400+
context "but it does not match X/Y" do
401+
let(:value) { "1" }
402+
403+
it "must return a validation error" do
404+
expect(subject.validate(value)).to eq(
405+
[false, "invalid shards value (#{value.inspect})"]
376406
)
377407
end
378408
end

0 commit comments

Comments
 (0)