]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
adding support for hash in the size function
authorgcmalloc <gcmalloc@gmail.com>
Tue, 21 Jul 2015 17:25:27 +0000 (19:25 +0200)
committergcmalloc <gcmalloc@gmail.com>
Wed, 22 Jul 2015 10:11:52 +0000 (12:11 +0200)
README.markdown
lib/puppet/parser/functions/size.rb
spec/functions/size_spec.rb

index 7eed5d76541294b5b17c4b67363fcc8c594f5d94..eef538af978c6d3d2c8b759def24c97b6b37a169 100644 (file)
@@ -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`
 
index cc207e3fadc40fc10477eb351fcfd1d4ac775704..0d6cc9613fba56f546abfe649ad7723569c47a50 100644 (file)
@@ -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')
index 6b64866830b8ffd174565b39f1d90f7c0433bf73..c0047ee21b1b5f9e8c6844c089b3a5055e2f8496 100755 (executable)
@@ -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) }