Skip to content

Commit

Permalink
Merge pull request #533 from HelenCampbell/MODULES-2614-Improved
Browse files Browse the repository at this point in the history
Modules 2614 improved numeric value handling on empty function
  • Loading branch information
DavidS committed Sep 28, 2015
2 parents 48b658f + c7c4d41 commit 76db981
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Converts the case of a string or of all strings in an array to lowercase. *Type*

#### `empty`

Returns true if the argument is an array or hash that contains no elements, or an empty string. *Type*: rvalue.
Returns true if the argument is an array or hash that contains no elements, or an empty string. Returns false when the argument is a numerical value. *Type*: rvalue.

#### `ensure_packages`

Expand Down
12 changes: 8 additions & 4 deletions lib/puppet/parser/functions/empty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ module Puppet::Parser::Functions

value = arguments[0]

unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String)
unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String) || value.is_a?(Numeric)
raise(Puppet::ParseError, 'empty(): Requires either ' +
'array, hash or string to work with')
'array, hash, string or integer to work with')
end

result = value.empty?
if value.is_a?(Numeric)
return false
else
result = value.empty?

return result
return result
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/acceptance/empty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
}
EOS

apply_manifest(pp, :catch_failures => true) do |r|
expect(r.stdout).to match(/Notice: output correct/)
end
end
it 'handles numerical values' do
pp = <<-EOS
$a = 7
$b = false
$o = empty($a)
if $o == $b {
notify { 'output correct': }
}
EOS

apply_manifest(pp, :catch_failures => true) do |r|
expect(r.stdout).to match(/Notice: output correct/)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/functions/empty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
describe 'empty' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError) }
it {
pending("Current implementation ignores parameters after the first.")
is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError)
}
it { is_expected.to run.with_params(0).and_return(false) }
it { is_expected.to run.with_params('').and_return(true) }
it { is_expected.to run.with_params('one').and_return(false) }

Expand Down

0 comments on commit 76db981

Please sign in to comment.