3
require File.dirname(__FILE__) + '/../../../spec_helper'
5
describe Puppet::Parser::AST::Selector do
7
@scope = Puppet::Parser::Scope.new()
10
describe "when evaluating" do
14
@param.stubs(:safeevaluate).with(@scope).returns("value")
16
@value1 = stub 'value1'
17
@param1 = stub_everything 'param1'
18
@param1.stubs(:safeevaluate).with(@scope).returns(@param1)
19
@param1.stubs(:respond_to?).with(:downcase).returns(false)
20
@value1.stubs(:param).returns(@param1)
21
@value1.stubs(:value).returns(@value1)
23
@value2 = stub 'value2'
24
@param2 = stub_everything 'param2'
25
@param2.stubs(:safeevaluate).with(@scope).returns(@param2)
26
@param2.stubs(:respond_to?).with(:downcase).returns(false)
27
@value2.stubs(:param).returns(@param2)
28
@value2.stubs(:value).returns(@value2)
30
@values = stub 'values', :instance_of? => true
31
@values.stubs(:each).multiple_yields(@value1, @value2)
33
@selector = Puppet::Parser::AST::Selector.new :param => @param, :values => @values
34
@selector.stubs(:fail)
37
it "should evaluate param" do
38
@param.expects(:safeevaluate).with(@scope)
40
@selector.evaluate(@scope)
43
it "should downcase the evaluated param value if allowed" do
44
Puppet.stubs(:[]).with(:casesensitive).returns(false)
46
@param.stubs(:safeevaluate).with(@scope).returns(value)
48
value.expects(:downcase)
50
@selector.evaluate(@scope)
53
it "should scan each option" do
54
@values.expects(:each).multiple_yields(@value1, @value2)
56
@selector.evaluate(@scope)
59
describe "when scanning values" do
60
it "should evaluate first matching option" do
61
@param2.stubs(:evaluate_match).with { |*arg| arg[0] == "value" }.returns(true)
62
@value2.expects(:safeevaluate).with(@scope)
64
@selector.evaluate(@scope)
67
it "should return the first matching evaluated option" do
68
@param2.stubs(:evaluate_match).with { |*arg| arg[0] == "value" }.returns(true)
69
@value2.stubs(:safeevaluate).with(@scope).returns(:result)
71
@selector.evaluate(@scope).should == :result
74
it "should evaluate the default option if none matched" do
75
@param1.stubs(:is_a?).with(Puppet::Parser::AST::Default).returns(true)
76
@value1.expects(:safeevaluate).with(@scope).returns(@param1)
78
@selector.evaluate(@scope)
81
it "should return the default evaluated option if none matched" do
82
result = stub 'result'
83
@param1.stubs(:is_a?).with(Puppet::Parser::AST::Default).returns(true)
84
@value1.stubs(:safeevaluate).returns(result)
86
@selector.evaluate(@scope).should == result
89
it "should return nil if nothing matched" do
90
@selector.evaluate(@scope).should be_nil
93
it "should delegate matching to evaluate_match" do
94
@param1.expects(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }
96
@selector.evaluate(@scope)
99
it "should transmit the sensitive parameter to evaluate_match" do
100
Puppet.stubs(:[]).with(:casesensitive).returns(:sensitive)
101
@param1.expects(:evaluate_match).with { |*arg| arg[2][:sensitive] == :sensitive }
103
@selector.evaluate(@scope)
106
it "should transmit the AST file and line to evaluate_match" do
107
@selector.file = :file
108
@selector.line = :line
109
@param1.expects(:evaluate_match).with { |*arg| arg[2][:file] == :file and arg[2][:line] == :line }
111
@selector.evaluate(@scope)
115
it "should evaluate the matching param" do
116
@param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
118
@value1.expects(:safeevaluate).with(@scope)
120
@selector.evaluate(@scope)
123
it "should return this evaluated option if it matches" do
124
@param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
125
@value1.stubs(:safeevaluate).with(@scope).returns(:result)
127
@selector.evaluate(@scope).should == :result
130
it "should unset scope ephemeral variables after option evaluation" do
131
@param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
132
@value1.stubs(:safeevaluate).with(@scope).returns(:result)
134
@scope.expects(:unset_ephemeral_var)
136
@selector.evaluate(@scope)
139
it "should not leak ephemeral variables even if evaluation fails" do
140
@param1.stubs(:evaluate_match).with { |*arg| arg[0] == "value" and arg[1] == @scope }.returns(true)
141
@value1.stubs(:safeevaluate).with(@scope).raises
143
@scope.expects(:unset_ephemeral_var)
145
lambda { @selector.evaluate(@scope) }.should raise_error
148
it "should fail if there is no default" do
149
@selector.expects(:fail)
151
@selector.evaluate(@scope)