2009-02-04 23:10:03 -05:00
# encoding: utf-8
2008-06-25 06:47:01 -04:00
require 'abstract_unit'
require 'controller/fake_models'
2009-08-11 18:02:05 -04:00
class TestController < ActionController :: Base
end
2008-11-28 15:31:54 -05:00
module RenderTestCases
def setup_view ( paths )
2008-06-25 06:47:01 -04:00
@assigns = { :secret = > 'in the sauce' }
2008-11-28 15:31:54 -05:00
@view = ActionView :: Base . new ( paths , @assigns )
2010-03-18 18:52:43 -04:00
@controller_view = TestController . new . view_context
2009-01-25 23:50:02 -05:00
# Reload and register danish language for testing
I18n . reload!
I18n . backend . store_translations 'da' , { }
2009-02-07 00:23:50 -05:00
I18n . backend . store_translations 'pt-BR' , { }
2009-01-25 23:50:02 -05:00
# Ensure original are still the same since we are reindexing view paths
2009-06-08 16:33:18 -04:00
assert_equal ORIGINAL_LOCALES , I18n . available_locales . map { | l | l . to_s } . sort
2008-06-25 06:47:01 -04:00
end
2011-12-21 14:31:53 -05:00
def test_render_without_options
2012-04-16 15:33:15 -04:00
e = assert_raises ( ArgumentError ) { @view . render ( ) }
2011-12-21 14:31:53 -05:00
assert_match " You invoked render but did not give any of :partial, :template, :inline, :file or :text option. " , e . message
end
2008-06-25 06:47:01 -04:00
def test_render_file
2011-09-22 09:37:38 -04:00
assert_equal " Hello world! " , @view . render ( :file = > " test/hello_world " )
2008-06-25 06:47:01 -04:00
end
2011-09-22 05:54:13 -04:00
# Test if :formats, :locale etc. options are passed correctly to the resolvers.
def test_render_file_with_format
2011-09-22 18:42:20 -04:00
assert_match " <h1>No Comment</h1> " , @view . render ( :file = > " comments/empty " , :formats = > [ :html ] )
assert_match " <error>No Comment</error> " , @view . render ( :file = > " comments/empty " , :formats = > [ :xml ] )
assert_match " <error>No Comment</error> " , @view . render ( :file = > " comments/empty " , :formats = > :xml )
2011-09-22 05:54:13 -04:00
end
2011-09-22 09:03:05 -04:00
def test_render_template_with_format
2011-09-22 18:42:20 -04:00
assert_match " <h1>No Comment</h1> " , @view . render ( :template = > " comments/empty " , :formats = > [ :html ] )
assert_match " <error>No Comment</error> " , @view . render ( :template = > " comments/empty " , :formats = > [ :xml ] )
2011-09-22 09:03:05 -04:00
end
2011-12-21 14:31:53 -05:00
2013-07-14 13:11:43 -04:00
def test_rendered_format_without_format
@view . render ( :inline = > " test " )
assert_equal :html , @view . lookup_context . rendered_format
end
2012-02-21 20:55:56 -05:00
def test_render_partial_implicitly_use_format_of_the_rendered_template
@view . lookup_context . formats = [ :json ]
assert_equal " Hello world " , @view . render ( :template = > " test/one " , :formats = > [ :html ] )
end
2012-07-11 04:18:52 -04:00
def test_render_partial_implicitly_use_format_of_the_rendered_partial
@view . lookup_context . formats = [ :html ]
assert_equal " Third level " , @view . render ( :template = > " test/html_template " )
end
def test_render_partial_use_last_prepended_format_for_partials_with_the_same_names
@view . lookup_context . formats = [ :html ]
2013-04-03 19:56:12 -04:00
assert_equal " \n HTML Template, but JSON partial " , @view . render ( :template = > " test/change_priority " )
2012-07-11 04:18:52 -04:00
end
2012-02-20 13:48:43 -05:00
def test_render_template_with_a_missing_partial_of_another_format
2012-02-21 20:55:56 -05:00
@view . lookup_context . formats = [ :html ]
2012-02-20 13:48:43 -05:00
assert_raise ActionView :: Template :: Error , " Missing partial /missing with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder]} " do
@view . render ( :template = > " with_format " , :formats = > [ :json ] )
end
end
2011-09-22 10:02:14 -04:00
def test_render_file_with_locale
assert_equal " <h1>Kein Kommentar</h1> " , @view . render ( :file = > " comments/empty " , :locale = > [ :de ] )
2011-09-22 17:51:44 -04:00
assert_equal " <h1>Kein Kommentar</h1> " , @view . render ( :file = > " comments/empty " , :locale = > :de )
2011-09-22 10:02:14 -04:00
end
2011-12-21 14:31:53 -05:00
2011-09-22 10:02:14 -04:00
def test_render_template_with_locale
assert_equal " <h1>Kein Kommentar</h1> " , @view . render ( :template = > " comments/empty " , :locale = > [ :de ] )
end
2011-12-21 14:31:53 -05:00
2011-09-22 10:02:14 -04:00
def test_render_file_with_handlers
assert_equal " <h1>No Comment</h1> \n " , @view . render ( :file = > " comments/empty " , :handlers = > [ :builder ] )
2011-09-22 17:51:44 -04:00
assert_equal " <h1>No Comment</h1> \n " , @view . render ( :file = > " comments/empty " , :handlers = > :builder )
2011-09-22 10:02:14 -04:00
end
2011-12-21 14:31:53 -05:00
2011-09-22 10:02:14 -04:00
def test_render_template_with_handlers
assert_equal " <h1>No Comment</h1> \n " , @view . render ( :template = > " comments/empty " , :handlers = > [ :builder ] )
end
2011-09-22 09:03:05 -04:00
2012-05-13 20:22:29 -04:00
def test_render_raw_template_with_handlers
assert_equal " <%= hello_world %> \n " , @view . render ( :template = > " plain_text " )
end
def test_render_raw_template_with_quotes
assert_equal %q;Here are some characters: !@#$%^&*()-="'}{`; + " \n " , @view . render ( :template = > " plain_text_with_characters " )
end
2012-09-17 11:07:08 -04:00
def test_render_ruby_template_with_handlers
2012-09-11 02:08:40 -04:00
assert_equal " Hello from Ruby code " , @view . render ( :template = > " ruby_template " )
end
2012-09-17 11:07:08 -04:00
def test_render_ruby_template_inline
assert_equal '4' , @view . render ( :inline = > " (2**2).to_s " , :type = > :ruby )
2012-09-11 02:08:40 -04:00
end
2011-09-22 10:02:14 -04:00
def test_render_file_with_localization_on_context_level
2010-03-10 16:11:48 -05:00
old_locale , @view . locale = @view . locale , :da
2009-09-19 14:22:09 -04:00
assert_equal " Hey verden " , @view . render ( :file = > " test/hello_world " )
ensure
2010-03-10 16:11:48 -05:00
@view . locale = old_locale
2009-01-25 23:50:02 -05:00
end
2009-02-07 00:23:50 -05:00
def test_render_file_with_dashed_locale
2010-03-10 16:11:48 -05:00
old_locale , @view . locale = @view . locale , :" pt-BR "
2009-04-22 20:16:28 -04:00
assert_equal " Ola mundo " , @view . render ( :file = > " test/hello_world " )
2009-02-07 00:23:50 -05:00
ensure
2010-03-10 16:11:48 -05:00
@view . locale = old_locale
2009-02-07 00:23:50 -05:00
end
2008-08-13 20:15:35 -04:00
def test_render_file_at_top_level
2008-11-19 08:00:16 -05:00
assert_equal 'Elastica' , @view . render ( :file = > '/shared' )
2008-08-13 20:15:35 -04:00
end
2008-06-25 06:47:01 -04:00
def test_render_file_with_full_path
2011-09-22 09:37:38 -04:00
template_path = File . join ( File . dirname ( __FILE__ ) , '../fixtures/test/hello_world' )
2008-07-02 22:38:58 -04:00
assert_equal " Hello world! " , @view . render ( :file = > template_path )
2008-06-25 06:47:01 -04:00
end
def test_render_file_with_instance_variables
2011-09-22 09:37:38 -04:00
assert_equal " The secret is in the sauce \n " , @view . render ( :file = > " test/render_file_with_ivar " )
2008-06-25 06:47:01 -04:00
end
def test_render_file_with_locals
locals = { :secret = > 'in the sauce' }
2011-09-22 09:37:38 -04:00
assert_equal " The secret is in the sauce \n " , @view . render ( :file = > " test/render_file_with_locals " , :locals = > locals )
2008-06-25 06:47:01 -04:00
end
def test_render_file_not_using_full_path_with_dot_in_path
2008-11-19 08:00:16 -05:00
assert_equal " The secret is in the sauce \n " , @view . render ( :file = > " test/dot.directory/render_file_with_ivar " )
2008-06-25 06:47:01 -04:00
end
2008-11-19 08:00:16 -05:00
def test_render_partial_from_default
assert_equal " only partial " , @view . render ( " test/partial_only " )
end
2008-06-25 06:47:01 -04:00
def test_render_partial
assert_equal " only partial " , @view . render ( :partial = > " test/partial_only " )
end
2008-08-13 20:15:35 -04:00
def test_render_partial_with_format
assert_equal 'partial html' , @view . render ( :partial = > 'test/partial' )
end
2011-09-22 18:42:20 -04:00
def test_render_partial_with_selected_format
assert_equal 'partial html' , @view . render ( :partial = > 'test/partial' , :formats = > :html )
assert_equal 'partial js' , @view . render ( :partial = > 'test/partial' , :formats = > [ :js ] )
end
2008-08-13 20:15:35 -04:00
def test_render_partial_at_top_level
2011-09-22 09:37:38 -04:00
# file fixtures/_top_level_partial_only (not fixtures/test)
2008-08-13 20:15:35 -04:00
assert_equal 'top level partial' , @view . render ( :partial = > '/top_level_partial_only' )
end
def test_render_partial_with_format_at_top_level
2011-09-22 09:37:38 -04:00
# file fixtures/_top_level_partial.html (not fixtures/test, with format extension)
2008-08-13 20:15:35 -04:00
assert_equal 'top level partial html' , @view . render ( :partial = > '/top_level_partial' )
end
2008-08-09 11:34:19 -04:00
def test_render_partial_with_locals
assert_equal " 5 " , @view . render ( :partial = > " test/counter " , :locals = > { :counter_counter = > 5 } )
end
2008-11-19 08:00:16 -05:00
def test_render_partial_with_locals_from_default
assert_equal " only partial " , @view . render ( " test/partial_only " , :counter_counter = > 5 )
end
2011-07-13 13:06:03 -04:00
def test_render_partial_with_invalid_name
2012-04-16 15:33:15 -04:00
e = assert_raises ( ArgumentError ) { @view . render ( :partial = > " test/200 " ) }
2011-07-13 13:06:03 -04:00
assert_equal " The partial name (test/200) is not a valid Ruby identifier; " +
2012-05-04 14:15:27 -04:00
" make sure your partial name starts with a lowercase letter or underscore, " +
" and is followed by any combination of letters, numbers and underscores. " , e . message
2012-04-16 13:53:51 -04:00
end
def test_render_partial_with_missing_filename
2012-04-16 15:33:15 -04:00
e = assert_raises ( ArgumentError ) { @view . render ( :partial = > " test/ " ) }
2012-04-16 13:53:51 -04:00
assert_equal " The partial name (test/) is not a valid Ruby identifier; " +
2012-05-04 14:15:27 -04:00
" make sure your partial name starts with a lowercase letter or underscore, " +
" and is followed by any combination of letters, numbers and underscores. " , e . message
2011-07-13 13:06:03 -04:00
end
2011-11-19 08:02:56 -05:00
def test_render_partial_with_incompatible_object
2012-04-16 15:33:15 -04:00
e = assert_raises ( ArgumentError ) { @view . render ( :partial = > nil ) }
2011-12-20 09:12:38 -05:00
assert_equal " ' #{ nil . inspect } ' is not an ActiveModel-compatible object. It must implement :to_partial_path. " , e . message
2011-11-19 08:02:56 -05:00
end
2012-07-18 05:02:33 -04:00
def test_render_partial_with_hyphen
e = assert_raises ( ArgumentError ) { @view . render ( :partial = > " test/a-in " ) }
assert_equal " The partial name (test/a-in) is not a valid Ruby identifier; " +
" make sure your partial name starts with a lowercase letter or underscore, " +
" and is followed by any combination of letters, numbers and underscores. " , e . message
end
2008-06-25 06:47:01 -04:00
def test_render_partial_with_errors
2012-04-16 15:33:15 -04:00
e = assert_raises ( ActionView :: Template :: Error ) { @view . render ( :partial = > " test/raise " ) }
2010-03-02 17:04:57 -05:00
assert_match %r!method.*doesnt_exist! , e . message
2008-09-20 23:57:45 -04:00
assert_equal " " , e . sub_template_message
assert_equal " 1 " , e . line_number
2010-05-09 05:52:26 -04:00
assert_equal " 1: <%= doesnt_exist %> " , e . annoted_source_code . strip
2008-09-20 23:57:45 -04:00
assert_equal File . expand_path ( " #{ FIXTURE_LOAD_PATH } /test/_raise.html.erb " ) , e . file_name
end
2012-08-02 20:23:06 -04:00
def test_render_error_indentation
e = assert_raises ( ActionView :: Template :: Error ) { @view . render ( :partial = > " test/raise_indentation " ) }
error_lines = e . annoted_source_code . split ( " \n " )
assert_match %r!error\shere! , e . message
assert_equal " 11 " , e . line_number
assert_equal " 9: <p>Ninth paragraph</p> " , error_lines . second
assert_equal " 10: <p>Tenth paragraph</p> " , error_lines . third
end
2008-09-20 23:57:45 -04:00
def test_render_sub_template_with_errors
2012-04-16 15:33:15 -04:00
e = assert_raises ( ActionView :: Template :: Error ) { @view . render ( :template = > " test/sub_template_raise " ) }
2010-03-02 17:04:57 -05:00
assert_match %r!method.*doesnt_exist! , e . message
2008-09-20 23:57:45 -04:00
assert_equal " Trace of template inclusion: #{ File . expand_path ( " #{ FIXTURE_LOAD_PATH } /test/sub_template_raise.html.erb " ) } " , e . sub_template_message
assert_equal " 1 " , e . line_number
assert_equal File . expand_path ( " #{ FIXTURE_LOAD_PATH } /test/_raise.html.erb " ) , e . file_name
2008-06-25 06:47:01 -04:00
end
2010-10-10 17:11:50 -04:00
def test_render_file_with_errors
2012-04-16 15:33:15 -04:00
e = assert_raises ( ActionView :: Template :: Error ) { @view . render ( :file = > File . expand_path ( " test/_raise " , FIXTURE_LOAD_PATH ) ) }
2010-10-10 17:11:50 -04:00
assert_match %r!method.*doesnt_exist! , e . message
assert_equal " " , e . sub_template_message
assert_equal " 1 " , e . line_number
assert_equal " 1: <%= doesnt_exist %> " , e . annoted_source_code . strip
assert_equal File . expand_path ( " #{ FIXTURE_LOAD_PATH } /test/_raise.html.erb " ) , e . file_name
end
2009-03-07 15:05:18 -05:00
def test_render_object
assert_equal " Hello: david " , @view . render ( :partial = > " test/customer " , :object = > Customer . new ( " david " ) )
end
2010-10-07 18:12:11 -04:00
def test_render_object_with_array
assert_equal " [1, 2, 3] " , @view . render ( :partial = > " test/object_inspector " , :object = > [ 1 , 2 , 3 ] )
end
2008-06-25 06:47:01 -04:00
def test_render_partial_collection
assert_equal " Hello: davidHello: mary " , @view . render ( :partial = > " test/customer " , :collection = > [ Customer . new ( " david " ) , Customer . new ( " mary " ) ] )
end
2008-08-09 11:34:19 -04:00
2010-12-25 09:54:34 -05:00
def test_render_partial_collection_as_by_string
assert_equal " david david davidmary mary mary " ,
@view . render ( :partial = > " test/customer_with_var " , :collection = > [ Customer . new ( " david " ) , Customer . new ( " mary " ) ] , :as = > 'customer' )
end
def test_render_partial_collection_as_by_symbol
2008-08-09 11:34:19 -04:00
assert_equal " david david davidmary mary mary " ,
2008-07-02 11:38:50 -04:00
@view . render ( :partial = > " test/customer_with_var " , :collection = > [ Customer . new ( " david " ) , Customer . new ( " mary " ) ] , :as = > :customer )
end
2008-08-09 11:34:19 -04:00
2008-07-02 12:19:41 -04:00
def test_render_partial_collection_without_as
2009-08-12 02:43:15 -04:00
assert_equal " local_inspector,local_inspector_counter " ,
2008-07-02 12:19:41 -04:00
@view . render ( :partial = > " test/local_inspector " , :collection = > [ Customer . new ( " mary " ) ] )
end
2008-06-25 06:47:01 -04:00
2008-08-19 20:09:04 -04:00
def test_render_partial_with_empty_collection_should_return_nil
assert_nil @view . render ( :partial = > " test/customer " , :collection = > [ ] )
end
def test_render_partial_with_nil_collection_should_return_nil
assert_nil @view . render ( :partial = > " test/customer " , :collection = > nil )
end
2008-10-30 16:07:47 -04:00
def test_render_partial_with_nil_values_in_collection
assert_equal " Hello: davidHello: Anonymous " , @view . render ( :partial = > " test/customer " , :collection = > [ Customer . new ( " david " ) , nil ] )
end
2012-04-26 19:24:53 -04:00
2012-03-06 10:20:56 -05:00
def test_render_partial_with_layout_using_collection_and_template
assert_equal " <b>Hello: Amazon</b><b>Hello: Yahoo</b> " , @view . render ( :partial = > " test/customer " , :layout = > 'test/b_layout_for_partial' , :collection = > [ Customer . new ( " Amazon " ) , Customer . new ( " Yahoo " ) ] )
end
2008-10-30 16:07:47 -04:00
2012-04-26 19:47:22 -04:00
def test_render_partial_with_layout_using_collection_and_template_makes_current_item_available_in_layout
assert_equal '<b class="amazon">Hello: Amazon</b><b class="yahoo">Hello: Yahoo</b>' ,
@view . render ( :partial = > " test/customer " , :layout = > 'test/b_layout_for_partial_with_object' , :collection = > [ Customer . new ( " Amazon " ) , Customer . new ( " Yahoo " ) ] )
2012-04-26 19:24:53 -04:00
end
2012-04-26 19:47:22 -04:00
def test_render_partial_with_layout_using_collection_and_template_makes_current_item_counter_available_in_layout
assert_equal '<b data-counter="0">Hello: Amazon</b><b data-counter="1">Hello: Yahoo</b>' ,
@view . render ( :partial = > " test/customer " , :layout = > 'test/b_layout_for_partial_with_object_counter' , :collection = > [ Customer . new ( " Amazon " ) , Customer . new ( " Yahoo " ) ] )
end
def test_render_partial_with_layout_using_object_and_template_makes_object_available_in_layout
assert_equal '<b class="amazon">Hello: Amazon</b>' ,
@view . render ( :partial = > " test/customer " , :layout = > 'test/b_layout_for_partial_with_object' , :object = > Customer . new ( " Amazon " ) )
2012-04-26 19:36:35 -04:00
end
2008-08-22 11:04:27 -04:00
def test_render_partial_with_empty_array_should_return_nil
assert_nil @view . render ( :partial = > [ ] )
end
2009-08-11 18:02:05 -04:00
def test_render_partial_using_string
assert_equal " Hello: Anonymous " , @controller_view . render ( 'customer' )
end
def test_render_partial_with_locals_using_string
assert_equal " Hola: david " , @controller_view . render ( 'customer_greeting' , :greeting = > 'Hola' , :customer_greeting = > Customer . new ( " david " ) )
end
def test_render_partial_using_object
assert_equal " Hello: lifo " ,
@controller_view . render ( Customer . new ( " lifo " ) , :greeting = > " Hello " )
end
def test_render_partial_using_collection
customers = [ Customer . new ( " Amazon " ) , Customer . new ( " Yahoo " ) ]
assert_equal " Hello: AmazonHello: Yahoo " ,
@controller_view . render ( customers , :greeting = > " Hello " )
end
2012-12-11 21:20:07 -05:00
def test_render_partial_without_object_or_collection_does_not_generate_partial_name_local_variable
exception = assert_raises ActionView :: Template :: Error do
@controller_view . render ( " partial_name_local_variable " )
end
assert_match " undefined local variable or method `partial_name_local_variable' " , exception . message
end
2008-06-25 06:47:01 -04:00
# TODO: The reason for this test is unclear, improve documentation
def test_render_partial_and_fallback_to_layout
assert_equal " Before (Josh) \n \n After " , @view . render ( :partial = > " test/layout_for_partial " , :locals = > { :name = > " Josh " } )
end
# TODO: The reason for this test is unclear, improve documentation
def test_render_missing_xml_partial_and_raise_missing_template
2009-01-22 17:18:10 -05:00
@view . formats = [ :xml ]
2012-04-16 15:33:15 -04:00
assert_raises ( ActionView :: MissingTemplate ) { @view . render ( :partial = > " test/layout_for_partial " ) }
2009-08-15 01:32:40 -04:00
ensure
@view . formats = nil
2008-06-25 06:47:01 -04:00
end
2011-02-03 07:09:21 -05:00
def test_render_layout_with_block_and_other_partial_inside
2011-09-22 09:37:38 -04:00
render = @view . render ( :layout = > " test/layout_with_partial_and_yield " ) { " Yield! " }
2011-02-03 07:09:21 -05:00
assert_equal " Before \n partial html \n Yield! \n After \n " , render
end
2008-06-25 06:47:01 -04:00
def test_render_inline
assert_equal " Hello, World! " , @view . render ( :inline = > " Hello, World! " )
end
def test_render_inline_with_locals
assert_equal " Hello, Josh! " , @view . render ( :inline = > " Hello, <%= name %>! " , :locals = > { :name = > " Josh " } )
end
def test_render_fallbacks_to_erb_for_unknown_types
2008-11-28 12:18:28 -05:00
assert_equal " Hello, World! " , @view . render ( :inline = > " Hello, World! " , :type = > :bar )
2008-06-25 06:47:01 -04:00
end
2008-07-18 17:00:20 -04:00
CustomHandler = lambda do | template |
" @output_buffer = '' \n " +
" @output_buffer << 'source: #{ template . source . inspect } ' \n "
2008-06-25 06:47:01 -04:00
end
2010-12-09 07:15:40 -05:00
def test_render_inline_with_render_from_to_proc
ActionView :: Template . register_template_handler :ruby_handler , :source . to_proc
assert_equal '3' , @view . render ( :inline = > " (1 + 2).to_s " , :type = > :ruby_handler )
end
2008-06-25 06:47:01 -04:00
def test_render_inline_with_compilable_custom_type
2008-07-18 17:00:20 -04:00
ActionView :: Template . register_template_handler :foo , CustomHandler
2008-07-11 16:39:22 -04:00
assert_equal 'source: "Hello, World!"' , @view . render ( :inline = > " Hello, World! " , :type = > :foo )
2008-06-25 06:47:01 -04:00
end
def test_render_inline_with_locals_and_compilable_custom_type
2008-07-18 17:00:20 -04:00
ActionView :: Template . register_template_handler :foo , CustomHandler
2008-07-11 16:39:22 -04:00
assert_equal 'source: "Hello, <%= name %>!"' , @view . render ( :inline = > " Hello, <%= name %>! " , :locals = > { :name = > " Josh " } , :type = > :foo )
2008-06-25 06:47:01 -04:00
end
2012-04-26 19:24:53 -04:00
2012-02-20 14:48:14 -05:00
def test_render_knows_about_types_registered_when_extensions_are_checked_earlier_in_initialization
ActionView :: Template :: Handlers . extensions
ActionView :: Template . register_template_handler :foo , CustomHandler
assert ActionView :: Template :: Handlers . extensions . include? ( :foo )
end
2008-10-28 18:31:33 -04:00
2009-02-24 11:41:45 -05:00
def test_render_ignores_templates_with_malformed_template_handlers
2011-09-22 09:37:38 -04:00
ActiveSupport :: Deprecation . silence do
%w( malformed malformed.erb malformed.html.erb malformed.en.html.erb ) . each do | name |
2013-06-20 17:38:38 -04:00
assert File . exists? ( File . expand_path ( " #{ FIXTURE_LOAD_PATH } /test/malformed/ #{ name } ~ " ) ) , " Malformed file ( #{ name } ~) which should be ignored does not exists "
2012-04-16 15:33:15 -04:00
assert_raises ( ActionView :: MissingTemplate ) { @view . render ( :file = > " test/malformed/ #{ name } " ) }
2011-09-22 09:37:38 -04:00
end
2009-02-24 11:41:45 -05:00
end
end
2008-10-28 18:31:33 -04:00
def test_render_with_layout
assert_equal %( <title></title> \n Hello world! \n ) ,
2011-09-22 09:37:38 -04:00
@view . render ( :file = > " test/hello_world " , :layout = > " layouts/yield " )
2008-10-28 18:31:33 -04:00
end
2010-06-12 23:00:10 -04:00
def test_render_with_layout_which_has_render_inline
assert_equal %( welcome \n Hello world! \n ) ,
2011-09-22 09:37:38 -04:00
@view . render ( :file = > " test/hello_world " , :layout = > " layouts/yield_with_render_inline_inside " )
2010-06-12 23:00:10 -04:00
end
2010-11-10 22:30:40 -05:00
def test_render_with_layout_which_renders_another_partial
assert_equal %( partial html \n Hello world! \n ) ,
2011-09-22 09:37:38 -04:00
@view . render ( :file = > " test/hello_world " , :layout = > " layouts/yield_with_render_partial_inside " )
2010-11-10 22:30:40 -05:00
end
def test_render_layout_with_block_and_yield
assert_equal %( Content from block! \n ) ,
@view . render ( :layout = > " layouts/yield_only " ) { " Content from block! " }
end
def test_render_layout_with_block_and_yield_with_params
assert_equal %( Yield! Content from block! \n ) ,
@view . render ( :layout = > " layouts/yield_with_params " ) { | param | " #{ param } Content from block! " }
end
def test_render_layout_with_block_which_renders_another_partial_and_yields
assert_equal %( partial html \n Content from block! \n ) ,
@view . render ( :layout = > " layouts/partial_and_yield " ) { " Content from block! " }
end
2010-11-11 08:08:17 -05:00
def test_render_partial_and_layout_without_block_with_locals
assert_equal %( Before ( Foo! ) \n partial html \n After ) ,
@view . render ( :partial = > 'test/partial' , :layout = > 'test/layout_for_partial' , :locals = > { :name = > 'Foo!' } )
end
def test_render_partial_and_layout_without_block_with_locals_and_rendering_another_partial
assert_equal %( Before ( Foo! ) \n partial html \n partial with partial \n \n After ) ,
@view . render ( :partial = > 'test/partial_with_partial' , :layout = > 'test/layout_for_partial' , :locals = > { :name = > 'Foo!' } )
end
2010-11-13 10:38:40 -05:00
def test_render_layout_with_a_nested_render_layout_call
assert_equal %( Before ( Foo! ) \n Before ( Bar! ) \n partial html \n After \n partial with layout \n \n After ) ,
@view . render ( :partial = > 'test/partial_with_layout' , :layout = > 'test/layout_for_partial' , :locals = > { :name = > 'Foo!' } )
end
def test_render_layout_with_a_nested_render_layout_call_using_block_with_render_partial
assert_equal %( Before ( Foo! ) \n Before ( Bar! ) \n \n partial html \n \n Afterpartial with layout \n \n After ) ,
@view . render ( :partial = > 'test/partial_with_layout_block_partial' , :layout = > 'test/layout_for_partial' , :locals = > { :name = > 'Foo!' } )
end
def test_render_layout_with_a_nested_render_layout_call_using_block_with_render_content
assert_equal %( Before ( Foo! ) \n Before ( Bar! ) \n \n Content from inside layout! \n \n Afterpartial with layout \n \n After ) ,
@view . render ( :partial = > 'test/partial_with_layout_block_content' , :layout = > 'test/layout_for_partial' , :locals = > { :name = > 'Foo!' } )
end
2012-12-02 10:26:11 -05:00
def test_render_partial_with_layout_raises_descriptive_error
e = assert_raises ( ActionView :: MissingTemplate ) { @view . render ( partial : 'test/partial' , layout : true ) }
assert_match " Missing partial /true with " , e . message
end
2008-10-28 18:31:33 -04:00
def test_render_with_nested_layout
2010-05-17 11:41:54 -04:00
assert_equal %( <title>title</title> \n \n <div id="column">column</div> \n <div id="content">content</div> \n ) ,
2011-09-22 09:37:38 -04:00
@view . render ( :file = > " test/nested_layout " , :layout = > " layouts/yield " )
2008-10-28 18:31:33 -04:00
end
2009-02-04 23:10:03 -05:00
2010-02-23 12:50:05 -05:00
def test_render_with_file_in_layout
assert_equal %( \n <title>title</title> \n \n ) ,
2011-09-22 09:37:38 -04:00
@view . render ( :file = > " test/layout_render_file " )
2010-02-23 12:50:05 -05:00
end
2010-08-11 09:23:07 -04:00
def test_render_layout_with_object
assert_equal %( <title>David</title> ) ,
2011-09-22 09:37:38 -04:00
@view . render ( :file = > " test/layout_render_object " )
2010-08-11 09:23:07 -04:00
end
2012-10-27 10:03:18 -04:00
2012-09-14 11:25:07 -04:00
def test_render_with_passing_couple_extensions_to_one_register_template_handler_function_call
ActionView :: Template . register_template_handler :foo1 , :foo2 , CustomHandler
assert_equal @view . render ( :inline = > " Hello, World! " , :type = > :foo1 ) , @view . render ( :inline = > " Hello, World! " , :type = > :foo2 )
end
2012-10-27 10:03:18 -04:00
2012-09-14 11:25:07 -04:00
def test_render_throws_exception_when_no_extensions_passed_to_register_template_handler_function_call
assert_raises ( ArgumentError ) { ActionView :: Template . register_template_handler CustomHandler }
end
2008-06-25 06:47:01 -04:00
end
2008-11-28 12:18:28 -05:00
2009-02-02 14:49:32 -05:00
class CachedViewRenderTest < ActiveSupport :: TestCase
2008-11-28 15:31:54 -05:00
include RenderTestCases
# Ensure view path cache is primed
def setup
view_paths = ActionController :: Base . view_paths
2011-05-09 05:17:24 -04:00
assert_equal ActionView :: OptimizedFileSystemResolver , view_paths . first . class
2008-11-28 15:31:54 -05:00
setup_view ( view_paths )
end
2010-04-02 22:21:19 -04:00
def teardown
GC . start
end
2008-11-28 15:31:54 -05:00
end
2009-02-02 14:49:32 -05:00
class LazyViewRenderTest < ActiveSupport :: TestCase
2008-11-28 15:31:54 -05:00
include RenderTestCases
2008-11-28 12:18:28 -05:00
# Test the same thing as above, but make sure the view path
# is not eager loaded
def setup
2010-03-08 10:32:40 -05:00
path = ActionView :: FileSystemResolver . new ( FIXTURE_LOAD_PATH )
2011-08-09 15:10:25 -04:00
view_paths = ActionView :: PathSet . new ( [ path ] )
2010-03-08 10:32:40 -05:00
assert_equal ActionView :: FileSystemResolver . new ( FIXTURE_LOAD_PATH ) , view_paths . first
2008-11-28 15:31:54 -05:00
setup_view ( view_paths )
2008-11-28 12:18:28 -05:00
end
2010-04-02 22:21:19 -04:00
def teardown
GC . start
end
2010-04-08 22:27:22 -04:00
2011-12-25 06:34:58 -05:00
def test_render_utf8_template_with_magic_comment
with_external_encoding Encoding :: ASCII_8BIT do
result = @view . render ( :file = > " test/utf8_magic " , :formats = > [ :html ] , :layouts = > " layouts/yield " )
assert_equal Encoding :: UTF_8 , result . encoding
assert_equal " \n Русский \n текст \n \n UTF-8 \n UTF-8 \n UTF-8 \n " , result
2010-04-08 22:27:22 -04:00
end
2011-12-25 06:34:58 -05:00
end
2010-04-08 22:27:22 -04:00
2011-12-25 06:34:58 -05:00
def test_render_utf8_template_with_default_external_encoding
with_external_encoding Encoding :: UTF_8 do
result = @view . render ( :file = > " test/utf8 " , :formats = > [ :html ] , :layouts = > " layouts/yield " )
assert_equal Encoding :: UTF_8 , result . encoding
assert_equal " Русский текст \n \n UTF-8 \n UTF-8 \n UTF-8 \n " , result
2010-04-08 22:27:22 -04:00
end
2011-12-25 06:34:58 -05:00
end
2010-04-08 22:27:22 -04:00
2011-12-25 06:34:58 -05:00
def test_render_utf8_template_with_incompatible_external_encoding
with_external_encoding Encoding :: SHIFT_JIS do
2012-04-16 15:33:15 -04:00
e = assert_raises ( ActionView :: Template :: Error ) { @view . render ( :file = > " test/utf8 " , :formats = > [ :html ] , :layouts = > " layouts/yield " ) }
assert_match 'Your template was not saved as valid Shift_JIS' , e . original_exception . message
2010-04-08 22:27:22 -04:00
end
2011-12-25 06:34:58 -05:00
end
2010-04-08 22:27:22 -04:00
2011-12-25 06:34:58 -05:00
def test_render_utf8_template_with_partial_with_incompatible_encoding
with_external_encoding Encoding :: SHIFT_JIS do
2012-04-16 15:33:15 -04:00
e = assert_raises ( ActionView :: Template :: Error ) { @view . render ( :file = > " test/utf8_magic_with_bare_partial " , :formats = > [ :html ] , :layouts = > " layouts/yield " ) }
assert_match 'Your template was not saved as valid Shift_JIS' , e . original_exception . message
2010-04-08 23:22:07 -04:00
end
2011-12-25 06:34:58 -05:00
end
2010-04-08 23:22:07 -04:00
2011-12-25 06:34:58 -05:00
def with_external_encoding ( encoding )
old = Encoding . default_external
silence_warnings { Encoding . default_external = encoding }
yield
ensure
silence_warnings { Encoding . default_external = old }
2010-04-08 22:27:22 -04:00
end
2008-11-28 12:18:28 -05:00
end