1 #! /usr/bin/env ruby -S rspec
4 describe "the delete function" do
5 let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
8 expect(Puppet::Parser::Functions.function("delete")).to eq("function_delete")
11 it "should raise a ParseError if there are fewer than 2 arguments" do
12 expect { scope.function_delete([]) }.to(raise_error(Puppet::ParseError))
15 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))
19 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))
23 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']))
28 it "should delete all instances of a substring from a string" do
29 result = scope.function_delete(['foobarbabarz', 'bar'])
30 expect(result).to(eq('foobaz'))
33 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}))
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}))
43 it "should not change origin array passed as argument" do
44 origin_array = ['a', 'b', 'c', 'd']
45 result = scope.function_delete([origin_array, 'b'])
46 expect(origin_array).to(eq(['a', 'b', 'c', 'd']))
49 it "should not change the origin string passed as argument" do
50 origin_string = 'foobarbabarz'
51 result = scope.function_delete([origin_string, 'bar'])
52 expect(origin_string).to(eq('foobarbabarz'))
55 it "should not change origin hash passed as argument" do
56 origin_hash = {'a' => 1, 'b' => 2, 'c' => 3}
57 result = scope.function_delete([origin_hash, 'b'])
58 expect(origin_hash).to(eq({'a' => 1, 'b' => 2, 'c' => 3}))