File: get_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (135 lines) | stat: -rw-r--r-- 4,431 bytes parent folder | download | duplicates (2)
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
129
130
131
132
133
134
135
require 'spec_helper'

require 'puppet_spec/compiler'
require 'matchers/resource'

describe 'the get function' do
  include PuppetSpec::Compiler
  include Matchers::Resource

  context 'returns undef value' do
    it 'when result is undef due to given undef' do
      expect( evaluate(source: "get(undef, '')")).to be_nil
    end

    it 'when result is undef due to navigation into undef' do
      expect( evaluate(source: "get(undef, '0')")).to be_nil
    end

    it 'when navigation results in explicit undef in an array' do
      expect( evaluate(source: "get([undef], '0')")).to be_nil
    end

    it 'when navigation results in explicit undef in a hash' do
      expect( evaluate(source: "get(a => undef, 'a')")).to be_nil
    end

    it 'when navigation references unavailable value in an array' do
      expect( evaluate(source: "get([1], '2')")).to be_nil
    end

    it 'when navigation references unavailable value in a hash' do
      expect( evaluate(source: "get(a => 43, 'b')")).to be_nil
    end
  end

  context 'returns default value' do
    it 'when result is undef due to given undef' do
      expect( evaluate(source: "get(undef, '', 'ok')")).to eql('ok')
    end

    it 'when result is undef due to navigation into undef' do
      expect( evaluate(source: "get(undef, '0', 'ok')")).to eql('ok')
    end

    it 'when navigation results in explicit undef in array' do
      expect( evaluate(source: "get([undef], '0', 'ok')")).to eql('ok')
    end

    it 'when navigation results in explicit undef in hash' do
      expect( evaluate(source: "get(a => undef, 'a', 'ok')")).to eql('ok')
    end

    it 'when navigation references unavailable value in an array' do
      expect( evaluate(source: "get([1], '2', 'ok')")).to eql('ok')
    end

    it 'when navigation references unavailable value in a hash' do
      expect( evaluate(source: "get(a => 43, 'b', 'ok')")).to eql('ok')
    end
  end

  it 'returns value if given empty navigation' do
    expect(evaluate(source: "get('ok', '')")).to eql('ok')
  end

  it 'navigates into array as given by navigation string' do
    expect( evaluate( source: "get(['nope', ['ok']], '1.0')")).to eql('ok')
  end

  it 'navigates into hash as given by navigation string' do
    expect( evaluate( source: "get(a => 'nope', b=> ['ok'], 'b.0')")).to eql('ok')
  end

  it 'navigates into hash with numeric key' do
    expect( evaluate( source: "get(a => 'nope', 0=> ['ok'], '0.0')")).to eql('ok')
  end

  it 'navigates into hash with numeric string but requires quoting' do
    expect( evaluate( source: "get(a => 'nope', '0'=> ['ok'], '\"0\".0')")).to eql('ok')
  end

  it 'can navigate a key with . when it is quoted' do
    expect(
      evaluate(
        variables: {'x' => {'a.b' => ['nope', ['ok']]}},
        source: "get($x, '\"a.b\".1.0')"
      )
    ).to eql('ok')
  end

  it 'an error is raised when navigating with string key into an array' do
    expect {
      evaluate(source: "get(['nope', ['ok']], '1.blue')")
    }.to raise_error(/The given data requires an Integer index/)
  end

  it 'calls a given block with EXPECTED_INTEGER_INDEX if navigating into array with string' do
    expect(evaluate(
             source:
               "get(['nope', ['ok']], '1.blue') |$error| {
                   if $error.issue_code =~ /^EXPECTED_INTEGER_INDEX$/ {'ok'} else { 'nope'}
                }"
           )).to eql('ok')
  end

  it 'calls a given block with EXPECTED_COLLECTION if navigating into something not Undef or Collection' do
    expect(evaluate(
             source:
               "get(['nope', /nah/], '1.blue') |$error| {
                  if $error.issue_code =~ /^EXPECTED_COLLECTION$/ {'ok'} else { 'nope'}
               }"
           )).to eql('ok')
  end

  context 'it does not pick the default value when undef is returned by error handling block' do
    it 'for "expected integer" case' do
      expect(evaluate(
               source: "get(['nope', ['ok']], '1.blue', 'nope') |$msg| { undef }"
             )).to be_nil
    end

    it 'for "expected collection" case' do
      expect(evaluate(
               source: "get(['nope', /nah/], '1.blue') |$msg| { undef }"
             )).to be_nil
    end
  end

  it 'does not call a given block if navigation string has syntax error' do
    expect {
      evaluate(source: "get(['nope', /nah/], '1....') |$msg| { fail('so sad') }")
    }.to raise_error(/Syntax error/)
  end

end