render_test.rb 24.7 KB
Newer Older
1
# encoding: utf-8
2 3 4
require 'abstract_unit'
require 'controller/fake_models'

Y
Yehuda Katz 已提交
5 6 7
class TestController < ActionController::Base
end

8 9
module RenderTestCases
  def setup_view(paths)
10
    @assigns = { :secret => 'in the sauce' }
11
    @view = ActionView::Base.new(paths, @assigns)
12
    @controller_view = TestController.new.view_context
J
Joshua Peek 已提交
13 14 15

    # Reload and register danish language for testing
    I18n.backend.store_translations 'da', {}
16
    I18n.backend.store_translations 'pt-BR', {}
J
Joshua Peek 已提交
17 18

    # Ensure original are still the same since we are reindexing view paths
19
    assert_equal ORIGINAL_LOCALES, I18n.available_locales.map {|l| l.to_s }.sort
20 21
  end

22
  def test_render_without_options
23
    e = assert_raises(ArgumentError) { @view.render() }
24
    assert_match(/You invoked render but did not give any of (.+) option./, e.message)
25 26
  end

27
  def test_render_file
28
    assert_equal "Hello world!", @view.render(:file => "test/hello_world")
29 30
  end

31 32
  # Test if :formats, :locale etc. options are passed correctly to the resolvers.
  def test_render_file_with_format
33 34 35
    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)
36 37
  end

38
  def test_render_template_with_format
39 40
    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])
41
  end
42

43 44 45 46 47
  def test_rendered_format_without_format
    @view.render(:inline => "test")
    assert_equal :html, @view.lookup_context.rendered_format
  end

48 49 50 51 52
  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

53 54 55 56 57 58 59
  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]
C
Carlos Antonio da Silva 已提交
60
    assert_equal "\nHTML Template, but JSON partial", @view.render(:template => "test/change_priority")
61 62
  end

63
  def test_render_template_with_a_missing_partial_of_another_format
64
    @view.lookup_context.formats = [:html]
65
    assert_raise ActionView::Template::Error, "Missing partial /_missing with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder]}" do
66 67 68 69
      @view.render(:template => "with_format", :formats => [:json])
    end
  end

70 71
  def test_render_file_with_locale
    assert_equal "<h1>Kein Kommentar</h1>", @view.render(:file => "comments/empty", :locale => [:de])
72
    assert_equal "<h1>Kein Kommentar</h1>", @view.render(:file => "comments/empty", :locale => :de)
73
  end
74

75 76 77
  def test_render_template_with_locale
    assert_equal "<h1>Kein Kommentar</h1>", @view.render(:template => "comments/empty", :locale => [:de])
  end
78

79 80
  def test_render_file_with_handlers
    assert_equal "<h1>No Comment</h1>\n", @view.render(:file => "comments/empty", :handlers => [:builder])
81
    assert_equal "<h1>No Comment</h1>\n", @view.render(:file => "comments/empty", :handlers => :builder)
82
  end
83

84 85 86
  def test_render_template_with_handlers
    assert_equal "<h1>No Comment</h1>\n", @view.render(:template => "comments/empty", :handlers => [:builder])
  end
87

88 89 90 91 92 93 94 95
  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

96
  def test_render_ruby_template_with_handlers
G
Guillermo Iguaran 已提交
97 98 99
    assert_equal "Hello from Ruby code", @view.render(:template => "ruby_template")
  end

100 101
  def test_render_ruby_template_inline
    assert_equal '4', @view.render(:inline => "(2**2).to_s", :type => :ruby)
G
Guillermo Iguaran 已提交
102 103
  end

104
  def test_render_file_with_localization_on_context_level
105
    old_locale, @view.locale = @view.locale, :da
106 107
    assert_equal "Hey verden", @view.render(:file => "test/hello_world")
  ensure
108
    @view.locale = old_locale
J
Joshua Peek 已提交
109 110
  end

111
  def test_render_file_with_dashed_locale
112
    old_locale, @view.locale = @view.locale, :"pt-BR"
113
    assert_equal "Ola mundo", @view.render(:file => "test/hello_world")
114
  ensure
115
    @view.locale = old_locale
116 117
  end

118
  def test_render_file_at_top_level
119
    assert_equal 'Elastica', @view.render(:file => '/shared')
120 121
  end

122
  def test_render_file_with_full_path
123
    template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world')
124
    assert_equal "Hello world!", @view.render(:file => template_path)
125 126 127
  end

  def test_render_file_with_instance_variables
128
    assert_equal "The secret is in the sauce\n", @view.render(:file => "test/render_file_with_ivar")
129 130 131 132
  end

  def test_render_file_with_locals
    locals = { :secret => 'in the sauce' }
133
    assert_equal "The secret is in the sauce\n", @view.render(:file => "test/render_file_with_locals", :locals => locals)
134 135 136
  end

  def test_render_file_not_using_full_path_with_dot_in_path
137
    assert_equal "The secret is in the sauce\n", @view.render(:file => "test/dot.directory/render_file_with_ivar")
138 139
  end

140 141 142 143
  def test_render_partial_from_default
    assert_equal "only partial", @view.render("test/partial_only")
  end

144 145 146 147
  def test_render_partial
    assert_equal "only partial", @view.render(:partial => "test/partial_only")
  end

148 149 150 151
  def test_render_partial_with_format
    assert_equal 'partial html', @view.render(:partial => 'test/partial')
  end

152 153 154 155 156
  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

157
  def test_render_partial_at_top_level
158
    # file fixtures/_top_level_partial_only (not fixtures/test)
159 160 161 162
    assert_equal 'top level partial', @view.render(:partial => '/top_level_partial_only')
  end

  def test_render_partial_with_format_at_top_level
163
    # file fixtures/_top_level_partial.html (not fixtures/test, with format extension)
164 165 166
    assert_equal 'top level partial html', @view.render(:partial => '/top_level_partial')
  end

167 168 169 170
  def test_render_partial_with_locals
    assert_equal "5", @view.render(:partial => "test/counter", :locals => { :counter_counter => 5 })
  end

171 172 173 174
  def test_render_partial_with_locals_from_default
    assert_equal "only partial", @view.render("test/partial_only", :counter_counter => 5)
  end

175
  def test_render_partial_with_invalid_name
176
    e = assert_raises(ArgumentError) { @view.render(:partial => "test/200") }
177
    assert_equal "The partial name (test/200) is not a valid Ruby identifier; " +
178 179
      "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
180 181 182
  end

  def test_render_partial_with_missing_filename
183
    e = assert_raises(ArgumentError) { @view.render(:partial => "test/") }
184
    assert_equal "The partial name (test/) is not a valid Ruby identifier; " +
185 186
      "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
187 188
  end

189
  def test_render_partial_with_incompatible_object
190
    e = assert_raises(ArgumentError) { @view.render(:partial => nil) }
191
    assert_equal "'#{nil.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.", e.message
192 193
  end

194 195 196 197 198 199 200
  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

201
  def test_render_partial_with_errors
202
    e = assert_raises(ActionView::Template::Error) { @view.render(:partial => "test/raise") }
203
    assert_match %r!method.*doesnt_exist!, e.message
204 205
    assert_equal "", e.sub_template_message
    assert_equal "1", e.line_number
206
    assert_equal "1: <%= doesnt_exist %>", e.annoted_source_code.strip
207 208 209
    assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
  end

210 211 212 213 214 215 216 217 218
  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

219
  def test_render_sub_template_with_errors
220
    e = assert_raises(ActionView::Template::Error) { @view.render(:template => "test/sub_template_raise") }
221
    assert_match %r!method.*doesnt_exist!, e.message
222 223 224
    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
225 226
  end

227
  def test_render_file_with_errors
228
    e = assert_raises(ActionView::Template::Error) { @view.render(:file => File.expand_path("test/_raise", FIXTURE_LOAD_PATH)) }
229 230 231 232 233 234 235
    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

236 237 238 239
  def test_render_object
    assert_equal "Hello: david", @view.render(:partial => "test/customer", :object => Customer.new("david"))
  end

240 241 242 243
  def test_render_object_with_array
    assert_equal "[1, 2, 3]", @view.render(:partial => "test/object_inspector", :object => [1, 2, 3])
  end

244 245 246
  def test_render_partial_collection
    assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
  end
247

248 249 250 251 252 253
  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
254
    assert_equal "david david davidmary mary mary",
255 256
      @view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer)
  end
257

258
  def test_render_partial_collection_without_as
259
    assert_equal "local_inspector,local_inspector_counter,local_inspector_iteration",
260 261
      @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ])
  end
262

263 264 265 266 267 268 269 270
  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

271 272 273
  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
274

275 276 277
  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
278

279 280 281
  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") ])
282 283
  end

284 285 286 287 288 289 290 291
  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"))
292 293
  end

294 295 296 297
  def test_render_partial_with_empty_array_should_return_nil
    assert_nil @view.render(:partial => [])
  end

Y
Yehuda Katz 已提交
298 299 300 301 302 303 304 305
  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

306 307 308 309 310 311 312 313 314 315
  def test_render_partial_with_object_uses_render_partial_path
    assert_equal "Hello: lifo",
      @controller_view.render(:partial => Customer.new("lifo"), :locals => {:greeting => "Hello"})
  end

  def test_render_partial_with_object_and_format_uses_render_partial_path
    assert_equal "<greeting>Hello</greeting><name>lifo</name>",
      @controller_view.render(:partial => Customer.new("lifo"), :formats => :xml, :locals => {:greeting => "Hello"})
  end

Y
Yehuda Katz 已提交
316 317 318 319 320 321 322 323 324 325 326
  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

E
Eugene Gilburg 已提交
327 328 329 330
  def test_render_partial_using_collection_without_path
    assert_equal "hi good customer: david0", @controller_view.render([ GoodCustomer.new("david") ], greeting: "hi")
  end

331 332 333 334
  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
335
    assert_instance_of NameError, exception.original_exception
336
    assert_equal :partial_name_local_variable, exception.original_exception.name
337 338
  end

339 340 341 342 343 344 345
  # TODO: The reason for this test is unclear, improve documentation
  def test_render_partial_and_fallback_to_layout
    assert_equal "Before (Josh)\n\nAfter", @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
346
    @view.formats = [:xml]
347
    assert_raises(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
348 349
  ensure
    @view.formats = nil
350 351
  end

A
Anton Astashov 已提交
352
  def test_render_layout_with_block_and_other_partial_inside
353
    render = @view.render(:layout => "test/layout_with_partial_and_yield") { "Yield!" }
A
Anton Astashov 已提交
354 355 356
    assert_equal "Before\npartial html\nYield!\nAfter\n", render
  end

357 358 359 360 361 362 363 364 365
  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
366
    assert_equal "Hello, World!", @view.render(:inline => "Hello, World!", :type => :bar)
367 368
  end

369 370 371
  CustomHandler = lambda do |template|
    "@output_buffer = ''\n" +
      "@output_buffer << 'source: #{template.source.inspect}'\n"
372 373
  end

374 375
  def test_render_inline_with_render_from_to_proc
    ActionView::Template.register_template_handler :ruby_handler, :source.to_proc
376 377 378
    assert_equal '3', @view.render(inline: "(1 + 2).to_s", type: :ruby_handler)
  ensure
    ActionView::Template.unregister_template_handler :ruby_handler
379 380
  end

381
  def test_render_inline_with_compilable_custom_type
382
    ActionView::Template.register_template_handler :foo, CustomHandler
383 384 385
    assert_equal 'source: "Hello, World!"', @view.render(inline: "Hello, World!", type: :foo)
  ensure
    ActionView::Template.unregister_template_handler :foo
386 387 388
  end

  def test_render_inline_with_locals_and_compilable_custom_type
389
    ActionView::Template.register_template_handler :foo, CustomHandler
390 391 392
    assert_equal 'source: "Hello, <%= name %>!"', @view.render(inline: "Hello, <%= name %>!", locals: { name: "Josh" }, type: :foo)
  ensure
    ActionView::Template.unregister_template_handler :foo
393
  end
394

E
Eugene Gilburg 已提交
395 396 397 398 399 400 401 402
  def test_render_body
    assert_equal 'some body', @view.render(body: 'some body')
  end

  def test_render_plain
    assert_equal 'some plaintext', @view.render(plain: 'some plaintext')
  end

403 404 405 406
  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)
407 408 409 410 411 412 413 414 415 416 417
  ensure
    ActionView::Template.unregister_template_handler :foo
  end

  def test_render_does_not_use_unregistered_extension_and_template_handler
    ActionView::Template.register_template_handler :foo, CustomHandler
    ActionView::Template.unregister_template_handler :foo
    assert_not ActionView::Template::Handlers.extensions.include?(:foo)
    assert_equal "Hello, World!", @view.render(inline: "Hello, World!", type: :foo)
  ensure
    ActionView::Template::Handlers.class_variable_get(:@@template_handlers).delete(:foo)
418
  end
419

420
  def test_render_ignores_templates_with_malformed_template_handlers
421 422
    ActiveSupport::Deprecation.silence do
      %w(malformed malformed.erb malformed.html.erb malformed.en.html.erb).each do |name|
A
Arun Agrawal 已提交
423
        assert File.exist?(File.expand_path("#{FIXTURE_LOAD_PATH}/test/malformed/#{name}~")), "Malformed file (#{name}~) which should be ignored does not exists"
424
        assert_raises(ActionView::MissingTemplate) { @view.render(:file => "test/malformed/#{name}") }
425
      end
426 427 428
    end
  end

429 430
  def test_render_with_layout
    assert_equal %(<title></title>\nHello world!\n),
431
      @view.render(:file => "test/hello_world", :layout => "layouts/yield")
432 433
  end

434 435
  def test_render_with_layout_which_has_render_inline
    assert_equal %(welcome\nHello world!\n),
436
      @view.render(:file => "test/hello_world", :layout => "layouts/yield_with_render_inline_inside")
437 438
  end

439 440
  def test_render_with_layout_which_renders_another_partial
    assert_equal %(partial html\nHello world!\n),
441
      @view.render(:file => "test/hello_world", :layout => "layouts/yield_with_render_partial_inside")
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  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\nContent from block!\n),
      @view.render(:layout => "layouts/partial_and_yield") { "Content from block!" }
  end

459 460 461 462 463 464 465 466 467 468
  def test_render_partial_and_layout_without_block_with_locals
    assert_equal %(Before (Foo!)\npartial html\nAfter),
      @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!)\npartial html\npartial with partial\n\nAfter),
      @view.render(:partial => 'test/partial_with_partial', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
  end

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
  def test_render_layout_with_a_nested_render_layout_call
    assert_equal %(Before (Foo!)\nBefore (Bar!)\npartial html\nAfter\npartial with layout\n\nAfter),
      @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!)\nBefore (Bar!)\n\n  partial html\n\nAfterpartial with layout\n\nAfter),
      @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!)\nBefore (Bar!)\n\n  Content from inside layout!\n\nAfterpartial with layout\n\nAfter),
      @view.render(:partial => 'test/partial_with_layout_block_content', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
  end

484 485
  def test_render_partial_with_layout_raises_descriptive_error
    e = assert_raises(ActionView::MissingTemplate) { @view.render(partial: 'test/partial', layout: true) }
486
    assert_match "Missing partial /_true with", e.message
487 488
  end

489
  def test_render_with_nested_layout
490
    assert_equal %(<title>title</title>\n\n<div id="column">column</div>\n<div id="content">content</div>\n),
491
      @view.render(:file => "test/nested_layout", :layout => "layouts/yield")
492
  end
493

494 495
  def test_render_with_file_in_layout
    assert_equal %(\n<title>title</title>\n\n),
496
      @view.render(:file => "test/layout_render_file")
497
  end
498 499 500

  def test_render_layout_with_object
    assert_equal %(<title>David</title>),
501
      @view.render(:file => "test/layout_render_object")
502
  end
503

504 505
  def test_render_with_passing_couple_extensions_to_one_register_template_handler_function_call
    ActionView::Template.register_template_handler :foo1, :foo2, CustomHandler
506 507 508
    assert_equal @view.render(inline: "Hello, World!", type: :foo1), @view.render(inline: "Hello, World!", type: :foo2)
  ensure
    ActionView::Template.unregister_template_handler :foo1, :foo2
509
  end
510

511 512 513
  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
514
end
515

Y
Yehuda Katz 已提交
516
class CachedViewRenderTest < ActiveSupport::TestCase
517 518 519 520 521
  include RenderTestCases

  # Ensure view path cache is primed
  def setup
    view_paths = ActionController::Base.view_paths
522
    assert_equal ActionView::OptimizedFileSystemResolver, view_paths.first.class
523 524
    setup_view(view_paths)
  end
W
wycats 已提交
525 526 527

  def teardown
    GC.start
528
    I18n.reload!
W
wycats 已提交
529
  end
530 531
end

Y
Yehuda Katz 已提交
532
class LazyViewRenderTest < ActiveSupport::TestCase
533 534
  include RenderTestCases

535 536 537
  # Test the same thing as above, but make sure the view path
  # is not eager loaded
  def setup
538
    path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH)
A
Aaron Patterson 已提交
539
    view_paths = ActionView::PathSet.new([path])
540
    assert_equal ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH), view_paths.first
541
    setup_view(view_paths)
542
  end
W
wycats 已提交
543 544 545

  def teardown
    GC.start
546
    I18n.reload!
W
wycats 已提交
547
  end
548

549 550 551 552 553
  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\nUTF-8\nUTF-8\nUTF-8\n", result
554
    end
555
  end
556

557 558 559 560 561
  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\nUTF-8\nUTF-8\nUTF-8\n", result
562
    end
563
  end
564

565 566
  def test_render_utf8_template_with_incompatible_external_encoding
    with_external_encoding Encoding::SHIFT_JIS do
567 568
      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
569
    end
570
  end
571

572 573
  def test_render_utf8_template_with_partial_with_incompatible_encoding
    with_external_encoding Encoding::SHIFT_JIS do
574 575
      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
576
    end
577
  end
578

579 580 581 582 583 584
  def with_external_encoding(encoding)
    old = Encoding.default_external
    silence_warnings { Encoding.default_external = encoding }
    yield
  ensure
    silence_warnings { Encoding.default_external = old }
585
  end
586
end