]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Reworked number-handling logic
authorSteve Huff <shuff@vecna.org>
Fri, 29 Mar 2013 16:03:33 +0000 (12:03 -0400)
committerSteve Huff <shuff@vecna.org>
Fri, 29 Mar 2013 16:54:37 +0000 (12:54 -0400)
No more coercing to String and regex matching.  Instead, we now coerce
to Integer at the beginning or raise an error if we cannot coerce to
Integer.

A consequence of this change is that the function will now accept
blatantly non-numeric strings as input, and return false.  This seems a
bit goofy to me, but it's how String#to_i works.  If we really don't
like this, then I'm open to suggestions.

lib/puppet/parser/functions/num2bool.rb
spec/unit/puppet/parser/functions/num2bool_spec.rb

index 638f6931699903a25116b152c86992459d310ae4..15dd5601e51f0c4649386d8d06879c832ef1b5ea 100644 (file)
@@ -2,39 +2,26 @@
 # 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
-
-    # Since we're matching against a regex, coerce to String
-    number = arguments[0].to_s
-
-    # Only numbers allowed ...
-    unless number.match(/^\-?\d+$/)
-      raise(Puppet::ParseError, 'num2bool(): Requires integer to work with')
-    end
+      "given (#{arguments.size} for 1)") if arguments.size != 1
 
-    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')
+    # we need to get an Integer out of this
+    begin
+      number = arguments[0].to_i
+    rescue NoMethodError
+      raise(Puppet::ParseError, 'num2bool(): Unable to parse number: ' + $!)
     end
 
-    return result
+    # Return true for any positive number and false otherwise
+    return number > 0 ? true : false
   end
 end
 
index e51ee45b75976efa35725276341a44c27737097a..5ce981e2ce98e4f2d22584a5a2f375fba3cb8034 100644 (file)
@@ -8,10 +8,14 @@ describe "the num2bool function" do
     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 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 return true if passed string 1" do
     result = scope.function_num2bool(["1"])
     result.should(be_true)
@@ -41,4 +45,9 @@ describe "the num2bool function" do
     result = scope.function_num2bool([-1])
     result.should(be_false)
   end
+
+  it "should return false if passed something non-numeric" do
+    result = scope.function_num2bool(["xyzzy"])
+    result.should(be_false)
+  end
 end