1
require 'abstract_unit'
2
require 'controller/fake_models'
4
class CompiledTemplatesTest < Test::Unit::TestCase
7
@explicit_view_paths = nil
8
@compiled_templates = ActionView::Base::CompiledTemplates
9
@compiled_templates.instance_methods.each do |m|
10
@compiled_templates.send(:remove_method, m) if m =~ /^_run_/
14
def test_template_gets_compiled
16
assert_equal 0, @compiled_templates.instance_methods.size
17
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
18
assert_equal 1, @compiled_templates.instance_methods.size
22
def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
24
assert_equal 0, @compiled_templates.instance_methods.size
25
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
26
assert_equal "Hello world!", render(:file => "test/hello_world.erb", :locals => {:foo => "bar"})
27
assert_equal 2, @compiled_templates.instance_methods.size
31
def test_compiled_template_will_not_be_recompiled_when_rendered_with_identical_local_assigns
33
assert_equal 0, @compiled_templates.instance_methods.size
34
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
35
ActionView::Template.any_instance.expects(:compile!).never
36
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
40
def test_template_changes_are_not_reflected_with_cached_template_loading
42
with_reloading(false) do
43
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
44
modify_template "test/hello_world.erb", "Goodbye world!" do
45
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
47
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
52
def test_template_changes_are_reflected_without_cached_template_loading
54
with_reloading(true) do
55
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
56
modify_template "test/hello_world.erb", "Goodbye world!" do
57
assert_equal "Goodbye world!", render(:file => "test/hello_world.erb")
59
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
64
def test_template_becomes_missing_if_deleted_without_cached_template_loading
65
with_reloading(true) do
66
assert_equal 'Hello world!', render(:file => 'test/hello_world.erb')
67
delete_template 'test/hello_world.erb' do
68
assert_raise(ActionView::MissingTemplate) { render(:file => 'test/hello_world.erb') }
70
assert_equal 'Hello world!', render(:file => 'test/hello_world.erb')
74
def test_swapping_template_handler_is_working_without_cached_template_loading
75
with_reloading(true) do
76
assert_equal 'Hello world!', render(:file => 'test/hello_world')
77
delete_template 'test/hello_world.erb' do
78
rename_template 'test/hello_world_from_rxml.builder', 'test/hello_world.builder' do
79
assert_equal "<html>\n <p>Hello</p>\n</html>\n", render(:file => 'test/hello_world')
82
assert_equal 'Hello world!', render(:file => 'test/hello_world')
86
def test_adding_localized_template_will_take_precedence_without_cached_template_loading
87
with_reloading(true) do
88
assert_equal 'Hello world!', render(:file => 'test/hello_world')
89
rename_template 'test/hello_world.da.html.erb', 'test/hello_world.en.html.erb' do
90
assert_equal 'Hey verden', render(:file => 'test/hello_world')
95
def test_deleting_localized_template_will_fall_back_to_non_localized_template_without_cached_template_loading
96
with_reloading(true) do
97
rename_template 'test/hello_world.da.html.erb', 'test/hello_world.en.html.erb' do
98
assert_equal 'Hey verden', render(:file => 'test/hello_world')
99
delete_template 'test/hello_world.en.html.erb' do
100
assert_equal 'Hello world!', render(:file => 'test/hello_world')
102
assert_equal 'Hey verden', render(:file => 'test/hello_world')
107
def test_parallel_reloadable_view_paths_are_working
108
with_reloading(true) do
109
view_paths_copy = new_reloadable_view_paths
110
assert_equal 'Hello world!', render(:file => 'test/hello_world')
111
with_view_paths(view_paths_copy, new_reloadable_view_paths) do
112
assert_equal 'Hello world!', render(:file => 'test/hello_world')
114
modify_template 'test/hello_world.erb', 'Goodbye world!' do
115
assert_equal 'Goodbye world!', render(:file => 'test/hello_world')
116
modify_template 'test/hello_world.erb', 'So long, world!' do
117
with_view_paths(view_paths_copy, new_reloadable_view_paths) do
118
assert_equal 'So long, world!', render(:file => 'test/hello_world')
120
assert_equal 'So long, world!', render(:file => 'test/hello_world')
128
view_paths = @explicit_view_paths || ActionController::Base.view_paths
129
ActionView::Base.new(view_paths, {}).render(*args)
132
def with_view_paths(*args)
133
args.each do |view_paths|
135
@explicit_view_paths = view_paths
138
@explicit_view_paths = nil
143
def reset_mtime_of(template_name, view_paths_to_use)
144
view_paths_to_use.find_template(template_name).previously_last_modified = 10.seconds.ago unless ActionView::Base.cache_template_loading?
147
def modify_template(template, content, view_paths_to_use = ActionController::Base.view_paths)
148
filename = filename_for(template)
149
old_content = File.read(filename)
151
File.open(filename, "wb+") { |f| f.write(content) }
152
reset_mtime_of(template, view_paths_to_use)
155
File.open(filename, "wb+") { |f| f.write(old_content) }
156
reset_mtime_of(template, view_paths_to_use)
160
def filename_for(template)
161
File.join(FIXTURE_LOAD_PATH, template)
164
def rename_template(old_name, new_name)
165
File.rename(filename_for(old_name), filename_for(new_name))
168
File.rename(filename_for(new_name), filename_for(old_name))
171
def delete_template(template, &block)
172
rename_template(template, File.join(File.dirname(template), "__#{File.basename(template)}"), &block)
175
def with_caching(perform_caching)
176
old_perform_caching = ActionController::Base.perform_caching
178
ActionController::Base.perform_caching = perform_caching
181
ActionController::Base.perform_caching = old_perform_caching
185
def with_reloading(reload_templates, view_paths_owner = ActionController::Base)
186
old_view_paths, old_cache_templates = view_paths_owner.view_paths, ActionView::Base.cache_template_loading
188
ActionView::Base.cache_template_loading = !reload_templates
189
view_paths_owner.view_paths = view_paths_for(reload_templates)
192
view_paths_owner.view_paths, ActionView::Base.cache_template_loading = old_view_paths, old_cache_templates
196
def new_reloadable_view_paths
197
ActionView::PathSet.new(CACHED_VIEW_PATHS.map(&:to_s))
200
def view_paths_for(reload_templates)
201
# reloadable paths are cheap to create
202
reload_templates ? new_reloadable_view_paths : CACHED_VIEW_PATHS