From e80207bede37e498c32bea6d56c46f1dd709721e Mon Sep 17 00:00:00 2001 From: Uwe Stuehler Date: Tue, 23 Oct 2012 16:43:03 +0200 Subject: Fix number of arguments check in flatten() The function only uses the first argument, so raise an error with too few arguments *and* with too many arguments. --- lib/puppet/parser/functions/flatten.rb | 2 +- spec/unit/puppet/parser/functions/flatten_spec.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb index 781da78..a1ed183 100644 --- a/lib/puppet/parser/functions/flatten.rb +++ b/lib/puppet/parser/functions/flatten.rb @@ -16,7 +16,7 @@ Would return: ['a','b','c'] ) do |arguments| raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + "given (#{arguments.size} for 1)") if arguments.size != 1 array = arguments[0] diff --git a/spec/unit/puppet/parser/functions/flatten_spec.rb b/spec/unit/puppet/parser/functions/flatten_spec.rb index d4dfd20..dba7a6b 100755 --- a/spec/unit/puppet/parser/functions/flatten_spec.rb +++ b/spec/unit/puppet/parser/functions/flatten_spec.rb @@ -11,6 +11,10 @@ describe "the flatten function" do lambda { scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) end + it "should raise a ParseError if there is more than 1 argument" do + lambda { scope.function_flatten([[], []]) }.should( raise_error(Puppet::ParseError)) + end + it "should flatten a complex data structure" do result = scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) result.should(eq(["a","b","c","d","e","f","g"])) -- cgit v1.2.3 From 5d5a4d466ed6530fa8b68162e96bdaaaf35066e1 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Mon, 17 Dec 2012 06:22:36 -0700 Subject: str2bool should return a boolean if called with a boolean --- lib/puppet/parser/functions/str2bool.rb | 5 +++++ spec/unit/puppet/parser/functions/str2bool_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index c320da6..9ea6dd5 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -14,6 +14,11 @@ like: 0, f, n, false, no to 'false'. "given (#{arguments.size} for 1)") if arguments.size < 1 string = arguments[0] + + # If string is already Boolean, return it + if !!string == string + return string + end unless string.is_a?(String) raise(Puppet::ParseError, 'str2bool(): Requires either ' + diff --git a/spec/unit/puppet/parser/functions/str2bool_spec.rb b/spec/unit/puppet/parser/functions/str2bool_spec.rb index 2782bbe..ef6350f 100644 --- a/spec/unit/puppet/parser/functions/str2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/str2bool_spec.rb @@ -21,4 +21,11 @@ describe "the str2bool function" do result = scope.function_str2bool(["undef"]) result.should(eq(false)) end + + it "should return the boolean it was called with" do + result = scope.function_str2bool([true]) + result.should(eq(true)) + result = scope.function_str2bool([false]) + result.should(eq(false)) + end end -- cgit v1.2.3