summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/ast/selector_spec.rb
blob: f32310b9255c0dfe998c840619e19032b0b8930f (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../spec_helper'

describe Puppet::Parser::AST::Selector do
    before :each do
        @scope = Puppet::Parser::Scope.new()
    end

    describe "when evaluating" do

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

            @value1 = stub 'value1'
            @param1 = stub_everything 'param1'
            @param1.stubs(:safeevaluate).with(@scope).returns(@param1)
            @param1.stubs(:respond_to?).with(:downcase).returns(false)
            @value1.stubs(:param).returns(@param1)
            @value1.stubs(:value).returns(@value1)

            @value2 = stub 'value2'
            @param2 = stub_everything 'param2'
            @param2.stubs(:safeevaluate).with(@scope).returns(@param2)
            @param2.stubs(:respond_to?).with(:downcase).returns(false)
            @value2.stubs(:param).returns(@param2)
            @value2.stubs(:value).returns(@value2)

            @values = stub 'values', :instance_of? => true
            @values.stubs(:each).multiple_yields(@value1, @value2)

            @selector = Puppet::Parser::AST::Selector.new :param => @param, :values => @values
            @selector.stubs(:fail)
        end

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

            @selector.evaluate(@scope)
        end

        it "should scan each option" do
            @values.expects(:each).multiple_yields(@value1, @value2)

            @selector.evaluate(@scope)
        end

        describe "when scanning values" do
            it "should evaluate first matching option" do
                @param2.stubs(:evaluate_match).with { |*arg| arg[0] == "value" }.returns(true)
                @value2.expects(:safeevaluate).with(@scope)

                @selector.evaluate(@scope)
            end

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

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

            it "should evaluate the default option if none matched" do
                @param1.stubs(:is_a?).with(Puppet::Parser::AST::Default).returns(true)
                @value1.expects(:safeevaluate).with(@scope).returns(@param1)

                @selector.evaluate(@scope)
            end

            it "should return the default evaluated option if none matched" do
                result = stub 'result'
                @param1.stubs(:is_a?).with(Puppet::Parser::AST::Default).returns(true)
                @value1.stubs(:safeevaluate).returns(result)

                @selector.evaluate(@scope).should == result
            end

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

            it "should delegate matching to evaluate_match" do
                @param1.expects(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }

                @selector.evaluate(@scope)
            end

            it "should evaluate the matching param" do
                @param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)

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

                @selector.evaluate(@scope)
            end

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

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

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

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

                @selector.evaluate(@scope)
            end

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

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

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

            it "should fail if there is no default" do
                @selector.expects(:fail)

                @selector.evaluate(@scope)
            end
        end
    end
    describe "when converting to string" do
        it "should produce a string version of this selector" do
            values = Puppet::Parser::AST::ASTArray.new :children => [ Puppet::Parser::AST::ResourceParam.new(:param => "type", :value => "value", :add => false) ]
            param = Puppet::Parser::AST::Variable.new :value => "myvar"
            selector = Puppet::Parser::AST::Selector.new :param => param, :values => values
            selector.to_s.should == "$myvar ? { type => value }"
        end
    end
end