adding support for hash in the size function

This commit is contained in:
gcmalloc 2015-07-21 19:25:27 +02:00
parent dde8aa095a
commit 5c79107863
3 changed files with 10 additions and 9 deletions

View file

@ -578,7 +578,7 @@ Randomizes the order of a string or array elements. *Type*: rvalue.
#### `size` #### `size`
Returns the number of elements in a string or an array. *Type*: rvalue. Returns the number of elements in a string, an array or a hash. *Type*: rvalue.
#### `sort` #### `sort`

View file

@ -2,11 +2,9 @@
# size.rb # size.rb
# #
# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ...
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:size, :type => :rvalue, :doc => <<-EOS newfunction(:size, :type => :rvalue, :doc => <<-EOS
Returns the number of elements in a string or array. Returns the number of elements in a string, an array or a hash
EOS EOS
) do |arguments| ) do |arguments|
@ -29,13 +27,13 @@ Returns the number of elements in a string or array.
Float(item) Float(item)
raise(Puppet::ParseError, 'size(): Requires either ' + raise(Puppet::ParseError, 'size(): Requires either ' +
'string or array to work with') 'string, array or hash to work with')
rescue ArgumentError rescue ArgumentError
result = item.size result = item.size
end end
elsif item.is_a?(Array) elsif item.is_a?(Array) || item.is_a?(Hash)
result = item.size result = item.size
else else
raise(Puppet::ParseError, 'size(): Unknown type given') raise(Puppet::ParseError, 'size(): Unknown type given')

View file

@ -8,15 +8,18 @@ describe 'size' do
is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
} }
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Unknown type given/) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Unknown type given/) }
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Unknown type given/) }
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Unknown type given/) } it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Unknown type given/) }
it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, /Requires either string or array to work/) } it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, /Requires either string, array or hash to work/) }
it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, /Requires either string or array to work/) } it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, /Requires either string, array or hash to work/) }
it { is_expected.to run.with_params([]).and_return(0) } it { is_expected.to run.with_params([]).and_return(0) }
it { is_expected.to run.with_params(['a']).and_return(1) } it { is_expected.to run.with_params(['a']).and_return(1) }
it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(3) } it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(3) }
it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(4) } it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(4) }
it { is_expected.to run.with_params({}).and_return(0) }
it { is_expected.to run.with_params({'1' => '2'}).and_return(1) }
it { is_expected.to run.with_params({'1' => '2', '4' => '4'}).and_return(2) }
it { is_expected.to run.with_params('').and_return(0) } it { is_expected.to run.with_params('').and_return(0) }
it { is_expected.to run.with_params('a').and_return(1) } it { is_expected.to run.with_params('a').and_return(1) }
it { is_expected.to run.with_params('abc').and_return(3) } it { is_expected.to run.with_params('abc').and_return(3) }