Skip to content

Commit bf42aa2

Browse files
committed
adding support for hash in the size function
1 parent dde8aa0 commit bf42aa2

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ Randomizes the order of a string or array elements. *Type*: rvalue.
578578

579579
#### `size`
580580

581-
Returns the number of elements in a string or an array. *Type*: rvalue.
581+
Returns the number of elements in a string, an array or a hash. *Type*: rvalue.
582582

583583
#### `sort`
584584

lib/puppet/parser/functions/size.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module Puppet::Parser::Functions
88
newfunction(:size, :type => :rvalue, :doc => <<-EOS
9-
Returns the number of elements in a string or array.
9+
Returns the number of elements in a string, an array or a hash
1010
EOS
1111
) do |arguments|
1212

@@ -29,14 +29,16 @@ module Puppet::Parser::Functions
2929
Float(item)
3030

3131
raise(Puppet::ParseError, 'size(): Requires either ' +
32-
'string or array to work with')
32+
'string, array or hash to work with')
3333

3434
rescue ArgumentError
3535
result = item.size
3636
end
3737

3838
elsif item.is_a?(Array)
3939
result = item.size
40+
elsif item.is_a?(Hash)
41+
result = item.size
4042
else
4143
raise(Puppet::ParseError, 'size(): Unknown type given')
4244
end

spec/functions/size_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
99
}
1010
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Unknown type given/) }
11-
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Unknown type given/) }
1211
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Unknown type given/) }
13-
it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, /Requires either string or array to work/) }
14-
it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, /Requires either string or array to work/) }
12+
it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, /Requires either string, array or hash to work/) }
13+
it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, /Requires either string, array or hash to work/) }
1514
it { is_expected.to run.with_params([]).and_return(0) }
1615
it { is_expected.to run.with_params(['a']).and_return(1) }
1716
it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(3) }
1817
it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(4) }
1918

19+
it { is_expected.to run.with_params({}).and_return(0) }
20+
it { is_expected.to run.with_params({'1' => '2'}).and_return(1) }
21+
it { is_expected.to run.with_params({'1' => '2', '4' => '4'}).and_return(2) }
22+
2023
it { is_expected.to run.with_params('').and_return(0) }
2124
it { is_expected.to run.with_params('a').and_return(1) }
2225
it { is_expected.to run.with_params('abc').and_return(3) }

0 commit comments

Comments
 (0)