From 51b6619d4e78d9948952dab4709274ab57f41206 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 3 Mar 2008 04:01:35 +0000 Subject: Refactor partial rendering into a PartialTemplate class. [Pratik] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8976 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/test/controller/custom_handler_test.rb | 6 +-- actionpack/test/controller/layout_test.rb | 4 +- actionpack/test/controller/new_render_test.rb | 20 ++++++- .../test/fixtures/test/_customer_counter.erb | 1 + .../test/template/compiled_templates_test.rb | 24 ++++----- actionpack/test/template/template_object_test.rb | 62 ++++++++++++++++++++++ 6 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 actionpack/test/fixtures/test/_customer_counter.erb create mode 100644 actionpack/test/template/template_object_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/custom_handler_test.rb b/actionpack/test/controller/custom_handler_test.rb index 932b8c15c3..cdde00cccc 100644 --- a/actionpack/test/controller/custom_handler_test.rb +++ b/actionpack/test/controller/custom_handler_test.rb @@ -5,9 +5,9 @@ class CustomHandler < ActionView::TemplateHandler @view = view end - def render( template, local_assigns ) - [ template, - local_assigns, + def render( template ) + [ template.source, + template.locals, @view ] end end diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 11ad4a20af..56d7f69774 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -35,8 +35,8 @@ class MabView < ActionView::TemplateHandler def initialize(view) end - def render(text, locals = {}) - text + def render(template) + template.source end end diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 928c46cb6d..0f14de54b1 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -146,6 +146,14 @@ class NewRenderTestController < ActionController::Base def partial_collection render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ] end + + def partial_collection_with_spacer + render :partial => "customer", :spacer_template => "partial_only", :collection => [ Customer.new("david"), Customer.new("mary") ] + end + + def partial_collection_with_counter + render :partial => "customer_counter", :collection => [ Customer.new("david"), Customer.new("mary") ] + end def partial_collection_with_locals render :partial => "customer_greeting", :collection => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" } @@ -712,11 +720,21 @@ EOS get :partial_collection assert_equal "Hello: davidHello: mary", @response.body end - + + def test_partial_collection_with_counter + get :partial_collection_with_counter + assert_equal "david1mary2", @response.body + end + def test_partial_collection_with_locals get :partial_collection_with_locals assert_equal "Bonjour: davidBonjour: mary", @response.body end + + def test_partial_collection_with_spacer + get :partial_collection_with_spacer + assert_equal "Hello: davidonly partialHello: mary", @response.body + end def test_partial_collection_shorthand_with_locals get :partial_collection_shorthand_with_locals diff --git a/actionpack/test/fixtures/test/_customer_counter.erb b/actionpack/test/fixtures/test/_customer_counter.erb new file mode 100644 index 0000000000..3435979dba --- /dev/null +++ b/actionpack/test/fixtures/test/_customer_counter.erb @@ -0,0 +1 @@ +<%= customer_counter.name %><%= customer_counter_counter %> \ No newline at end of file diff --git a/actionpack/test/template/compiled_templates_test.rb b/actionpack/test/template/compiled_templates_test.rb index ad002e4cce..b969575235 100644 --- a/actionpack/test/template/compiled_templates_test.rb +++ b/actionpack/test/template/compiled_templates_test.rb @@ -109,9 +109,9 @@ class CompiledTemplateTests < Test::Unit::TestCase # All templates are rendered at t+2 Time.expects(:now).times(windows ? 2 : 3).returns(t + 2.seconds) - v.send(:compile_and_render_template, @handler, ta) - v.send(:compile_and_render_template, @handler, tb) - v.send(:compile_and_render_template, @handler, ts) unless windows + v.send(:render_template, ta) + v.send(:render_template, tb) + v.send(:render_template, ts) unless windows a_n = v.method_names[@a] b_n = v.method_names[@b] s_n = v.method_names[@s] unless windows @@ -129,9 +129,9 @@ class CompiledTemplateTests < Test::Unit::TestCase assert !@handler.send(:compile_template?, ta) assert !@handler.send(:compile_template?, tb) assert !@handler.send(:compile_template?, ts) unless windows - v.send(:compile_and_render_template, @handler, ta) - v.send(:compile_and_render_template, @handler, tb) - v.send(:compile_and_render_template, @handler, ts) unless windows + v.send(:render_template, ta) + v.send(:render_template, tb) + v.send(:render_template, ts) unless windows # none of the files have changed since last compile assert @handler.compile_time[a_n] < t + 3.seconds assert @handler.compile_time[b_n] < t + 3.seconds @@ -154,9 +154,9 @@ class CompiledTemplateTests < Test::Unit::TestCase # Only the symlink template gets rendered at t+3 Time.stubs(:now).returns(t + 3.seconds) unless windows - v.send(:compile_and_render_template, @handler, ta) - v.send(:compile_and_render_template, @handler, tb) - v.send(:compile_and_render_template, @handler, ts) unless windows + v.send(:render_template, ta) + v.send(:render_template, tb) + v.send(:render_template, ts) unless windows # the symlink has changed since last compile assert @handler.compile_time[a_n] < t + 3.seconds assert @handler.compile_time[b_n] < t + 3.seconds @@ -179,9 +179,9 @@ class CompiledTemplateTests < Test::Unit::TestCase assert @handler.send(:compile_template?, ts) unless windows Time.expects(:now).times(windows ? 1 : 2).returns(t + 5.seconds) - v.send(:compile_and_render_template, @handler, ta) - v.send(:compile_and_render_template, @handler, tb) - v.send(:compile_and_render_template, @handler, ts) unless windows + v.send(:render_template, ta) + v.send(:render_template, tb) + v.send(:render_template, ts) unless windows # the file at the end of the symlink has changed since last compile # both the symlink and the file at the end of it should be recompiled assert @handler.compile_time[a_n] < t + 5.seconds diff --git a/actionpack/test/template/template_object_test.rb b/actionpack/test/template/template_object_test.rb new file mode 100644 index 0000000000..780df17f0c --- /dev/null +++ b/actionpack/test/template/template_object_test.rb @@ -0,0 +1,62 @@ +require 'abstract_unit' + +class TemplateObjectTest < Test::Unit::TestCase + LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures') + ActionView::TemplateFinder.process_view_paths(LOAD_PATH_ROOT) + + class TemplateTest < Test::Unit::TestCase + def setup + @view = ActionView::Base.new(LOAD_PATH_ROOT) + @path = "test/hello_world.erb" + end + + def test_should_create_valid_template + template = ActionView::Template.new(@view, @path, true) + + assert_kind_of ActionView::TemplateHandlers::ERB, template.handler + assert_equal "test/hello_world.erb", template.path + assert_nil template.instance_variable_get(:"@source") + assert_equal "erb", template.extension + end + + uses_mocha 'Template preparation tests' do + + def test_should_prepare_template_properly + template = ActionView::Template.new(@view, @path, true) + view = template.instance_variable_get(:"@view") + + view.expects(:evaluate_assigns) + template.handler.expects(:compile_template).with(template) + view.expects(:method_names).returns({}) + + template.prepare! + end + + end + end + + class PartialTemplateTest < Test::Unit::TestCase + def setup + @view = ActionView::Base.new(LOAD_PATH_ROOT) + @path = "test/partial_only" + end + + def test_should_create_valid_partial_template + template = ActionView::PartialTemplate.new(@view, @path, nil) + + assert_equal "test/_partial_only", template.path + assert_equal :partial_only, template.variable_name + + assert template.locals.has_key?(:object) + assert template.locals.has_key?(:partial_only) + end + + uses_mocha 'Partial template preparation tests' do + def test_should_prepare_on_initialization + ActionView::PartialTemplate.any_instance.expects(:prepare!) + template = ActionView::PartialTemplate.new(@view, @path, 1) + end + end + end + +end -- cgit v1.2.3