From f3e79ddcd56a221c7799b35efde7e9803a5c7923 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Mon, 1 Jun 2015 12:21:59 +0100 Subject: Convert tests to use plain rspec-puppet Tests in the new style produces the following documentation output: abs should not eq nil should run abs() and raise an Puppet::ParseError should run abs(-34) and return 34 should run abs("-34") and return 34 should run abs(34) and return 34 should run abs("34") and return 34 --- spec/functions/delete_spec.rb | 102 ++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 48 deletions(-) (limited to 'spec/functions/delete_spec.rb') diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index c8edd78..6c4747b 100755 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -1,61 +1,67 @@ -#! /usr/bin/env ruby -S rspec require 'spec_helper' -describe "the delete function" do - let(:scope) { PuppetlabsSpec::PuppetInternals.scope } +describe 'delete' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) } - it "should exist" do - expect(Puppet::Parser::Functions.function("delete")).to eq("function_delete") + describe 'deleting from an array' do + it { is_expected.to run.with_params([], '').and_return([]) } + it { is_expected.to run.with_params([], 'two').and_return([]) } + it { is_expected.to run.with_params(['two'], 'two').and_return([]) } + it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) } + it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) } + it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) } + it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) } + it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) } end - it "should raise a ParseError if there are fewer than 2 arguments" do - expect { scope.function_delete([]) }.to(raise_error(Puppet::ParseError)) + describe 'deleting from a string' do + it { is_expected.to run.with_params('', '').and_return('') } + it { is_expected.to run.with_params('bar', '').and_return('bar') } + it { is_expected.to run.with_params('', 'bar').and_return('') } + it { is_expected.to run.with_params('bar', 'bar').and_return('') } + it { is_expected.to run.with_params('barbar', 'bar').and_return('') } + it { is_expected.to run.with_params('barfoobar', 'bar').and_return('foo') } + it { is_expected.to run.with_params('foobarbabarz', 'bar').and_return('foobaz') } + it { is_expected.to run.with_params('foobarbabarz', ['foo', 'bar']).and_return('baz') } + # this is so sick + it { is_expected.to run.with_params('barfoobar', ['barbar', 'foo']).and_return('barbar') } + it { is_expected.to run.with_params('barfoobar', ['foo', 'barbar']).and_return('') } end - it "should raise a ParseError if there are greater than 2 arguments" do - expect { scope.function_delete([[], 'foo', 'bar']) }.to(raise_error(Puppet::ParseError)) + describe 'deleting from an array' do + it { is_expected.to run.with_params({}, '').and_return({}) } + it { is_expected.to run.with_params({}, 'key').and_return({}) } + it { is_expected.to run.with_params({'key' => 'value'}, 'key').and_return({}) } + it { is_expected.to run \ + .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, 'key2') \ + .and_return( {'key1' => 'value1', 'key3' => 'value3'}) + } + it { is_expected.to run \ + .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['key1', 'key2']) \ + .and_return( {'key3' => 'value3'}) + } end - it "should raise a TypeError if a number is passed as the first argument" do - expect { scope.function_delete([1, 'bar']) }.to(raise_error(TypeError)) + it "should leave the original array intact" do + argument1 = ['one','two','three'] + original1 = argument1.dup + result = subject.call([argument1,'two']) + expect(argument1).to eq(original1) end - - it "should delete all instances of an element from an array" do - result = scope.function_delete([['a', 'b', 'c', 'b'], 'b']) - expect(result).to(eq(['a', 'c'])) - end - - it "should delete all instances of a substring from a string" do - result = scope.function_delete(['foobarbabarz', 'bar']) - expect(result).to(eq('foobaz')) + it "should leave the original string intact" do + argument1 = 'onetwothree' + original1 = argument1.dup + result = subject.call([argument1,'two']) + expect(argument1).to eq(original1) end - - it "should delete a key from a hash" do - result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, 'b']) - expect(result).to(eq({'a' => 1, 'c' => 3})) + it "should leave the original hash intact" do + argument1 = {'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'} + original1 = argument1.dup + result = subject.call([argument1,'key2']) + expect(argument1).to eq(original1) end - - it 'should accept an array of items to delete' do - result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, ['b', 'c']]) - expect(result).to(eq({'a' => 1})) - end - - it "should not change origin array passed as argument" do - origin_array = ['a', 'b', 'c', 'd'] - result = scope.function_delete([origin_array, 'b']) - expect(origin_array).to(eq(['a', 'b', 'c', 'd'])) - end - - it "should not change the origin string passed as argument" do - origin_string = 'foobarbabarz' - result = scope.function_delete([origin_string, 'bar']) - expect(origin_string).to(eq('foobarbabarz')) - end - - it "should not change origin hash passed as argument" do - origin_hash = {'a' => 1, 'b' => 2, 'c' => 3} - result = scope.function_delete([origin_hash, 'b']) - expect(origin_hash).to(eq({'a' => 1, 'b' => 2, 'c' => 3})) - end - end -- cgit v1.2.3 From 0d46515b57cea60d4d5f1e4d81a75a448a7a73a8 Mon Sep 17 00:00:00 2001 From: Joseph Yaworski Date: Mon, 11 Apr 2016 22:09:24 -0400 Subject: Add support for regular expressions to delete --- spec/functions/delete_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'spec/functions/delete_spec.rb') diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index 6c4747b..998f9a6 100755 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -12,6 +12,7 @@ describe 'delete' do it { is_expected.to run.with_params([], 'two').and_return([]) } it { is_expected.to run.with_params(['two'], 'two').and_return([]) } it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) } + it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) } @@ -32,7 +33,7 @@ describe 'delete' do it { is_expected.to run.with_params('barfoobar', ['foo', 'barbar']).and_return('') } end - describe 'deleting from an array' do + describe 'deleting from a hash' do it { is_expected.to run.with_params({}, '').and_return({}) } it { is_expected.to run.with_params({}, 'key').and_return({}) } it { is_expected.to run.with_params({'key' => 'value'}, 'key').and_return({}) } @@ -44,6 +45,10 @@ describe 'delete' do .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['key1', 'key2']) \ .and_return( {'key3' => 'value3'}) } + it { is_expected.to run \ + .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['^key\d']) \ + .and_return({}) + } end it "should leave the original array intact" do -- cgit v1.2.3 From 232de137f1018060b256b1f3f649be0b6d7d9952 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Mon, 25 Apr 2016 14:33:43 -0700 Subject: Revert "Add support for regular expressions to delete" This reverts commit 0d46515b57cea60d4d5f1e4d81a75a448a7a73a8. It introduced backwards-incompatible functionality. --- spec/functions/delete_spec.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'spec/functions/delete_spec.rb') diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index 998f9a6..6c4747b 100755 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -12,7 +12,6 @@ describe 'delete' do it { is_expected.to run.with_params([], 'two').and_return([]) } it { is_expected.to run.with_params(['two'], 'two').and_return([]) } it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) } - it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) } @@ -33,7 +32,7 @@ describe 'delete' do it { is_expected.to run.with_params('barfoobar', ['foo', 'barbar']).and_return('') } end - describe 'deleting from a hash' do + describe 'deleting from an array' do it { is_expected.to run.with_params({}, '').and_return({}) } it { is_expected.to run.with_params({}, 'key').and_return({}) } it { is_expected.to run.with_params({'key' => 'value'}, 'key').and_return({}) } @@ -45,10 +44,6 @@ describe 'delete' do .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['key1', 'key2']) \ .and_return( {'key3' => 'value3'}) } - it { is_expected.to run \ - .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['^key\d']) \ - .and_return({}) - } end it "should leave the original array intact" do -- cgit v1.2.3 From 19752a7ff378a35f287bf5351d466a1eae399266 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Mon, 25 Apr 2016 14:34:21 -0700 Subject: Remove todo for delete() and update spec This spec should verify that substring matches are not removed in the future --- spec/functions/delete_spec.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/functions/delete_spec.rb') diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index 6c4747b..cf696ac 100755 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -13,6 +13,7 @@ describe 'delete' do it { is_expected.to run.with_params(['two'], 'two').and_return([]) } it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) } + it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) } -- cgit v1.2.3 From 540546b9b41745bbc4821f9966ae301dc0b5056a Mon Sep 17 00:00:00 2001 From: Joseph Yaworski Date: Tue, 12 Apr 2016 16:53:07 -0400 Subject: Use reject instead of delete_if --- spec/functions/delete_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'spec/functions/delete_spec.rb') diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index cf696ac..fd2a8ad 100755 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -4,6 +4,7 @@ describe 'delete' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], 'two') } it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) } it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) } @@ -12,11 +13,15 @@ describe 'delete' do it { is_expected.to run.with_params([], 'two').and_return([]) } it { is_expected.to run.with_params(['two'], 'two').and_return([]) } it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) } + it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) } + it { is_expected.to run.with_params(['ab', 'b', 'c', 'b'], 'b').and_return(['ab', 'c']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) } + it { is_expected.to run.with_params(['abracadabra'], 'abr').and_return(['abracadabra']) } + it { is_expected.to run.with_params(['abracadabra'], '^.*jimbob.*$').and_return(['abracadabra']) } end describe 'deleting from a string' do -- cgit v1.2.3 From dd71c0288052dd3a96e730ff198f5c0a8d640946 Mon Sep 17 00:00:00 2001 From: Joseph Yaworski Date: Wed, 11 May 2016 13:21:24 -0400 Subject: Add a delete_regex function To maintain backwards compatibility, add a delete_regex function instead of modifying delete itself. --- spec/functions/delete_spec.rb | 3 --- 1 file changed, 3 deletions(-) (limited to 'spec/functions/delete_spec.rb') diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index fd2a8ad..b44accf 100755 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -13,15 +13,12 @@ describe 'delete' do it { is_expected.to run.with_params([], 'two').and_return([]) } it { is_expected.to run.with_params(['two'], 'two').and_return([]) } it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) } - it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) } it { is_expected.to run.with_params(['ab', 'b', 'c', 'b'], 'b').and_return(['ab', 'c']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) } - it { is_expected.to run.with_params(['abracadabra'], 'abr').and_return(['abracadabra']) } - it { is_expected.to run.with_params(['abracadabra'], '^.*jimbob.*$').and_return(['abracadabra']) } end describe 'deleting from a string' do -- cgit v1.2.3