diff options
| author | Brice Figureau <brice-puppet@daysofwonder.com> | 2008-09-20 14:14:44 +0200 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2008-09-21 02:32:51 +1000 |
| commit | 7a3a38f58c099244c2a8b490f0b69c2fa63f3e16 (patch) | |
| tree | c5bf762a1f3a7f9f0fa0a23eb442290e929e5a61 /spec/unit/parser | |
| parent | 16793d221f95b2430260c38cd7c36bb8a5ef8d49 (diff) | |
| download | puppet-7a3a38f58c099244c2a8b490f0b69c2fa63f3e16.tar.gz puppet-7a3a38f58c099244c2a8b490f0b69c2fa63f3e16.tar.xz puppet-7a3a38f58c099244c2a8b490f0b69c2fa63f3e16.zip | |
Add rspec unit test for the append operator
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'spec/unit/parser')
| -rwxr-xr-x | spec/unit/parser/ast/vardef.rb | 47 | ||||
| -rwxr-xr-x | spec/unit/parser/lexer.rb | 3 | ||||
| -rwxr-xr-x | spec/unit/parser/parser.rb | 34 | ||||
| -rwxr-xr-x | spec/unit/parser/scope.rb | 37 |
4 files changed, 120 insertions, 1 deletions
diff --git a/spec/unit/parser/ast/vardef.rb b/spec/unit/parser/ast/vardef.rb new file mode 100755 index 000000000..6bd355c89 --- /dev/null +++ b/spec/unit/parser/ast/vardef.rb @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::VarDef do + before :each do + @scope = Puppet::Parser::Scope.new() + end + + describe "when evaluating" do + + it "should evaluate arguments" do + name = mock 'name' + value = mock 'value' + + name.expects(:safeevaluate).with(@scope) + value.expects(:safeevaluate).with(@scope) + + vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, + :line => nil + vardef.evaluate(@scope) + end + + it "should be in append=false mode if called without append" do + name = stub 'name', :safeevaluate => "var" + value = stub 'value', :safeevaluate => "1" + + @scope.expects(:setvar).with { |name,value,file,line,append| append == nil } + + vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, + :line => nil + vardef.evaluate(@scope) + end + + it "should call scope in append mode if append is true" do + name = stub 'name', :safeevaluate => "var" + value = stub 'value', :safeevaluate => "1" + + @scope.expects(:setvar).with { |name,value,file,line,append| append == true } + + vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, + :line => nil, :append => true + vardef.evaluate(@scope) + end + + end +end diff --git a/spec/unit/parser/lexer.rb b/spec/unit/parser/lexer.rb index fb666054d..fed1ade7d 100755 --- a/spec/unit/parser/lexer.rb +++ b/spec/unit/parser/lexer.rb @@ -135,7 +135,8 @@ describe Puppet::Parser::Lexer::TOKENS do :QMARK => '?', :BACKSLASH => '\\', :FARROW => '=>', - :PARROW => '+>' + :PARROW => '+>', + :APPENDS => '+=' }.each do |name, string| it "should have a token named #{name.to_s}" do Puppet::Parser::Lexer::TOKENS[name].should_not be_nil diff --git a/spec/unit/parser/parser.rb b/spec/unit/parser/parser.rb new file mode 100755 index 000000000..94b19be40 --- /dev/null +++ b/spec/unit/parser/parser.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe Puppet::Parser do + + AST = Puppet::Parser::AST + + before :each do + @parser = Puppet::Parser::Parser.new :environment => "development" + end + + describe "when parsing append operator" do + + it "should not raise syntax errors" do + lambda { @parser.parse("$var += something") }.should_not raise_error + end + + it "shouldraise syntax error on incomplete syntax " do + lambda { @parser.parse("$var += ") }.should raise_error + end + + it "should call AST::VarDef with append=true" do + AST::VarDef.expects(:new).with { |h| h[:append] == true } + @parser.parse("$var += 2") + end + + it "should work with arrays too" do + AST::VarDef.expects(:new).with { |h| h[:append] == true } + @parser.parse("$var += ['test']") + end + + end +end diff --git a/spec/unit/parser/scope.rb b/spec/unit/parser/scope.rb new file mode 100755 index 000000000..ec8ab6d7d --- /dev/null +++ b/spec/unit/parser/scope.rb @@ -0,0 +1,37 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe Puppet::Parser::Scope do + before :each do + @scope = Puppet::Parser::Scope.new() + @topscope = Puppet::Parser::Scope.new() + @scope.stubs(:parent).returns(@topscope) + end + + describe Puppet::Parser::Scope, "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) + end + + it "it should lookup current variable value" do + @scope.expects(:lookupvar).with("var").returns("2") + @scope.setvar("var","1",nil,nil,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) + @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) + @scope.lookupvar("var").should == [4,2] + end + + end +end |
