-
-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathhelpers_spec.rb
More file actions
54 lines (43 loc) · 1.81 KB
/
helpers_spec.rb
File metadata and controls
54 lines (43 loc) · 1.81 KB
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
require 'spec_helper'
require_relative '../../../lib/config/integrations/helpers/helpers'
describe 'Helpers' do
subject { Class.new.send(:include, Config::Integrations::Helpers).new }
describe '#to_dotted_hash' do
context 'only the source is specified' do
it 'returns a hash with a nil key (default)' do
expect(subject.to_dotted_hash 3).to eq({nil => 3})
end
end
context 'with invalid arguments' do
it 'raises an error' do
expect { subject.to_dotted_hash(3, target: [1, 2, 7], namespace: 2) }
.to raise_error(ArgumentError, 'target must be a hash (given: Array)')
end
end
context 'all arguments specified' do
it 'returns a hash with the namespace as the key' do
expect(subject.to_dotted_hash(3, namespace: 'ns')).to eq({'ns' => 3})
end
it 'returns a new hash with a dotted string key prefixed with namespace' do
expect(subject.to_dotted_hash({hello: {cruel: 'world'}}, namespace: 'ns'))
.to eq({'ns.hello.cruel' => 'world'})
end
it 'returns the same hash as passed as a parameter' do
target = {something: 'inside'}
target_id = target.object_id
result = subject.to_dotted_hash(2, target: target, namespace: 'ns')
expect(result).to eq({:something => 'inside', 'ns' => 2})
expect(result.object_id).to eq target_id
end
it 'returns a hash when given a source with mixed nested types (hashes & arrays)' do
expect(subject.to_dotted_hash(
{hello: {evil: [:cruel, 'world', and: {dark: 'universe'}]}}, namespace: 'ns'))
.to eq(
{"ns.hello.evil.0" => :cruel,
"ns.hello.evil.1" => "world",
"ns.hello.evil.2.and.dark" => "universe"}
)
end
end
end
end