dry-configurable 0.7.0 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,114 +0,0 @@
1
- RSpec.describe Dry::Configurable::ArgumentParser do
2
- let(:klass) { Dry::Configurable::ArgumentParser }
3
-
4
- context 'with no args' do
5
- let(:parsed) { klass.new([]) }
6
-
7
- it 'return default values' do
8
- expect(parsed.value).to eq nil
9
- expect(parsed.options).to eq({})
10
- end
11
- end
12
-
13
- context 'with value and options' do
14
- let(:parsed) { klass.new([value, options]) }
15
-
16
- context 'valid options' do
17
- let(:value) { 'dry-rb' }
18
- let(:options) do
19
- { reader: true }
20
- end
21
-
22
- it 'returns correct value and options' do
23
- expect(parsed.value).to eq 'dry-rb'
24
- expect(parsed.options).to eq(reader: true)
25
- end
26
- end
27
-
28
- context 'invalid options' do
29
- let(:value) { 'dry-rb' }
30
- let(:options) do
31
- { writer: true }
32
- end
33
-
34
- it 'returns correct values and empty options' do
35
- expect(parsed.value).to eq 'dry-rb'
36
- expect(parsed.options).to eq({})
37
- end
38
- end
39
-
40
- context 'values as hash' do
41
- let(:value) do
42
- { db: 'dry-rb' }
43
- end
44
- let(:options) do
45
- { reader: true }
46
- end
47
-
48
- it 'returns correct values and empty options' do
49
- expect(parsed.value).to eq(db: 'dry-rb')
50
- expect(parsed.options).to eq(reader: true)
51
- end
52
- end
53
-
54
- context 'values as array' do
55
- let(:value) { [1, 2, 3] }
56
- let(:options) do
57
- { reader: true }
58
- end
59
-
60
- it 'returns correct values and empty options' do
61
- expect(parsed.value).to eq([1, 2, 3])
62
- expect(parsed.options).to eq(reader: true)
63
- end
64
- end
65
- end
66
-
67
- context 'with value only' do
68
- let(:parsed) { klass.new([value]) }
69
- context 'valid options' do
70
- let(:value) { 'dry-rb' }
71
-
72
- it 'returns correct value and options' do
73
- expect(parsed.value).to eq 'dry-rb'
74
- expect(parsed.options).to eq({})
75
- end
76
- end
77
-
78
- context 'with hash with non option key' do
79
- let(:value) do
80
- { writer: true }
81
- end
82
-
83
- it 'returns correct value and options' do
84
- expect(parsed.value).to eq(writer: true)
85
- expect(parsed.options).to eq({})
86
- end
87
- end
88
-
89
- context 'with hash with option key' do
90
- let(:value) do
91
- { reader: true, writer: true }
92
- end
93
-
94
- it 'returns correct value and options' do
95
- expect(parsed.value).to eq nil
96
- expect(parsed.options).to eq(reader: true)
97
- end
98
- end
99
- end
100
-
101
- context 'with options only' do
102
- let(:parsed) { klass.new([options]) }
103
- context 'valid options' do
104
- let(:options) do
105
- { reader: true }
106
- end
107
-
108
- it 'returns correct value and options' do
109
- expect(parsed.value).to eq nil
110
- expect(parsed.options).to eq(reader: true)
111
- end
112
- end
113
- end
114
- end
@@ -1,55 +0,0 @@
1
- RSpec.describe Dry::Configurable::Config::Value do
2
- let(:klass) { Dry::Configurable::Config::Value }
3
- let(:config) { klass.new(name, value, processor) }
4
- let(:name) { :db }
5
- let(:value) { 'test' }
6
- let(:processor) { ->(v) { v } }
7
-
8
- describe '#initialize' do
9
- it 'coerces string name to symbol' do
10
- config = klass.new('db', value, processor)
11
-
12
- expect(config.name).to eq(:db)
13
- end
14
- end
15
-
16
- describe '#name' do
17
- subject! { config.name }
18
-
19
- it { is_expected.to eq(name) }
20
- end
21
-
22
- describe '#value' do
23
- subject! { config.value }
24
-
25
- context 'when value is not NONE' do
26
- it { is_expected.to eq(value) }
27
- end
28
-
29
- context 'when value is NONE' do
30
- let(:value) { klass::NONE }
31
-
32
- it { is_expected.to be(nil) }
33
- end
34
- end
35
-
36
- describe '#processor' do
37
- subject! { config.processor }
38
-
39
- it { is_expected.to eq(processor) }
40
- end
41
-
42
- describe '#none?' do
43
- subject! { config.none? }
44
-
45
- context 'when value is not NONE' do
46
- it { is_expected.to be(false) }
47
- end
48
-
49
- context 'when value is NONE' do
50
- let(:value) { klass::NONE }
51
-
52
- it { is_expected.to be(true) }
53
- end
54
- end
55
- end
@@ -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