PageRenderTime 157ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/rails/actionpack/test/template/compiled_templates_test.rb

https://github.com/skdaksh/authlogic_example
Ruby | 203 lines | 179 code | 23 blank | 1 comment | 2 complexity | e0ecc3823a3415dca86ebd0e3b9c5540 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'controller/fake_models'
  3. class CompiledTemplatesTest < Test::Unit::TestCase
  4. def setup
  5. @compiled_templates = ActionView::Base::CompiledTemplates
  6. @compiled_templates.instance_methods.each do |m|
  7. @compiled_templates.send(:remove_method, m) if m =~ /^_run_/
  8. end
  9. end
  10. def test_template_gets_compiled
  11. with_caching(true) do
  12. assert_equal 0, @compiled_templates.instance_methods.size
  13. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  14. assert_equal 1, @compiled_templates.instance_methods.size
  15. end
  16. end
  17. def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
  18. with_caching(true) do
  19. assert_equal 0, @compiled_templates.instance_methods.size
  20. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  21. assert_equal "Hello world!", render(:file => "test/hello_world.erb", :locals => {:foo => "bar"})
  22. assert_equal 2, @compiled_templates.instance_methods.size
  23. end
  24. end
  25. def test_compiled_template_will_not_be_recompiled_when_rendered_with_identical_local_assigns
  26. with_caching(true) do
  27. assert_equal 0, @compiled_templates.instance_methods.size
  28. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  29. ActionView::Template.any_instance.expects(:compile!).never
  30. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  31. end
  32. end
  33. def test_template_changes_are_not_reflected_with_cached_template_loading
  34. with_caching(true) do
  35. with_reloading(false) do
  36. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  37. modify_template "test/hello_world.erb", "Goodbye world!" do
  38. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  39. end
  40. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  41. end
  42. end
  43. end
  44. def test_template_changes_are_reflected_without_cached_template_loading
  45. with_caching(true) do
  46. with_reloading(true) do
  47. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  48. modify_template "test/hello_world.erb", "Goodbye world!" do
  49. assert_equal "Goodbye world!", render(:file => "test/hello_world.erb")
  50. end
  51. assert_equal "Hello world!", render(:file => "test/hello_world.erb")
  52. end
  53. end
  54. end
  55. def test_template_becomes_missing_if_deleted_without_cached_template_loading
  56. with_reloading(true) do
  57. assert_equal 'Hello world!', render(:file => 'test/hello_world.erb')
  58. delete_template 'test/hello_world.erb' do
  59. assert_raise(ActionView::MissingTemplate) { render(:file => 'test/hello_world.erb') }
  60. end
  61. assert_equal 'Hello world!', render(:file => 'test/hello_world.erb')
  62. end
  63. end
  64. def test_swapping_template_handler_is_working_without_cached_template_loading
  65. with_reloading(true) do
  66. assert_equal 'Hello world!', render(:file => 'test/hello_world')
  67. delete_template 'test/hello_world.erb' do
  68. rename_template 'test/hello_world_from_rxml.builder', 'test/hello_world.builder' do
  69. assert_equal "<html>\n <p>Hello</p>\n</html>\n", render(:file => 'test/hello_world')
  70. end
  71. end
  72. assert_equal 'Hello world!', render(:file => 'test/hello_world')
  73. end
  74. end
  75. def test_adding_localized_template_will_take_precedence_without_cached_template_loading
  76. with_reloading(true) do
  77. assert_equal 'Hello world!', render(:file => 'test/hello_world')
  78. rename_template 'test/hello_world.da.html.erb', 'test/hello_world.en.html.erb' do
  79. assert_equal 'Hey verden', render(:file => 'test/hello_world')
  80. end
  81. end
  82. end
  83. def test_deleting_localized_template_will_fall_back_to_non_localized_template_without_cached_template_loading
  84. with_reloading(true) do
  85. rename_template 'test/hello_world.da.html.erb', 'test/hello_world.en.html.erb' do
  86. assert_equal 'Hey verden', render(:file => 'test/hello_world')
  87. delete_template 'test/hello_world.en.html.erb' do
  88. assert_equal 'Hello world!', render(:file => 'test/hello_world')
  89. end
  90. assert_equal 'Hey verden', render(:file => 'test/hello_world')
  91. end
  92. end
  93. end
  94. def test_parallel_reloadable_view_paths_are_working
  95. with_reloading(true) do
  96. view_paths_copy = new_reloadable_view_paths
  97. assert_equal 'Hello world!', render(:file => 'test/hello_world')
  98. with_view_paths(view_paths_copy, new_reloadable_view_paths) do
  99. assert_equal 'Hello world!', render(:file => 'test/hello_world')
  100. end
  101. modify_template 'test/hello_world.erb', 'Goodbye world!' do
  102. assert_equal 'Goodbye world!', render(:file => 'test/hello_world')
  103. modify_template 'test/hello_world.erb', 'So long, world!' do
  104. with_view_paths(view_paths_copy, new_reloadable_view_paths) do
  105. assert_equal 'So long, world!', render(:file => 'test/hello_world')
  106. end
  107. assert_equal 'So long, world!', render(:file => 'test/hello_world')
  108. end
  109. end
  110. end
  111. end
  112. private
  113. def render(*args)
  114. view_paths = @explicit_view_paths || ActionController::Base.view_paths
  115. ActionView::Base.new(view_paths, {}).render(*args)
  116. end
  117. def with_view_paths(*args)
  118. args.each do |view_paths|
  119. begin
  120. @explicit_view_paths = view_paths
  121. yield
  122. ensure
  123. @explicit_view_paths = nil
  124. end
  125. end
  126. end
  127. def reset_mtime_of(template_name, view_paths_to_use)
  128. view_paths_to_use.find_template(template_name).previously_last_modified = 10.seconds.ago unless ActionView::Base.cache_template_loading?
  129. end
  130. def modify_template(template, content, view_paths_to_use = ActionController::Base.view_paths)
  131. filename = filename_for(template)
  132. old_content = File.read(filename)
  133. begin
  134. File.open(filename, "wb+") { |f| f.write(content) }
  135. reset_mtime_of(template, view_paths_to_use)
  136. yield
  137. ensure
  138. File.open(filename, "wb+") { |f| f.write(old_content) }
  139. reset_mtime_of(template, view_paths_to_use)
  140. end
  141. end
  142. def filename_for(template)
  143. File.join(FIXTURE_LOAD_PATH, template)
  144. end
  145. def rename_template(old_name, new_name)
  146. File.rename(filename_for(old_name), filename_for(new_name))
  147. yield
  148. ensure
  149. File.rename(filename_for(new_name), filename_for(old_name))
  150. end
  151. def delete_template(template, &block)
  152. rename_template(template, File.join(File.dirname(template), "__#{File.basename(template)}"), &block)
  153. end
  154. def with_caching(perform_caching)
  155. old_perform_caching = ActionController::Base.perform_caching
  156. begin
  157. ActionController::Base.perform_caching = perform_caching
  158. yield
  159. ensure
  160. ActionController::Base.perform_caching = old_perform_caching
  161. end
  162. end
  163. def with_reloading(reload_templates, view_paths_owner = ActionController::Base)
  164. old_view_paths, old_cache_templates = view_paths_owner.view_paths, ActionView::Base.cache_template_loading
  165. begin
  166. ActionView::Base.cache_template_loading = !reload_templates
  167. view_paths_owner.view_paths = view_paths_for(reload_templates)
  168. yield
  169. ensure
  170. view_paths_owner.view_paths, ActionView::Base.cache_template_loading = old_view_paths, old_cache_templates
  171. end
  172. end
  173. def new_reloadable_view_paths
  174. ActionView::PathSet.new(CACHED_VIEW_PATHS.map(&:to_s))
  175. end
  176. def view_paths_for(reload_templates)
  177. # reloadable paths are cheap to create
  178. reload_templates ? new_reloadable_view_paths : CACHED_VIEW_PATHS
  179. end
  180. end