diff options
| author | Luke Kanies <luke@madstop.com> | 2008-10-17 09:01:04 -0500 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2008-10-17 09:01:04 -0500 |
| commit | 8aee40de69e6fe8d67ab58a2e223443b15820584 (patch) | |
| tree | 89e230df3b43302a542f2cb6869f63e2fb93f6d8 /spec/unit/parser | |
| parent | 1b517d2fb048603bd1743a662bde74e8ae4b13dc (diff) | |
| parent | a74ec60d33dee1c592ec858faeccc23d7a7b79f3 (diff) | |
Merge branch '0.24.x' Removed the 'after' blocks that call Type.clear,
since that method is deprecated.
Conflicts:
CHANGELOG
bin/puppetca
lib/puppet/file_serving/fileset.rb
lib/puppet/network/xmlrpc/client.rb
lib/puppet/type/file/selcontext.rb
spec/unit/file_serving/metadata.rb
spec/unit/type/file.rb
Diffstat (limited to 'spec/unit/parser')
| -rwxr-xr-x | spec/unit/parser/ast/arithmetic_operator.rb | 73 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/boolean_operator.rb | 53 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/collexpr.rb | 92 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/comparison_operator.rb | 52 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/minus.rb | 36 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/nop.rb | 20 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/not.rb | 30 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/resource_override.rb | 51 | ||||
| -rwxr-xr-x | spec/unit/parser/ast/resource_reference.rb | 63 | ||||
| -rwxr-xr-x | spec/unit/parser/collector.rb | 10 | ||||
| -rwxr-xr-x | spec/unit/parser/lexer.rb | 57 | ||||
| -rwxr-xr-x | spec/unit/parser/parser.rb | 113 | ||||
| -rwxr-xr-x | spec/unit/parser/resource.rb | 10 | ||||
| -rwxr-xr-x | spec/unit/parser/scope.rb | 50 | ||||
| -rwxr-xr-x | spec/unit/parser/templatewrapper.rb | 14 |
15 files changed, 715 insertions, 9 deletions
diff --git a/spec/unit/parser/ast/arithmetic_operator.rb b/spec/unit/parser/ast/arithmetic_operator.rb new file mode 100755 index 000000000..24d6ad47d --- /dev/null +++ b/spec/unit/parser/ast/arithmetic_operator.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::ArithmeticOperator do + + AST = Puppet::Parser::AST + + before :each do + @scope = Puppet::Parser::Scope.new() + @one = stub 'lval', :safeevaluate => 1 + @two = stub 'rval', :safeevaluate => 2 + end + + it "should evaluate both branches" do + lval = stub "lval" + lval.expects(:safeevaluate).with(@scope).returns(1) + rval = stub "rval" + rval.expects(:safeevaluate).with(@scope).returns(2) + + 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 + 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) + end + + + %w{ + - * / << >>}.each do |op| + it "should call ruby Numeric '#{op}'" do + one = stub 'one' + two = stub '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) + operator.evaluate(@scope) + end + end + + 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.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.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" ) + + operator = AST::ArithmeticOperator.new :lval => one, :operator => "+", :rval => two + operator.evaluate(@scope).should == 3 + end + +end diff --git a/spec/unit/parser/ast/boolean_operator.rb b/spec/unit/parser/ast/boolean_operator.rb new file mode 100755 index 000000000..7304e2a10 --- /dev/null +++ b/spec/unit/parser/ast/boolean_operator.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::BooleanOperator do + + 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) + end + + it "should evaluate left operand inconditionally" do + lval = stub "lval" + lval.expects(:safeevaluate).with(@scope).returns("true") + rval = stub "rval", :safeevaluate => false + rval.expects(:safeevaluate).never + + operator = AST::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval + operator.evaluate(@scope) + end + + it "should evaluate right 'and' operand only if left operand is true" 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.evaluate(@scope) + end + + it "should evaluate right 'or' operand only if left operand is false" 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.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 + 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 + 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 + end + +end diff --git a/spec/unit/parser/ast/collexpr.rb b/spec/unit/parser/ast/collexpr.rb new file mode 100755 index 000000000..e5e6e0d7a --- /dev/null +++ b/spec/unit/parser/ast/collexpr.rb @@ -0,0 +1,92 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::CollExpr do + + AST = Puppet::Parser::AST + + before :each do + @scope = Puppet::Parser::Scope.new() + end + + describe "when evaluating with two operands" do + before :each do + @test1 = mock 'test1' + @test1.expects(:safeevaluate).with(@scope).returns("test1") + @test2 = mock 'test2' + @test2.expects(:safeevaluate).with(@scope).returns("test2") + end + + it "should evaluate both" do + 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=>"==") + result = collexpr.evaluate(@scope) + result[0].should == "param_values.value = 'test2' and param_names.name = 'test1'" + result[1].should be_an_instance_of(Proc) + end + + it "should propagate expression type and form to child if expression themselves" do + [@test1, @test2].each do |t| + t.expects(:is_a?).returns(true) + t.expects(:form).returns(false) + t.expects(:type).returns(false) + t.expects(:type=) + t.expects(:form=) + end + + collexpr = AST::CollExpr.new(:test1 => @test1, :test2 => @test2, :oper=>"==", :form => true, :type => true) + result = collexpr.evaluate(@scope) + end + + describe "and when evaluating the produced code" do + before :each do + @resource = mock 'resource' + @resource.expects(:[]).with("test1").at_least(1).returns("test2") + end + + it "should evaluate like the original expression for ==" do + 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.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) + 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) + lambda { collexpr.evaluate(@scope) }.should raise_error(Puppet::ParseError) + end + end + end + + it "should check for array member equality if resource parameter is an array for ==" do + array = mock 'array', :safeevaluate => "array" + test1 = mock 'test1' + test1.expects(:safeevaluate).with(@scope).returns("test1") + + resource = mock 'resource' + resource.expects(:[]).with("array").at_least(1).returns(["test1","test2","test3"]) + 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 + end + +end
\ No newline at end of file diff --git a/spec/unit/parser/ast/comparison_operator.rb b/spec/unit/parser/ast/comparison_operator.rb new file mode 100755 index 000000000..dbea349f2 --- /dev/null +++ b/spec/unit/parser/ast/comparison_operator.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby + +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 ) + end + + it "should evaluate both branches" do + lval = stub "lval" + lval.expects(:safeevaluate).with(@scope) + rval = stub "rval" + rval.expects(:safeevaluate).with(@scope) + + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => lval, :operator => "==", :rval => rval + operator.evaluate(@scope) + 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.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.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" ) + + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => one, :operator => "<", :rval => two + operator.evaluate(@scope).should == true + end + +end diff --git a/spec/unit/parser/ast/minus.rb b/spec/unit/parser/ast/minus.rb new file mode 100755 index 000000000..83bd92d0d --- /dev/null +++ b/spec/unit/parser/ast/minus.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::Minus do + before :each do + @scope = Puppet::Parser::Scope.new() + end + + it "should evaluate its argument" do + value = stub "value" + value.expects(:safeevaluate).with(@scope).returns(123) + + operator = Puppet::Parser::AST::Minus.new :value => value + operator.evaluate(@scope) + end + + it "should fail if argument is not a string or integer" do + array_ast = stub 'array_ast', :safeevaluate => [2] + operator = Puppet::Parser::AST::Minus.new :value => array_ast + lambda { operator.evaluate(@scope) }.should raise_error + end + + it "should work with integer as string" do + string = stub 'string', :safeevaluate => "123" + operator = Puppet::Parser::AST::Minus.new :value => string + operator.evaluate(@scope).should == -123 + end + + it "should work with integers" do + int = stub 'int', :safeevaluate => 123 + operator = Puppet::Parser::AST::Minus.new :value => int + operator.evaluate(@scope).should == -123 + end + +end diff --git a/spec/unit/parser/ast/nop.rb b/spec/unit/parser/ast/nop.rb new file mode 100755 index 000000000..5a7132586 --- /dev/null +++ b/spec/unit/parser/ast/nop.rb @@ -0,0 +1,20 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::Nop do + + before do + @scope = mock 'scope' + end + + it "should do nothing on evaluation" do + Puppet::Parser::AST.expects(:safeevaluate).never + Puppet::Parser::AST::Nop.new({}).evaluate(@scope) + end + + it "should not return anything" do + Puppet::Parser::AST::Nop.new({}).evaluate(@scope).should be_nil + end + +end diff --git a/spec/unit/parser/ast/not.rb b/spec/unit/parser/ast/not.rb new file mode 100755 index 000000000..0fe2deddd --- /dev/null +++ b/spec/unit/parser/ast/not.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::Not do + before :each do + @scope = Puppet::Parser::Scope.new() + @true_ast = Puppet::Parser::AST::Boolean.new( :value => true) + @false_ast = Puppet::Parser::AST::Boolean.new( :value => false) + end + + it "should evaluate its child expression" do + val = stub "val" + val.expects(:safeevaluate).with(@scope) + + operator = Puppet::Parser::AST::Not.new :value => val + operator.evaluate(@scope) + end + + it "should return true for ! false" do + operator = Puppet::Parser::AST::Not.new :value => @false_ast + operator.evaluate(@scope).should == true + end + + it "should return false for ! true" do + operator = Puppet::Parser::AST::Not.new :value => @true_ast + operator.evaluate(@scope).should == false + end + +end diff --git a/spec/unit/parser/ast/resource_override.rb b/spec/unit/parser/ast/resource_override.rb new file mode 100755 index 000000000..3fbeb323c --- /dev/null +++ b/spec/unit/parser/ast/resource_override.rb @@ -0,0 +1,51 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::ResourceOverride do + + AST = Puppet::Parser::AST + + before :each do + @compiler = stub 'compiler' + @scope = Puppet::Parser::Scope.new(:compiler => @compiler) + @params = AST::ASTArray.new({}) + @compiler.stubs(:add_override) + end + + it "should evaluate the overriden object" 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) + end + + it "should tell the compiler to override the resource with our own" do + @compiler.expects(:add_override) + + klass = stub 'klass', :title => "title", :type => "one" + object = mock 'object', :safeevaluate => klass + 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.should be_an_instance_of(Puppet::Parser::Resource) + override.title.should == "title" + override.type.should == "One" + end + + it "should return an array of overriden resources when called with an array of titles" do + klass1 = stub 'klass1', :title => "title1", :type => "one" + klass2 = stub 'klass2', :title => "title2", :type => "one" + + object = mock 'object', :safeevaluate => [klass1,klass2] + + 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 diff --git a/spec/unit/parser/ast/resource_reference.rb b/spec/unit/parser/ast/resource_reference.rb new file mode 100755 index 000000000..e4b7c763b --- /dev/null +++ b/spec/unit/parser/ast/resource_reference.rb @@ -0,0 +1,63 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::ResourceReference do + + AST = Puppet::Parser::AST + + before :each do + @scope = Puppet::Parser::Scope.new() + end + + def newref(title, type) + title = stub 'title', :safeevaluate => title + ref = AST::ResourceReference.new(:type => type, :title => title) + end + + it "should evaluate correctly reference to builtin types" do + newref("/tmp/yay", "File").evaluate(@scope).to_s.should == "File[/tmp/yay]" + end + + %{ "one::two" "one-two"}.each do |type| + it "should evaluate correctly reference to define" do + klass = stub 'klass', :title => "three", :classname => type + @scope.stubs(:finddefine).returns(klass) + + newref("three", type).evaluate(@scope).to_ref.should == Puppet::Parser::Resource::Reference.new( :type => type, :title => "three" ).to_ref + end + end + + it "should be able to call qualified_class" do + klass = stub 'klass', :title => "three", :classname => "one" + @scope.expects(:findclass).with("one").returns(klass) + newref("three","class").qualified_class(@scope,"one").should == "one" + end + + it "should be able to find qualified classes when evaluating" do + klass = stub 'klass', :title => "one", :classname => "one" + @scope.stubs(:findclass).returns(klass) + + evaled = newref("one", "class").evaluate(@scope) + evaled.type.should == "Class" + evaled.title.should == "one" + end + + 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.stubs(:qualified_type).with(@scope).returns("Resource") + + ref.evaluate(@scope).should have(2).elements + end + + 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.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 diff --git a/spec/unit/parser/collector.rb b/spec/unit/parser/collector.rb index 2dfae6786..ede583b96 100755 --- a/spec/unit/parser/collector.rb +++ b/spec/unit/parser/collector.rb @@ -234,16 +234,16 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do @collector.evaluate end - it "should return all matching resources from the current compile" do + it "should return all matching resources from the current compile and mark them non-virtual and non-exported" do stub_rails(true) one = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true two = stub 'two', :type => "Mytype", :virtual? => true, :exported? => true - one.stubs(:exported=) - one.stubs(:virtual=) - two.stubs(:exported=) - two.stubs(:virtual=) + one.expects(:exported=).with(false) + one.expects(:virtual=).with(false) + two.expects(:exported=).with(false) + two.expects(:virtual=).with(false) @compiler.expects(:resources).returns([one, two]) diff --git a/spec/unit/parser/lexer.rb b/spec/unit/parser/lexer.rb index fed1ade7d..3b0df96a9 100755 --- a/spec/unit/parser/lexer.rb +++ b/spec/unit/parser/lexer.rb @@ -4,6 +4,27 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/parser/lexer' +describe Puppet::Parser::Lexer do + describe "when reading strings" do + before { @lexer = Puppet::Parser::Lexer.new } + it "should increment the line count for every carriage return in the string" do + @lexer.line = 10 + @lexer.string = "this\nis\natest'" + @lexer.slurpstring("'") + + @lexer.line.should == 12 + end + + it "should not increment the line count for escapes in the string" do + @lexer.line = 10 + @lexer.string = "this\\nis\\natest'" + @lexer.slurpstring("'") + + @lexer.line.should == 10 + end + end +end + describe Puppet::Parser::Lexer::Token do before do @token = Puppet::Parser::Lexer::Token.new(%r{something}, :NAME) @@ -136,7 +157,13 @@ describe Puppet::Parser::Lexer::TOKENS do :BACKSLASH => '\\', :FARROW => '=>', :PARROW => '+>', - :APPENDS => '+=' + :APPENDS => '+=', + :PLUS => '+', + :MINUS => '-', + :DIV => '/', + :TIMES => '*', + :LSHIFT => '<<', + :RSHIFT => '>>', }.each do |name, string| it "should have a token named #{name.to_s}" do Puppet::Parser::Lexer::TOKENS[name].should_not be_nil @@ -213,10 +240,34 @@ describe Puppet::Parser::Lexer::TOKENS[:NAME] do end describe Puppet::Parser::Lexer::TOKENS[:NUMBER] do - before { @token = Puppet::Parser::Lexer::TOKENS[:NUMBER] } + before do + @token = Puppet::Parser::Lexer::TOKENS[:NUMBER] +# @regex = Regexp.new('^'+@token.regex.source+'$') + @regex = @token.regex + end it "should match against numeric terms" do - @token.regex.should =~ "2982383139" + @regex.should =~ "2982383139" + end + + it "should match against float terms" do + @regex.should =~ "29823.235" + end + + it "should match against hexadecimal terms" do + @regex.should =~ "0xBEEF0023" + end + + it "should match against float with exponent terms" do + @regex.should =~ "10e23" + end + + it "should match against float terms with negative exponents" do + @regex.should =~ "10e-23" + end + + it "should match against float terms with fractional parts and exponent" do + @regex.should =~ "1.234e23" end it "should return the NAME token and the value" do diff --git a/spec/unit/parser/parser.rb b/spec/unit/parser/parser.rb index 94b19be40..c0d22a2cc 100755 --- a/spec/unit/parser/parser.rb +++ b/spec/unit/parser/parser.rb @@ -8,6 +8,7 @@ describe Puppet::Parser do before :each do @parser = Puppet::Parser::Parser.new :environment => "development" + @true_ast = AST::Boolean.new :value => true end describe "when parsing append operator" do @@ -31,4 +32,114 @@ describe Puppet::Parser do end end -end + + 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) } + @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" + } + @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]=="<" + } + @parser.parse("if 1 < 2 { $var = 1 }") + + end + + end + + 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" + } + @parser.parse("if (1 > 2) and (1 == 2) { $var = 1 }") + end + + it "should raise an error on incorrect expression" do + lambda { @parser.parse("if (1 > 2 > ) or (1 == 2) { $var = 1 }") }.should raise_error + end + + end + + describe Puppet::Parser, "when parsing resource references" do + + it "should not raise syntax errors" do + lambda { @parser.parse('exec { test: param => File["a"] }') }.should_not raise_error + end + + it "should not raise syntax errors with multiple references" 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) + } + @parser.parse('exec { test: command => File["a","b"] }') + end + end + + describe Puppet::Parser, "when parsing resource overrides" do + + it "should not raise syntax errors" do + lambda { @parser.parse('Resource["title"] { param => value }') }.should_not raise_error + end + + it "should not raise syntax errors with multiple overrides" 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) + } + @parser.parse('Resource["title1","title2"] { param => value }') + end + + end + + describe Puppet::Parser, "when parsing if statements" do + + it "should not raise errors with empty if" do + lambda { @parser.parse("if true { }") }.should_not raise_error + end + + it "should not raise errors with empty else" do + lambda { @parser.parse("if false { notice('if') } else { }") }.should_not raise_error + end + + it "should not raise errors with empty if and else" do + lambda { @parser.parse("if false { } else { }") }.should_not raise_error + end + + it "should create a nop node for empty branch" do + AST::Nop.expects(:new) + @parser.parse("if true { }") + end + + it "should create a nop node for empty else branch" do + AST::Nop.expects(:new) + @parser.parse("if true { notice('test') } else { }") + end + + end + + end diff --git a/spec/unit/parser/resource.rb b/spec/unit/parser/resource.rb index 6b2021916..63cfbc2ed 100755 --- a/spec/unit/parser/resource.rb +++ b/spec/unit/parser/resource.rb @@ -176,6 +176,16 @@ describe Puppet::Parser::Resource do @resource["noop"].should == "false" end + it "should copy all metaparams that it finds" do + @scope.setvar("require", "container") + @scope.setvar("notify", "container") + + @resource.class.publicize_methods(:add_metaparams) { @resource.add_metaparams } + + @resource["require"].should == "container" + @resource["notify"].should == "container" + end + it "should stack relationship metaparams from its container if it already has them" do @resource.set_parameter("require", "resource") @scope.setvar("require", "container") diff --git a/spec/unit/parser/scope.rb b/spec/unit/parser/scope.rb index ec8ab6d7d..fa76c4ff2 100755 --- a/spec/unit/parser/scope.rb +++ b/spec/unit/parser/scope.rb @@ -34,4 +34,54 @@ describe Puppet::Parser::Scope do end end + + describe Puppet::Parser::Scope, "when calling number?" do + + it "should return nil if called with anything not a number" do + Puppet::Parser::Scope.number?([2]).should be_nil + end + + it "should return a Fixnum for a Fixnum" do + Puppet::Parser::Scope.number?(2).should be_an_instance_of(Fixnum) + end + + it "should return a Float for a Float" do + Puppet::Parser::Scope.number?(2.34).should be_an_instance_of(Float) + end + + it "should return 234 for '234'" do + Puppet::Parser::Scope.number?("234").should == 234 + end + + it "should return nil for 'not a number'" do + Puppet::Parser::Scope.number?("not a number").should be_nil + end + + it "should return 23.4 for '23.4'" do + Puppet::Parser::Scope.number?("23.4").should == 23.4 + end + + it "should return 23.4e13 for '23.4e13'" do + Puppet::Parser::Scope.number?("23.4e13").should == 23.4e13 + end + + it "should understand negative numbers" do + Puppet::Parser::Scope.number?("-234").should == -234 + end + + it "should know how to convert exponential float numbers ala '23e13'" do + Puppet::Parser::Scope.number?("23e13").should == 23e13 + end + + it "should understand hexadecimal numbers" do + Puppet::Parser::Scope.number?("0x234").should == 0x234 + end + + it "should understand octal numbers" do + Puppet::Parser::Scope.number?("0755").should == 0755 + end + + + end + end diff --git a/spec/unit/parser/templatewrapper.rb b/spec/unit/parser/templatewrapper.rb index 2d4bd141b..20ea76921 100755 --- a/spec/unit/parser/templatewrapper.rb +++ b/spec/unit/parser/templatewrapper.rb @@ -55,6 +55,20 @@ describe Puppet::Parser::TemplateWrapper do 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.classes().should == ["class1", "class2"] + end + + it "should allow you to retrieve the defined tags with tags" do + catalog = mock 'catalog', :tags => ["tag1", "tag2"] + @scope.expects(:catalog).returns( catalog ) + tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) + 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") |
