blob: 239e8f591fe64419e7a567e5fa9488f826fdb459 (
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
|
require 'spec_helper'
describe Hashie::Extensions::StrictKeyAccess do
class StrictKeyAccessHash < Hash
include Hashie::Extensions::StrictKeyAccess
end
shared_examples_for 'StrictKeyAccess with valid key' do |options = {}|
before { pending_for(options[:pending]) } if options[:pending]
context 'set' do
let(:new_value) { 42 }
it('returns value') do
expect(instance.send(:[]=, valid_key, new_value)).to eq new_value
end
end
context 'access' do
it('returns value') do
expect(instance[valid_key]).to eq valid_value
end
end
context 'lookup' do
it('returns key') do
expect(instance.key(valid_value)).to eq valid_key
end
end
end
shared_examples_for 'StrictKeyAccess with invalid key' do |options = {}|
before { pending_for(options[:pending]) } if options[:pending]
context 'access' do
it('raises an error') do
# Formatting of the error message varies on Rubinius and ruby-head
expect { instance[invalid_key] }.to raise_error KeyError
end
end
context 'lookup' do
it('raises an error') do
# Formatting of the error message does not vary here because raised by StrictKeyAccess
expect { instance.key(invalid_value) }.to raise_error KeyError
end
end
end
shared_examples_for 'StrictKeyAccess raises KeyError instead of allowing defaults' do
context '#default' do
it 'raises an error' do
expect { instance.default(invalid_key) }
.to raise_error Hashie::Extensions::StrictKeyAccess::DefaultError
end
end
context '#default=' do
it 'raises an error' do
expect { instance.default = invalid_key }
.to raise_error Hashie::Extensions::StrictKeyAccess::DefaultError
end
end
context '#default_proc' do
it 'raises an error' do
expect { instance.default_proc }
.to raise_error Hashie::Extensions::StrictKeyAccess::DefaultError
end
end
context '#default_proc=' do
it 'raises an error' do
expect { instance.default_proc = proc {} }
.to raise_error Hashie::Extensions::StrictKeyAccess::DefaultError
end
end
end
let(:klass) { StrictKeyAccessHash }
let(:instance) { StrictKeyAccessHash.new(*initialization_args) }
let(:initialization_args) do
[
{ valid_key => valid_value }
]
end
let(:valid_key) { :abc }
let(:valid_value) { 'def' }
let(:invalid_key) { :mega }
let(:invalid_value) { 'death' }
context '.new' do
context 'no defaults at initialization' do
let(:initialization_args) { [] }
before do
instance.merge!(valid_key => valid_value)
end
it_behaves_like 'StrictKeyAccess with valid key'
it_behaves_like 'StrictKeyAccess with invalid key'
it_behaves_like 'StrictKeyAccess raises KeyError instead of allowing defaults'
end
context 'with defaults at initialization' do
before do
instance.merge!(valid_key => valid_value)
end
it_behaves_like 'StrictKeyAccess with valid key'
it_behaves_like 'StrictKeyAccess with invalid key'
it_behaves_like 'StrictKeyAccess raises KeyError instead of allowing defaults'
end
it_behaves_like 'StrictKeyAccess with invalid key'
it_behaves_like 'StrictKeyAccess raises KeyError instead of allowing defaults'
end
context '.[]' do
let(:instance) { StrictKeyAccessHash[*initialization_args] }
it_behaves_like 'StrictKeyAccess with valid key', pending: { engine: 'rbx' }
it_behaves_like 'StrictKeyAccess with invalid key', pending: { engine: 'rbx' }
it_behaves_like 'StrictKeyAccess raises KeyError instead of allowing defaults'
end
end
|