]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Loosen the restrictions of upcase and allow for recursion of the objects and only...
authorTravis Fields <travis@puppetlabs.com>
Sat, 28 Feb 2015 01:40:32 +0000 (17:40 -0800)
committerTravis Fields <travis@puppetlabs.com>
Mon, 2 Mar 2015 18:45:43 +0000 (10:45 -0800)
README.markdown
lib/puppet/parser/functions/upcase.rb
spec/functions/upcase_spec.rb

index 3d2d1b9a33a855d97e54859d223e7f24e395bfe7..0ea0c4e9b037aeac1f4ed52b45cee26b4ece8b19 100644 (file)
@@ -475,7 +475,7 @@ Takes a single string value as an argument. *Type*: rvalue
 
 You can also use this with arrays. For example, `unique(["a","a","b","b","c","c"])` returns ["a","b","c"]. *Type*: rvalue
 
-* `upcase`: Converts a string or an array of strings to uppercase. For example, `upcase("abcd")` returns 'ABCD'. *Type*: rvalue
+* `upcase`: Converts an object, array or hash of objects that respond to upcase to uppercase. For example, `upcase("abcd")` returns 'ABCD'.  *Type*: rvalue
 
 * `uriescape`: Urlencodes a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue
 
index 2b05db4bad407f8d9faf0546857c857f2848078b..0226a885c8da9e6350b2397999766e226459c759 100644 (file)
@@ -17,22 +17,22 @@ Will return:
   ) do |arguments|
 
     raise(Puppet::ParseError, "upcase(): Wrong number of arguments " +
-                                "given (#{arguments.size} for 1)") if arguments.size < 1
+                                "given (#{arguments.size} for 1)") if arguments.size != 1
 
     value = arguments[0]
 
-    unless value.is_a?(Array) || value.is_a?(String) || value.is_a?(Hash)
+    unless value.is_a?(Array) || value.is_a?(Hash) || value.respond_to?(:upcase)
       raise(Puppet::ParseError, 'upcase(): Requires an ' +
-                                  'array, string or hash to work with')
+                                  'array, hash or object that responds to upcase in order to work')
     end
 
     if value.is_a?(Array)
       # Numbers in Puppet are often string-encoded which is troublesome ...
-      result = value.collect { |i| i.is_a?(String) ? i.upcase : i }
+      result = value.collect { |i| function_upcase([i]) }
     elsif value.is_a?(Hash)
       result = {}
       value.each_pair do |k, v|
-        result.merge!({k.upcase => v.collect! { |p| p.upcase }})
+        result[function_upcase([k])] = function_upcase([v])
       end
     else
       result = value.upcase
index a50a3ab3a108f5270929aa00737a228ac0f03b5e..0689099cd4d8b10f4335f02d1d393a2c54aa5363 100755 (executable)
@@ -36,4 +36,23 @@ describe "the upcase function" do
         scope.function_upcase([{'test' => %w(this that and other thing)}])
     ).to eq({'TEST' => %w(THIS THAT AND OTHER THING)})
   end
+
+  if :test.respond_to?(:upcase)
+    it 'should accept hashes of symbols' do
+      expect(
+          scope.function_upcase([{:test => [:this, :that, :other]}])
+      ).to eq({:TEST => [:THIS, :THAT, :OTHER]})
+    end
+    it 'should return upcase symbol' do
+      expect(
+          scope.function_upcase([:test])
+      ).to eq(:TEST)
+    end
+    it 'should return mixed objects in upcease' do
+      expect(
+          scope.function_upcase([[:test, 'woot']])
+      ).to eq([:TEST, 'WOOT'])
+
+    end
+  end
 end