X-Git-Url: https://git.adam-barratt.org.uk/?p=mirror%2Fdsa-puppet.git;a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Fspec%2Ffunctions%2Fmerge_spec.rb;h=7bffaab851fcde5f1c8a5bcde5e8f4017c5f9bc4;hp=25e2658f6e7044ecfae2986ac3ab794317e915fc;hb=30caaa85aed7015ca0d77216bff175eebd917eb7;hpb=6f656bd4265e3dab13b9af2bf96e9044322e9d8f diff --git a/3rdparty/modules/stdlib/spec/functions/merge_spec.rb b/3rdparty/modules/stdlib/spec/functions/merge_spec.rb index 25e2658f6..7bffaab85 100644 --- a/3rdparty/modules/stdlib/spec/functions/merge_spec.rb +++ b/3rdparty/modules/stdlib/spec/functions/merge_spec.rb @@ -2,18 +2,34 @@ require 'spec_helper' describe 'merge' do it { is_expected.not_to eq(nil) } - it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } - it { is_expected.to run.with_params({}, 'two').and_raise_error(Puppet::ParseError, %r{unexpected argument type String}) } - it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, %r{unexpected argument type (Fixnum|Integer)}) } it { - pending 'should not special case this' - is_expected.to run.with_params({}).and_return({}) + is_expected.to run \ + .with_params({}, 'two') \ + .and_raise_error( + ArgumentError, \ + Regexp.new(Regexp.escape("rejected: parameter 'args' expects a value of type Undef, Hash, or String[0, 0], got String")), + ) + } + it { + is_expected.to run \ + .with_params({}, 1) \ + .and_raise_error(ArgumentError, %r{parameter 'args' expects a value of type Undef, Hash, or String, got Integer}) } + it { + is_expected.to run \ + .with_params({ 'one' => 1, 'three' => { 'four' => 4 } }, 'two' => 'dos', 'three' => { 'five' => 5 }) \ + .and_return('one' => 1, 'three' => { 'five' => 5 }, 'two' => 'dos') + } + + it { is_expected.to run.with_params.and_return({}) } + it { is_expected.to run.with_params({}).and_return({}) } it { is_expected.to run.with_params({}, {}).and_return({}) } it { is_expected.to run.with_params({}, {}, {}).and_return({}) } + describe 'should accept empty strings as puppet undef' do it { is_expected.to run.with_params({}, '').and_return({}) } end + it { is_expected.to run.with_params({ 'key' => 'value' }, {}).and_return('key' => 'value') } it { is_expected.to run.with_params({}, 'key' => 'value').and_return('key' => 'value') } it { is_expected.to run.with_params({ 'key' => 'value1' }, 'key' => 'value2').and_return('key' => 'value2') } @@ -22,4 +38,43 @@ describe 'merge' do .with_params({ 'key1' => 'value1' }, { 'key2' => 'value2' }, 'key3' => 'value3') \ .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3') } + describe 'should accept iterable and merge produced hashes' do + it { + is_expected.to run \ + .with_params([1, 2, 3]) \ + .with_lambda { |_hsh, val| { val => val } } \ + .and_return(1 => 1, 2 => 2, 3 => 3) + } + it { + is_expected.to run \ + .with_params([1, 2, 3]) \ + .with_lambda { |_hsh, val| { val => val } unless val == 2 } \ + .and_return(1 => 1, 3 => 3) + } + it { + is_expected.to run \ + .with_params([1, 2, 3]) \ + # rubocop:disable Style/Semicolon + .with_lambda { |_hsh, val| raise StopIteration if val == 3; { val => val } } \ + .and_return(1 => 1, 2 => 2) + } + it { + is_expected.to run \ + .with_params(['a', 'b', 'b', 'c', 'b']) \ + .with_lambda { |hsh, val| { val => (hsh[val] || 0) + 1 } } \ + .and_return('a' => 1, 'b' => 3, 'c' => 1) + } + it { + is_expected.to run \ + .with_params(['a', 'b', 'c']) \ + .with_lambda { |_hsh, idx, val| { idx => val } } \ + .and_return(0 => 'a', 1 => 'b', 2 => 'c') + } + it { + is_expected.to run \ + .with_params('a' => 'A', 'b' => 'B', 'c' => 'C') \ + .with_lambda { |_hsh, key, val| { key => "#{key}#{val}" } } \ + .and_return('a' => 'aA', 'b' => 'bB', 'c' => 'cC') + } + end end