Skip to content

Commit

Permalink
adding support for hash in the size function
Browse files Browse the repository at this point in the history
  • Loading branch information
gcmalloc committed Jul 22, 2015
1 parent dde8aa0 commit bf42aa2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
6 changes: 4 additions & 2 deletions lib/puppet/parser/functions/size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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|

Expand All @@ -29,14 +29,16 @@ module Puppet::Parser::Functions
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)
result = item.size
elsif item.is_a?(Hash)
result = item.size
else
raise(Puppet::ParseError, 'size(): Unknown type given')
end
Expand Down
9 changes: 6 additions & 3 deletions spec/functions/size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
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) }
Expand Down

0 comments on commit bf42aa2

Please sign in to comment.