Skip to content

Commit 8ec6f8d

Browse files
author
Travis Fields
committed
MODULES-1606 add ability to pass array to delete for items to delete
1 parent 9febb8b commit 8ec6f8d

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

lib/puppet/parser/functions/delete.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ module Puppet::Parser::Functions
1919
2020
delete('abracadabra', 'bra')
2121
Would return: 'acada'
22-
EOS
22+
EOS
2323
) do |arguments|
2424

2525
if (arguments.size != 2) then
2626
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
27-
"given #{arguments.size} for 2.")
27+
"given #{arguments.size} for 2.")
2828
end
2929

3030
collection = arguments[0].dup
31-
item = arguments[1]
32-
33-
case collection
34-
when Array, Hash
35-
collection.delete item
36-
when String
37-
collection.gsub! item, ''
38-
else
39-
raise(TypeError, "delete(): First argument must be an Array, " +
40-
"String, or Hash. Given an argument of class #{collection.class}.")
31+
Array(arguments[1]).each do |item|
32+
case collection
33+
when Array, Hash
34+
collection.delete item
35+
when String
36+
collection.gsub! item, ''
37+
else
38+
raise(TypeError, "delete(): First argument must be an Array, " +
39+
"String, or Hash. Given an argument of class #{collection.class}.")
40+
end
4141
end
4242
collection
4343
end

spec/functions/delete_spec.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,53 @@
99
end
1010

1111
it "should raise a ParseError if there are fewer than 2 arguments" do
12-
expect { scope.function_delete([]) }.to( raise_error(Puppet::ParseError))
12+
expect { scope.function_delete([]) }.to(raise_error(Puppet::ParseError))
1313
end
1414

1515
it "should raise a ParseError if there are greater than 2 arguments" do
16-
expect { scope.function_delete([[], 'foo', 'bar']) }.to( raise_error(Puppet::ParseError))
16+
expect { scope.function_delete([[], 'foo', 'bar']) }.to(raise_error(Puppet::ParseError))
1717
end
1818

1919
it "should raise a TypeError if a number is passed as the first argument" do
20-
expect { scope.function_delete([1, 'bar']) }.to( raise_error(TypeError))
20+
expect { scope.function_delete([1, 'bar']) }.to(raise_error(TypeError))
2121
end
2222

2323
it "should delete all instances of an element from an array" do
24-
result = scope.function_delete([['a','b','c','b'],'b'])
25-
expect(result).to(eq(['a','c']))
24+
result = scope.function_delete([['a', 'b', 'c', 'b'], 'b'])
25+
expect(result).to(eq(['a', 'c']))
2626
end
2727

2828
it "should delete all instances of a substring from a string" do
29-
result = scope.function_delete(['foobarbabarz','bar'])
29+
result = scope.function_delete(['foobarbabarz', 'bar'])
3030
expect(result).to(eq('foobaz'))
3131
end
3232

3333
it "should delete a key from a hash" do
34-
result = scope.function_delete([{ 'a' => 1, 'b' => 2, 'c' => 3 },'b'])
35-
expect(result).to(eq({ 'a' => 1, 'c' => 3 }))
34+
result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, 'b'])
35+
expect(result).to(eq({'a' => 1, 'c' => 3}))
36+
end
37+
38+
it 'should accept an array of items to delete' do
39+
result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, ['b', 'c']])
40+
expect(result).to(eq({'a' => 1}))
3641
end
3742

3843
it "should not change origin array passed as argument" do
39-
origin_array = ['a','b','c','d']
44+
origin_array = ['a', 'b', 'c', 'd']
4045
result = scope.function_delete([origin_array, 'b'])
41-
expect(origin_array).to(eq(['a','b','c','d']))
46+
expect(origin_array).to(eq(['a', 'b', 'c', 'd']))
4247
end
4348

4449
it "should not change the origin string passed as argument" do
4550
origin_string = 'foobarbabarz'
46-
result = scope.function_delete([origin_string,'bar'])
51+
result = scope.function_delete([origin_string, 'bar'])
4752
expect(origin_string).to(eq('foobarbabarz'))
4853
end
4954

5055
it "should not change origin hash passed as argument" do
51-
origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
56+
origin_hash = {'a' => 1, 'b' => 2, 'c' => 3}
5257
result = scope.function_delete([origin_hash, 'b'])
53-
expect(origin_hash).to(eq({ 'a' => 1, 'b' => 2, 'c' => 3 }))
58+
expect(origin_hash).to(eq({'a' => 1, 'b' => 2, 'c' => 3}))
5459
end
5560

5661
end

0 commit comments

Comments
 (0)