diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-09-23 03:06:29 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-09-23 03:06:29 +0300 |
commit | 98dbb0a488d7b0093f352938210d9578b0f7a8a6 (patch) | |
tree | 25654204f8de2672556a696199fa209b8f8ff1b3 /spec/lib/gitlab/utils | |
parent | 9ce26d3dfdf4194f32c470cd3102b4376a53ef2f (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/utils')
-rw-r--r-- | spec/lib/gitlab/utils/inline_hash_spec.rb | 62 | ||||
-rw-r--r-- | spec/lib/gitlab/utils/safe_inline_hash_spec.rb | 35 |
2 files changed, 97 insertions, 0 deletions
diff --git a/spec/lib/gitlab/utils/inline_hash_spec.rb b/spec/lib/gitlab/utils/inline_hash_spec.rb new file mode 100644 index 00000000000..806117eddd0 --- /dev/null +++ b/spec/lib/gitlab/utils/inline_hash_spec.rb @@ -0,0 +1,62 @@ +# 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 diff --git a/spec/lib/gitlab/utils/safe_inline_hash_spec.rb b/spec/lib/gitlab/utils/safe_inline_hash_spec.rb new file mode 100644 index 00000000000..8d8163f3baa --- /dev/null +++ b/spec/lib/gitlab/utils/safe_inline_hash_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +describe Gitlab::Utils::SafeInlineHash do + context '.merge_keys!' do + let(:source) { { 'foo' => { 'bar' => 'baz' } } } + let(:validator) { instance_double(Gitlab::Utils::DeepSize, valid?: valid) } + + subject { described_class.merge_keys!(source, prefix: 'safe', connector: '::') } + + before do + allow(Gitlab::Utils::DeepSize) + .to receive(:new) + .with(source) + .and_return(validator) + end + + context 'when hash is too big' do + let(:valid) { false } + + it 'raises an exception' do + expect { subject }.to raise_error ArgumentError, 'The Hash is too big' + end + end + + context 'when hash has an acceptaable size' do + let(:valid) { true } + + it 'returns a result of InlineHash' do + is_expected.to eq('safe::foo::bar' => 'baz') + end + end + end +end |