summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/ast
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/ast
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/ast')
-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
4 files changed, 30 insertions, 28 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