|
114 | 114 | end |
115 | 115 | end |
116 | 116 |
|
| 117 | + context 'when fail_on_missing option' do |
| 118 | + before { Config.reset } |
| 119 | + |
| 120 | + context 'is set to true' do |
| 121 | + before { Config.setup { |cfg| cfg.fail_on_missing = true } } |
| 122 | + |
| 123 | + it 'should raise an error when accessing a missing key' do |
| 124 | + config = Config.load_files("#{fixture_path}/empty1.yml") |
| 125 | + |
| 126 | + expect { config.not_existing_method }.to raise_error(KeyError) |
| 127 | + expect { config[:not_existing_method] }.to raise_error(KeyError) |
| 128 | + end |
| 129 | + |
| 130 | + it 'should raise an error when accessing a removed key' do |
| 131 | + config = Config.load_files("#{fixture_path}/empty1.yml") |
| 132 | + |
| 133 | + config.tmp_existing = 1337 |
| 134 | + expect(config.tmp_existing).to eq(1337) |
| 135 | + |
| 136 | + config.delete_field(:tmp_existing) |
| 137 | + expect { config.tmp_existing }.to raise_error(KeyError) |
| 138 | + expect { config[:tmp_existing] }.to raise_error(KeyError) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context 'is set to false' do |
| 143 | + before { Config.setup { |cfg| cfg.fail_on_missing = false } } |
| 144 | + |
| 145 | + it 'should return nil when accessing a missing key' do |
| 146 | + config = Config.load_files("#{fixture_path}/empty1.yml") |
| 147 | + |
| 148 | + expect(config.not_existing_method).to eq(nil) |
| 149 | + expect(config[:not_existing_method]).to eq(nil) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + context '#key? and #has_key? methods' do |
| 155 | + let(:config) { |
| 156 | + config = Config.load_files("#{fixture_path}/empty1.yml") |
| 157 | + config.existing = nil |
| 158 | + config.send('complex_value=', nil) |
| 159 | + config.send('even_more_complex_value==', nil) |
| 160 | + config.nested = Config.load_files("#{fixture_path}/empty2.yml") |
| 161 | + config.nested.existing = nil |
| 162 | + config |
| 163 | + } |
| 164 | + |
| 165 | + it 'should test if a value exists for a given key' do |
| 166 | + expect(config.key?(:not_existing)).to eq(false) |
| 167 | + expect(config.key?(:complex_value)).to eq(true) |
| 168 | + expect(config.key?('even_more_complex_value='.to_sym)).to eq(true) |
| 169 | + expect(config.key?(:nested)).to eq(true) |
| 170 | + expect(config.nested.key?(:not_existing)).to eq(false) |
| 171 | + expect(config.nested.key?(:existing)).to eq(true) |
| 172 | + end |
| 173 | + |
| 174 | + it 'should be sensible to key\'s class' do |
| 175 | + expect(config.key?(:existing)).to eq(true) |
| 176 | + expect(config.key?('existing')).to eq(false) |
| 177 | + end |
| 178 | + end |
117 | 179 | end |
0 commit comments