|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe 'mysql::normalise_and_deepmerge' do |
| 4 | + it 'exists' do |
| 5 | + is_expected.not_to eq(nil) |
| 6 | + end |
| 7 | + |
| 8 | + it 'throws error with no arguments' do |
| 9 | + is_expected.to run.with_params.and_raise_error(Puppet::ParseError) |
| 10 | + end |
| 11 | + |
| 12 | + it 'throws error with only one argument' do |
| 13 | + is_expected.to run.with_params('one' => 1).and_raise_error(Puppet::ParseError) |
| 14 | + end |
| 15 | + |
| 16 | + it 'accepts empty strings as puppet undef' do |
| 17 | + is_expected.to run.with_params({}, '') |
| 18 | + end |
| 19 | + |
| 20 | + # rubocop:disable RSpec/NamedSubject |
| 21 | + index_values = ['one', 'two', 'three'] |
| 22 | + expected_values_one = ['1', '2', '2'] |
| 23 | + it 'merge two hashes' do |
| 24 | + new_hash = subject.execute({ 'one' => '1', 'two' => '1' }, 'two' => '2', 'three' => '2') |
| 25 | + index_values.each_with_index do |index, expected| |
| 26 | + expect(new_hash[index]).to eq(expected_values_one[expected]) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + it 'merges multiple hashes' do |
| 31 | + hash = subject.execute({ 'one' => 1 }, { 'one' => '2' }, 'one' => '3') |
| 32 | + expect(hash['one']).to eq('3') |
| 33 | + end |
| 34 | + |
| 35 | + it 'accepts empty hashes' do |
| 36 | + is_expected.to run.with_params({}, {}, {}).and_return({}) |
| 37 | + end |
| 38 | + |
| 39 | + expected_values_two = [1, 2, 'four' => 4] |
| 40 | + it 'merges subhashes' do |
| 41 | + hash = subject.execute({ 'one' => 1 }, 'two' => 2, 'three' => { 'four' => 4 }) |
| 42 | + index_values.each_with_index do |index, expected| |
| 43 | + expect(hash[index]).to eq(expected_values_two[expected]) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + it 'appends to subhashes' do |
| 48 | + hash = subject.execute({ 'one' => { 'two' => 2 } }, 'one' => { 'three' => 3 }) |
| 49 | + expect(hash['one']).to eq('two' => 2, 'three' => 3) |
| 50 | + end |
| 51 | + |
| 52 | + expected_values_three = [1, 'dos', { 'four' => 4, 'five' => 5 }] |
| 53 | + it 'appends to subhashes 2' do |
| 54 | + hash = subject.execute({ 'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }, 'two' => 'dos', 'three' => { 'five' => 5 }) |
| 55 | + index_values.each_with_index do |index, expected| |
| 56 | + expect(hash[index]).to eq(expected_values_three[expected]) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + index_values_two = ['key1', 'key2'] |
| 61 | + expected_values_four = [{ 'a' => 1, 'b' => 99 }, 'c' => 3] |
| 62 | + it 'appends to subhashes 3' do |
| 63 | + hash = subject.execute({ 'key1' => { 'a' => 1, 'b' => 2 }, 'key2' => { 'c' => 3 } }, 'key1' => { 'b' => 99 }) |
| 64 | + index_values_two.each_with_index do |index, expected| |
| 65 | + expect(hash[index]).to eq(expected_values_four[expected]) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + it 'equates keys mod dash and underscore #value' do |
| 70 | + hash = subject.execute({ 'a-b-c' => 1 }, 'a_b_c' => 10) |
| 71 | + expect(hash['a_b_c']).to eq(10) |
| 72 | + end |
| 73 | + it 'equates keys mod dash and underscore #not' do |
| 74 | + hash = subject.execute({ 'a-b-c' => 1 }, 'a_b_c' => 10) |
| 75 | + expect(hash).not_to have_key('a-b-c') |
| 76 | + end |
| 77 | + |
| 78 | + index_values_three = ['a_b_c', 'b-c-d'] |
| 79 | + expected_values_five = [10, { 'e-f-g' => 3, 'c_d_e' => 12 }] |
| 80 | + index_values_error = ['a-b-c', 'b_c_d'] |
| 81 | + index_values_three.each_with_index do |index, expected| |
| 82 | + it 'keeps style of the last when keys are equal mod dash and underscore #value' do |
| 83 | + hash = subject.execute({ 'a-b-c' => 1, 'b_c_d' => { 'c-d-e' => 2, 'e-f-g' => 3 } }, 'a_b_c' => 10, 'b-c-d' => { 'c_d_e' => 12 }) |
| 84 | + expect(hash[index]).to eq(expected_values_five[expected]) |
| 85 | + end |
| 86 | + it 'keeps style of the last when keys are equal mod dash and underscore #not' do |
| 87 | + hash = subject.execute({ 'a-b-c' => 1, 'b_c_d' => { 'c-d-e' => 2, 'e-f-g' => 3 } }, 'a_b_c' => 10, 'b-c-d' => { 'c_d_e' => 12 }) |
| 88 | + expect(hash).not_to have_key(index_values_error[expected]) |
| 89 | + end |
| 90 | + end |
| 91 | + # rubocop:enable RSpec/NamedSubject |
| 92 | +end |
0 commit comments