summaryrefslogtreecommitdiff
path: root/spec/integration/parser/undef_param_spec.rb
blob: 3a9208ebf6f483e70e8528954ea12e0b3525d045 (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
require 'spec_helper'
require 'puppet_spec/compiler'

describe "Parameter passing" do
  include PuppetSpec::Compiler

  before :each do
    # DataBinding will be consulted before falling back to a default value,
    # but we aren't testing that here
    Puppet::DataBinding.indirection.stubs(:find)
  end

  def expect_the_message_to_be(message, node = Puppet::Node.new('the node'))
    catalog = compile_to_catalog(yield, node)
    catalog.resource('Notify', 'something')[:message].should == message
  end

  def expect_puppet_error(message, node = Puppet::Node.new('the node'))
    expect { compile_to_catalog(yield, node) }.to raise_error(Puppet::Error, message)
  end

  it "overrides the default when a value is given" do
    expect_the_message_to_be('2') do <<-MANIFEST
      define a($x=1) { notify { 'something': message => $x }}
      a {'a': x => 2}
      MANIFEST
    end
  end

  it "shadows an inherited variable with the default value when undef is passed" do
    expect_the_message_to_be('default') do <<-MANIFEST
      class a { $x = 'inherited' }
      class b($x='default') inherits a { notify { 'something': message => $x }}
      class { 'b': x => undef}
      MANIFEST
    end
  end

  it "uses a default value that comes from an inherited class when the parameter is undef" do
    expect_the_message_to_be('inherited') do <<-MANIFEST
      class a { $x = 'inherited' }
      class b($y=$x) inherits a { notify { 'something': message => $y }}
      class { 'b': y => undef}
      MANIFEST
    end
  end

  it "uses a default value that references another variable when the parameter is passed as undef" do
    expect_the_message_to_be('a') do <<-MANIFEST
        define a($a = $title) { notify { 'something': message => $a }}
        a {'a': a => undef}
      MANIFEST
    end
  end

  it "uses the default when 'undef' is given'" do
    expect_the_message_to_be('1') do <<-MANIFEST
        define a($x=1) { notify { 'something': message => $x }}
        a {'a': x => undef}
      MANIFEST
    end
  end

  it "uses the default when no parameter is provided" do
    expect_the_message_to_be('1') do <<-MANIFEST
        define a($x=1) { notify { 'something': message => $x }}
        a {'a': }
      MANIFEST
    end
  end

  it "uses a value of undef when the default is undef and no parameter is provided" do
    expect_the_message_to_be(true) do <<-MANIFEST
        define a($x=undef) { notify { 'something': message => $x == undef}}
        a {'a': }
    MANIFEST
    end
  end

  it "errors when no parameter is provided and there is no default" do
    expect_puppet_error(/^Must pass x to A\[a\].*/) do <<-MANIFEST
        define a($x) { notify { 'something': message => $x }}
        a {'a': }
    MANIFEST
    end
  end
end