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
|
require 'puppet'
require 'spec_helper'
require 'puppet_spec/compiler'
require 'matchers/resource'
require 'shared_behaviours/iterative_functions'
describe 'the reduce method' do
include PuppetSpec::Compiler
include Matchers::Resource
before :all do
# enable switching back
@saved_parser = Puppet[:parser]
# These tests only work with future parser
end
after :all do
# switch back to original
Puppet[:parser] = @saved_parser
end
before :each do
node = Puppet::Node.new("floppy", :environment => 'production')
@compiler = Puppet::Parser::Compiler.new(node)
@scope = Puppet::Parser::Scope.new(@compiler)
@topscope = @scope.compiler.topscope
@scope.parent = @topscope
Puppet[:parser] = 'future'
end
context "should be callable as" do
it 'reduce on an array' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$b = $a.reduce |$memo, $x| { $memo + $x }
file { "/file_$b": ensure => present }
MANIFEST
expect(catalog).to have_resource("File[/file_6]").with_parameter(:ensure, 'present')
end
it 'reduce on an array with captures rest in lambda' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$b = $a.reduce |*$mx| { $mx[0] + $mx[1] }
file { "/file_$b": ensure => present }
MANIFEST
expect(catalog).to have_resource("File[/file_6]").with_parameter(:ensure, 'present')
end
it 'reduce on enumerable type' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = Integer[1,3]
$b = $a.reduce |$memo, $x| { $memo + $x }
file { "/file_$b": ensure => present }
MANIFEST
expect(catalog).to have_resource("File[/file_6]").with_parameter(:ensure, 'present')
end
it 'reduce on an array with start value' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = [1,2,3]
$b = $a.reduce(4) |$memo, $x| { $memo + $x }
file { "/file_$b": ensure => present }
MANIFEST
expect(catalog).to have_resource("File[/file_10]").with_parameter(:ensure, 'present')
end
it 'reduce on a hash' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = {a=>1, b=>2, c=>3}
$start = [ignored, 4]
$b = $a.reduce |$memo, $x| {['sum', $memo[1] + $x[1]] }
file { "/file_${$b[0]}_${$b[1]}": ensure => present }
MANIFEST
expect(catalog).to have_resource("File[/file_sum_6]").with_parameter(:ensure, 'present')
end
it 'reduce on a hash with start value' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = {a=>1, b=>2, c=>3}
$start = ['ignored', 4]
$b = $a.reduce($start) |$memo, $x| { ['sum', $memo[1] + $x[1]] }
file { "/file_${$b[0]}_${$b[1]}": ensure => present }
MANIFEST
expect(catalog).to have_resource("File[/file_sum_10]").with_parameter(:ensure, 'present')
end
end
it_should_behave_like 'all iterative functions argument checks', 'reduce'
end
|