summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser
diff options
context:
space:
mode:
authorMarkus Roberts <Markus@reality.com>2010-10-24 23:29:38 -0700
committerNick Lewis <nick@puppetlabs.com>2011-04-12 12:47:32 -0700
commit739260b28d7c09bacbb34cd4f4d4a32cfee01385 (patch)
tree9dc514930f0f433786211a9c8c07742aec99ae01 /spec/unit/parser
parentd7201ed38929f867d31bc9b916e90679606dc9f8 (diff)
downloadpuppet-739260b28d7c09bacbb34cd4f4d4a32cfee01385.tar.gz
puppet-739260b28d7c09bacbb34cd4f4d4a32cfee01385.tar.xz
puppet-739260b28d7c09bacbb34cd4f4d4a32cfee01385.zip
Towards 5027 -- add options hash to lookupvar as with setvar
This patch adds an options hash to lookupvar analogous to the one taken by setvar and uses it to pass in source location for error reporting. It also fixes the mechanism used by setvar (file was not being passed correctly), adds line and file information to errors in templates, and extends/corrects tests. As presently written it does not gather userful line numbers from inline templates and there are no tests for the template line number generation.
Diffstat (limited to 'spec/unit/parser')
-rwxr-xr-xspec/unit/parser/ast/arithmetic_operator_spec.rb10
-rwxr-xr-xspec/unit/parser/ast/comparison_operator_spec.rb4
-rwxr-xr-xspec/unit/parser/ast/leaf_spec.rb25
-rwxr-xr-xspec/unit/parser/ast/vardef_spec.rb19
-rwxr-xr-xspec/unit/parser/templatewrapper_spec.rb40
5 files changed, 49 insertions, 49 deletions
diff --git a/spec/unit/parser/ast/arithmetic_operator_spec.rb b/spec/unit/parser/ast/arithmetic_operator_spec.rb
index 0aab6f080..9b54f09d8 100755
--- a/spec/unit/parser/ast/arithmetic_operator_spec.rb
+++ b/spec/unit/parser/ast/arithmetic_operator_spec.rb
@@ -60,14 +60,4 @@ describe Puppet::Parser::AST::ArithmeticOperator do
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/comparison_operator_spec.rb b/spec/unit/parser/ast/comparison_operator_spec.rb
index 3efe28bb6..060827390 100755
--- a/spec/unit/parser/ast/comparison_operator_spec.rb
+++ b/spec/unit/parser/ast/comparison_operator_spec.rb
@@ -95,8 +95,8 @@ describe Puppet::Parser::AST::ComparisonOperator do
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)
+ one.expects(:safeevaluate).with(@scope).returns(1)
+ two.expects(:safeevaluate).with(@scope).returns(2)
operator = Puppet::Parser::AST::ComparisonOperator.new :lval => one, :operator => "<", :rval => two
operator.evaluate(@scope).should == true
diff --git a/spec/unit/parser/ast/leaf_spec.rb b/spec/unit/parser/ast/leaf_spec.rb
index 97c996b40..64fdcd659 100755
--- a/spec/unit/parser/ast/leaf_spec.rb
+++ b/spec/unit/parser/ast/leaf_spec.rb
@@ -107,7 +107,7 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
describe "when evaluating" do
it "should evaluate the variable part if necessary" do
- @scope.stubs(:lookupvar).with("a").returns(["b"])
+ @scope.stubs(:lookupvar).with { |name,options| name == 'a'}.returns(["b"])
variable = stub 'variable', :evaluate => "a"
access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => variable, :key => 0 )
@@ -118,7 +118,7 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
end
it "should evaluate the access key part if necessary" do
- @scope.stubs(:lookupvar).with("a").returns(["b"])
+ @scope.stubs(:lookupvar).with { |name,options| name == 'a'}.returns(["b"])
index = stub 'index', :evaluate => 0
access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => index )
@@ -129,7 +129,7 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
end
it "should be able to return an array member" do
- @scope.stubs(:lookupvar).with("a").returns(["val1", "val2", "val3"])
+ @scope.stubs(:lookupvar).with { |name,options| name == 'a'}.returns(["val1", "val2", "val3"])
access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => 1 )
@@ -153,7 +153,7 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
end
it "should be able to return an hash value" do
- @scope.stubs(:lookupvar).with("a").returns({ "key1" => "val1", "key2" => "val2", "key3" => "val3" })
+ @scope.stubs(:lookupvar).with { |name,options| name == 'a'}.returns({ "key1" => "val1", "key2" => "val2", "key3" => "val3" })
access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key2" )
@@ -177,7 +177,7 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
end
it "should raise an error if the variable wasn't in the scope" do
- @scope.stubs(:lookupvar).with("a").returns(nil)
+ @scope.stubs(:lookupvar).with { |name,options| name == 'a'}.returns(nil)
access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key2" )
@@ -190,7 +190,7 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
end
it "should work with recursive hash access" do
- @scope.stubs(:lookupvar).with("a").returns({ "key" => { "subkey" => "b" }})
+ @scope.stubs(:lookupvar).with { |name,options| name == 'a'}.returns({ "key" => { "subkey" => "b" }})
access1 = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key")
access2 = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => access1, :key => "subkey")
@@ -199,7 +199,7 @@ describe Puppet::Parser::AST::HashOrArrayAccess do
end
it "should work with interleaved array and hash access" do
- @scope.stubs(:lookupvar).with("a").returns({ "key" => [ "a" , "b" ]})
+ @scope.stubs(:lookupvar).with { |name,options| name == 'a'}.returns({ "key" => [ "a" , "b" ]})
access1 = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key")
access2 = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => access1, :key => 1)
@@ -332,16 +332,21 @@ end
describe Puppet::Parser::AST::Variable do
before :each do
@scope = stub 'scope'
- @var = Puppet::Parser::AST::Variable.new(:value => "myvar")
+ @var = Puppet::Parser::AST::Variable.new(:value => "myvar", :file => 'my.pp', :line => 222)
end
it "should lookup the variable in scope" do
- @scope.expects(:lookupvar).with("myvar").returns(:myvalue)
+ @scope.expects(:lookupvar).with { |name,options| name == "myvar" }.returns(:myvalue)
+ @var.safeevaluate(@scope).should == :myvalue
+ end
+
+ it "should pass the source location to lookupvar" do
+ @scope.expects(:lookupvar).with { |name,options| name == "myvar" and options[:file] == 'my.pp' and options[:line] == 222 }.returns(:myvalue)
@var.safeevaluate(@scope).should == :myvalue
end
it "should return undef if the variable wasn't set" do
- @scope.expects(:lookupvar).with("myvar").returns(:undefined)
+ @scope.expects(:lookupvar).with { |name,options| name == "myvar" }.returns(:undefined)
@var.safeevaluate(@scope).should == :undef
end
diff --git a/spec/unit/parser/ast/vardef_spec.rb b/spec/unit/parser/ast/vardef_spec.rb
index a462e8ef5..5a851bb13 100755
--- a/spec/unit/parser/ast/vardef_spec.rb
+++ b/spec/unit/parser/ast/vardef_spec.rb
@@ -16,8 +16,7 @@ describe Puppet::Parser::AST::VarDef do
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 = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, :line => nil
vardef.evaluate(@scope)
end
@@ -27,8 +26,7 @@ describe Puppet::Parser::AST::VarDef do
@scope.expects(:setvar).with { |name,value,options| options[:append] == nil }
- vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil,
- :line => nil
+ vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, :line => nil
vardef.evaluate(@scope)
end
@@ -38,8 +36,17 @@ describe Puppet::Parser::AST::VarDef do
@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
+ vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => nil, :line => nil, :append => true
+ vardef.evaluate(@scope)
+ end
+
+ it "should call pass the source location to setvar" do
+ name = stub 'name', :safeevaluate => "var"
+ value = stub 'value', :safeevaluate => "1"
+
+ @scope.expects(:setvar).with { |name,value,options| options[:file] == 'setvar.pp' and options[:line] == 917 }
+
+ vardef = Puppet::Parser::AST::VarDef.new :name => name, :value => value, :file => 'setvar.pp', :line => 917
vardef.evaluate(@scope)
end
diff --git a/spec/unit/parser/templatewrapper_spec.rb b/spec/unit/parser/templatewrapper_spec.rb
index 4a713b8a8..0c7199ba1 100755
--- a/spec/unit/parser/templatewrapper_spec.rb
+++ b/spec/unit/parser/templatewrapper_spec.rb
@@ -17,6 +17,12 @@ describe Puppet::Parser::TemplateWrapper do
@tw = Puppet::Parser::TemplateWrapper.new(@scope)
end
+ def mock_template(source=nil)
+ template_mock = mock("template", :result => "woot!")
+ ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
+ template_mock.expects(:filename=).with(source)
+ end
+
it "should create a new object TemplateWrapper from a scope" do
tw = Puppet::Parser::TemplateWrapper.new(@scope)
@@ -54,41 +60,38 @@ describe Puppet::Parser::TemplateWrapper do
end
it "should return the processed template contents with a call to result" do
- template_mock = mock("template", :result => "woot!")
+ mock_template("/tmp/fake_template")
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)
-
+ mock_template
@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").returns("is good")
+ @scope.expects(:lookupvar).with { |name,options| name == "chicken"}.returns("is good")
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").returns(:undefined)
+ @scope.expects(:lookupvar).with { |name,options| name == "chicken"}.returns(:undefined)
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").returns("is good")
+ @scope.expects(:lookupvar).with { |name,options| name == "chicken"}.returns("is good")
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").returns(:undefined)
+ @scope.expects(:lookupvar).with { |name,options| name == "chicken"}.returns(:undefined)
tw = Puppet::Parser::TemplateWrapper.new(@scope)
tw.has_variable?("chicken").should eql(false)
end
@@ -114,9 +117,7 @@ describe Puppet::Parser::TemplateWrapper do
end
it "should set all of the scope's variables as instance variables" do
- template_mock = mock("template", :result => "woot!")
- ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
-
+ mock_template
@scope.expects(:to_hash).returns("one" => "foo")
@tw.result("template contents")
@@ -124,8 +125,7 @@ describe Puppet::Parser::TemplateWrapper do
end
it "should not error out if one of the variables is a symbol" do
- template_mock = mock("template", :result => "woot!")
- ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
+ mock_template
@scope.expects(:to_hash).returns(:_timestamp => "1234")
@tw.result("template contents")
@@ -133,13 +133,11 @@ describe Puppet::Parser::TemplateWrapper do
%w{! . ; :}.each do |badchar|
it "should translate #{badchar} to _ when setting the instance variables" do
- template_mock = mock("template", :result => "woot!")
- ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
-
- @scope.expects(:to_hash).returns("one#{badchar}" => "foo")
- @tw.result("template contents")
+ mock_template
+ @scope.expects(:to_hash).returns("one#{badchar}" => "foo")
+ @tw.result("template contents")
- @tw.instance_variable_get("@one_").should == "foo"
- end
+ @tw.instance_variable_get("@one_").should == "foo"
+ end
end
end