7d1d7634a8ce645ecd88aac2613a12f1a641d22e
[feedcatcher.git] / vendor / rails / actionpack / test / template / compiled_templates_test.rb
1 require 'abstract_unit'
2 require 'controller/fake_models'
3
4 class CompiledTemplatesTest < Test::Unit::TestCase
5
6 def setup
7 @compiled_templates = ActionView::Base::CompiledTemplates
8 @compiled_templates.instance_methods.each do |m|
9 @compiled_templates.send(:remove_method, m) if m =~ /^_run_/
10 end
11 end
12
13 def test_template_gets_compiled
14 with_caching(true) do
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
18 end
19 end
20
21 def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
22 with_caching(true) do
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
27 end
28 end
29
30 def test_compiled_template_will_not_be_recompiled_when_rendered_with_identical_local_assigns
31 with_caching(true) do
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")
36 end
37 end
38
39 def test_template_changes_are_not_reflected_with_cached_template_loading
40 with_caching(true) do
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")
45 end
46 assert_equal "Hello world!", render(:file => "test/hello_world.erb")
47 end
48 end
49 end
50
51 def test_template_changes_are_reflected_without_cached_template_loading
52 with_caching(true) do
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")
57 end
58 assert_equal "Hello world!", render(:file => "test/hello_world.erb")
59 end
60 end
61 end
62
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') }
68 end
69 assert_equal 'Hello world!', render(:file => 'test/hello_world.erb')
70 end
71 end
72
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')
79 end
80 end
81 assert_equal 'Hello world!', render(:file => 'test/hello_world')
82 end
83 end
84
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')
90 end
91 end
92 end
93
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')
100 end
101 assert_equal 'Hey verden', render(:file => 'test/hello_world')
102 end
103 end
104 end
105
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')
112 end
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')
118 end
119 assert_equal 'So long, world!', render(:file => 'test/hello_world')
120 end
121 end
122 end
123 end
124
125 private
126 def render(*args)
127 view_paths = @explicit_view_paths || ActionController::Base.view_paths
128 ActionView::Base.new(view_paths, {}).render(*args)
129 end
130
131 def with_view_paths(*args)
132 args.each do |view_paths|
133 begin
134 @explicit_view_paths = view_paths
135 yield
136 ensure
137 @explicit_view_paths = nil
138 end
139 end
140 end
141
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?
144 end
145
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)
149 begin
150 File.open(filename, "wb+") { |f| f.write(content) }
151 reset_mtime_of(template, view_paths_to_use)
152 yield
153 ensure
154 File.open(filename, "wb+") { |f| f.write(old_content) }
155 reset_mtime_of(template, view_paths_to_use)
156 end
157 end
158
159 def filename_for(template)
160 File.join(FIXTURE_LOAD_PATH, template)
161 end
162
163 def rename_template(old_name, new_name)
164 File.rename(filename_for(old_name), filename_for(new_name))
165 yield
166 ensure
167 File.rename(filename_for(new_name), filename_for(old_name))
168 end
169
170 def delete_template(template, &block)
171 rename_template(template, File.join(File.dirname(template), "__#{File.basename(template)}"), &block)
172 end
173
174 def with_caching(perform_caching)
175 old_perform_caching = ActionController::Base.perform_caching
176 begin
177 ActionController::Base.perform_caching = perform_caching
178 yield
179 ensure
180 ActionController::Base.perform_caching = old_perform_caching
181 end
182 end
183
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
186 begin
187 ActionView::Base.cache_template_loading = !reload_templates
188 view_paths_owner.view_paths = view_paths_for(reload_templates)
189 yield
190 ensure
191 view_paths_owner.view_paths, ActionView::Base.cache_template_loading = old_view_paths, old_cache_templates
192 end
193 end
194
195 def new_reloadable_view_paths
196 ActionView::PathSet.new(CACHED_VIEW_PATHS.map(&:to_s))
197 end
198
199 def view_paths_for(reload_templates)
200 # reloadable paths are cheap to create
201 reload_templates ? new_reloadable_view_paths : CACHED_VIEW_PATHS
202 end
203 end