summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/templatewrapper_spec.rb
diff options
context:
space:
mode:
authorMarkus Roberts <Markus@reality.com>2010-06-28 17:10:20 -0700
committerMarkus Roberts <Markus@reality.com>2010-06-28 17:10:20 -0700
commitfdc8c3509b5ac5bc170c54d72b2c2bafb58409f2 (patch)
tree6ed2204d72c6924e867a1320d3b7e6728035f1a1 /spec/unit/parser/templatewrapper_spec.rb
parent9a94ee274c39c261cd49e688a7bd7ea0eb73af50 (diff)
downloadpuppet-fdc8c3509b5ac5bc170c54d72b2c2bafb58409f2.tar.gz
puppet-fdc8c3509b5ac5bc170c54d72b2c2bafb58409f2.tar.xz
puppet-fdc8c3509b5ac5bc170c54d72b2c2bafb58409f2.zip
[#3994-part 3] rename spec tests from *_spec_spec to *_spec.rb
Part 2 re-did the change on the spec files, which it shouldn't have.
Diffstat (limited to 'spec/unit/parser/templatewrapper_spec.rb')
-rwxr-xr-xspec/unit/parser/templatewrapper_spec.rb144
1 files changed, 144 insertions, 0 deletions
diff --git a/spec/unit/parser/templatewrapper_spec.rb b/spec/unit/parser/templatewrapper_spec.rb
new file mode 100755
index 000000000..1b4121643
--- /dev/null
+++ b/spec/unit/parser/templatewrapper_spec.rb
@@ -0,0 +1,144 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe Puppet::Parser::TemplateWrapper do
+ before(:each) do
+ @known_resource_types = Puppet::Resource::TypeCollection.new("env")
+ @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("mynode"))
+ @compiler.environment.stubs(:known_resource_types).returns @known_resource_types
+ @scope = Puppet::Parser::Scope.new :compiler => @compiler
+
+ @file = "fake_template"
+ Puppet::Parser::Files.stubs(:find_template).returns("/tmp/fake_template")
+ FileTest.stubs(:exists?).returns("true")
+ 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" 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::Parser::Files.expects(:find_template).with("fake_template", @scope.environment.to_s).returns("/tmp/fake_template")
+ File.expects(:read).with("/tmp/fake_template").returns("template content")
+
+ @tw.file = @file
+ end
+
+ it "should mark the file for watching" do
+ Puppet::Parser::Files.expects(:find_template).returns("/tmp/fake_template")
+ File.stubs(:read)
+
+ @known_resource_types.expects(:watch_file).with("/tmp/fake_template")
+ @tw.file = @file
+ end
+
+ it "should fail if a template cannot be found" do
+ Puppet::Parser::Files.expects(:find_template).returns nil
+
+ lambda { @tw.file = @file }.should raise_error(Puppet::ParseError)
+ end
+
+ 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)
+ 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)
+ 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)
+ 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)
+ 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)
+ 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)
+ 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)
+ 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!")
+ ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
+
+ @scope.expects(:to_hash).returns("one" => "foo")
+ @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!")
+ ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
+
+ @scope.expects(:to_hash).returns(:_timestamp => "1234")
+ @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!")
+ ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
+
+ @scope.expects(:to_hash).returns("one#{badchar}" => "foo")
+ @tw.result("template contents")
+
+ @tw.instance_variable_get("@one_").should == "foo"
+ end
+ end
+end