2019-09-22 20:06:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Utils::InlineHash do
|
2019-09-22 20:06:29 -04:00
|
|
|
describe '.merge_keys' do
|
|
|
|
subject { described_class.merge_keys(source) }
|
|
|
|
|
|
|
|
let(:source) do
|
|
|
|
{
|
|
|
|
nested_param: {
|
2019-10-01 02:06:13 -04:00
|
|
|
key: :Value
|
2019-09-22 20:06:29 -04:00
|
|
|
},
|
|
|
|
'root_param' => 'Root',
|
2019-10-01 02:06:13 -04:00
|
|
|
unnested_symbol_key: :unnested_symbol_value,
|
|
|
|
12 => 22,
|
2019-09-22 20:06:29 -04:00
|
|
|
'very' => {
|
|
|
|
'deep' => {
|
|
|
|
'nested' => {
|
|
|
|
'param' => 'Deep nested value'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'transforms a nested hash into a one-level hash' do
|
|
|
|
is_expected.to eq(
|
2019-10-01 02:06:13 -04:00
|
|
|
'nested_param.key' => :Value,
|
2019-09-22 20:06:29 -04:00
|
|
|
'root_param' => 'Root',
|
2019-10-01 02:06:13 -04:00
|
|
|
:unnested_symbol_key => :unnested_symbol_value,
|
|
|
|
12 => 22,
|
2019-09-22 20:06:29 -04:00
|
|
|
'very.deep.nested.param' => 'Deep nested value'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retains key insertion order' do
|
|
|
|
expect(subject.keys)
|
2019-10-01 02:06:13 -04:00
|
|
|
.to eq(['nested_param.key', 'root_param', :unnested_symbol_key, 12, 'very.deep.nested.param'])
|
2019-09-22 20:06:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a custom connector' do
|
|
|
|
subject { described_class.merge_keys(source, connector: '::') }
|
|
|
|
|
|
|
|
it 'uses the connector to merge keys' do
|
|
|
|
is_expected.to eq(
|
2019-10-01 02:06:13 -04:00
|
|
|
'nested_param::key' => :Value,
|
2019-09-22 20:06:29 -04:00
|
|
|
'root_param' => 'Root',
|
2019-10-01 02:06:13 -04:00
|
|
|
:unnested_symbol_key => :unnested_symbol_value,
|
|
|
|
12 => 22,
|
2019-09-22 20:06:29 -04:00
|
|
|
'very::deep::nested::param' => 'Deep nested value'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a starter prefix' do
|
|
|
|
subject { described_class.merge_keys(source, prefix: 'options') }
|
|
|
|
|
|
|
|
it 'prefixes all the keys' do
|
|
|
|
is_expected.to eq(
|
2019-10-01 02:06:13 -04:00
|
|
|
'options.nested_param.key' => :Value,
|
2019-09-22 20:06:29 -04:00
|
|
|
'options.root_param' => 'Root',
|
2019-10-01 02:06:13 -04:00
|
|
|
'options.unnested_symbol_key' => :unnested_symbol_value,
|
|
|
|
'options.12' => 22,
|
2019-09-22 20:06:29 -04:00
|
|
|
'options.very.deep.nested.param' => 'Deep nested value'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|