diff options
| author | Luke Kanies <luke@madstop.com> | 2008-12-02 16:26:54 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2008-12-02 16:26:54 -0600 |
| commit | 99a9b5a045af6f1c68619792a45603cbe450652d (patch) | |
| tree | 579963d464c0529d4e9250f937d495e415f1e867 /spec/unit/parser | |
| parent | f73e13e7464edab73443857d628602b89361c220 (diff) | |
| parent | 278bfe83015312292360f727d6532a143610db0d (diff) | |
| download | puppet-99a9b5a045af6f1c68619792a45603cbe450652d.tar.gz puppet-99a9b5a045af6f1c68619792a45603cbe450652d.tar.xz puppet-99a9b5a045af6f1c68619792a45603cbe450652d.zip | |
Merge branch '0.24.x'
Conflicts:
bin/puppetca
lib/puppet/type/group.rb
lib/puppet/type/tidy.rb
lib/puppet/util/settings.rb
Also edited the following files so tests will pass:
lib/puppet/type/component.rb
spec/unit/ssl/certificate_request.rb
spec/unit/type/computer.rb
spec/unit/type/mcx.rb
spec/unit/type/resources.rb
spec/unit/util/settings.rb
spec/unit/util/storage.rb
test/ral/type/zone.rb
Diffstat (limited to 'spec/unit/parser')
| -rw-r--r-- | spec/unit/parser/ast.rb | 37 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/arithmetic_operator.rb | 20 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/boolean_operator.rb | 18 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/collexpr.rb | 22 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/comparison_operator.rb | 60 | ||||
| -rw-r--r-- | spec/unit/parser/ast/function.rb | 77 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/resource_override.rb | 14 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/resource_reference.rb | 10 | ||||
| -rwxr-xr-x | spec/unit/parser/compiler.rb | 41 | ||||
| -rw-r--r-- | spec/unit/parser/functions.rb | 83 | ||||
| -rw-r--r-- | spec/unit/parser/functions/inline_template.rb | 59 | ||||
| -rw-r--r-- | spec/unit/parser/functions/template.rb | 62 | ||||
| -rwxr-xr-x | spec/unit/parser/lexer.rb | 94 | ||||
| -rwxr-xr-x | spec/unit/parser/parser.rb | 87 | ||||
| -rwxr-xr-x | spec/unit/parser/resource/reference.rb | 20 | ||||
| -rwxr-xr-x | spec/unit/parser/templatewrapper.rb | 57 |
16 files changed, 651 insertions, 110 deletions
diff --git a/spec/unit/parser/ast.rb b/spec/unit/parser/ast.rb new file mode 100644 index 000000000..513943725 --- /dev/null +++ b/spec/unit/parser/ast.rb @@ -0,0 +1,37 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/parser/ast' + +describe Puppet::Parser::AST do + + it "should have a doc accessor" do + ast = Puppet::Parser::AST.new({}) + ast.should respond_to(:doc) + end + + it "should have a use_docs accessor to indicate it wants documentation" do + ast = Puppet::Parser::AST.new({}) + ast.should respond_to(:use_docs) + end + + [ Puppet::Parser::AST::Collection, Puppet::Parser::AST::Definition, Puppet::Parser::AST::Else, + Puppet::Parser::AST::Function, Puppet::Parser::AST::HostClass, Puppet::Parser::AST::IfStatement, + Puppet::Parser::AST::Node, Puppet::Parser::AST::Resource, Puppet::Parser::AST::ResourceDefaults, + Puppet::Parser::AST::ResourceOverride, Puppet::Parser::AST::VarDef + ].each do |k| + it "#{k}.use_docs should return true" do + ast = k.new({}) + ast.use_docs.should be_true + end + end + + describe "when initializing" do + it "should store the doc argument if passed" do + ast = Puppet::Parser::AST.new(:doc => "documentation") + ast.doc.should == "documentation" + end + end + +end
\ No newline at end of file diff --git a/spec/unit/parser/ast/arithmetic_operator.rb b/spec/unit/parser/ast/arithmetic_operator.rb index 24d6ad47d..ce6d42faf 100755 --- a/spec/unit/parser/ast/arithmetic_operator.rb +++ b/spec/unit/parser/ast/arithmetic_operator.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe Puppet::Parser::AST::ArithmeticOperator do - AST = Puppet::Parser::AST + ast = Puppet::Parser::AST before :each do @scope = Puppet::Parser::Scope.new() @@ -18,19 +18,19 @@ describe Puppet::Parser::AST::ArithmeticOperator do rval = stub "rval" rval.expects(:safeevaluate).with(@scope).returns(2) - operator = AST::ArithmeticOperator.new :rval => rval, :operator => "+", :lval => lval + operator = ast::ArithmeticOperator.new :rval => rval, :operator => "+", :lval => lval operator.evaluate(@scope) end it "should fail for an unknown operator" do - lambda { operator = AST::ArithmeticOperator.new :lval => @one, :operator => "%", :rval => @two }.should raise_error + lambda { operator = ast::ArithmeticOperator.new :lval => @one, :operator => "%", :rval => @two }.should raise_error end it "should call Puppet::Parser::Scope.number?" do Puppet::Parser::Scope.expects(:number?).with(1).returns(1) Puppet::Parser::Scope.expects(:number?).with(2).returns(2) - AST::ArithmeticOperator.new(:lval => @one, :operator => "+", :rval => @two).evaluate(@scope) + ast::ArithmeticOperator.new(:lval => @one, :operator => "+", :rval => @two).evaluate(@scope) end @@ -38,7 +38,7 @@ describe Puppet::Parser::AST::ArithmeticOperator do it "should call ruby Numeric '#{op}'" do one = stub 'one' two = stub 'two' - operator = AST::ArithmeticOperator.new :lval => @one, :operator => op, :rval => @two + operator = ast::ArithmeticOperator.new :lval => @one, :operator => op, :rval => @two Puppet::Parser::Scope.stubs(:number?).with(1).returns(one) Puppet::Parser::Scope.stubs(:number?).with(2).returns(two) one.expects(:send).with(op,two) @@ -49,24 +49,24 @@ describe Puppet::Parser::AST::ArithmeticOperator do it "should work even with numbers embedded in strings" do two = stub 'two', :safeevaluate => "2" one = stub 'one', :safeevaluate => "1" - operator = AST::ArithmeticOperator.new :lval => two, :operator => "+", :rval => one + operator = ast::ArithmeticOperator.new :lval => two, :operator => "+", :rval => one operator.evaluate(@scope).should == 3 end it "should work even with floats" do two = stub 'two', :safeevaluate => 2.53 one = stub 'one', :safeevaluate => 1.80 - operator = AST::ArithmeticOperator.new :lval => two, :operator => "+", :rval => one + operator = ast::ArithmeticOperator.new :lval => two, :operator => "+", :rval => one operator.evaluate(@scope).should == 4.33 end it "should work for variables too" do @scope.expects(:lookupvar).with("one").returns(1) @scope.expects(:lookupvar).with("two").returns(2) - one = AST::Variable.new( :value => "one" ) - two = AST::Variable.new( :value => "two" ) + one = ast::Variable.new( :value => "one" ) + two = ast::Variable.new( :value => "two" ) - operator = AST::ArithmeticOperator.new :lval => one, :operator => "+", :rval => two + operator = ast::ArithmeticOperator.new :lval => one, :operator => "+", :rval => two operator.evaluate(@scope).should == 3 end diff --git a/spec/unit/parser/ast/boolean_operator.rb b/spec/unit/parser/ast/boolean_operator.rb index 7304e2a10..98e173bf0 100755 --- a/spec/unit/parser/ast/boolean_operator.rb +++ b/spec/unit/parser/ast/boolean_operator.rb @@ -4,12 +4,12 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe Puppet::Parser::AST::BooleanOperator do - AST = Puppet::Parser::AST + ast = Puppet::Parser::AST before :each do @scope = Puppet::Parser::Scope.new() - @true_ast = AST::Boolean.new( :value => true) - @false_ast = AST::Boolean.new( :value => false) + @true_ast = ast::Boolean.new( :value => true) + @false_ast = ast::Boolean.new( :value => false) end it "should evaluate left operand inconditionally" do @@ -18,7 +18,7 @@ describe Puppet::Parser::AST::BooleanOperator do rval = stub "rval", :safeevaluate => false rval.expects(:safeevaluate).never - operator = AST::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval + operator = ast::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval operator.evaluate(@scope) end @@ -26,7 +26,7 @@ describe Puppet::Parser::AST::BooleanOperator do lval = stub "lval", :safeevaluate => true rval = stub "rval", :safeevaluate => false rval.expects(:safeevaluate).with(@scope).returns(false) - operator = AST::BooleanOperator.new :rval => rval, :operator => "and", :lval => lval + operator = ast::BooleanOperator.new :rval => rval, :operator => "and", :lval => lval operator.evaluate(@scope) end @@ -34,20 +34,20 @@ describe Puppet::Parser::AST::BooleanOperator do lval = stub "lval", :safeevaluate => false rval = stub "rval", :safeevaluate => false rval.expects(:safeevaluate).with(@scope).returns(false) - operator = AST::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval + operator = ast::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval operator.evaluate(@scope) end it "should return true for false OR true" do - AST::BooleanOperator.new(:rval => @true_ast, :operator => "or", :lval => @false_ast).evaluate(@scope).should be_true + ast::BooleanOperator.new(:rval => @true_ast, :operator => "or", :lval => @false_ast).evaluate(@scope).should be_true end it "should return false for true AND false" do - AST::BooleanOperator.new(:rval => @true_ast, :operator => "and", :lval => @false_ast ).evaluate(@scope).should be_false + ast::BooleanOperator.new(:rval => @true_ast, :operator => "and", :lval => @false_ast ).evaluate(@scope).should be_false end it "should return true for true AND true" do - AST::BooleanOperator.new(:rval => @true_ast, :operator => "and", :lval => @true_ast ).evaluate(@scope).should be_true + ast::BooleanOperator.new(:rval => @true_ast, :operator => "and", :lval => @true_ast ).evaluate(@scope).should be_true end end diff --git a/spec/unit/parser/ast/collexpr.rb b/spec/unit/parser/ast/collexpr.rb index e5e6e0d7a..5f0ca941e 100755 --- a/spec/unit/parser/ast/collexpr.rb +++ b/spec/unit/parser/ast/collexpr.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe Puppet::Parser::AST::CollExpr do - AST = Puppet::Parser::AST + ast = Puppet::Parser::AST before :each do @scope = Puppet::Parser::Scope.new() @@ -19,12 +19,12 @@ describe Puppet::Parser::AST::CollExpr do end it "should evaluate both" do - collexpr = AST::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==") + collexpr = ast::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==") collexpr.evaluate(@scope) end it "should produce a textual representation and code of the expression" do - collexpr = AST::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==") + collexpr = ast::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==") result = collexpr.evaluate(@scope) result[0].should == "param_values.value = 'test2' and param_names.name = 'test1'" result[1].should be_an_instance_of(Proc) @@ -39,7 +39,7 @@ describe Puppet::Parser::AST::CollExpr do t.expects(:form=) end - collexpr = AST::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==", :form => true, :type => true) + collexpr = ast::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==", :form => true, :type => true) result = collexpr.evaluate(@scope) end @@ -50,25 +50,25 @@ describe Puppet::Parser::AST::CollExpr do end it "should evaluate like the original expression for ==" do - collexpr = AST::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper => "==") + collexpr = ast::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper => "==") collexpr.evaluate(@scope)[1].call(@resource).should === (@resource["test1"] == "test2") end it "should evaluate like the original expression for !=" do - collexpr = AST::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper => "!=") + collexpr = ast::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper => "!=") collexpr.evaluate(@scope)[1].call(@resource).should === (@resource["test1"] != "test2") end end it "should warn if this is an exported collection containing parenthesis (unsupported)" do - collexpr = AST::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==", :parens => true, :form => :exported) + collexpr = ast::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==", :parens => true, :form => :exported) Puppet.expects(:warning) collexpr.evaluate(@scope) end %w{and or}.each do |op| it "should raise an error if this is an exported collection with #{op} operator (unsupported)" do - collexpr = AST::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=> op, :form => :exported) + collexpr = ast::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=> op, :form => :exported) lambda { collexpr.evaluate(@scope) }.should raise_error(Puppet::ParseError) end end @@ -81,12 +81,12 @@ describe Puppet::Parser::AST::CollExpr do resource = mock 'resource' resource.expects(:[]).with("array").at_least(1).returns(["test1","test2","test3"]) - collexpr = AST::CollExpr.new(:test1 => array, :test2 => test1, :oper => "==") + collexpr = ast::CollExpr.new(:test1 => array, :test2 => test1, :oper => "==") collexpr.evaluate(@scope)[1].call(resource).should be_true end it "should raise an error for invalid operator" do - lambda { collexpr = AST::CollExpr.new(:oper=>">") }.should raise_error + lambda { collexpr = ast::CollExpr.new(:oper=>">") }.should raise_error end -end
\ No newline at end of file +end diff --git a/spec/unit/parser/ast/comparison_operator.rb b/spec/unit/parser/ast/comparison_operator.rb index dbea349f2..80e8307fa 100755 --- a/spec/unit/parser/ast/comparison_operator.rb +++ b/spec/unit/parser/ast/comparison_operator.rb @@ -5,8 +5,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe Puppet::Parser::AST::ComparisonOperator do before :each do @scope = Puppet::Parser::Scope.new() - @one = Puppet::Parser::AST::FlatString.new( :value => 1 ) - @two = Puppet::Parser::AST::FlatString.new( :value => 2 ) + @one = stub 'one', :safeevaluate => "1" + @two = stub 'two', :safeevaluate => "2" end it "should evaluate both branches" do @@ -19,34 +19,74 @@ describe Puppet::Parser::AST::ComparisonOperator do operator.evaluate(@scope) end + it "should convert arguments strings to numbers if they are" do + Puppet::Parser::Scope.expects(:number?).with("1").returns(1) + Puppet::Parser::Scope.expects(:number?).with("2").returns(2) + + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @one, :operator => "==", :rval => @two + operator.evaluate(@scope) + end + + %w{< > <= >= ==}.each do |oper| + it "should use string comparison #{oper} if operands are strings" do + lval = stub 'one', :safeevaluate => "one" + rval = stub 'two', :safeevaluate => "two" + Puppet::Parser::Scope.stubs(:number?).with("one").returns(nil) + Puppet::Parser::Scope.stubs(:number?).with("two").returns(nil) + + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => lval, :operator => oper, :rval => rval + operator.evaluate(@scope).should == "one".send(oper,"two") + end + end + + it "should fail with arguments of different types" do + lval = stub 'one', :safeevaluate => "one" + rval = stub 'two', :safeevaluate => "2" + Puppet::Parser::Scope.stubs(:number?).with("one").returns(nil) + Puppet::Parser::Scope.stubs(:number?).with("2").returns(2) + + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => lval, :operator => ">", :rval => rval + lambda { operator.evaluate(@scope) }.should raise_error(ArgumentError) + end + it "should fail for an unknown operator" do lambda { operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @one, :operator => "or", :rval => @two }.should raise_error end %w{< > <= >= ==}.each do |oper| it "should return the result of using '#{oper}' to compare the left and right sides" do - one = stub 'one', :safeevaluate => "1" - two = stub 'two', :safeevaluate => "2" - operator = Puppet::Parser::AST::ComparisonOperator.new :lval => one, :operator => oper, :rval => two + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @one, :operator => oper, :rval => @two + operator.evaluate(@scope).should == 1.send(oper,2) end end it "should return the result of using '!=' to compare the left and right sides" do - one = stub 'one', :safeevaluate => "1" - two = stub 'two', :safeevaluate => "2" - operator = Puppet::Parser::AST::ComparisonOperator.new :lval => one, :operator => '!=', :rval => two + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @one, :operator => '!=', :rval => @two + operator.evaluate(@scope).should == true end it "should work for variables too" do - @scope.expects(:lookupvar).with("one").returns(1) - @scope.expects(:lookupvar).with("two").returns(2) one = Puppet::Parser::AST::Variable.new( :value => "one" ) two = Puppet::Parser::AST::Variable.new( :value => "two" ) + + @scope.expects(:lookupvar).with("one").returns(1) + @scope.expects(:lookupvar).with("two").returns(2) operator = Puppet::Parser::AST::ComparisonOperator.new :lval => one, :operator => "<", :rval => two operator.evaluate(@scope).should == true end + # see ticket #1759 + %w{< > <= >=}.each do |oper| + it "should return the correct result of using '#{oper}' to compare 10 and 9" do + ten = stub 'one', :safeevaluate => "10" + nine = stub 'two', :safeevaluate => "9" + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => ten, :operator => oper, :rval => nine + + operator.evaluate(@scope).should == 10.send(oper,9) + end + end + end diff --git a/spec/unit/parser/ast/function.rb b/spec/unit/parser/ast/function.rb new file mode 100644 index 000000000..15420132f --- /dev/null +++ b/spec/unit/parser/ast/function.rb @@ -0,0 +1,77 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::Function do + before :each do + @scope = mock 'scope' + end + + describe "when initializing" do + it "should not fail if the function doesn't exist" do + Puppet::Parser::Functions.stubs(:function).returns(false) + + lambda{ Puppet::Parser::AST::Function.new :name => "dontexist" }.should_not raise_error(Puppet::ParseError) + + end + end + + describe "when evaluating" do + + it "should fail if the function doesn't exist" do + Puppet::Parser::Functions.stubs(:function).returns(false) + func = Puppet::Parser::AST::Function.new :name => "dontexist" + + lambda{ func.evaluate(@scope) }.should raise_error(Puppet::ParseError) + end + + it "should fail if the function is a statement used as rvalue" do + Puppet::Parser::Functions.stubs(:function).with("exist").returns(true) + Puppet::Parser::Functions.stubs(:rvalue?).with("exist").returns(false) + + func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :rvalue + + lambda{ func.evaluate(@scope) }.should raise_error(Puppet::ParseError, "Function 'exist' does not return a value") + end + + it "should fail if the function is an rvalue used as statement" do + Puppet::Parser::Functions.stubs(:function).with("exist").returns(true) + Puppet::Parser::Functions.stubs(:rvalue?).with("exist").returns(true) + + func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :statement + + lambda{ func.evaluate(@scope) }.should raise_error(Puppet::ParseError,"Function 'exist' must be the value of a statement") + end + + it "should evaluate its arguments" do + argument = stub 'arg' + Puppet::Parser::Functions.stubs(:function).with("exist").returns(true) + func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :statement, :arguments => argument + @scope.stubs(:function_exist) + + argument.expects(:safeevaluate).with(@scope).returns("argument") + + func.evaluate(@scope) + end + + it "should call the underlying ruby function" do + argument = stub 'arg', :safeevaluate => "nothing" + Puppet::Parser::Functions.stubs(:function).with("exist").returns(true) + func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :statement, :arguments => argument + + @scope.expects(:function_exist).with("nothing") + + func.evaluate(@scope) + end + + it "should return the ruby function return for rvalue functions" do + argument = stub 'arg', :safeevaluate => "nothing" + Puppet::Parser::Functions.stubs(:function).with("exist").returns(true) + func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :statement, :arguments => argument + @scope.stubs(:function_exist).with("nothing").returns("returning") + + func.evaluate(@scope).should == "returning" + end + + end +end diff --git a/spec/unit/parser/ast/resource_override.rb b/spec/unit/parser/ast/resource_override.rb index 3fbeb323c..c1520bf78 100755 --- a/spec/unit/parser/ast/resource_override.rb +++ b/spec/unit/parser/ast/resource_override.rb @@ -4,12 +4,12 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe Puppet::Parser::AST::ResourceOverride do - AST = Puppet::Parser::AST + ast = Puppet::Parser::AST before :each do @compiler = stub 'compiler' @scope = Puppet::Parser::Scope.new(:compiler => @compiler) - @params = AST::ASTArray.new({}) + @params = ast::ASTArray.new({}) @compiler.stubs(:add_override) end @@ -17,7 +17,7 @@ describe Puppet::Parser::AST::ResourceOverride do klass = stub 'klass', :title => "title", :type => "type" object = mock 'object' object.expects(:safeevaluate).with(@scope).returns(klass) - AST::ResourceOverride.new(:object => object, :params => @params ).evaluate(@scope) + ast::ResourceOverride.new(:object => object, :params => @params ).evaluate(@scope) end it "should tell the compiler to override the resource with our own" do @@ -25,13 +25,13 @@ describe Puppet::Parser::AST::ResourceOverride do klass = stub 'klass', :title => "title", :type => "one" object = mock 'object', :safeevaluate => klass - AST::ResourceOverride.new(:object => object , :params => @params).evaluate(@scope) + ast::ResourceOverride.new(:object => object , :params => @params).evaluate(@scope) end it "should return the overriden resource directly when called with one item" do klass = stub 'klass', :title => "title", :type => "one" object = mock 'object', :safeevaluate => klass - override = AST::ResourceOverride.new(:object => object , :params => @params).evaluate(@scope) + override = ast::ResourceOverride.new(:object => object , :params => @params).evaluate(@scope) override.should be_an_instance_of(Puppet::Parser::Resource) override.title.should == "title" override.type.should == "One" @@ -43,9 +43,9 @@ describe Puppet::Parser::AST::ResourceOverride do object = mock 'object', :safeevaluate => [klass1,klass2] - override = AST::ResourceOverride.new(:object => object , :params => @params).evaluate(@scope) + override = ast::ResourceOverride.new(:object => object , :params => @params).evaluate(@scope) override.should have(2).elements override.each {|o| o.should be_an_instance_of(Puppet::Parser::Resource) } end -end
\ No newline at end of file +end diff --git a/spec/unit/parser/ast/resource_reference.rb b/spec/unit/parser/ast/resource_reference.rb index e4b7c763b..ce2915a97 100755 --- a/spec/unit/parser/ast/resource_reference.rb +++ b/spec/unit/parser/ast/resource_reference.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe Puppet::Parser::AST::ResourceReference do - AST = Puppet::Parser::AST + ast = Puppet::Parser::AST before :each do @scope = Puppet::Parser::Scope.new() @@ -12,7 +12,7 @@ describe Puppet::Parser::AST::ResourceReference do def newref(title, type) title = stub 'title', :safeevaluate => title - ref = AST::ResourceReference.new(:type => type, :title => title) + ref = Puppet::Parser::AST::ResourceReference.new(:type => type, :title => title) end it "should evaluate correctly reference to builtin types" do @@ -45,7 +45,7 @@ describe Puppet::Parser::AST::ResourceReference do it "should return an array of reference if given an array of titles" do titles = mock 'titles', :safeevaluate => ["title1","title2"] - ref = AST::ResourceReference.new( :title => titles, :type => "Resource" ) + ref = ast::ResourceReference.new( :title => titles, :type => "Resource" ) ref.stubs(:qualified_type).with(@scope).returns("Resource") ref.evaluate(@scope).should have(2).elements @@ -53,11 +53,11 @@ describe Puppet::Parser::AST::ResourceReference do it "should qualify class of all titles for Class resource references" do titles = mock 'titles', :safeevaluate => ["title1","title2"] - ref = AST::ResourceReference.new( :title => titles, :type => "Class" ) + ref = ast::ResourceReference.new( :title => titles, :type => "Class" ) ref.expects(:qualified_class).with(@scope,"title1").returns("class") ref.expects(:qualified_class).with(@scope,"title2").returns("class") ref.evaluate(@scope) end -end
\ No newline at end of file +end diff --git a/spec/unit/parser/compiler.rb b/spec/unit/parser/compiler.rb index c70fe8e13..bf50d8790 100755 --- a/spec/unit/parser/compiler.rb +++ b/spec/unit/parser/compiler.rb @@ -2,6 +2,34 @@ require File.dirname(__FILE__) + '/../../spec_helper' +class CompilerTestResource + attr_accessor :builtin, :virtual, :evaluated, :type, :title + + def initialize(type, title) + @type = type + @title = title + end + + def ref + "%s[%s]" % [type.to_s.capitalize, title] + end + + def evaluated? + @evaluated + end + + def builtin? + @builtin + end + + def virtual? + @virtual + end + + def evaluate + end +end + describe Puppet::Parser::Compiler do before :each do @node = Puppet::Node.new "testnode" @@ -164,11 +192,12 @@ describe Puppet::Parser::Compiler do end it "should evaluate unevaluated resources" do - resource = stub 'notevaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => false, :virtual? => false + resource = CompilerTestResource.new(:file, "testing") + @compiler.add_resource(@scope, resource) # We have to now mark the resource as evaluated - resource.expects(:evaluate).with { |*whatever| resource.stubs(:evaluated?).returns true } + resource.expects(:evaluate).with { |*whatever| resource.evaluated = true } @compiler.compile end @@ -182,14 +211,14 @@ describe Puppet::Parser::Compiler do end it "should evaluate unevaluated resources created by evaluating other resources" do - resource = stub 'notevaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => false, :virtual? => false + resource = CompilerTestResource.new(:file, "testing") @compiler.add_resource(@scope, resource) - resource2 = stub 'created', :ref => "File[other]", :builtin? => false, :evaluated? => false, :virtual? => false + resource2 = CompilerTestResource.new(:file, "other") # We have to now mark the resource as evaluated - resource.expects(:evaluate).with { |*whatever| resource.stubs(:evaluated?).returns(true); @compiler.add_resource(@scope, resource2) } - resource2.expects(:evaluate).with { |*whatever| resource2.stubs(:evaluated?).returns(true) } + resource.expects(:evaluate).with { |*whatever| resource.evaluated = true; @compiler.add_resource(@scope, resource2) } + resource2.expects(:evaluate).with { |*whatever| resource2.evaluated = true } @compiler.compile diff --git a/spec/unit/parser/functions.rb b/spec/unit/parser/functions.rb new file mode 100644 index 000000000..fe449139d --- /dev/null +++ b/spec/unit/parser/functions.rb @@ -0,0 +1,83 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe Puppet::Parser::Functions do + + before(:each) do + end + + after(:each) do + # Rationale: + # our various tests will almost all register to Pupet::Parser::Functions + # a new function called "name". All tests are required to stub Puppet::Parser::Scope + # so that +no+ new real ruby method are defined. + # After each test, we want to leave the whole Puppet::Parser::Functions environment + # as it was before we were called, hence we call rmfunction (which might not succeed + # if the function hasn't been registered in the test). It is also important in this + # section to stub +remove_method+ here so that we don't pollute the scope. + Puppet::Parser::Scope.stubs(:remove_method) + begin + Puppet::Parser::Functions.rmfunction("name") + rescue + end + end + + describe "when calling newfunction" do + it "should create the function in the scope class" do + Puppet::Parser::Scope.expects(:define_method).with { |name,block| name == "function_name" } + + Puppet::Parser::Functions.newfunction("name", :type => :rvalue) + end + + it "should raise an error if the function already exists" do + Puppet::Parser::Scope.expects(:define_method).with { |name,block| name == "function_name" }.once + Puppet::Parser::Functions.newfunction("name", :type => :rvalue) + + lambda { Puppet::Parser::Functions.newfunction("name", :type => :rvalue) }.should raise_error + end + + it "should raise an error if the function type is not correct" do + Puppet::Parser::Scope.expects(:define_method).with { |name,block| name == "function_name" }.never + + lambda { Puppet::Parser::Functions.newfunction("name", :type => :unknown) }.should raise_error + end + end + + describe "when calling rmfunction" do + it "should remove the function in the scope class" do + Puppet::Parser::Scope.expects(:define_method).with { |name,block| name == "function_name" } + Puppet::Parser::Functions.newfunction("name", :type => :rvalue) + + Puppet::Parser::Scope.expects(:remove_method).with("function_name").once + + Puppet::Parser::Functions.rmfunction("name") + end + + it "should raise an error if the function doesn't exists" do + lambda { Puppet::Parser::Functions.rmfunction("name") }.should raise_error + end + end + + describe "when calling function to test function existance" do + + it "should return false if the function doesn't exist" do + Puppet::Parser::Functions.autoloader.stubs(:load) + + Puppet::Parser::Functions.function("name").should be_false + end + + it "should return it's name if the function exists" do + Puppet::Parser::Scope.expects(:define_method).with { |name,block| name == "function_name" } + Puppet::Parser::Functions.newfunction("name", :type => :rvalue) + + Puppet::Parser::Functions.function("name").should == "function_name" + end + + it "should try to autoload the function if it doesn't exist yet" do + Puppet::Parser::Functions.autoloader.expects(:load) + + Puppet::Parser::Functions.function("name") + end + end +end diff --git a/spec/unit/parser/functions/inline_template.rb b/spec/unit/parser/functions/inline_template.rb new file mode 100644 index 000000000..19e1a3b2a --- /dev/null +++ b/spec/unit/parser/functions/inline_template.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe "the inline_template function" do + + before :each do + @scope = Puppet::Parser::Scope.new() + end + + it "should exist" do + Puppet::Parser::Functions.function("inline_template").should == "function_inline_template" + end + + it "should create a TemplateWrapper when called" do + tw = stub_everything 'template_wrapper' + + Puppet::Parser::TemplateWrapper.expects(:new).returns(tw) + + @scope.function_inline_template("test") + end + + it "should pass the template string to TemplateWrapper.result" do + tw = stub_everything 'template_wrapper' + Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw) + + tw.expects(:result).with("test") + + @scope.function_inline_template("test") + end + + it "should return what TemplateWrapper.result returns" do + tw = stub_everything 'template_wrapper' + Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw) + + tw.expects(:result).returns("template contents evaluated") + + @scope.function_inline_template("test").should == "template contents evaluated" + end + + it "should concatenate template wrapper outputs for multiple templates" do + tw1 = stub_everything "template_wrapper1" + tw2 = stub_everything "template_wrapper2" + Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw1,tw2) + tw1.stubs(:result).returns("result1") + tw2.stubs(:result).returns("result2") + + @scope.function_inline_template(["1","2"]).should == "result1result2" + end + + it "should raise an error if the template raises an error" do + tw = stub_everything 'template_wrapper' + Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw) + tw.stubs(:result).raises + + lambda { @scope.function_inline_template("1") }.should raise_error(Puppet::ParseError) + end + +end
\ No newline at end of file diff --git a/spec/unit/parser/functions/template.rb b/spec/unit/parser/functions/template.rb new file mode 100644 index 000000000..8fc64d0c3 --- /dev/null +++ b/spec/unit/parser/functions/template.rb @@ -0,0 +1,62 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe "the template function" do + + before :each do + @scope = Puppet::Parser::Scope.new() + end + + it "should exist" do + Puppet::Parser::Functions.function("template").should == "function_template" + end + + it "should create a TemplateWrapper when called" do + tw = stub_everything 'template_wrapper' + + Puppet::Parser::TemplateWrapper.expects(:new).returns(tw) + + @scope.function_template("test") + end + + it "should give the template filename to the TemplateWrapper" do + tw = stub_everything 'template_wrapper' + Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw) + + tw.expects(:file=).with("test") + + @scope.function_template("test") + end + + it "should return what TemplateWrapper.result returns" do + tw = stub_everything 'template_wrapper' + Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw) + tw.stubs(:file=).with("test") + + tw.expects(:result).returns("template contents evaluated") + + @scope.function_template("test").should == "template contents evaluated" + end + + it "should concatenate template wrapper outputs for multiple templates" do + tw1 = stub_everything "template_wrapper1" + tw2 = stub_everything "template_wrapper2" + Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw1,tw2) + tw1.stubs(:file=).with("1") + tw2.stubs(:file=).with("2") + tw1.stubs(:result).returns("result1") + tw2.stubs(:result).returns("result2") + + @scope.function_template(["1","2"]).should == "result1result2" + end + + it "should raise an error if the template raises an error" do + tw = stub_everything 'template_wrapper' + Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw) + tw.stubs(:result).raises + + lambda { @scope.function_template("1") }.should raise_error(Puppet::ParseError) + end + +end
\ No newline at end of file diff --git a/spec/unit/parser/lexer.rb b/spec/unit/parser/lexer.rb index d62d99258..24c34632f 100755 --- a/spec/unit/parser/lexer.rb +++ b/spec/unit/parser/lexer.rb @@ -30,7 +30,7 @@ describe Puppet::Parser::Lexer::Token do @token = Puppet::Parser::Lexer::Token.new(%r{something}, :NAME) end - [:regex, :name, :string, :skip, :incr_line, :skip_text].each do |param| + [:regex, :name, :string, :skip, :incr_line, :skip_text, :accumulate].each do |param| it "should have a #{param.to_s} reader" do @token.should be_respond_to(param) end @@ -208,6 +208,42 @@ describe Puppet::Parser::Lexer::TOKENS do end end +describe Puppet::Parser::Lexer::TOKENS[:CLASSNAME] do + before { @token = Puppet::Parser::Lexer::TOKENS[:CLASSNAME] } + + it "should match against lower-case alpha-numeric terms separated by double colons" do + @token.regex.should =~ "one::two" + end + + it "should match against many lower-case alpha-numeric terms separated by double colons" do + @token.regex.should =~ "one::two::three::four::five" + end + + it "should match against lower-case alpha-numeric terms prefixed by double colons" do + @token.regex.should =~ "::one" + end +end + +describe Puppet::Parser::Lexer::TOKENS[:CLASSREF] do + before { @token = Puppet::Parser::Lexer::TOKENS[:CLASSREF] } + + it "should match against single upper-case alpha-numeric terms" do + @token.regex.should =~ "One" + end + + it "should match against upper-case alpha-numeric terms separated by double colons" do + @token.regex.should =~ "One::Two" + end + + it "should match against many upper-case alpha-numeric terms separated by double colons" do + @token.regex.should =~ "One::Two::Three::Four::Five" + end + + it "should match against upper-case alpha-numeric terms prefixed by double colons" do + @token.regex.should =~ "::One" + end +end + describe Puppet::Parser::Lexer::TOKENS[:NAME] do before { @token = Puppet::Parser::Lexer::TOKENS[:NAME] } @@ -285,6 +321,14 @@ describe Puppet::Parser::Lexer::TOKENS[:COMMENT] do it "should be marked to get skipped" do @token.skip?.should be_true end + + it "should be marked to accumulate" do + @token.accumulate?.should be_true + end + + it "'s block should return the comment without the #" do + @token.convert(@lexer,"# this is a comment")[1].should == "this is a comment" + end end describe Puppet::Parser::Lexer::TOKENS[:MLCOMMENT] do @@ -313,6 +357,16 @@ describe Puppet::Parser::Lexer::TOKENS[:MLCOMMENT] do match[1].should == " first " end + it "should be marked to accumulate" do + @token.accumulate?.should be_true + end + + it "'s block should return the comment without the comment marks" do + @lexer.stubs(:line=).with(0) + + @token.convert(@lexer,"/* this is a comment */")[1].should == "this is a comment" + end + end describe Puppet::Parser::Lexer::TOKENS[:RETURN] do @@ -383,6 +437,43 @@ describe Puppet::Parser::Lexer::TOKENS[:VARIABLE] do end end +describe Puppet::Parser::Lexer, "when lexing comments" do + before { @lexer = Puppet::Parser::Lexer.new } + + it "should accumulate token in munge_token" do + token = stub 'token', :skip => true, :accumulate? => true, :incr_line => nil, :skip_text => false + + token.stubs(:convert).with(@lexer, "# this is a comment").returns([token, " this is a comment"]) + @lexer.munge_token(token, "# this is a comment") + @lexer.munge_token(token, "# this is a comment") + + @lexer.getcomment.should == " this is a comment\n this is a comment\n" + end + + it "should add a new comment stack level on LBRACE" do + @lexer.string = "{" + + @lexer.expects(:commentpush) + + @lexer.fullscan + end + + it "should return the current comments on getcomment" do + @lexer.string = "# comment" + @lexer.fullscan + + @lexer.getcomment.should == "comment\n" + end + + it "should discard the previous comments on blank line" do + @lexer.string = "# 1\n\n# 2" + @lexer.fullscan + + @lexer.getcomment.should == "2\n" + end + +end + # FIXME: We need to rewrite all of these tests, but I just don't want to take the time right now. describe "Puppet::Parser::Lexer in the old tests" do before { @lexer = Puppet::Parser::Lexer.new } @@ -538,6 +629,7 @@ describe "Puppet::Parser::Lexer in the old tests" do @lexer.fullscan[0].should == [:CLASSREF, foo] end end + end require 'puppettest/support/utils' diff --git a/spec/unit/parser/parser.rb b/spec/unit/parser/parser.rb index 077f93d98..b764dee97 100755 --- a/spec/unit/parser/parser.rb +++ b/spec/unit/parser/parser.rb @@ -4,11 +4,11 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe Puppet::Parser do - AST = Puppet::Parser::AST + ast = Puppet::Parser::AST before :each do @parser = Puppet::Parser::Parser.new :environment => "development" - @true_ast = AST::Boolean.new :value => true + @true_ast = Puppet::Parser::AST::Boolean.new :value => true end describe "when parsing append operator" do @@ -21,13 +21,13 @@ describe Puppet::Parser 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 } + 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 } + ast::VarDef.expects(:new).with { |h| h[:append] == true } @parser.parse("$var += ['test']") end @@ -35,22 +35,21 @@ describe Puppet::Parser do describe Puppet::Parser, "when parsing 'if'" do it "not, it should create the correct ast objects" do - AST::Not.expects(:new).with { |h| h[:value].is_a?(AST::Boolean) } + ast::Not.expects(:new).with { |h| h[:value].is_a?(ast::Boolean) } @parser.parse("if ! true { $var = 1 }") - end it "boolean operation, it should create the correct ast objects" do - AST::BooleanOperator.expects(:new).with { - |h| h[:rval].is_a?(AST::Boolean) and h[:lval].is_a?(AST::Boolean) and h[:operator]=="or" + ast::BooleanOperator.expects(:new).with { + |h| h[:rval].is_a?(ast::Boolean) and h[:lval].is_a?(ast::Boolean) and h[:operator]=="or" } @parser.parse("if true or true { $var = 1 }") end it "comparison operation, it should create the correct ast objects" do - AST::ComparisonOperator.expects(:new).with { - |h| h[:lval].is_a?(AST::Name) and h[:rval].is_a?(AST::Name) and h[:operator]=="<" + ast::ComparisonOperator.expects(:new).with { + |h| h[:lval].is_a?(ast::Name) and h[:rval].is_a?(ast::Name) and h[:operator]=="<" } @parser.parse("if 1 < 2 { $var = 1 }") @@ -60,14 +59,15 @@ describe Puppet::Parser do describe Puppet::Parser, "when parsing if complex expressions" do it "should create a correct ast tree" do - AST::ComparisonOperator.expects(:new).with { - |h| h[:rval].is_a?(AST::Name) and h[:lval].is_a?(AST::Name) and h[:operator]==">" - }.returns("whatever") - AST::ComparisonOperator.expects(:new).with { - |h| h[:rval].is_a?(AST::Name) and h[:lval].is_a?(AST::Name) and h[:operator]=="==" - }.returns("whatever") - AST::BooleanOperator.expects(:new).with { - |h| h[:rval]=="whatever" and h[:lval]=="whatever" and h[:operator]=="and" + aststub = stub_everything 'ast' + ast::ComparisonOperator.expects(:new).with { + |h| h[:rval].is_a?(ast::Name) and h[:lval].is_a?(ast::Name) and h[:operator]==">" + }.returns(aststub) + ast::ComparisonOperator.expects(:new).with { + |h| h[:rval].is_a?(ast::Name) and h[:lval].is_a?(ast::Name) and h[:operator]=="==" + }.returns(aststub) + ast::BooleanOperator.expects(:new).with { + |h| h[:rval]==aststub and h[:lval]==aststub and h[:operator]=="and" } @parser.parse("if (1 > 2) and (1 == 2) { $var = 1 }") end @@ -88,10 +88,10 @@ describe Puppet::Parser do lambda { @parser.parse('exec { test: param => File["a","b"] }') }.should_not raise_error end - it "should create an AST::ResourceReference" do - AST::Resource.stubs(:new) - AST::ResourceReference.expects(:new).with { |arg| - arg[:line]==1 and arg[:type]=="File" and arg[:title].is_a?(AST::ASTArray) + it "should create an ast::ResourceReference" do + ast::Resource.stubs(:new) + ast::ResourceReference.expects(:new).with { |arg| + arg[:line]==1 and arg[:type]=="File" and arg[:title].is_a?(ast::ASTArray) } @parser.parse('exec { test: command => File["a","b"] }') end @@ -107,9 +107,9 @@ describe Puppet::Parser do lambda { @parser.parse('Resource["title1","title2"] { param => value }') }.should_not raise_error end - it "should create an AST::ResourceOverride" do - AST::ResourceOverride.expects(:new).with { |arg| - arg[:line]==1 and arg[:object].is_a?(AST::ResourceReference) and arg[:params].is_a?(AST::ResourceParam) + it "should create an ast::ResourceOverride" do + ast::ResourceOverride.expects(:new).with { |arg| + arg[:line]==1 and arg[:object].is_a?(ast::ResourceReference) and arg[:params].is_a?(ast::ResourceParam) } @parser.parse('Resource["title1","title2"] { param => value }') end @@ -131,12 +131,12 @@ describe Puppet::Parser do end it "should create a nop node for empty branch" do - AST::Nop.expects(:new) + ast::Nop.expects(:new) @parser.parse("if true { }") end it "should create a nop node for empty else branch" do - AST::Nop.expects(:new) + ast::Nop.expects(:new) @parser.parse("if true { notice('test') } else { }") end @@ -177,12 +177,12 @@ describe Puppet::Parser do before :each do @one = stub 'one', :is_a? => true - @one.stubs(:is_a?).with(AST::ASTArray).returns(false) - @one.stubs(:is_a?).with(AST).returns(true) + @one.stubs(:is_a?).with(ast::ASTArray).returns(false) + @one.stubs(:is_a?).with(ast).returns(true) @two = stub 'two' - @two.stubs(:is_a?).with(AST::ASTArray).returns(false) - @two.stubs(:is_a?).with(AST).returns(true) + @two.stubs(:is_a?).with(ast::ASTArray).returns(false) + @two.stubs(:is_a?).with(ast).returns(true) end it "should return the first class" do @@ -199,7 +199,30 @@ describe Puppet::Parser do klass1.code.children.should == [@one,@two] end + end + describe Puppet::Parser, "when parsing comments before statement" do + it "should associate the documentation to the statement AST node" do + ast = @parser.parse(""" + # comment + class test {} + """) + + ast[:classes]["test"].doc.should == "comment\n" + end end + describe Puppet::Parser, "when building ast nodes" do + it "should get lexer comments if ast node declares use_docs" do + lexer = stub 'lexer' + ast = mock 'ast', :nil? => false, :use_docs => true, :doc => "" + @parser.stubs(:lexer).returns(lexer) + + Puppet::Parser::AST::Definition.expects(:new).returns(ast) + lexer.expects(:getcomment).returns("comment") + ast.expects(:doc=).with("comment") + + @parser.ast(Puppet::Parser::AST::Definition) + end + end end diff --git a/spec/unit/parser/resource/reference.rb b/spec/unit/parser/resource/reference.rb index 147f772d1..bb1452692 100755 --- a/spec/unit/parser/resource/reference.rb +++ b/spec/unit/parser/resource/reference.rb @@ -72,4 +72,24 @@ describe Puppet::Parser::Resource::Reference, " when modeling defined types" do ref.builtin?.should be_false ref.definedtype.object_id.should == @nodedef.object_id end + + it "should only look for fully qualified classes" do + top = @parser.newclass "top" + sub = @parser.newclass "other::top" + + scope = @compiler.topscope.class.new(:parent => @compiler.topscope, :namespace => "other", :parser => @parser) + + ref = @type.new(:type => "class", :title => "top", :scope => scope) + ref.definedtype.classname.should equal(top.classname) + end + + it "should only look for fully qualified definitions" do + top = @parser.newdefine "top" + sub = @parser.newdefine "other::top" + + scope = @compiler.topscope.class.new(:parent => @compiler.topscope, :namespace => "other", :parser => @parser) + + ref = @type.new(:type => "top", :title => "foo", :scope => scope) + ref.definedtype.classname.should equal(top.classname) + end end diff --git a/spec/unit/parser/templatewrapper.rb b/spec/unit/parser/templatewrapper.rb index 532776223..fd9efa8af 100755 --- a/spec/unit/parser/templatewrapper.rb +++ b/spec/unit/parser/templatewrapper.rb @@ -10,99 +10,118 @@ describe Puppet::Parser::TemplateWrapper do @file = "fake_template" Puppet::Module.stubs(:find_template).returns("/tmp/fake_template") FileTest.stubs(:exists?).returns("true") - @tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) + File.stubs(:read).with("/tmp/fake_template").returns("template content") + @tw = Puppet::Parser::TemplateWrapper.new(@scope) end - it "should create a new object TemplateWrapper from a scope and a file" do + it "should create a new object TemplateWrapper from a scope" do + tw = Puppet::Parser::TemplateWrapper.new(@scope) + + tw.should be_a_kind_of(Puppet::Parser::TemplateWrapper) + end + + it "should check template file existance and read its content" do Puppet::Module.expects(:find_template).with("fake_template", "foo").returns("/tmp/fake_template") FileTest.expects(:exists?).with("/tmp/fake_template").returns(true) - tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) - tw.should be_a_kind_of(Puppet::Parser::TemplateWrapper) + File.expects(:read).with("/tmp/fake_template").returns("template content") + + @tw.file = @file end - it "should turn into a string like template[name]" do + it "should turn into a string like template[name] for file based template" do + @tw.file = @file @tw.to_s.should eql("template[/tmp/fake_template]") end + it "should turn into a string like template[inline] for string-based template" do + @tw.to_s.should eql("template[inline]") + end + it "should return the processed template contents with a call to result" do template_mock = mock("template", :result => "woot!") File.expects(:read).with("/tmp/fake_template").returns("template contents") ERB.expects(:new).with("template contents", 0, "-").returns(template_mock) + + @tw.file = @file @tw.result.should eql("woot!") end + it "should return the processed template contents with a call to result and a string" do + template_mock = mock("template", :result => "woot!") + ERB.expects(:new).with("template contents", 0, "-").returns(template_mock) + + @tw.result("template contents").should eql("woot!") + end + it "should return the contents of a variable if called via method_missing" do @scope.expects(:lookupvar).with("chicken", false).returns("is good") - tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) + tw = Puppet::Parser::TemplateWrapper.new(@scope) tw.chicken.should eql("is good") end it "should throw an exception if a variable is called via method_missing and it does not exist" do @scope.expects(:lookupvar).with("chicken", false).returns(:undefined) - tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) - lambda { tw.chicken }.should raise_error(Puppet::ParseError) + tw = Puppet::Parser::TemplateWrapper.new(@scope) + lambda { tw.chicken }.should raise_error(Puppet::ParseError) end it "should allow you to check whether a variable is defined with has_variable?" do @scope.expects(:lookupvar).with("chicken", false).returns("is good") - tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) + tw = Puppet::Parser::TemplateWrapper.new(@scope) tw.has_variable?("chicken").should eql(true) end it "should allow you to check whether a variable is not defined with has_variable?" do @scope.expects(:lookupvar).with("chicken", false).returns(:undefined) - tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) + tw = Puppet::Parser::TemplateWrapper.new(@scope) tw.has_variable?("chicken").should eql(false) end it "should allow you to retrieve the defined classes with classes" do catalog = mock 'catalog', :classes => ["class1", "class2"] @scope.expects(:catalog).returns( catalog ) - tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) + tw = Puppet::Parser::TemplateWrapper.new(@scope) tw.classes().should == ["class1", "class2"] end it "should allow you to retrieve all the tags with all_tags" do catalog = mock 'catalog', :tags => ["tag1", "tag2"] @scope.expects(:catalog).returns( catalog ) - tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) + tw = Puppet::Parser::TemplateWrapper.new(@scope) tw.all_tags().should == ["tag1","tag2"] end it "should allow you to retrieve the tags defined in the current scope" do @scope.expects(:tags).returns( ["tag1", "tag2"] ) - tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) + tw = Puppet::Parser::TemplateWrapper.new(@scope) tw.tags().should == ["tag1","tag2"] end it "should set all of the scope's variables as instance variables" do template_mock = mock("template", :result => "woot!") - File.expects(:read).with("/tmp/fake_template").returns("template contents") ERB.expects(:new).with("template contents", 0, "-").returns(template_mock) @scope.expects(:to_hash).returns("one" => "foo") - @tw.result + @tw.result("template contents") @tw.instance_variable_get("@one").should == "foo" end it "should not error out if one of the variables is a symbol" do template_mock = mock("template", :result => "woot!") - File.expects(:read).with("/tmp/fake_template").returns("template contents") ERB.expects(:new).with("template contents", 0, "-").returns(template_mock) @scope.expects(:to_hash).returns(:_timestamp => "1234") - @tw.result + @tw.result("template contents") end %w{! . ; :}.each do |badchar| it "should translate #{badchar} to _ when setting the instance variables" do template_mock = mock("template", :result => "woot!") - File.expects(:read).with("/tmp/fake_template").returns("template contents") ERB.expects(:new).with("template contents", 0, "-").returns(template_mock) @scope.expects(:to_hash).returns("one#{badchar}" => "foo") - @tw.result + @tw.result("template contents") @tw.instance_variable_get("@one_").should == "foo" end |
