# frozen_string_literal: true require 'fast_spec_helper' describe Gitlab::Utils::InlineHash do describe '.merge_keys' do subject { described_class.merge_keys(source) } let(:source) do { nested_param: { key: 'Value' }, 'root_param' => 'Root', 'very' => { 'deep' => { 'nested' => { 'param' => 'Deep nested value' } } } } end it 'transforms a nested hash into a one-level hash' do is_expected.to eq( 'nested_param.key' => 'Value', 'root_param' => 'Root', 'very.deep.nested.param' => 'Deep nested value' ) end it 'retains key insertion order' do expect(subject.keys) .to eq(%w(nested_param.key root_param very.deep.nested.param)) 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( 'nested_param::key' => 'Value', 'root_param' => 'Root', '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( 'options.nested_param.key' => 'Value', 'options.root_param' => 'Root', 'options.very.deep.nested.param' => 'Deep nested value' ) end end end end