diff options
| author | Brice Figureau <brice-puppet@daysofwonder.com> | 2009-07-26 14:03:14 +0200 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-08-01 11:15:28 +1000 |
| commit | f357a9192647fc5d436d4c934b1912f59996389f (patch) | |
| tree | c6d94f9a4b284f95eb5c1a7927cd25995924d970 /spec/unit/parser | |
| parent | d40ef291191e627a91d6ec73b438853e2d3a73e8 (diff) | |
| download | puppet-f357a9192647fc5d436d4c934b1912f59996389f.tar.gz puppet-f357a9192647fc5d436d4c934b1912f59996389f.tar.xz puppet-f357a9192647fc5d436d4c934b1912f59996389f.zip | |
Implement ephemeral scope variables
Those variables have been created to be short lived and used mainly
to define temporary special variables.
They do not persist after a call to unset_ephemeral_var.
Also Scope#set_ephemeral_from can be used to promote a regexp
MatchData to ephemeral values.
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'spec/unit/parser')
| -rwxr-xr-x | spec/unit/parser/ast/vardef.rb | 4 | ||||
| -rwxr-xr-x | spec/unit/parser/scope.rb | 68 |
2 files changed, 63 insertions, 9 deletions
diff --git a/spec/unit/parser/ast/vardef.rb b/spec/unit/parser/ast/vardef.rb index 14de68923..9730ceedb 100755 --- a/spec/unit/parser/ast/vardef.rb +++ b/spec/unit/parser/ast/vardef.rb @@ -25,7 +25,7 @@ describe Puppet::Parser::AST::VarDef do name = stub 'name', :safeevaluate => "var" value = stub 'value', :safeevaluate => "1" - @scope.expects(:setvar).with { |name,value,file,line,append| append == nil } + @scope.expects(:setvar).with { |name,value,options| options[:append] == nil } vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, :line => nil @@ -36,7 +36,7 @@ describe Puppet::Parser::AST::VarDef do name = stub 'name', :safeevaluate => "var" value = stub 'value', :safeevaluate => "1" - @scope.expects(:setvar).with { |name,value,file,line,append| append == true } + @scope.expects(:setvar).with { |name,value,options| options[:append] == true } vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, :line => nil, :append => true diff --git a/spec/unit/parser/scope.rb b/spec/unit/parser/scope.rb index a9784e03b..641a3f946 100755 --- a/spec/unit/parser/scope.rb +++ b/spec/unit/parser/scope.rb @@ -111,24 +111,24 @@ describe Puppet::Parser::Scope do describe "when setvar is called with append=true" do it "should raise error if the variable is already defined in this scope" do - @scope.setvar("var","1",nil,nil,false) - lambda { @scope.setvar("var","1",nil,nil,true) }.should raise_error(Puppet::ParseError) + @scope.setvar("var","1", :append => false) + lambda { @scope.setvar("var","1", :append => true) }.should raise_error(Puppet::ParseError) end it "it should lookup current variable value" do @scope.expects(:lookupvar).with("var").returns("2") - @scope.setvar("var","1",nil,nil,true) + @scope.setvar("var","1", :append => true) end it "it should store the concatenated string '42'" do - @topscope.setvar("var","4",nil,nil,false) - @scope.setvar("var","2",nil,nil,true) + @topscope.setvar("var","4", :append => false) + @scope.setvar("var","2", :append => true) @scope.lookupvar("var").should == "42" end it "it should store the concatenated array [4,2]" do - @topscope.setvar("var",[4],nil,nil,false) - @scope.setvar("var",[2],nil,nil,true) + @topscope.setvar("var",[4], :append => false) + @scope.setvar("var",[2], :append => true) @scope.lookupvar("var").should == [4,2] end @@ -195,4 +195,58 @@ describe Puppet::Parser::Scope do Puppet::Parser::Scope.number?("0x89g").should be_nil end end + + describe "when using ephemeral variables" do + it "should store the variable value" do + @scope.setvar("1", :value, :ephemeral => true) + + @scope.lookupvar("1").should == :value + end + + it "should remove the variable value when unset_ephemeral_var is called" do + @scope.setvar("1", :value, :ephemeral => true) + @scope.stubs(:parent).returns(nil) + + @scope.unset_ephemeral_var + + @scope.lookupvar("1", false).should == :undefined + end + + it "should not remove classic variables when unset_ephemeral_var is called" do + @scope.setvar("myvar", :value1) + @scope.setvar("1", :value2, :ephemeral => true) + @scope.stubs(:parent).returns(nil) + + @scope.unset_ephemeral_var + + @scope.lookupvar("myvar", false).should == :value1 + end + end + + describe "when setting ephemeral vars from matches" do + before :each do + @match = stub 'match', :is_a? => true + @match.stubs(:[]).with(0).returns("this is a string") + @match.stubs(:captures).returns([]) + @scope.stubs(:setvar) + end + + it "should accept only MatchData" do + lambda { @scope.ephemeral_from("match") }.should raise_error + end + + it "should set $0 with the full match" do + @scope.expects(:setvar).with { |*arg| arg[0] == "0" and arg[1] == "this is a string" and arg[2][:ephemeral] } + + @scope.ephemeral_from(@match) + end + + it "should set every capture as ephemeral var" do + @match.stubs(:captures).returns([:capture1,:capture2]) + @scope.expects(:setvar).with { |*arg| arg[0] == "1" and arg[1] == :capture1 and arg[2][:ephemeral] } + @scope.expects(:setvar).with { |*arg| arg[0] == "2" and arg[1] == :capture2 and arg[2][:ephemeral] } + + @scope.ephemeral_from(@match) + end + end end |
