summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/templatewrapper_spec.rb
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/templatewrapper_spec.rb
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/templatewrapper_spec.rb')
-rwxr-xr-xspec/unit/parser/templatewrapper_spec.rb40
1 files changed, 19 insertions, 21 deletions
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