Skip to content

Commit

Permalink
Add Hash to upcase
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Fields committed Feb 25, 2015
1 parent 3da8d17 commit 7021b1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/puppet/parser/functions/upcase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@ module Puppet::Parser::Functions
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
Expand Down
8 changes: 7 additions & 1 deletion spec/functions/upcase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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
Expand All @@ -30,4 +30,10 @@ class AlsoString < String
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

0 comments on commit 7021b1f

Please sign in to comment.