~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/test/template/compiled_templates_test.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

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