summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/ast/casestatement_spec.rb
blob: e21190706139ef3cfa1bcf8fe1af388a2452b3fc (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
#!/usr/bin/env rspec
require 'spec_helper'

describe Puppet::Parser::AST::CaseStatement do
  before :each do
    @scope = Puppet::Parser::Scope.new
  end

  describe "when evaluating" do

    before :each do
      @test = stub 'test'
      @test.stubs(:safeevaluate).with(@scope).returns("value")

      @option1 = Puppet::Parser::AST::CaseOpt.new({})
      @option1.stubs(:eachopt)
      @option1.stubs(:default?).returns false
      @option2 = Puppet::Parser::AST::CaseOpt.new({})
      @option2.stubs(:eachopt)
      @option2.stubs(:default?).returns false

      @options = Puppet::Parser::AST::ASTArray.new(:children => [@option1, @option2])

      @casestmt = Puppet::Parser::AST::CaseStatement.new :test => @test, :options => @options
    end

    it "should evaluate test" do
      @test.expects(:safeevaluate).with(@scope)

      @casestmt.evaluate(@scope)
    end

    it "should scan each option" do
      @casestmt.evaluate(@scope)
    end

    describe "when scanning options" do
      before :each do
        @opval1 = stub_everything 'opval1'
        @option1.stubs(:eachopt).yields(@opval1)

        @opval2 = stub_everything 'opval2'
        @option2.stubs(:eachopt).yields(@opval2)
      end

      it "should evaluate each sub-option" do
        @option1.expects(:eachopt)
        @option2.expects(:eachopt)

        @casestmt.evaluate(@scope)
      end

      it "should evaluate first matching option" do
        @opval2.stubs(:evaluate_match).with { |*arg| arg[0] == "value" }.returns(true)
        @option2.expects(:safeevaluate).with(@scope)

        @casestmt.evaluate(@scope)
      end

      it "should return the first matching evaluated option" do
        @opval2.stubs(:evaluate_match).with { |*arg| arg[0] == "value" }.returns(true)
        @option2.stubs(:safeevaluate).with(@scope).returns(:result)

        @casestmt.evaluate(@scope).should == :result
      end

      it "should evaluate the default option if none matched" do
        @option1.stubs(:default?).returns(true)
        @option1.expects(:safeevaluate).with(@scope)

        @casestmt.evaluate(@scope)
      end

      it "should return the default evaluated option if none matched" do
        @option1.stubs(:default?).returns(true)
        @option1.stubs(:safeevaluate).with(@scope).returns(:result)

        @casestmt.evaluate(@scope).should == :result
      end

      it "should return nil if nothing matched" do
        @casestmt.evaluate(@scope).should be_nil
      end

      it "should match and set scope ephemeral variables" do
        @opval1.expects(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }

        @casestmt.evaluate(@scope)
      end

      it "should evaluate this regex option if it matches" do
        @opval1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)

        @option1.expects(:safeevaluate).with(@scope)

        @casestmt.evaluate(@scope)
      end

      it "should return this evaluated regex option if it matches" do
        @opval1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
        @option1.stubs(:safeevaluate).with(@scope).returns(:result)

        @casestmt.evaluate(@scope).should == :result
      end

      it "should unset scope ephemeral variables after option evaluation" do
        @scope.stubs(:ephemeral_level).returns(:level)
        @opval1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
        @option1.stubs(:safeevaluate).with(@scope).returns(:result)

        @scope.expects(:unset_ephemeral_var).with(:level)

        @casestmt.evaluate(@scope)
      end

      it "should not leak ephemeral variables even if evaluation fails" do
        @scope.stubs(:ephemeral_level).returns(:level)
        @opval1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
        @option1.stubs(:safeevaluate).with(@scope).raises

        @scope.expects(:unset_ephemeral_var).with(:level)

        lambda { @casestmt.evaluate(@scope) }.should raise_error
      end
    end

  end

  it "should match if any of the provided options evaluate as true" do
    ast = nil
    AST = Puppet::Parser::AST

    tests = {
      "one" => %w{a b c},
      "two" => %w{e f g}
    }
    options = tests.collect do |result, values|
      values = values.collect { |v| AST::Leaf.new :value => v }

      AST::CaseOpt.new(
        :value      => AST::ASTArray.new(:children => values),
        :statements => AST::Leaf.new(:value => result)
      )
    end
    options << AST::CaseOpt.new(
      :value      => AST::Default.new(:value => "default"), 
      :statements => AST::Leaf.new(:value => "default")
    )

    ast = nil
    param = AST::Variable.new(:value => "testparam")
    ast = AST::CaseStatement.new(:test => param, :options => options)

    tests.each do |should, values|
      values.each do |value|
        @scope = Puppet::Parser::Scope.new
        @scope.setvar("testparam", value)
        result = ast.evaluate(@scope)

        result.should == should
      end
    end
  end
end