]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
(19864) num2bool match fix
authorSteve Huff <shuff@vecna.org>
Fri, 29 Mar 2013 19:06:36 +0000 (15:06 -0400)
committerSteve Huff <shuff@vecna.org>
Fri, 29 Mar 2013 19:06:36 +0000 (15:06 -0400)
This is a bit more heavy-handed than I might like, but it does appear to
do the right things:

* accepts numeric input appropriately, truncating floats
* matches string input against a regex, then coerces number-looking
  strings to int
* makes a best effort to coerce anything else to a string, then subjects
  it to the same treatment
* raises an error in the event of incorrect number of arguments or
  non-number-looking strings

I've also included some additional unit tests.

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

index 15dd5601e51f0c4649386d8d06879c832ef1b5ea..cf98f805c14c9ba600e144ee389a9a78ecabee2a 100644 (file)
@@ -13,13 +13,32 @@ become true.
     raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " +
       "given (#{arguments.size} for 1)") if arguments.size != 1
 
-    # 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: ' + $!)
+    number = arguments[0]
+
+    case number
+    when Numeric
+      # Yay, it's a number
+    when String
+      # Deal with strings later
+    else
+      begin
+        number = number.to_s
+      rescue NoMethodError
+        raise(Puppet::ParseError, 'num2bool(): Unable to parse argument: ' + $!)
+      end
+    end
+
+    case number
+    when String
+      # Only accept strings that look somewhat like numbers
+      unless number =~ /^-?\d+/
+        raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number")
+      end
     end
 
+    # Truncate floats
+    number = number.to_i
+
     # Return true for any positive number and false otherwise
     return number > 0 ? true : false
   end
index 5ce981e2ce98e4f2d22584a5a2f375fba3cb8034..038881f041a9b0daba2c86c5f59b90ff17206b90 100644 (file)
@@ -16,6 +16,10 @@ describe "the num2bool function" 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)
@@ -26,6 +30,16 @@ describe "the num2bool function" do
     result.should(be_true)
   end
 
+  it "should return true if passed array with string 1" do
+    result = scope.function_num2bool([["1"]])
+    result.should(be_true)
+  end
+
+  it "should return true if passed array with 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)
@@ -36,6 +50,16 @@ describe "the num2bool function" do
     result.should(be_false)
   end
 
+  it "should return false if passed array with string 0" do
+    result = scope.function_num2bool([["0"]])
+    result.should(be_false)
+  end
+
+  it "should return false if passed array with 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)
@@ -46,8 +70,14 @@ describe "the num2bool function" do
     result.should(be_false)
   end
 
-  it "should return false if passed something non-numeric" do
-    result = scope.function_num2bool(["xyzzy"])
+  it "should return false if passed array with string -1" do
+    result = scope.function_num2bool([["-1"]])
     result.should(be_false)
   end
+
+  it "should return false if passed array with number -1" do
+    result = scope.function_num2bool([[-1]])
+    result.should(be_false)
+  end
+
 end