Skip to content

Add Hash to upcase #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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