File: partition_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 (40 lines) | stat: -rw-r--r-- 1,410 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
require 'spec_helper'

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

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

  context 'for an array' do
    it 'partitions by item' do
      manifest = "notify { String(partition(['', b, ab]) |$s| { $s.empty }): }"
      expect(compile_to_catalog(manifest)).to have_resource("Notify[[[''], ['b', 'ab']]]")
    end

    it 'partitions by index, item' do
      manifest = "notify { String(partition(['', b, ab]) |$i, $s| { $i == 2 or $s.empty }): }"
      expect(compile_to_catalog(manifest)).to have_resource("Notify[[['', 'ab'], ['b']]]")
    end
  end

  context 'for a hash' do
    it 'partitions by key-value pair' do
      manifest = "notify { String(partition(a => [1, 2], b => []) |$kv| { $kv[1].empty }): }"
      expect(compile_to_catalog(manifest)).to have_resource("Notify[[[['b', []]], [['a', [1, 2]]]]]")
    end

    it 'partitions by key, value' do
      manifest = "notify { String(partition(a => [1, 2], b => []) |$k, $v| { $v.empty }): }"
      expect(compile_to_catalog(manifest)).to have_resource("Notify[[[['b', []]], [['a', [1, 2]]]]]")
    end
  end

  context 'for a string' do
    it 'fails' do
      manifest = "notify { String(partition('something') |$s| { $s.empty }): }"
      expect { compile_to_catalog(manifest) }.to raise_error(Puppet::PreformattedError)
    end
  end
end