-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash_remapper_spec.rb
260 lines (215 loc) · 7.2 KB
/
hash_remapper_spec.rb
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# frozen_string_literal: true
RSpec.describe HashRemapper do
let(:original_hash) do
{
test: 42,
'data' => [
1,
2,
'string!'
],
ignore: :me,
nested: {
hash: :data,
really: {
deep: true
}
},
recursive: [
{ number: 21 },
{ number: 42 },
{ test: 13 }
]
}
end
context 'within static method call' do
it 'maps original keys to the new ones' do
# Test shallow mapping
new_hash = HashRemapper.remap(
original_hash,
test: :magic_number,
'data' => :data,
ignore: :dont_ignore,
nested: :internal
)
expect(new_hash).to eq(
magic_number: 42,
data: [1, 2, 'string!'],
dont_ignore: :me,
internal: { hash: :data, really: { deep: true } }
)
# Test deep mapping
new_hash = HashRemapper.remap(
original_hash,
test: :magic_number,
'data' => :data,
ignore: :dont_ignore,
_: [[:internal, :secret, :private], :nested]
)
expect(new_hash).to eq(
magic_number: 42,
data: [1, 2, 'string!'],
dont_ignore: :me,
internal: { secret: { private: { hash: :data, really: { deep: true } } } }
)
end
it 'auto-ignores all the skipped keys' do
new_hash = HashRemapper.remap(
original_hash,
test: :magic_number
)
expect(new_hash).to eq(magic_number: 42)
end
it 'passes trough all the skipped keys if pass_trough == true' do
new_hash = HashRemapper.remap(
original_hash,
true,
test: :magic_number
)
expected_hash = {
'data' => [1, 2, 'string!'],
ignore: :me,
magic_number: 42,
nested: { hash: :data, really: { deep: true } },
recursive: [{ number: 21 }, { number: 42 }, { test: 13 }]
}
expect(new_hash).to eq(expected_hash)
end
it 'preprocesses the value with a lambda' do
new_hash = HashRemapper.remap(
original_hash,
_: ->(_, __) { [:test, 21] }
)
expect(new_hash).to eq(test: 21)
end
it 'allows remap keys within preprocessing' do
new_hash = HashRemapper.remap(
original_hash,
test: ->(data, _) { [:magic_number, data.to_s] }
)
expect(new_hash).to eq(magic_number: '42')
end
it 'allows to keep data subsets only' do
new_hash = HashRemapper.remap(
original_hash,
'data' => ->(data, _) { ['data', data[0..1]] }
)
expect(new_hash).to eq('data' => [1, 2])
end
it 'allows to include data with the original keynames' do
new_hash = HashRemapper.remap(
original_hash,
test: :magic_number,
ignore: :ignore
)
expect(new_hash).to eq(magic_number: 42, ignore: :me)
end
it 'allows to use global context (original hash) to create composite fields' do
new_hash = HashRemapper.remap(
original_hash,
test: ->(data, context) { [:magic_number, data + context['data'][1]] }
)
expect(new_hash).to eq(magic_number: 44)
end
it 'merges values if the key already exists and supports #merge' do
# test with a shallow target
new_hash = HashRemapper.remap(
original_hash,
_: ->(_, __) { [:magic_number, { one: 1 }] },
__: ->(_, __) { [:magic_number, { two: 2 }] }
)
expect(new_hash).to eq(magic_number: { one: 1, two: 2 })
# test with a deep target
new_hash = HashRemapper.remap(
original_hash,
_: ->(_, __) { [[:magic_number, :nested_key], { one: 1 }] },
__: ->(_, __) { [[:magic_number, :nested_key], { two: 2 }] }
)
expect(new_hash).to eq(magic_number: { nested_key: { one: 1, two: 2 }})
end
it 'replaces values if the key already exists and doesn\'t support #merge' do
new_hash = HashRemapper.remap(
original_hash,
_: ->(_, __) { [:magic_number, 42] },
__: ->(_, __) { [:magic_number, 21] }
)
expect(new_hash).to eq(magic_number: 21)
end
it 'allows to assign static defaults through lambdas' do
new_hash = HashRemapper.remap(
original_hash,
_: ->(_, __) { [:magic_number, 21] }
)
expect(new_hash).to eq(magic_number: 21)
end
it 'allows to remap to the deep values within the context' do
new_hash = HashRemapper.remap(
original_hash,
_: [:magic_bool, {path: 'nested.really.deep'}]
)
expect(new_hash).to eq(magic_bool: true)
end
it 'allows to remap to the deep values recursively' do
# test falling into default nil on unstrict digging
expect(HashRemapper.remap(
original_hash,
_: [:magic_numbers, {path: 'recursive.*.number', strict: false}]
)).to eq(magic_numbers: [21, 42, nil])
# test default path falling into "*"
expect(HashRemapper.remap(
original_hash,
_: [:magic_numbers, {}]
)).to eq(magic_numbers: original_hash.deep_symbolize_keys)
# test failing on wrong path with strict digging
expect {HashRemapper.remap(
original_hash,
_: [:magic_numbers, {path: 'recursive.*.number'}]
)}.to raise_error(HashDigger::DigError)
# test falling into custom default on unstrict digging
expect(HashRemapper.remap(
original_hash,
_: [:magic_numbers, {path: 'recursive.*.number', strict: false, default: 3.14}]
)).to eq(magic_numbers: [21, 42, 3.14])
# test handling the result with custom lambda
expect(HashRemapper.remap(
original_hash,
_: [:magic_numbers, {path: 'recursive.*.number', strict: false, lambda: ->(result) { result.compact }}]
)).to eq(magic_numbers: [21, 42])
end
it 'allows to use native digging from v0.1.0 for backward compartability' do
new_hash = HashRemapper.remap(
original_hash,
test: [:magic_bool, %i[nested really deep]]
)
expect(new_hash).to eq(magic_bool: true)
end
it 'allows to create completely new keys' do
new_hash = HashRemapper.remap(
original_hash,
test: :magic_number,
absolutely_new_key: ->(_, __) { [:absolutely_new_key, 'shiny new value'] }
)
expect(new_hash).to eq(magic_number: 42, absolutely_new_key: 'shiny new value')
end
it 'allows to create nested target keys (mkdir-p-like behaviour)' do
# test deep target from shallow source
new_hash = HashRemapper.remap(
original_hash,
_: [[:nested, :new, :key], :test]
)
expect(new_hash).to eq({ nested: { new: { key: 42 } } })
# test deep target from deep source (old digging API v0.1.0)
new_hash = HashRemapper.remap(
original_hash,
_: [[:nested, :new, :key], [:nested, :really, :deep]]
)
expect(new_hash).to eq({ nested: { new: { key: true } } })
# test deep target from deep source (new digging API >= v0.2.0)
new_hash = HashRemapper.remap(
original_hash,
_: [[:new, :deeply, :nested, :value], {path: 'recursive.*.number', strict: false, default: 3.14}]
)
expect(new_hash).to eq({ new: { deeply: { nested: { value: [21, 42, 3.14] } } } })
end
end
end