1 require 'abstract_unit'
2 require 'controller/fake_models'
4 class CompiledTemplatesTest
< Test
::Unit::TestCase
7 @compiled_templates = ActionView
::Base::CompiledTemplates
8 @compiled_templates.instance_methods
.each
do |m
|
9 @compiled_templates.send(:remove_method, m
) if m
=~
/^_run_/
13 def test_template_gets_compiled
15 assert_equal
0, @compiled_templates.instance_methods
.size
16 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
17 assert_equal
1, @compiled_templates.instance_methods
.size
21 def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
23 assert_equal
0, @compiled_templates.instance_methods
.size
24 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
25 assert_equal
"Hello world!", render(:file => "test/hello_world.erb", :locals => {:foo => "bar"})
26 assert_equal
2, @compiled_templates.instance_methods
.size
30 def test_compiled_template_will_not_be_recompiled_when_rendered_with_identical_local_assigns
32 assert_equal
0, @compiled_templates.instance_methods
.size
33 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
34 ActionView
::Template.any_instance
.expects(:compile!).never
35 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
39 def test_template_changes_are_not_reflected_with_cached_template_loading
41 with_reloading(false) do
42 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
43 modify_template
"test/hello_world.erb", "Goodbye world!" do
44 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
46 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
51 def test_template_changes_are_reflected_without_cached_template_loading
53 with_reloading(true) do
54 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
55 modify_template
"test/hello_world.erb", "Goodbye world!" do
56 assert_equal
"Goodbye world!", render(:file => "test/hello_world.erb")
58 assert_equal
"Hello world!", render(:file => "test/hello_world.erb")
63 def test_template_becomes_missing_if_deleted_without_cached_template_loading
64 with_reloading(true) do
65 assert_equal
'Hello world!', render(:file => 'test/hello_world.erb')
66 delete_template
'test/hello_world.erb' do
67 assert_raise(ActionView
::MissingTemplate) { render(:file => 'test/hello_world.erb') }
69 assert_equal
'Hello world!', render(:file => 'test/hello_world.erb')
73 def test_swapping_template_handler_is_working_without_cached_template_loading
74 with_reloading(true) do
75 assert_equal
'Hello world!', render(:file => 'test/hello_world')
76 delete_template
'test/hello_world.erb' do
77 rename_template
'test/hello_world_from_rxml.builder', 'test/hello_world.builder' do
78 assert_equal
"<html>\n <p>Hello</p>\n</html>\n", render(:file => 'test/hello_world')
81 assert_equal
'Hello world!', render(:file => 'test/hello_world')
85 def test_adding_localized_template_will_take_precedence_without_cached_template_loading
86 with_reloading(true) do
87 assert_equal
'Hello world!', render(:file => 'test/hello_world')
88 rename_template
'test/hello_world.da.html.erb', 'test/hello_world.en.html.erb' do
89 assert_equal
'Hey verden', render(:file => 'test/hello_world')
94 def test_deleting_localized_template_will_fall_back_to_non_localized_template_without_cached_template_loading
95 with_reloading(true) do
96 rename_template
'test/hello_world.da.html.erb', 'test/hello_world.en.html.erb' do
97 assert_equal
'Hey verden', render(:file => 'test/hello_world')
98 delete_template
'test/hello_world.en.html.erb' do
99 assert_equal
'Hello world!', render(:file => 'test/hello_world')
101 assert_equal
'Hey verden', render(:file => 'test/hello_world')
106 def test_parallel_reloadable_view_paths_are_working
107 with_reloading(true) do
108 view_paths_copy
= new_reloadable_view_paths
109 assert_equal
'Hello world!', render(:file => 'test/hello_world')
110 with_view_paths(view_paths_copy
, new_reloadable_view_paths
) do
111 assert_equal
'Hello world!', render(:file => 'test/hello_world')
113 modify_template
'test/hello_world.erb', 'Goodbye world!' do
114 assert_equal
'Goodbye world!', render(:file => 'test/hello_world')
115 modify_template
'test/hello_world.erb', 'So long, world!' do
116 with_view_paths(view_paths_copy
, new_reloadable_view_paths
) do
117 assert_equal
'So long, world!', render(:file => 'test/hello_world')
119 assert_equal
'So long, world!', render(:file => 'test/hello_world')
127 view_paths
= @explicit_view_paths || ActionController
::Base.view_paths
128 ActionView
::Base.new(view_paths
, {}).render(*args
)
131 def with_view_paths(*args
)
132 args
.each
do |view_paths
|
134 @explicit_view_paths = view_paths
137 @explicit_view_paths = nil
142 def reset_mtime_of(template_name
, view_paths_to_use
)
143 view_paths_to_use
.find_template(template_name
).previously_last_modified
= 10.seconds
.ago
unless ActionView
::Base.cache_template_loading
?
146 def modify_template(template
, content
, view_paths_to_use
= ActionController
::Base.view_paths
)
147 filename
= filename_for(template
)
148 old_content
= File
.read(filename
)
150 File
.open(filename
, "wb+") { |f
| f
.write(content
) }
151 reset_mtime_of(template
, view_paths_to_use
)
154 File
.open(filename
, "wb+") { |f
| f
.write(old_content
) }
155 reset_mtime_of(template
, view_paths_to_use
)
159 def filename_for(template
)
160 File
.join(FIXTURE_LOAD_PATH
, template
)
163 def rename_template(old_name
, new_name
)
164 File
.rename(filename_for(old_name
), filename_for(new_name
))
167 File
.rename(filename_for(new_name
), filename_for(old_name
))
170 def delete_template(template
, &block
)
171 rename_template(template
, File
.join(File
.dirname(template
), "__#{File.basename(template)}"), &block
)
174 def with_caching(perform_caching
)
175 old_perform_caching
= ActionController
::Base.perform_caching
177 ActionController
::Base.perform_caching
= perform_caching
180 ActionController
::Base.perform_caching
= old_perform_caching
184 def with_reloading(reload_templates
, view_paths_owner
= ActionController
::Base)
185 old_view_paths
, old_cache_templates
= view_paths_owner
.view_paths
, ActionView
::Base.cache_template_loading
187 ActionView
::Base.cache_template_loading
= !reload_templates
188 view_paths_owner
.view_paths
= view_paths_for(reload_templates
)
191 view_paths_owner
.view_paths
, ActionView
::Base.cache_template_loading
= old_view_paths
, old_cache_templates
195 def new_reloadable_view_paths
196 ActionView
::PathSet.new(CACHED_VIEW_PATHS
.map(&:to_s))
199 def view_paths_for(reload_templates
)
200 # reloadable paths are cheap to create
201 reload_templates
? new_reloadable_view_paths
: CACHED_VIEW_PATHS