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