]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Add Hash to upcase
authorTravis Fields <travis@puppetlabs.com>
Wed, 25 Feb 2015 19:39:27 +0000 (11:39 -0800)
committerTravis Fields <travis@puppetlabs.com>
Wed, 25 Feb 2015 19:39:27 +0000 (11:39 -0800)
lib/puppet/parser/functions/upcase.rb
spec/functions/upcase_spec.rb

index 4302b29e7480177c124d7be2af9e7cfdce9670dd..22eae3a44e9949b6ec25f999b983cf419001fb0e 100644 (file)
@@ -13,22 +13,27 @@ Converts a string or an array of strings to uppercase.
 Will return:
 
     ASDF
-    EOS
+  EOS
   ) 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)
-      raise(Puppet::ParseError, 'upcase(): Requires either ' +
-        'array or string to work with')
+    unless value.is_a?(Array) || value.is_a?(String) || value.is_a?(Hash)
+      raise(Puppet::ParseError, 'upcase(): Requires an ' +
+                                  'array, string or hash to work with')
     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 }
+    elsif value.is_a?(Hash)
+      result = {}
+      result << value.each_pair do |k, v|
+        return {k.upcase => v.collect! { |p| p.upcase }}
+      end
     else
       result = value.upcase
     end
index 3cf8b055249922505c009c10fb66321e76894319..a50a3ab3a108f5270929aa00737a228ac0f03b5e 100755 (executable)
@@ -9,7 +9,7 @@ describe "the upcase function" do
   end
 
   it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_upcase([]) }.to( raise_error(Puppet::ParseError))
+    expect { scope.function_upcase([]) }.to(raise_error(Puppet::ParseError))
   end
 
   it "should upcase a string" do
@@ -30,4 +30,10 @@ describe "the upcase function" do
     result = scope.function_upcase([value])
     result.should(eq('ABC'))
   end
+
+  it 'should accept hashes and return uppercase' do
+    expect(
+        scope.function_upcase([{'test' => %w(this that and other thing)}])
+    ).to eq({'TEST' => %w(THIS THAT AND OTHER THING)})
+  end
 end