From e6916f83fd35989db4b86dfb10716c9198994389 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 10:04:05 -0400 Subject: Enable num2bool to accept numeric input Also ignore rspec fixtures directory --- spec/unit/puppet/parser/functions/num2bool_spec.rb | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'spec/unit/puppet/parser/functions') diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb index 640c689..e51ee45 100644 --- a/spec/unit/puppet/parser/functions/num2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -12,13 +12,33 @@ describe "the num2bool function" do lambda { scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError)) end - it "should return true if 1" do + 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 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 number -1" do + result = scope.function_num2bool([-1]) + result.should(be_false) + end end -- cgit v1.2.3 From 4a5218a8af8c3ffaf9ea2348a3900b19d6a95416 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 12:03:33 -0400 Subject: Reworked number-handling logic 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. --- spec/unit/puppet/parser/functions/num2bool_spec.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'spec/unit/puppet/parser/functions') diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb index e51ee45..5ce981e 100644 --- a/spec/unit/puppet/parser/functions/num2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -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 -- cgit v1.2.3 From 8d217f0012fef332642faf485ad187773a95bcc1 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 15:06:36 -0400 Subject: (19864) num2bool match fix 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. --- spec/unit/puppet/parser/functions/num2bool_spec.rb | 34 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'spec/unit/puppet/parser/functions') diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb index 5ce981e..038881f 100644 --- a/spec/unit/puppet/parser/functions/num2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -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 -- cgit v1.2.3 From c372f177708df4c844337e9901646b7b84b86cd8 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Mon, 1 Apr 2013 11:44:09 -0400 Subject: Cleanup per adrianthebo suggestions * use Float() to process string arguments * get rid of doubly nested arrays * removing needless ternary operator * improving error message handling --- spec/unit/puppet/parser/functions/num2bool_spec.rb | 36 ++++++---------------- 1 file changed, 10 insertions(+), 26 deletions(-) (limited to 'spec/unit/puppet/parser/functions') diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb index 038881f..b56196d 100644 --- a/spec/unit/puppet/parser/functions/num2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -25,18 +25,13 @@ describe "the num2bool function" do 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 true if passed array with string 1" do - result = scope.function_num2bool([["1"]]) + 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 array with number 1" do - result = scope.function_num2bool([[1]]) + it "should return true if passed number 1" do + result = scope.function_num2bool([1]) result.should(be_true) end @@ -50,34 +45,23 @@ 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) end - it "should return false if passed number -1" do - result = scope.function_num2bool([-1]) + 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 array with string -1" do - result = scope.function_num2bool([["-1"]]) + 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 array with number -1" do - result = scope.function_num2bool([[-1]]) + it "should return false if passed float -1.5" do + result = scope.function_num2bool([-1.5]) result.should(be_false) end - end -- cgit v1.2.3