Sha256: d44b4232c74f04be1bbe6e5b8992d2424fc8e2766fe765ce2ceae4ac8f758667

Contents?: true

Size: 1.4 KB

Versions: 300

Compression:

Stored size: 1.4 KB

Contents

require 'spec_helper'

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

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

  context 'for an array' do
    it 'groups by item' do
      manifest = "notify { String(group_by([a, b, ab]) |$s| { $s.length }): }"
      expect(compile_to_catalog(manifest)).to have_resource("Notify[{1 => ['a', 'b'], 2 => ['ab']}]")
    end

    it 'groups by index, item' do
      manifest = "notify { String(group_by([a, b, ab]) |$i, $s| { $i%2 + $s.length }): }"
      expect(compile_to_catalog(manifest)).to have_resource("Notify[{1 => ['a'], 2 => ['b', 'ab']}]")
    end
  end

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

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

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

Version data entries

300 entries across 300 versions & 1 rubygems

Version Path
puppet-8.3.0 spec/unit/functions/group_by_spec.rb
puppet-8.3.0-x86-mingw32 spec/unit/functions/group_by_spec.rb
puppet-8.3.0-x64-mingw32 spec/unit/functions/group_by_spec.rb
puppet-8.3.0-universal-darwin spec/unit/functions/group_by_spec.rb
puppet-8.4.0 spec/unit/functions/group_by_spec.rb
puppet-8.4.0-x86-mingw32 spec/unit/functions/group_by_spec.rb
puppet-8.4.0-x64-mingw32 spec/unit/functions/group_by_spec.rb
puppet-8.4.0-universal-darwin spec/unit/functions/group_by_spec.rb
puppet-7.28.0 spec/unit/functions/group_by_spec.rb
puppet-7.28.0-x86-mingw32 spec/unit/functions/group_by_spec.rb
puppet-7.28.0-x64-mingw32 spec/unit/functions/group_by_spec.rb
puppet-7.28.0-universal-darwin spec/unit/functions/group_by_spec.rb
puppet-8.3.1 spec/unit/functions/group_by_spec.rb
puppet-8.3.1-x86-mingw32 spec/unit/functions/group_by_spec.rb
puppet-8.3.1-x64-mingw32 spec/unit/functions/group_by_spec.rb
puppet-8.3.1-universal-darwin spec/unit/functions/group_by_spec.rb
puppet-7.27.0 spec/unit/functions/group_by_spec.rb
puppet-7.27.0-x86-mingw32 spec/unit/functions/group_by_spec.rb
puppet-7.27.0-x64-mingw32 spec/unit/functions/group_by_spec.rb
puppet-7.27.0-universal-darwin spec/unit/functions/group_by_spec.rb