Skip to content

Commit 66865f2

Browse files
committed
Merge branch 'maint-refactor_validate_slength'
2 parents 32dbac0 + 24911db commit 66865f2

File tree

2 files changed

+74
-63
lines changed

2 files changed

+74
-63
lines changed

lib/puppet/parser/functions/validate_slength.rb

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,51 @@ module Puppet::Parser::Functions
2121
2222
ENDHEREDOC
2323

24-
raise Puppet::ParseError, ("validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)") unless args.length == 2 or args.length == 3
24+
raise Puppet::ParseError, "validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)" unless args.length == 2 or args.length == 3
2525

26-
unless (args[0].is_a?(String) or args[0].is_a?(Array))
27-
raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}")
28-
end
26+
input, max_length, min_length = *args
2927

3028
begin
31-
max_length = args[1].to_i
32-
rescue NoMethodError => e
33-
raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the max length parameter to an integer - sorry: " + e.message )
29+
max_length = Integer(max_length)
30+
raise ArgumentError if max_length <= 0
31+
rescue ArgumentError, TypeError
32+
raise Puppet::ParseError, "validate_slength(): Expected second argument to be a positive Numeric, got #{max_length}:#{max_length.class}"
3433
end
3534

36-
unless args.length == 2
35+
if min_length
3736
begin
38-
min_length = Integer(args[2])
39-
rescue StandardError => e
40-
raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the min length parameter to an integer - sorry: " + e.message )
37+
min_length = Integer(min_length)
38+
raise ArgumentError if min_length < 0
39+
rescue ArgumentError, TypeError
40+
raise Puppet::ParseError, "validate_slength(): Expected third argument to be unset or a positive Numeric, got #{min_length}:#{min_length.class}"
4141
end
4242
else
4343
min_length = 0
4444
end
4545

46-
raise Puppet::ParseError, ("validate_slength(): please pass a positive number as max_length") unless max_length > 0
47-
raise Puppet::ParseError, ("validate_slength(): please pass a positive number as min_length") unless min_length >= 0
48-
raise Puppet::ParseError, ("validate_slength(): please pass a min length that is smaller than the maximum") unless min_length <= max_length
46+
if min_length > max_length
47+
raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument"
48+
end
4949

50-
case args[0]
51-
when String
52-
raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been between #{min_length} and #{max_length} characters") unless args[0].length <= max_length and min_length <= args[0].length
53-
when Array
54-
args[0].each do |arg|
55-
if arg.is_a?(String)
56-
unless ( arg.is_a?(String) and arg.length <= max_length and min_length <= arg.length)
57-
raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been between #{min_length} and #{max_length} characters")
58-
end
59-
else
60-
raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}")
61-
end
50+
validator = lambda do |str|
51+
unless str.length <= max_length and str.length >= min_length
52+
raise Puppet::ParseError, "validate_slength(): Expected length of #{input.inspect} to be between #{min_length} and #{max_length}, was #{input.length}"
53+
end
54+
end
55+
56+
case input
57+
when String
58+
validator.call(input)
59+
when Array
60+
input.each_with_index do |arg, pos|
61+
if arg.is_a? String
62+
validator.call(arg)
63+
else
64+
raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got #{arg.class}"
6265
end
63-
else
64-
raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}")
66+
end
67+
else
68+
raise Puppet::ParseError, "validate_slength(): Expected first argument to be a String or Array, got #{input.class}"
6569
end
6670
end
6771
end

spec/unit/puppet/parser/functions/validate_slength_spec.rb

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,59 @@
99
Puppet::Parser::Functions.function("validate_slength").should == "function_validate_slength"
1010
end
1111

12-
it "should raise a ParseError if there is less than 2 arguments" do
13-
expect { scope.function_validate_slength([]) }.to(raise_error(Puppet::ParseError))
14-
expect { scope.function_validate_slength(["asdf"]) }.to(raise_error(Puppet::ParseError))
15-
end
12+
describe "validating the input argument types" do
13+
it "raises an error if there are less than two arguments" do
14+
expect { scope.function_validate_slength([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
15+
end
1616

17-
it "should raise a ParseError if argument 2 doesn't convert to a fixnum" do
18-
expect { scope.function_validate_slength(["moo",["2"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/))
19-
end
17+
it "raises an error if there are more than three arguments" do
18+
expect { scope.function_validate_slength(['input', 1, 2, 3]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
19+
end
2020

21-
it "should raise a ParseError if argument 2 converted, but to 0, e.g. a string" do
22-
expect { scope.function_validate_slength(["moo","monkey"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/))
23-
end
21+
it "raises an error if the first argument is not a string" do
22+
expect { scope.function_validate_slength([Object.new, 2, 1]) }.to raise_error Puppet::ParseError, /Expected first argument.*got .*Object/
23+
end
2424

25-
it "should raise a ParseError if argument 2 converted, but to 0" do
26-
expect { scope.function_validate_slength(["moo","0"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/))
27-
end
25+
it "raises an error if the second argument cannot be cast to an Integer" do
26+
expect { scope.function_validate_slength(['input', Object.new]) }.to raise_error Puppet::ParseError, /Expected second argument.*got .*Object/
27+
end
2828

29-
it "should raise a ParseError if argument 3 doesn't convert to a fixnum" do
30-
expect { scope.function_validate_slength(["moo",2,["3"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/))
31-
end
29+
it "raises an error if the third argument cannot be cast to an Integer" do
30+
expect { scope.function_validate_slength(['input', 1, Object.new]) }.to raise_error Puppet::ParseError, /Expected third argument.*got .*Object/
31+
end
3232

33-
it "should raise a ParseError if argument 3 converted, but to 0, e.g. a string" do
34-
expect { scope.function_validate_slength(["moo",2,"monkey"]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/))
33+
it "raises an error if the second argument is smaller than the third argument" do
34+
expect { scope.function_validate_slength(['input', 1, 2]) }.to raise_error Puppet::ParseError, /Expected second argument to be larger than third argument/
35+
end
3536
end
3637

37-
it "should fail if string greater then size" do
38-
expect { scope.function_validate_slength(["test", 2]) }.to(raise_error(Puppet::ParseError, /It should have been between 0 and 2/))
39-
end
38+
describe "validating the input string length" do
39+
describe "when the input is a string" do
40+
it "fails validation if the string is larger than the max length" do
41+
expect { scope.function_validate_slength(['input', 1]) }.to raise_error Puppet::ParseError, /Expected length .* between 0 and 1, was 5/
42+
end
4043

41-
it "should fail if the min length is larger than the max length" do
42-
expect { scope.function_validate_slength(["test", 10, 15]) }.to(raise_error(Puppet::ParseError, /pass a min length that is smaller than the max/))
43-
end
44+
it "fails validation if the string is less than the min length" do
45+
expect { scope.function_validate_slength(['input', 10, 6]) }.to raise_error Puppet::ParseError, /Expected length .* between 6 and 10, was 5/
46+
end
4447

45-
it "should fail if you pass an array of something other than strings" do
46-
expect { scope.function_validate_slength([["moo",["moo"],Hash.new["moo" => 7]], 7]) }.to(raise_error(Puppet::ParseError, /is not a string, it's a/))
47-
end
48+
it "doesn't raise an error if the string is under the max length" do
49+
scope.function_validate_slength(['input', 10])
50+
end
4851

49-
it "should fail if you pass something other than a string or array" do
50-
expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError, /please pass a string, or an array of strings/))
51-
end
52+
it "doesn't raise an error if the string is equal to the max length" do
53+
scope.function_validate_slength(['input', 5])
54+
end
5255

53-
it "should not fail if string is smaller or equal to size" do
54-
expect { scope.function_validate_slength(["test", 5]) }.to_not(raise_error(Puppet::ParseError))
55-
end
56+
it "doesn't raise an error if the string is equal to the min length" do
57+
scope.function_validate_slength(['input', 10, 5])
58+
end
59+
end
5660

57-
it "should not fail if array of string is are all smaller or equal to size" do
58-
expect { scope.function_validate_slength([["moo","foo","bar"], 5]) }.to_not(raise_error(Puppet::ParseError))
61+
describe "when the input is an array" do
62+
it "fails validation if one of the array elements is not a string" do
63+
expect { scope.function_validate_slength([["a", "b", Object.new], 2]) }.to raise_error Puppet::ParseError, /Expected element at array position 2 .*String, got .*Object/
64+
end
65+
end
5966
end
6067
end

0 commit comments

Comments
 (0)