Skip to content

(19864) num2bool match fix #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pkg/
.DS_Store
metadata.json
coverage/
spec/fixtures/
Gemfile.lock
.bundle/
vendor/bundle/
43 changes: 23 additions & 20 deletions lib/puppet/parser/functions/num2bool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@
# num2bool.rb
#

# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ...

module Puppet::Parser::Functions
newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
This function converts a number into a true boolean. Zero becomes false. Numbers
higher then 0 become true.
This function converts a number or a string representation of a number into a
true boolean. Zero or anything non-numeric becomes false. Numbers higher then 0
become true.
EOS
) do |arguments|

raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1
"given (#{arguments.size} for 1)") if arguments.size != 1

number = arguments[0]

# Only numbers allowed ...
unless number.match(/^\-?\d+$/)
raise(Puppet::ParseError, 'num2bool(): Requires integer to work with')
case number
when Numeric
# Yay, it's a number
when String
begin
number = Float(number)
rescue ArgumentError => ex
raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}")
end
else
begin
number = number.to_s
rescue NoMethodError => ex
raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}")
end
end

result = case number
when /^0$/
false
when /^\-?\d+$/
# Numbers in Puppet are often string-encoded which is troublesome ...
number = number.to_i
# We yield true for any positive number and false otherwise ...
number > 0 ? true : false
else
raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given')
end
# Truncate Floats
number = number.to_i

return result
# Return true for any positive number and false otherwise
return number > 0
end
end

Expand Down
49 changes: 46 additions & 3 deletions spec/unit/puppet/parser/functions/num2bool_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,60 @@
Puppet::Parser::Functions.function("num2bool").should == "function_num2bool"
end

it "should raise a ParseError if there is less than 1 arguments" do
it "should raise a ParseError if there are no arguments" do
lambda { scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError))
end

it "should return true if 1" do
it "should raise a ParseError if there are more than 1 arguments" do
lambda { scope.function_num2bool(["foo","bar"]) }.should( raise_error(Puppet::ParseError))
end

it "should raise a ParseError if passed something non-numeric" do
lambda { scope.function_num2bool(["xyzzy"]) }.should( raise_error(Puppet::ParseError))
end

it "should return true if passed string 1" do
result = scope.function_num2bool(["1"])
result.should(be_true)
end

it "should return false if 0" do
it "should return true if passed string 1.5" do
result = scope.function_num2bool(["1.5"])
result.should(be_true)
end

it "should return true if passed number 1" do
result = scope.function_num2bool([1])
result.should(be_true)
end

it "should return false if passed string 0" do
result = scope.function_num2bool(["0"])
result.should(be_false)
end

it "should return false if passed number 0" do
result = scope.function_num2bool([0])
result.should(be_false)
end

it "should return false if passed string -1" do
result = scope.function_num2bool(["-1"])
result.should(be_false)
end

it "should return false if passed string -1.5" do
result = scope.function_num2bool(["-1.5"])
result.should(be_false)
end

it "should return false if passed number -1" do
result = scope.function_num2bool([-1])
result.should(be_false)
end

it "should return false if passed float -1.5" do
result = scope.function_num2bool([-1.5])
result.should(be_false)
end
end