blob: 4b57e91c2e113d00e277d6ba14b5e19533e99ce7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GitlabSettings::Options, :aggregate_failures, feature_category: :shared do
let(:config) { { foo: { bar: 'baz' } } }
subject(:options) { described_class.build(config) }
describe '.build' do
context 'when argument is a hash' do
it 'creates a new GitlabSettings::Options instance' do
options = described_class.build(config)
expect(options).to be_a described_class
expect(options.foo).to be_a described_class
expect(options.foo.bar).to eq 'baz'
end
end
end
describe '#[]' do
it 'accesses the configuration key as string' do
expect(options['foo']).to be_a described_class
expect(options['foo']['bar']).to eq 'baz'
expect(options['inexistent']).to be_nil
end
it 'accesses the configuration key as symbol' do
expect(options[:foo]).to be_a described_class
expect(options[:foo][:bar]).to eq 'baz'
expect(options[:inexistent]).to be_nil
end
end
describe '#[]=' do
it 'changes the configuration key as string' do
options['foo']['bar'] = 'anothervalue'
expect(options['foo']['bar']).to eq 'anothervalue'
end
it 'changes the configuration key as symbol' do
options[:foo][:bar] = 'anothervalue'
expect(options[:foo][:bar]).to eq 'anothervalue'
end
context 'when key does not exist' do
it 'creates a new configuration by string key' do
options['inexistent'] = 'value'
expect(options['inexistent']).to eq 'value'
end
it 'creates a new configuration by string key' do
options[:inexistent] = 'value'
expect(options[:inexistent]).to eq 'value'
end
end
end
describe '#key?' do
it 'checks if a string key exists' do
expect(options.key?('foo')).to be true
expect(options.key?('inexistent')).to be false
end
it 'checks if a symbol key exists' do
expect(options.key?(:foo)).to be true
expect(options.key?(:inexistent)).to be false
end
end
describe '#to_hash' do
it 'returns the hash representation of the config' do
expect(options.to_hash).to eq('foo' => { 'bar' => 'baz' })
end
end
describe '#merge' do
it 'merges a hash to the existing options' do
expect(options.merge(more: 'configs').to_hash).to eq(
'foo' => { 'bar' => 'baz' },
'more' => 'configs'
)
end
context 'when the merge hash replaces existing configs' do
it 'merges a hash to the existing options' do
expect(options.merge(foo: 'configs').to_hash).to eq('foo' => 'configs')
end
end
end
describe '#deep_merge' do
it 'merges a hash to the existing options' do
expect(options.deep_merge(foo: { more: 'configs' }).to_hash).to eq('foo' => {
'bar' => 'baz',
'more' => 'configs'
})
end
context 'when the merge hash replaces existing configs' do
it 'merges a hash to the existing options' do
expect(options.deep_merge(foo: { bar: 'configs' }).to_hash).to eq('foo' => {
'bar' => 'configs'
})
end
end
end
describe '#is_a?' do
it 'returns false for anything different of Hash or GitlabSettings::Options' do
expect(options.is_a?(described_class)).to be true
expect(options.is_a?(Hash)).to be true
expect(options.is_a?(String)).to be false
end
end
describe '#method_missing' do
context 'when method is an option' do
it 'delegates methods to options keys' do
expect(options.foo.bar).to eq('baz')
end
it 'uses methods to change options values' do
expect { options.foo = 1 }
.to change { options.foo }
.to(1)
end
end
context 'when method is not an option' do
it 'delegates the method to the internal options hash' do
expect { options.foo.delete('bar') }
.to change { options.to_hash }
.to({ 'foo' => {} })
end
end
context 'when method is not an option and does not exist in hash' do
it 'raises GitlabSettings::MissingSetting' do
expect { options.anything }
.to raise_error(
::GitlabSettings::MissingSetting,
"option 'anything' not defined"
)
end
end
end
end
|