Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / dig44_spec.rb
1 require 'spec_helper'
2
3 describe 'dig44' do
4
5   let(:data) do
6     {
7         'a' => {
8             'g' => '2',
9             'e' => [
10                 'f0',
11                 'f1',
12                 {
13                     'x' => {
14                         'y' => 'z'
15                     }
16                 },
17                 'f3',
18             ]
19         },
20         'b' => true,
21         'c' => false,
22         'd' => '1',
23         'e' => :undef,
24         'f' => nil,
25     }
26   end
27
28   let(:utf8_data) do
29     {
30         'ẵ' => {
31             'в' => [
32                 '©',
33                 'ĝ',
34                 'に',
35             ]
36         }
37     }
38   end
39
40   context 'single values' do
41     it 'should exist' do
42       is_expected.not_to be_nil
43     end
44
45     it 'should require two arguments' do
46       is_expected.to run.with_params().and_raise_error(ArgumentError)
47     end
48
49     it 'should fail if the data is not a structure' do
50       is_expected.to run.with_params('test', []).and_raise_error(Puppet::Error)
51     end
52
53     it 'should fail if the path is not an array' do
54       is_expected.to run.with_params({}, '').and_raise_error(Puppet::Error)
55     end
56
57     it 'should return the value if the value is string' do
58       is_expected.to run.with_params(data, ['d'], 'default').and_return('1')
59     end
60
61     it 'should return true if the value is true' do
62       is_expected.to run.with_params(data, ['b'], 'default').and_return(true)
63     end
64
65     it 'should return false if the value is false' do
66       is_expected.to run.with_params(data, ['c'], 'default').and_return(false)
67     end
68
69     it 'should return the default if the value is nil' do
70       is_expected.to run.with_params(data, ['f'], 'default').and_return('default')
71     end
72
73     it 'should return the default if the value is :undef (same as nil)' do
74       is_expected.to run.with_params(data, ['e'], 'default').and_return('default')
75     end
76
77     it 'should return the default if the path is not found' do
78       is_expected.to run.with_params(data, ['missing'], 'default').and_return('default')
79     end
80   end
81
82   context 'structure values' do
83
84     it 'should be able to extract a deeply nested hash value' do
85       is_expected.to run.with_params(data, %w(a g), 'default').and_return('2')
86     end
87
88     it 'should return the default value if the path is too long' do
89       is_expected.to run.with_params(data, %w(a g c d), 'default').and_return('default')
90     end
91
92     it 'should support an array index (number) in the path' do
93       is_expected.to run.with_params(data, ['a', 'e', 1], 'default').and_return('f1')
94     end
95
96     it 'should support an array index (string) in the path' do
97       is_expected.to run.with_params(data, %w(a e 1), 'default').and_return('f1')
98     end
99
100     it 'should return the default value if an array index is not a number' do
101       is_expected.to run.with_params(data, %w(a b c), 'default').and_return('default')
102     end
103
104     it 'should return the default value if and index is out of array length' do
105       is_expected.to run.with_params(data, %w(a e 5), 'default').and_return('default')
106     end
107
108     it 'should be able to path though both arrays and hashes' do
109       is_expected.to run.with_params(data, %w(a e 2 x y), 'default').and_return('z')
110     end
111
112     it 'should return "nil" if value is not found and no default value is provided' do
113       is_expected.to run.with_params(data, %w(a 1)).and_return(nil)
114     end
115   end
116
117   context 'Internationalization (i18N) values' do
118
119     it 'should be able to return a unicode character' do
120       is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 0]).and_return('©')
121     end
122
123     it 'should be able to return a utf8 character' do
124       is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 1]).and_return('ĝ')
125     end
126
127     it 'should be able to return a double byte character' do
128       is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 2]).and_return('に')
129     end
130   end
131 end