dry-configurable 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,186 +0,0 @@
1
- RSpec.describe Dry::Configurable::Config do
2
- let(:klass) { Dry::Configurable::Config }
3
- let(:config) { klass.create(settings) }
4
- let(:settings) do
5
- [
6
- value_class.new(:db, 'sqlite', ->(v) { "#{v}:memory" }),
7
- value_class.new(:user, 'root', ->(v) { v }),
8
- value_class.new(:pass, none, ->(v) { v })
9
- ]
10
- end
11
- let(:value_class) { Dry::Configurable::Config::Value }
12
- let(:none) { value_class::NONE }
13
-
14
- describe '.create' do
15
- it 'creates a config subclass from the given settings' do
16
- expect(config.class).to be < klass
17
-
18
- expect(config.db).to eq('sqlite:memory')
19
- expect(config.user).to eq('root')
20
- expect(config.pass).to be(nil)
21
-
22
- expect { config.db = 'ineedm0ar' }.to change(config, :db)
23
- .from('sqlite:memory')
24
- .to('ineedm0ar:memory')
25
- expect { config.user = 'whoami' }.to change(config, :user)
26
- .from('root')
27
- .to('whoami')
28
- expect { config.pass = 'h4xz0rz' }.to change(config, :pass)
29
- .from(nil)
30
- .to('h4xz0rz')
31
- end
32
- end
33
-
34
- describe '#clone' do
35
- subject!(:clone) { config.clone }
36
-
37
- it 'clones and returns the config' do
38
- expect(clone.db).to eq(config.db)
39
- expect(clone.user).to eq(config.user)
40
- expect(clone.pass).to eq(config.pass)
41
- is_expected.to_not be(config)
42
- end
43
- end
44
-
45
- describe '#dup' do
46
- subject!(:dup) { config.dup }
47
-
48
- it 'dups and returns the config' do
49
- expect(dup.db).to eq(config.db)
50
- expect(dup.user).to eq(config.user)
51
- expect(dup.pass).to eq(config.pass)
52
- is_expected.to_not be(config)
53
- end
54
- end
55
-
56
- describe '#finalize!' do
57
- subject!(:dup) { config.finalize! }
58
-
59
- it 'freezes itself and the config' do
60
- expect { config.user = 'whoami' }
61
- .to raise_error(Dry::Configurable::FrozenConfig, 'Cannot modify frozen config')
62
- end
63
- end
64
-
65
- describe '#to_h' do
66
- subject! { config.to_h }
67
-
68
- context 'without nesting' do
69
- it 'returns a config hash' do
70
- is_expected.to eq(
71
- db: 'sqlite:memory',
72
- user: 'root',
73
- pass: nil
74
- )
75
- end
76
- end
77
-
78
- context 'with nesting' do
79
- let(:nested_setting) do
80
- ::Dry::Configurable::NestedConfig.new do
81
- setting(:bar, 'baz') { |v| v }
82
- end.tap(&:create_config)
83
- end
84
- let(:settings) do
85
- [
86
- value_class.new(:db, 'sqlite', ->(v) { "#{v}:memory" }),
87
- value_class.new(:user, 'root', ->(v) { v }),
88
- value_class.new(:pass, none, ->(v) { v }),
89
- value_class.new(:foo, nested_setting, ->(v) { v })
90
- ]
91
- end
92
- it 'returns a config hash' do
93
- is_expected.to eq(
94
- db: 'sqlite:memory',
95
- user: 'root',
96
- pass: nil,
97
- foo: {
98
- bar: 'baz'
99
- }
100
- )
101
- end
102
- end
103
- end
104
-
105
- describe '#to_hash' do
106
- subject! { config.to_hash }
107
-
108
- context 'without nesting' do
109
- it 'returns a config hash' do
110
- is_expected.to eq(
111
- db: 'sqlite:memory',
112
- user: 'root',
113
- pass: nil
114
- )
115
- end
116
- end
117
-
118
- context 'with nesting' do
119
- let(:nested_setting) do
120
- klass.create([value_class.new(:bar, 'baz', ->(v) { v })])
121
- end
122
- let(:settings) do
123
- [
124
- value_class.new(:db, 'sqlite', ->(v) { "#{v}:memory" }),
125
- value_class.new(:user, 'root', ->(v) { v }),
126
- value_class.new(:pass, none, ->(v) { v }),
127
- value_class.new(:foo, nested_setting, ->(v) { v })
128
- ]
129
- end
130
- it 'returns a config hash' do
131
- is_expected.to eq(
132
- db: 'sqlite:memory',
133
- user: 'root',
134
- pass: nil,
135
- foo: {
136
- bar: 'baz'
137
- }
138
- )
139
- end
140
- end
141
- end
142
-
143
- describe '#[]' do
144
- it 'returns given setting' do
145
- expect(config[:db]).to eq('sqlite:memory')
146
- expect(config[:user]).to eq('root')
147
- expect(config[:pass]).to be(nil)
148
- end
149
-
150
- it 'raises an ArgumentError when setting does not exist' do
151
- expect { config[:unknown] }.to raise_error(
152
- ArgumentError, '+unknown+ is not a setting name'
153
- )
154
- end
155
-
156
- it 'accepts setting name as a string' do
157
- expect(config['user']).to eq('root')
158
- end
159
- end
160
-
161
- describe '#[]=' do
162
- it 'sets given setting' do
163
- expect { config[:db] = 'ineedm0ar' }.to change(config, :db)
164
- .from('sqlite:memory')
165
- .to('ineedm0ar:memory')
166
- expect { config[:user] = 'whoami' }.to change(config, :user)
167
- .from('root')
168
- .to('whoami')
169
- expect { config[:pass] = 'h4xz0rz' }.to change(config, :pass)
170
- .from(nil)
171
- .to('h4xz0rz')
172
- end
173
-
174
- it 'raises an ArgumentError when setting does not exist' do
175
- expect { config[:unknown] = 'unknown' }.to raise_error(
176
- ArgumentError, '+unknown+ is not a setting name'
177
- )
178
- end
179
-
180
- it 'accepts setting name as a string' do
181
- expect { config['user'] = 'whoami' }.to change(config, :user)
182
- .from('root')
183
- .to('whoami')
184
- end
185
- end
186
- end