deep_merge_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. require 'spec_helper'
  2. describe 'deep_merge' do
  3. it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  4. it { is_expected.to run.with_params('key' => 'value').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
  5. it { is_expected.to run.with_params({}, '2').and_raise_error(Puppet::ParseError, %r{unexpected argument type String}) }
  6. it { is_expected.to run.with_params({}, 2).and_raise_error(Puppet::ParseError, %r{unexpected argument}) }
  7. it { is_expected.to run.with_params({}, '').and_return({}) }
  8. it { is_expected.to run.with_params({}, {}).and_return({}) }
  9. it { is_expected.to run.with_params({}, {}, {}).and_return({}) }
  10. it { is_expected.to run.with_params({}, {}, {}, {}).and_return({}) }
  11. it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return('key' => 'value') }
  12. it { is_expected.to run.with_params({ 'key1' => 'value1' }, 'key2' => 'value2').and_return('key1' => 'value1', 'key2' => 'value2') }
  13. describe 'when arguments have key collisions' do
  14. it 'prefers values from the last hash' do
  15. is_expected.to run \
  16. .with_params({ 'key1' => 'value1', 'key2' => 'value2' }, 'key2' => 'replacement_value', 'key3' => 'value3') \
  17. .and_return('key1' => 'value1', 'key2' => 'replacement_value', 'key3' => 'value3')
  18. end
  19. it {
  20. is_expected.to run \
  21. .with_params({ 'key1' => 'value1' }, { 'key1' => 'value2' }, 'key1' => 'value3') \
  22. .and_return('key1' => 'value3')
  23. }
  24. end
  25. describe 'when arguments have subhashes' do
  26. it {
  27. is_expected.to run \
  28. .with_params({ 'key1' => 'value1' }, 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' }) \
  29. .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' })
  30. }
  31. it {
  32. is_expected.to run \
  33. .with_params({ 'key1' => { 'subkey1' => 'value1' } }, 'key1' => { 'subkey2' => 'value2' }) \
  34. .and_return('key1' => { 'subkey1' => 'value1', 'subkey2' => 'value2' })
  35. }
  36. it {
  37. is_expected.to run \
  38. .with_params({ 'key1' => { 'subkey1' => { 'subsubkey1' => 'value1' } } }, 'key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } }) \
  39. .and_return('key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } })
  40. }
  41. end
  42. arguments = { 'key1' => 'value1' }, { 'key2' => 'value2' }
  43. originals = [arguments[0].dup, arguments[1].dup]
  44. it 'does not change the original hashes' do
  45. subject.call([arguments[0], arguments[1]])
  46. arguments.each_with_index do |argument, index|
  47. expect(argument).to eq(originals[index])
  48. end
  49. end
  50. context 'with UTF8 and double byte characters' do
  51. it { is_expected.to run.with_params({ 'ĸέỹ1' => 'ϋǻļủë1' }, 'この文字列' => '万').and_return('ĸέỹ1' => 'ϋǻļủë1', 'この文字列' => '万') }
  52. end
  53. end