Add Hash to upcase

This commit is contained in:
Travis Fields 2015-02-25 11:39:27 -08:00
parent 3da8d17390
commit 7021b1f55c
2 changed files with 17 additions and 6 deletions

View 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

View file

@ -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