Skip to content

Commit dca8bdb

Browse files
committed
Merge pull request #489 from gcmalloc/master
adding support for hash in the size function
2 parents dde8aa0 + 5c79107 commit dca8bdb

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
# size.rb
33
#
44

5-
# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ...
6-
75
module Puppet::Parser::Functions
86
newfunction(:size, :type => :rvalue, :doc => <<-EOS
9-
Returns the number of elements in a string or array.
7+
Returns the number of elements in a string, an array or a hash
108
EOS
119
) do |arguments|
1210

@@ -29,13 +27,13 @@ module Puppet::Parser::Functions
2927
Float(item)
3028

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

3432
rescue ArgumentError
3533
result = item.size
3634
end
3735

38-
elsif item.is_a?(Array)
36+
elsif item.is_a?(Array) || item.is_a?(Hash)
3937
result = item.size
4038
else
4139
raise(Puppet::ParseError, 'size(): Unknown type given')

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)