summaryrefslogtreecommitdiff
path: root/spec/unit/functions/map_spec.rb
blob: e1b09cf24b75b58eb824ad2d2d1148b7939cea05 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require 'puppet'
require 'spec_helper'
require 'puppet_spec/compiler'
require 'matchers/resource'

require 'shared_behaviours/iterative_functions'

describe 'the map method' do
  include PuppetSpec::Compiler
  include Matchers::Resource

  before :each do
    Puppet[:parser] = "future"
  end

  context "using future parser" do
    it 'map on an array (multiplying each value by 2)' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = [1,2,3]
        $a.map |$x|{ $x*2}.each |$v|{
          file { "/file_$v": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_2]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_4]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_6]").with_parameter(:ensure, 'present')
    end

    it 'map on an enumerable type (multiplying each value by 2)' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = Integer[1,3]
        $a.map |$x|{ $x*2}.each |$v|{
          file { "/file_$v": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_2]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_4]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_6]").with_parameter(:ensure, 'present')
    end

    it 'map on an integer (multiply each by 3)' do
      catalog = compile_to_catalog(<<-MANIFEST)
        3.map |$x|{ $x*3}.each |$v|{
          file { "/file_$v": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_0]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_3]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_6]").with_parameter(:ensure, 'present')
    end

    it 'map on a string' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = {a=>x, b=>y}
        "ab".map |$x|{$a[$x]}.each |$v|{
          file { "/file_$v": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_x]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_y]").with_parameter(:ensure, 'present')
    end

    it 'map on an array (multiplying value by 10 in even index position)' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = [1,2,3]
        $a.map |$i, $x|{ if $i % 2 == 0 {$x} else {$x*10}}.each |$v|{
          file { "/file_$v": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_20]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_3]").with_parameter(:ensure, 'present')
    end

    it 'map on a hash selecting keys' do
      catalog = compile_to_catalog(<<-MANIFEST)
      $a = {'a'=>1,'b'=>2,'c'=>3}
      $a.map |$x|{ $x[0]}.each |$k|{
          file { "/file_$k": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_a]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_b]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_c]").with_parameter(:ensure, 'present')
    end

    it 'map on a hash selecting keys - using two block parameters' do
      catalog = compile_to_catalog(<<-MANIFEST)
      $a = {'a'=>1,'b'=>2,'c'=>3}
      $a.map |$k,$v|{ file { "/file_$k": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_a]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_b]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_c]").with_parameter(:ensure, 'present')
    end

    it 'map on a hash using captures-last parameter' do
      catalog = compile_to_catalog(<<-MANIFEST)
      $a = {'a'=>present,'b'=>absent,'c'=>present}
      $a.map |*$kv|{ file { "/file_${kv[0]}": ensure => $kv[1] } }
      MANIFEST

      expect(catalog).to have_resource("File[/file_a]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_b]").with_parameter(:ensure, 'absent')
      expect(catalog).to have_resource("File[/file_c]").with_parameter(:ensure, 'present')
    end

    it 'each on a hash selecting value' do
      catalog = compile_to_catalog(<<-MANIFEST)
      $a = {'a'=>1,'b'=>2,'c'=>3}
      $a.map |$x|{ $x[1]}.each |$k|{ file { "/file_$k": ensure => present } }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_2]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_3]").with_parameter(:ensure, 'present')
    end

    it 'each on a hash selecting value - using two block parameters' do
      catalog = compile_to_catalog(<<-MANIFEST)
      $a = {'a'=>1,'b'=>2,'c'=>3}
      $a.map |$k,$v|{ file { "/file_$v": ensure => present } }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_2]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_3]").with_parameter(:ensure, 'present')
    end

    context "handles data type corner cases" do
      it "map gets values that are false" do
        catalog = compile_to_catalog(<<-MANIFEST)
          $a = [false,false]
          $a.map |$x| { $x }.each |$i, $v| {
            file { "/file_$i.$v": ensure => present }
          }
        MANIFEST

        expect(catalog).to have_resource("File[/file_0.false]").with_parameter(:ensure, 'present')
        expect(catalog).to have_resource("File[/file_1.false]").with_parameter(:ensure, 'present')
      end

      it "map gets values that are nil" do
        Puppet::Parser::Functions.newfunction(:nil_array, :type => :rvalue) do |args|
          [nil]
        end
        catalog = compile_to_catalog(<<-MANIFEST)
          $a = nil_array()
          $a.map |$x| { $x }.each |$i, $v| {
            file { "/file_$i.$v": ensure => present }
          }
        MANIFEST

        expect(catalog).to have_resource("File[/file_0.]").with_parameter(:ensure, 'present')
      end
    end

  it_should_behave_like 'all iterative functions argument checks', 'map'
  it_should_behave_like 'all iterative functions hash handling', 'map'
  end
end