bidu-core_ext 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,143 @@
1
+ shared_examples 'a hash clean method' do |method|
2
+ context 'when hash has one level' do
3
+ let(:subject) do
4
+ { a: 1, b: nil, c: '', d: {} }
5
+ end
6
+
7
+ let(:expected) do
8
+ { a: 1 }
9
+ end
10
+
11
+ it 'cleans the hash from empty and nil values' do
12
+ expect(subject.send(method)).to eq(expected)
13
+ end
14
+ end
15
+
16
+ context 'when hash has two levels' do
17
+ let(:subject) do
18
+ { a: 1, c: '', d: { e: 1 }, f: { g: { b: nil } } }
19
+ end
20
+
21
+ let(:expected) do
22
+ { a: 1, d: { e: 1 } }
23
+ end
24
+
25
+ it 'cleans the hash from empty and nil values' do
26
+ expect(subject.send(method)).to eq(expected)
27
+ end
28
+ end
29
+
30
+ context 'when hash has many levels' do
31
+ let(:subject) do
32
+ { a: 1, d: { e: { k: { l: { m: { n: 1 } } } } }, f: { g: { h: { i: { j: { c: '' } } } } } }
33
+ end
34
+
35
+ let(:expected) do
36
+ { a: 1, d: { e: { k: { l: { m: { n: 1 } } } } } }
37
+ end
38
+
39
+ it 'cleans the hash from empty and nil values' do
40
+ expect(subject.send(method)).to eq(expected)
41
+ end
42
+ end
43
+
44
+ context 'when hash has one nil value and one valid value' do
45
+ let(:subject) do
46
+ { a: { b: 1, c: nil } }
47
+ end
48
+
49
+ let(:expected) do
50
+ { a: { b: 1 } }
51
+ end
52
+
53
+ it 'cleans the hash from empty and nil values' do
54
+ expect(subject.send(method)).to eq(expected)
55
+ end
56
+ end
57
+
58
+ context 'when hash has arrays' do
59
+ let(:subject) do
60
+ { a: [] }
61
+ end
62
+
63
+ let(:expected) do
64
+ {}
65
+ end
66
+
67
+ it 'cleans the hash from empty and nil values' do
68
+ expect(subject.send(method)).to eq(expected)
69
+ end
70
+ end
71
+
72
+ context 'when hash has arrays with hashes' do
73
+ let(:subject) do
74
+ { a: [{ c: nil }] }
75
+ end
76
+
77
+ let(:expected) do
78
+ {}
79
+ end
80
+
81
+ it 'cleans the hash from empty and nil values' do
82
+ expect(subject.send(method)).to eq(expected)
83
+ end
84
+ end
85
+
86
+ context 'when hash has arrays with hashes with valid values' do
87
+ let(:subject) do
88
+ { a: [{ c: 1 }] }
89
+ end
90
+
91
+ let(:expected) do
92
+ { a: [{ c: 1 }] }
93
+ end
94
+
95
+ it 'cleans the hash from empty and nil values' do
96
+ expect(subject.send(method)).to eq(expected)
97
+ end
98
+ end
99
+
100
+ context 'when hash has arrays with hashes with valid and invalid values' do
101
+ let(:subject) do
102
+ { a: [{ c: nil }, { d: 1 }] }
103
+ end
104
+
105
+ let(:expected) do
106
+ { a: [{ d: 1 }] }
107
+ end
108
+
109
+ it 'cleans the hash from empty and nil values' do
110
+ expect(subject.send(method)).to eq(expected)
111
+ end
112
+ end
113
+ end
114
+
115
+ shared_examples 'an array clean method' do |method|
116
+ context 'when array has one level' do
117
+ let(:subject) do
118
+ [1, nil, '', {}, []]
119
+ end
120
+
121
+ let(:expected) do
122
+ [1]
123
+ end
124
+
125
+ it 'cleans the hash from empty and nil values' do
126
+ expect(subject.send(method)).to eq(expected)
127
+ end
128
+ end
129
+
130
+ context 'when array has many levels' do
131
+ let(:subject) do
132
+ [1, nil, '', {}, [[[{ a: [[[[[[[]]]]]]] }]]], [[[[[[[2]]]]]]], [{ a: [2] }]]
133
+ end
134
+
135
+ let(:expected) do
136
+ [1, [[[[[[[2]]]]]]], [{ a: [2] }]]
137
+ end
138
+
139
+ it 'cleans the hash from empty and nil values' do
140
+ expect(subject.send(method)).to eq(expected)
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,92 @@
1
+ shared_examples 'a class with change_key method' do
2
+ let(:hash) do
3
+ { 'a' => 1, b: 2, c: { d: 3, e: 4 }, f: [{ g: 5 }, { h: 6 }] }
4
+ end
5
+
6
+ describe :change_keys do
7
+
8
+ it_behaves_like 'a method that is able to change keys', :change_keys
9
+ it 'does not affects the original hash' do
10
+ expect do
11
+ hash.change_keys(recursive: true) { |k| "foo_#{k}" }
12
+ end.not_to change { hash }
13
+ end
14
+ end
15
+
16
+ describe :change_keys! do
17
+ it_behaves_like 'a method that is able to change keys', :change_keys!
18
+
19
+ it 'affects the original hash' do
20
+ expect do
21
+ hash.change_keys!(recursive: true) { |k| "foo_#{k}" }
22
+ end.to change { hash }
23
+ end
24
+ end
25
+ end
26
+
27
+ shared_examples 'result is as expected' do
28
+ it 'is as expected' do
29
+ expect(result).to eq(expected)
30
+ end
31
+ end
32
+
33
+ shared_examples 'a method that is able to change keys' do |method|
34
+ let(:foo_sym_transformation) do
35
+ hash.public_send(method) { |k| "foo_#{k}".to_sym }
36
+ end
37
+
38
+ context 'with simple level hash' do
39
+ let(:hash) { { 'a' => 1, b: 2 } }
40
+
41
+ context 'with string transformation' do
42
+ let(:result) do
43
+ hash.public_send(method) { |k| "foo_#{k}" }
44
+ end
45
+ let(:expected) { { 'foo_a' => 1, 'foo_b' => 2 } }
46
+ it_behaves_like 'result is as expected'
47
+ end
48
+
49
+ context 'with symbol transformation' do
50
+ let(:result) { foo_sym_transformation }
51
+ let(:expected) { { foo_a: 1, foo_b: 2 } }
52
+ it_behaves_like 'result is as expected'
53
+ end
54
+ end
55
+
56
+ context 'with recursive hash' do
57
+ let(:hash) { { 'a' => 1, b: { c: 3, 'd' => 4 } } }
58
+ let(:result) { hash.public_send(method, options) { |k| "foo_#{k}" } }
59
+ let(:expected) do
60
+ { 'foo_a' => 1, 'foo_b' => { 'foo_c' => 3, 'foo_d' => 4 } }
61
+ end
62
+
63
+ context 'when no options are given' do
64
+ let(:options) { {} }
65
+ it_behaves_like 'result is as expected'
66
+ end
67
+
68
+ context 'when options are given' do
69
+ let(:options) { { recursive: recursive } }
70
+
71
+ context 'with recursion' do
72
+ let(:recursive) { true }
73
+ it_behaves_like 'result is as expected'
74
+ end
75
+
76
+ context 'without recursion' do
77
+ let(:recursive) { false }
78
+ let(:expected) { { 'foo_a' => 1, 'foo_b' => { c: 3, 'd' => 4 } } }
79
+ it_behaves_like 'result is as expected'
80
+ end
81
+ end
82
+ end
83
+
84
+ context 'with many many levels' do
85
+ let(:hash) { { a: 1, b: { c: 2, d: { e: 3, f: 4 } } } }
86
+ let(:expected) do
87
+ { foo_a: 1, foo_b: { foo_c: 2, foo_d: { foo_e: 3, foo_f: 4 } } }
88
+ end
89
+ let(:result) { foo_sym_transformation }
90
+ it_behaves_like 'result is as expected'
91
+ end
92
+ end
@@ -0,0 +1,43 @@
1
+ shared_examples 'a class with append_keys method' do
2
+ describe :prepend_to_keys do
3
+ it 'accepts block to change the keys' do
4
+ expect({ a: 1, 'b' => 2 }.prepend_to_keys('foo_')).to eq(foo_a: 1, 'foo_b' => 2)
5
+ end
6
+ it 'applies the block recursively' do
7
+ expect({ 'a' => 1, b: { c: 3, 'd' => 4 } }.prepend_to_keys('foo_')).to eq('foo_a' => 1, foo_b: { foo_c: 3, 'foo_d' => 4 })
8
+ end
9
+ it 'changes type when type option is passed' do
10
+ expect({ 'a' => 1, b: 2 }.prepend_to_keys('foo_', type: :string)).to eq('foo_a' => 1, 'foo_b' => 2)
11
+ end
12
+ it 'changes type when type option is passed' do
13
+ expect({ 'a' => 1, b: 2 }.prepend_to_keys('foo_', type: :symbol)).to eq(foo_a: 1, foo_b: 2)
14
+ end
15
+ it 'keep type when type option is passed as keep' do
16
+ expect({ 'a' => 1, b: 2 }.prepend_to_keys('foo_', type: :keep)).to eq('foo_a' => 1, foo_b: 2)
17
+ end
18
+ it 'applies to array as well' do
19
+ expect({ 'a' => 1, b: [{ c: 2 }, { d: 3 }] }.prepend_to_keys('foo_', type: :keep)).to eq('foo_a' => 1, foo_b: [{ foo_c: 2 }, { foo_d: 3 }])
20
+ end
21
+ end
22
+
23
+ describe :append_to_keys do
24
+ it 'accepts block to change the keys' do
25
+ expect({ a: 1, 'b' => 2 }.append_to_keys('_bar')).to eq(a_bar: 1, 'b_bar' => 2)
26
+ end
27
+ it 'applies the block recursively' do
28
+ expect({ 'a' => 1, b: { c: 3, 'd' => 4 } }.append_to_keys('_bar')).to eq('a_bar' => 1, b_bar: { c_bar: 3, 'd_bar' => 4 })
29
+ end
30
+ it 'changes type when type option is passed' do
31
+ expect({ 'a' => 1, b: 2 }.append_to_keys('_bar', type: :string)).to eq('a_bar' => 1, 'b_bar' => 2)
32
+ end
33
+ it 'changes type when type option is passed' do
34
+ expect({ 'a' => 1, b: 2 }.append_to_keys('_bar', type: :symbol)).to eq(a_bar: 1, b_bar: 2)
35
+ end
36
+ it 'keep type when type option is passed as keep' do
37
+ expect({ 'a' => 1, b: 2 }.append_to_keys('_bar', type: :keep)).to eq('a_bar' => 1, b_bar: 2)
38
+ end
39
+ it 'applies to array as well' do
40
+ expect({ 'a' => 1, b: [{ c: 2 }, { d: 3 }] }.append_to_keys('_bar', type: :keep)).to eq('a_bar' => 1, b_bar: [{ c_bar: 2 }, { d_bar: 3 }])
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,285 @@
1
+ shared_examples 'a class with camlize_keys method' do
2
+ describe :lower_camelize_keys do
3
+ let(:expected) { { inputKey: 'value' } }
4
+
5
+ context 'with underscore keys' do
6
+ let(:hash) { { input_key: 'value' } }
7
+
8
+ it 'converts the keys to lower camel case' do
9
+ expect(hash.lower_camelize_keys).to eq(expected)
10
+ end
11
+
12
+ it 'does not change original hash' do
13
+ expect { hash.lower_camelize_keys }.not_to change { hash }
14
+ end
15
+ end
16
+
17
+ context 'with camel case keys' do
18
+ let(:hash) { { InputKey: 'value' } }
19
+
20
+ it 'converts the keys to lower camel case' do
21
+ expect(hash.lower_camelize_keys).to eq(expected)
22
+ end
23
+ end
24
+
25
+ context 'with string keys' do
26
+ let(:expected) { { 'inputKey' => 'value' } }
27
+ let(:hash) { { 'InputKey' => 'value' } }
28
+
29
+ it 'converts the keys to lower camel case' do
30
+ expect(hash.lower_camelize_keys).to eq(expected)
31
+ end
32
+ end
33
+
34
+ context 'with deep keys change' do
35
+ let(:expected) { { inputKey: { innerKey: 'value' } } }
36
+ let(:hash) { { InputKey: { InnerKey: 'value' } } }
37
+
38
+ it 'converts the keys to lower camel case' do
39
+ expect(hash.lower_camelize_keys).to eq(expected)
40
+ end
41
+ end
42
+
43
+ context 'with array keys change' do
44
+ let(:expected) { { inputKey: [{ innerKey: 'value' }] } }
45
+ let(:hash) { { InputKey: [{ InnerKey: 'value' }] } }
46
+
47
+ it 'converts the keys to camle case' do
48
+ expect(hash.lower_camelize_keys).to eq(expected)
49
+ end
50
+ end
51
+
52
+ context 'without recursive options' do
53
+ context 'with deep keys change' do
54
+ let(:expected) { { inputKey: { InnerKey: 'value' } } }
55
+ let(:hash) { { InputKey: { InnerKey: 'value' } } }
56
+
57
+ it 'converts the keys to lower camel case' do
58
+ expect(hash.lower_camelize_keys(recursive: false)).to eq(expected)
59
+ end
60
+ end
61
+
62
+ context 'with array keys change' do
63
+ let(:expected) { { inputKey: [{ InnerKey: 'value' }] } }
64
+ let(:hash) { { InputKey: [{ InnerKey: 'value' }] } }
65
+
66
+ it 'converts the keys to camle case' do
67
+ expect(hash.lower_camelize_keys(recursive: false)).to eq(expected)
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ describe :lower_camelize_keys! do
74
+ let(:expected) { { inputKey: 'value' } }
75
+
76
+ context 'with underscore keys' do
77
+ let(:hash) { { input_key: 'value' } }
78
+
79
+ it 'converts the keys to lower camel case' do
80
+ expect(hash.lower_camelize_keys!).to eq(expected)
81
+ end
82
+
83
+ it 'does not change original hash' do
84
+ expect { hash.lower_camelize_keys! }.to change { hash }
85
+ end
86
+ end
87
+
88
+ context 'with camel case keys' do
89
+ let(:hash) { { InputKey: 'value' } }
90
+
91
+ it 'converts the keys to lower camel case' do
92
+ expect(hash.lower_camelize_keys!).to eq(expected)
93
+ end
94
+ end
95
+
96
+ context 'with string keys' do
97
+ let(:expected) { { 'inputKey' => 'value' } }
98
+ let(:hash) { { 'InputKey' => 'value' } }
99
+
100
+ it 'converts the keys to lower camel case' do
101
+ expect(hash.lower_camelize_keys).to eq(expected)
102
+ end
103
+ end
104
+
105
+ context 'with deep keys change' do
106
+ let(:expected) { { inputKey: { innerKey: 'value' } } }
107
+ let(:hash) { { InputKey: { InnerKey: 'value' } } }
108
+
109
+ it 'converts the keys to lower camel case' do
110
+ expect(hash.lower_camelize_keys!).to eq(expected)
111
+ end
112
+ end
113
+
114
+ context 'with array keys change' do
115
+ let(:expected) { { inputKey: [{ innerKey: 'value' }] } }
116
+ let(:hash) { { InputKey: [{ InnerKey: 'value' }] } }
117
+
118
+ it 'converts the keys to camle case' do
119
+ expect(hash.lower_camelize_keys!).to eq(expected)
120
+ end
121
+ end
122
+
123
+ context 'without recursive options' do
124
+ context 'with deep keys change' do
125
+ let(:expected) { { inputKey: { InnerKey: 'value' } } }
126
+ let(:hash) { { InputKey: { InnerKey: 'value' } } }
127
+
128
+ it 'converts the keys to lower camel case' do
129
+ expect(hash.lower_camelize_keys!(recursive: false)).to eq(expected)
130
+ end
131
+ end
132
+
133
+ context 'with array keys change' do
134
+ let(:expected) { { inputKey: [{ InnerKey: 'value' }] } }
135
+ let(:hash) { { InputKey: [{ InnerKey: 'value' }] } }
136
+
137
+ it 'converts the keys to camle case' do
138
+ expect(hash.lower_camelize_keys!(recursive: false)).to eq(expected)
139
+ end
140
+ end
141
+ end
142
+ end
143
+
144
+ describe :camelize_keys do
145
+ let(:expected) { { InputKey: 'value' } }
146
+
147
+ context 'with underscore keys' do
148
+ let(:hash) { { input_key: 'value' } }
149
+
150
+ it 'converts the keys to camel case' do
151
+ expect(hash.camelize_keys).to eq(expected)
152
+ end
153
+
154
+ it 'does not change original hash' do
155
+ expect { hash.camelize_keys }.not_to change { hash }
156
+ end
157
+ end
158
+
159
+ context 'with lower camel case keys' do
160
+ let(:hash) { { inputKey: 'value' } }
161
+
162
+ it 'converts the keys to camle case' do
163
+ expect(hash.camelize_keys).to eq(expected)
164
+ end
165
+ end
166
+
167
+ context 'with string keys' do
168
+ let(:expected) { { 'InputKey' => 'value' } }
169
+ let(:hash) { { 'inputKey' => 'value' } }
170
+
171
+ it 'converts the keys to lower camel case' do
172
+ expect(hash.camelize_keys).to eq(expected)
173
+ end
174
+ end
175
+
176
+ context 'with deep keys change' do
177
+ let(:expected) { { InputKey: { InnerKey: 'value' } } }
178
+ let(:hash) { { inputKey: { innerKey: 'value' } } }
179
+
180
+ it 'converts the keys to camle case' do
181
+ expect(hash.camelize_keys).to eq(expected)
182
+ end
183
+ end
184
+
185
+ context 'with array keys change' do
186
+ let(:expected) { { InputKey: [{ InnerKey: 'value' }] } }
187
+ let(:hash) { { inputKey: [{ innerKey: 'value' }] } }
188
+
189
+ it 'converts the keys to camle case' do
190
+ expect(hash.camelize_keys).to eq(expected)
191
+ end
192
+ end
193
+
194
+ context 'without recursive options' do
195
+ context 'with deep keys change' do
196
+ let(:expected) { { InputKey: { InnerKey: 'value' } } }
197
+ let(:hash) { { inputKey: { InnerKey: 'value' } } }
198
+
199
+ it 'converts the keys to lower camel case' do
200
+ expect(hash.camelize_keys(recursive: false)).to eq(expected)
201
+ end
202
+ end
203
+
204
+ context 'with array keys change' do
205
+ let(:expected) { { InputKey: [{ InnerKey: 'value' }] } }
206
+ let(:hash) { { inputKey: [{ InnerKey: 'value' }] } }
207
+
208
+ it 'converts the keys to camle case' do
209
+ expect(hash.camelize_keys(recursive: false)).to eq(expected)
210
+ end
211
+ end
212
+ end
213
+ end
214
+
215
+ describe :camelize_keys! do
216
+ let(:expected) { { InputKey: 'value' } }
217
+
218
+ context 'with underscore keys' do
219
+ let(:hash) { { input_key: 'value' } }
220
+
221
+ it 'converts the keys to camel case' do
222
+ expect(hash.camelize_keys!).to eq(expected)
223
+ end
224
+
225
+ it 'does not change original hash' do
226
+ expect { hash.camelize_keys! }.to change { hash }
227
+ end
228
+ end
229
+
230
+ context 'with lower camel case keys' do
231
+ let(:hash) { { inputKey: 'value' } }
232
+
233
+ it 'converts the keys to camle case' do
234
+ expect(hash.camelize_keys!).to eq(expected)
235
+ end
236
+ end
237
+
238
+ context 'with string keys' do
239
+ let(:expected) { { 'InputKey' => 'value' } }
240
+ let(:hash) { { 'inputKey' => 'value' } }
241
+
242
+ it 'converts the keys to lower camel case' do
243
+ expect(hash.camelize_keys!).to eq(expected)
244
+ end
245
+ end
246
+
247
+ context 'with deep keys change' do
248
+ let(:expected) { { InputKey: { InnerKey: 'value' } } }
249
+ let(:hash) { { inputKey: { innerKey: 'value' } } }
250
+
251
+ it 'converts the keys to camle case' do
252
+ expect(hash.camelize_keys!).to eq(expected)
253
+ end
254
+ end
255
+
256
+ context 'with array keys change' do
257
+ let(:expected) { { InputKey: [{ InnerKey: 'value' }] } }
258
+ let(:hash) { { inputKey: [{ innerKey: 'value' }] } }
259
+
260
+ it 'converts the keys to camle case' do
261
+ expect(hash.camelize_keys!).to eq(expected)
262
+ end
263
+ end
264
+
265
+ context 'without recursive options' do
266
+ context 'with deep keys change' do
267
+ let(:expected) { { InputKey: { InnerKey: 'value' } } }
268
+ let(:hash) { { inputKey: { InnerKey: 'value' } } }
269
+
270
+ it 'converts the keys to lower camel case' do
271
+ expect(hash.camelize_keys!(recursive: false)).to eq(expected)
272
+ end
273
+ end
274
+
275
+ context 'with array keys change' do
276
+ let(:hash) { { inputKey: [{ InnerKey: 'value' }] } }
277
+ let(:expected) { { InputKey: [{ InnerKey: 'value' }] } }
278
+
279
+ it 'converts the keys to camle case' do
280
+ expect(hash.camelize_keys!(recursive: false)).to eq(expected)
281
+ end
282
+ end
283
+ end
284
+ end
285
+ end