File: dig44_spec.rb

package info (click to toggle)
puppet-module-puppetlabs-stdlib 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,804 kB
  • sloc: ruby: 16,417; sh: 46; makefile: 2
file content (128 lines) | stat: -rw-r--r-- 3,709 bytes parent folder | download
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
require 'spec_helper'

describe 'dig44' do
  let(:data) do
    {
      'a' => {
        'g' => '2',
        'e' => [
          'f0',
          'f1',
          {
            'x' => {
              'y' => 'z',
            },
          },
          'f3',
        ],
      },
      'b' => true,
      'c' => false,
      'd' => '1',
      'e' => :undef,
      'f' => nil,
    }
  end

  let(:utf8_data) do
    {
      'ẵ' => {
        'в' => [
          '©',
          'ĝ',
          'に',
        ],
      },
    }
  end

  context 'with single values' do
    it 'exists' do
      is_expected.not_to be_nil
    end

    it 'requires two arguments' do
      is_expected.to run.with_params.and_raise_error(ArgumentError)
    end

    it 'fails if the data is not a structure' do
      is_expected.to run.with_params('test', []).and_raise_error(Puppet::Error)
    end

    it 'fails if the path is not an array' do
      is_expected.to run.with_params({}, '').and_raise_error(Puppet::Error)
    end

    it 'returns the value if the value is string' do
      is_expected.to run.with_params(data, ['d'], 'default').and_return('1')
    end

    it 'returns true if the value is true' do
      is_expected.to run.with_params(data, ['b'], 'default').and_return(true)
    end

    it 'returns false if the value is false' do
      is_expected.to run.with_params(data, ['c'], 'default').and_return(false)
    end

    it 'returns the default if the value is nil' do
      is_expected.to run.with_params(data, ['f'], 'default').and_return('default')
    end

    it 'returns the default if the value is :undef (same as nil)' do
      is_expected.to run.with_params(data, ['e'], 'default').and_return('default')
    end

    it 'returns the default if the path is not found' do
      is_expected.to run.with_params(data, ['missing'], 'default').and_return('default')
    end
  end

  context 'with structured values' do
    it 'is able to extract a deeply nested hash value' do
      is_expected.to run.with_params(data, ['a', 'g'], 'default').and_return('2')
    end

    it 'returns the default value if the path is too long' do
      is_expected.to run.with_params(data, ['a', 'g', 'c', 'd'], 'default').and_return('default')
    end

    it 'supports an array index (number) in the path' do
      is_expected.to run.with_params(data, ['a', 'e', 1], 'default').and_return('f1')
    end

    it 'supports an array index (string) in the path' do
      is_expected.to run.with_params(data, ['a', 'e', '1'], 'default').and_return('f1')
    end

    it 'returns the default value if an array index is not a number' do
      is_expected.to run.with_params(data, ['a', 'b', 'c'], 'default').and_return('default')
    end

    it 'returns the default value if and index is out of array length' do
      is_expected.to run.with_params(data, ['a', 'e', '5'], 'default').and_return('default')
    end

    it 'is able to path though both arrays and hashes' do
      is_expected.to run.with_params(data, ['a', 'e', '2', 'x', 'y'], 'default').and_return('z')
    end

    it 'returns "nil" if value is not found and no default value is provided' do
      is_expected.to run.with_params(data, ['a', '1']).and_return(nil)
    end
  end

  context 'with internationalization (i18N) values' do
    it 'is able to return a unicode character' do
      is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 0]).and_return('©')
    end

    it 'is able to return a utf8 character' do
      is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 1]).and_return('ĝ')
    end

    it 'is able to return a double byte character' do
      is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 2]).and_return('に')
    end
  end
end