From 88cfaf8f277f2521eac9844f8b12d800a49cbafd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 17 Mar 2010 19:09:17 -0700 Subject: [PATCH] Added class level method to render string for the TemplateRenderer --- lib/vagrant/commands.rb | 2 +- lib/vagrant/util/template_renderer.rb | 21 +++++- test/vagrant/commands_test.rb | 8 +-- test/vagrant/util/template_renderer_test.rb | 75 ++++++++++++++------- 4 files changed, 72 insertions(+), 34 deletions(-) diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 23dc8264f..6f1944dee 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -23,7 +23,7 @@ error # Copy over the rootfile template into this directory default_box ||= "base" File.open(rootfile_path, 'w+') do |f| - f.write(TemplateRenderer.render!(Env::ROOTFILE_NAME, :default_box => default_box)) + f.write(TemplateRenderer.render(Env::ROOTFILE_NAME, :default_box => default_box)) end end diff --git a/lib/vagrant/util/template_renderer.rb b/lib/vagrant/util/template_renderer.rb index 7fc101fd6..560aa5c8c 100644 --- a/lib/vagrant/util/template_renderer.rb +++ b/lib/vagrant/util/template_renderer.rb @@ -11,12 +11,27 @@ module Vagrant # takes a block which will be passed the renderer prior to rendering, which # allows the caller to set any view variables within the renderer itself. # - # @param [String] template Name of the template file, without the extension # @return [String] Rendered template - def render!(template, data={}) + def render(*args) + render_with(:render, *args) + end + + # Render a given string and return the result. This method optionally + # takes a block which will be passed the renderer prior to rendering, which + # allows the caller to set any view variables within the renderer itself. + # + # @param [String] template The template data string. + # @return [String] Rendered template + def render_string(*args) + render_with(:render_string, *args) + end + + # Method used internally to DRY out the other renderers. This method + # creates and sets up the renderer before calling a specified method on it. + def render_with(method, template, data={}) renderer = new(template, data) yield renderer if block_given? - renderer.render + renderer.send(method.to_sym) end end diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index e51028c3c..236d5a689 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -17,7 +17,7 @@ class CommandsTest < Test::Unit::TestCase File.stubs(:open).yields(@file) @rootfile_path = File.join(Dir.pwd, Vagrant::Env::ROOTFILE_NAME) - Vagrant::Util::TemplateRenderer.stubs(:render!) + Vagrant::Util::TemplateRenderer.stubs(:render) end should "error and exit if a rootfile already exists" do @@ -28,7 +28,7 @@ class CommandsTest < Test::Unit::TestCase should "write to the rootfile path using the template renderer" do result = "foo" - Vagrant::Util::TemplateRenderer.expects(:render!).returns(result).once + Vagrant::Util::TemplateRenderer.expects(:render).returns(result).once @file.expects(:write).with(result).once File.expects(:open).with(@rootfile_path, 'w+').yields(@file) @@ -37,12 +37,12 @@ class CommandsTest < Test::Unit::TestCase should "use the given base box if given" do box = "zooo" - Vagrant::Util::TemplateRenderer.expects(:render!).with(Vagrant::Env::ROOTFILE_NAME, :default_box => box) + Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Env::ROOTFILE_NAME, :default_box => box) Vagrant::Commands.init(box) end should "use the default `base` if no box is given" do - Vagrant::Util::TemplateRenderer.expects(:render!).with(Vagrant::Env::ROOTFILE_NAME, :default_box => "base") + Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Env::ROOTFILE_NAME, :default_box => "base") Vagrant::Commands.init end end diff --git a/test/vagrant/util/template_renderer_test.rb b/test/vagrant/util/template_renderer_test.rb index 2820ee512..669430c10 100644 --- a/test/vagrant/util/template_renderer_test.rb +++ b/test/vagrant/util/template_renderer_test.rb @@ -77,39 +77,62 @@ class TemplateRendererUtilTest < Test::Unit::TestCase end end - context "class-level render! method" do - setup do - @template = "foo" - @r = Vagrant::Util::TemplateRenderer.new(@template) - @r.stubs(:render) + context "class methods" do + context "render_with method" do + setup do + @template = "foo" + @r = Vagrant::Util::TemplateRenderer.new(@template) + @r.stubs(:render) - Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r) - end + @method = :rawr - should "use the first argument as the template" do - template = "foo" - Vagrant::Util::TemplateRenderer.expects(:new).with(template, {}).returns(@r) - Vagrant::Util::TemplateRenderer.render!(template) - end + Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r) + end - should "send in additional argument to the renderer" do - template = "foo" - data = {:hey => :foo} - Vagrant::Util::TemplateRenderer.expects(:new).with(template, data).returns(@r) - Vagrant::Util::TemplateRenderer.render!(template, data) - end + should "use the second argument as the template" do + Vagrant::Util::TemplateRenderer.expects(:new).with(@template, {}).returns(@r) + Vagrant::Util::TemplateRenderer.render_with(@method, @template) + end - should "yield a block if given with the renderer as the argument" do - @r.expects(:yielded=).with(true).once - Vagrant::Util::TemplateRenderer.render!(@template) do |r| - r.yielded = true + should "send in additional argument to the renderer" do + data = {:hey => :foo} + Vagrant::Util::TemplateRenderer.expects(:new).with(@template, data).returns(@r) + Vagrant::Util::TemplateRenderer.render_with(@method, @template, data) + end + + should "yield a block if given with the renderer as the argument" do + @r.expects(:yielded=).with(true).once + Vagrant::Util::TemplateRenderer.render_with(@method, @template) do |r| + r.yielded = true + end + end + + should "render the result using the given method" do + result = mock('result') + @r.expects(@method).returns(result) + assert_equal result, Vagrant::Util::TemplateRenderer.render_with(@method, @template) + end + + should "convert the given method to a sym prior to calling" do + @r.expects(@method.to_sym).returns(nil) + Vagrant::Util::TemplateRenderer.render_with(@method.to_s, @template) end end - should "render the result" do - result = mock('result') - @r.expects(:render).returns(result) - assert_equal result, Vagrant::Util::TemplateRenderer.render!(@template) + context "render method" do + should "call render_with the render! method" do + args = ["foo", "bar", "baz"] + Vagrant::Util::TemplateRenderer.expects(:render_with).with(:render, *args) + Vagrant::Util::TemplateRenderer.render(*args) + end + end + + context "render_string method" do + should "call render_with the render! method" do + args = ["foo", "bar", "baz"] + Vagrant::Util::TemplateRenderer.expects(:render_with).with(:render_string, *args) + Vagrant::Util::TemplateRenderer.render_string(*args) + end end end end