File: dig_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (58 lines) | stat: -rw-r--r-- 2,044 bytes parent folder | download | duplicates (4)
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
require 'spec_helper'

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

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

  it 'returns a value from an array index via integer index' do
    expect(compile_to_catalog("notify { [testing].dig(0): }")).to have_resource('Notify[testing]')
  end

  it 'returns undef if given an undef key' do
  expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test-Undef-ing]')
    notify { "test-${type([testing].dig(undef))}-ing": }
    SOURCE
  end

  it 'returns undef if starting with undef' do
  expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test-Undef-ing]')
    notify { "test-${type(undef.dig(undef))}-ing": }
    SOURCE
  end

  it 'returns a value from an hash key via given key' do
    expect(compile_to_catalog("notify { {key => testing}.dig(key): }")).to have_resource('Notify[testing]')
  end

  it 'continues digging if result is an array' do
    expect(compile_to_catalog("notify { [nope, [testing]].dig(1, 0): }")).to have_resource('Notify[testing]')
  end

  it 'continues digging if result is a hash' do
    expect(compile_to_catalog("notify { [nope, {yes => testing}].dig(1, yes): }")).to have_resource('Notify[testing]')
  end

  it 'stops digging when step is undef' do
    expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[testing]')
    $result = [nope, {yes => testing}].dig(1, no, 2)
    notify { "test${result}ing": }
    SOURCE
  end

  it 'errors if step is neither Array nor Hash' do
    expect { compile_to_catalog(<<-SOURCE)}.to raise_error(/The given data does not contain a Collection at \[1, "yes"\], got 'String'/)
    $result = [nope, {yes => testing}].dig(1, yes, 2)
    notify { "test${result}ing": }
    SOURCE
  end

  it 'errors if not given a non Collection as the starting point' do
    expect { compile_to_catalog(<<-SOURCE)}.to raise_error(/'dig' parameter 'data' expects a value of type Undef or Collection, got String/)
    "hello".dig(1, yes, 2)
    SOURCE
  end

end