diff options
author | David Schmitt <david.schmitt@puppetlabs.com> | 2015-07-22 11:57:16 +0100 |
---|---|---|
committer | David Schmitt <david.schmitt@puppetlabs.com> | 2015-07-22 11:57:16 +0100 |
commit | dca8bdba59990cb8a3451cd87d0244c92ca94901 (patch) | |
tree | 20b289ee8fce9b39285cbd94bc890b980a14dc94 | |
parent | dde8aa095af5c7caa28c8dd0aa9ea31565482317 (diff) | |
parent | 5c79107863a42a9d347637146f0c0f728f9b92ad (diff) | |
download | puppet-stdlib-dca8bdba59990cb8a3451cd87d0244c92ca94901.tar.gz puppet-stdlib-dca8bdba59990cb8a3451cd87d0244c92ca94901.tar.bz2 |
Merge pull request #489 from gcmalloc/master
adding support for hash in the size function
-rw-r--r-- | README.markdown | 2 | ||||
-rw-r--r-- | lib/puppet/parser/functions/size.rb | 8 | ||||
-rwxr-xr-x | spec/functions/size_spec.rb | 9 |
3 files changed, 10 insertions, 9 deletions
diff --git a/README.markdown b/README.markdown index 7eed5d7..eef538a 100644 --- a/README.markdown +++ b/README.markdown @@ -578,7 +578,7 @@ Randomizes the order of a string or array elements. *Type*: rvalue. #### `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` diff --git a/lib/puppet/parser/functions/size.rb b/lib/puppet/parser/functions/size.rb index cc207e3..0d6cc96 100644 --- a/lib/puppet/parser/functions/size.rb +++ b/lib/puppet/parser/functions/size.rb @@ -2,11 +2,9 @@ # size.rb # -# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... - module Puppet::Parser::Functions 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 ) do |arguments| @@ -29,13 +27,13 @@ Returns the number of elements in a string or array. Float(item) raise(Puppet::ParseError, 'size(): Requires either ' + - 'string or array to work with') + 'string, array or hash to work with') rescue ArgumentError result = item.size end - elsif item.is_a?(Array) + elsif item.is_a?(Array) || item.is_a?(Hash) result = item.size else raise(Puppet::ParseError, 'size(): Unknown type given') diff --git a/spec/functions/size_spec.rb b/spec/functions/size_spec.rb index 6b64866..c0047ee 100755 --- a/spec/functions/size_spec.rb +++ b/spec/functions/size_spec.rb @@ -8,15 +8,18 @@ describe 'size' do 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({}).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.0').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, array or hash to work/) } 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(['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({}).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('a').and_return(1) } it { is_expected.to run.with_params('abc').and_return(3) } |