Skip to content
Newer
Older
100644 269 lines (219 sloc) 9.25 KB
6c02744 @josevalim Add tests for lookup context.
josevalim authored
1 require "abstract_unit"
2 require "abstract_controller/rendering"
3
4 ActionView::LookupContext::DetailsKey.class_eval do
5 def self.details_keys
6 @details_keys
7 end
8 end
9
10 class LookupContextTest < ActiveSupport::TestCase
11 def setup
12 @lookup_context = ActionView::LookupContext.new(FIXTURE_LOAD_PATH, {})
13 end
14
15 def teardown
16 I18n.locale = :en
17 ActionView::LookupContext::DetailsKey.details_keys.clear
18 end
19
20 test "process view paths on initialization" do
21 assert_kind_of ActionView::PathSet, @lookup_context.view_paths
22 end
23
24 test "normalizes details on initialization" do
f28d856 @josevalim Improve performance of the rendering stack by freezing formats as a s…
josevalim authored
25 assert_equal Mime::SET, @lookup_context.formats
26 assert_equal :en, @lookup_context.locale
6c02744 @josevalim Add tests for lookup context.
josevalim authored
27 end
28
f28d856 @josevalim Improve performance of the rendering stack by freezing formats as a s…
josevalim authored
29 test "allows me to freeze and retrieve frozen formats" do
30 @lookup_context.formats.freeze
31 assert @lookup_context.formats.frozen?
6c02744 @josevalim Add tests for lookup context.
josevalim authored
32 end
33
34 test "allows me to change some details to execute an specific block of code" do
8dd731b @josevalim Move more normalization up to the lookup context, so it does not have…
josevalim authored
35 formats = Mime::SET
6c02744 @josevalim Add tests for lookup context.
josevalim authored
36 @lookup_context.update_details(:locale => :pt) do
f28d856 @josevalim Improve performance of the rendering stack by freezing formats as a s…
josevalim authored
37 assert_equal formats, @lookup_context.formats
38 assert_equal :pt, @lookup_context.locale
6c02744 @josevalim Add tests for lookup context.
josevalim authored
39 end
f28d856 @josevalim Improve performance of the rendering stack by freezing formats as a s…
josevalim authored
40 assert_equal formats, @lookup_context.formats
41 assert_equal :en, @lookup_context.locale
6c02744 @josevalim Add tests for lookup context.
josevalim authored
42 end
43
44 test "provides getters and setters for formats" do
5273bd9 @josevalim Make AP test suite green once again and speed up performance in layou…
josevalim authored
45 @lookup_context.formats = [:html]
6c02744 @josevalim Add tests for lookup context.
josevalim authored
46 assert_equal [:html], @lookup_context.formats
47 end
48
49 test "handles */* formats" do
6b10185 @josevalim Use Mime::Type references.
josevalim authored
50 @lookup_context.formats = ["*/*"]
6c02744 @josevalim Add tests for lookup context.
josevalim authored
51 assert_equal Mime::SET, @lookup_context.formats
52 end
53
fadd1fa @mathieuravaux Fixes ticket #6379. Improved the handling of Accept headers containin…
mathieuravaux authored
54 test "handles explicitly defined */* formats fallback to :js" do
55 @lookup_context.formats = [:js, Mime::ALL]
0a1ba4e @josevalim Always expand */*
josevalim authored
56 assert_equal [:js, *Mime::SET.symbols], @lookup_context.formats
fadd1fa @mathieuravaux Fixes ticket #6379. Improved the handling of Accept headers containin…
mathieuravaux authored
57 end
58
f28d856 @josevalim Improve performance of the rendering stack by freezing formats as a s…
josevalim authored
59 test "adds :html fallback to :js formats" do
60 @lookup_context.formats = [:js]
61 assert_equal [:js, :html], @lookup_context.formats
62 end
63
6c02744 @josevalim Add tests for lookup context.
josevalim authored
64 test "provides getters and setters for locale" do
65 @lookup_context.locale = :pt
66 assert_equal :pt, @lookup_context.locale
67 end
68
69 test "changing lookup_context locale, changes I18n.locale" do
70 @lookup_context.locale = :pt
71 assert_equal :pt, I18n.locale
72 end
73
74 test "delegates changing the locale to the I18n configuration object if it contains a lookup_context object" do
75 begin
76 I18n.config = AbstractController::I18nProxy.new(I18n.config, @lookup_context)
77 @lookup_context.locale = :pt
78 assert_equal :pt, I18n.locale
79 assert_equal :pt, @lookup_context.locale
80 ensure
262b2ea @josevalim Solve SystemStackError when changing locale inside ActionMailer [#5329
josevalim authored
81 I18n.config = I18n.config.original_config
6c02744 @josevalim Add tests for lookup context.
josevalim authored
82 end
83
84 assert_equal :pt, I18n.locale
85 end
86
87 test "find templates using the given view paths and configured details" do
6c5a3bb @artemave all tests pass
artemave authored
88 template = @lookup_context.find("hello_world", %w(test))
6c02744 @josevalim Add tests for lookup context.
josevalim authored
89 assert_equal "Hello world!", template.source
90
91 @lookup_context.locale = :da
6c5a3bb @artemave all tests pass
artemave authored
92 template = @lookup_context.find("hello_world", %w(test))
6c02744 @josevalim Add tests for lookup context.
josevalim authored
93 assert_equal "Hey verden", template.source
94 end
95
f28d856 @josevalim Improve performance of the rendering stack by freezing formats as a s…
josevalim authored
96 test "found templates respects given formats if one cannot be found from template or handler" do
97 ActionView::Template::Handlers::ERB.expects(:default_format).returns(nil)
98 @lookup_context.formats = [:text]
6c5a3bb @artemave all tests pass
artemave authored
99 template = @lookup_context.find("hello_world", %w(test))
f28d856 @josevalim Improve performance of the rendering stack by freezing formats as a s…
josevalim authored
100 assert_equal [:text], template.formats
101 end
102
6c02744 @josevalim Add tests for lookup context.
josevalim authored
103 test "adds fallbacks to view paths when required" do
104 assert_equal 1, @lookup_context.view_paths.size
105
106 @lookup_context.with_fallbacks do
107 assert_equal 3, @lookup_context.view_paths.size
5ec2718 @josevalim Do not allow templates coming from Fallback resolvers to store a virt…
josevalim authored
108 assert @lookup_context.view_paths.include?(ActionView::FallbackFileSystemResolver.new(""))
109 assert @lookup_context.view_paths.include?(ActionView::FallbackFileSystemResolver.new("/"))
6c02744 @josevalim Add tests for lookup context.
josevalim authored
110 end
111 end
112
113 test "add fallbacks just once in nested fallbacks calls" do
114 @lookup_context.with_fallbacks do
115 @lookup_context.with_fallbacks do
116 assert_equal 3, @lookup_context.view_paths.size
117 end
118 end
119 end
120
121 test "generates a new details key for each details hash" do
122 keys = []
123 keys << @lookup_context.details_key
124 assert_equal 1, keys.uniq.size
125
126 @lookup_context.locale = :da
127 keys << @lookup_context.details_key
128 assert_equal 2, keys.uniq.size
129
130 @lookup_context.locale = :en
131 keys << @lookup_context.details_key
132 assert_equal 2, keys.uniq.size
133
5273bd9 @josevalim Make AP test suite green once again and speed up performance in layou…
josevalim authored
134 @lookup_context.formats = [:html]
6c02744 @josevalim Add tests for lookup context.
josevalim authored
135 keys << @lookup_context.details_key
136 assert_equal 3, keys.uniq.size
137
138 @lookup_context.formats = nil
139 keys << @lookup_context.details_key
140 assert_equal 3, keys.uniq.size
141 end
142
143 test "gives the key forward to the resolver, so it can be used as cache key" do
144 @lookup_context.view_paths = ActionView::FixtureResolver.new("test/_foo.erb" => "Foo")
6c5a3bb @artemave all tests pass
artemave authored
145 template = @lookup_context.find("foo", %w(test), true)
6c02744 @josevalim Add tests for lookup context.
josevalim authored
146 assert_equal "Foo", template.source
147
148 # Now we are going to change the template, but it won't change the returned template
149 # since we will hit the cache.
150 @lookup_context.view_paths.first.hash["test/_foo.erb"] = "Bar"
6c5a3bb @artemave all tests pass
artemave authored
151 template = @lookup_context.find("foo", %w(test), true)
6c02744 @josevalim Add tests for lookup context.
josevalim authored
152 assert_equal "Foo", template.source
153
154 # This time we will change the locale. The updated template should be picked since
155 # lookup_context generated a new key after we changed the locale.
156 @lookup_context.locale = :da
6c5a3bb @artemave all tests pass
artemave authored
157 template = @lookup_context.find("foo", %w(test), true)
6c02744 @josevalim Add tests for lookup context.
josevalim authored
158 assert_equal "Bar", template.source
159
160 # Now we will change back the locale and it will still pick the old template.
161 # This is expected because lookup_context will reuse the previous key for :en locale.
162 @lookup_context.locale = :en
6c5a3bb @artemave all tests pass
artemave authored
163 template = @lookup_context.find("foo", %w(test), true)
6c02744 @josevalim Add tests for lookup context.
josevalim authored
164 assert_equal "Foo", template.source
165
166 # Finally, we can expire the cache. And the expected template will be used.
167 @lookup_context.view_paths.first.clear_cache
6c5a3bb @artemave all tests pass
artemave authored
168 template = @lookup_context.find("foo", %w(test), true)
6c02744 @josevalim Add tests for lookup context.
josevalim authored
169 assert_equal "Bar", template.source
170 end
c776080 @josevalim Allow cache to be temporarily disabled through lookup_context.
josevalim authored
171
172 test "can disable the cache on demand" do
173 @lookup_context.view_paths = ActionView::FixtureResolver.new("test/_foo.erb" => "Foo")
6c5a3bb @artemave all tests pass
artemave authored
174 old_template = @lookup_context.find("foo", %w(test), true)
c776080 @josevalim Allow cache to be temporarily disabled through lookup_context.
josevalim authored
175
6c5a3bb @artemave all tests pass
artemave authored
176 template = @lookup_context.find("foo", %w(test), true)
c776080 @josevalim Allow cache to be temporarily disabled through lookup_context.
josevalim authored
177 assert_equal template, old_template
178
179 assert @lookup_context.cache
180 template = @lookup_context.disable_cache do
181 assert !@lookup_context.cache
6c5a3bb @artemave all tests pass
artemave authored
182 @lookup_context.find("foo", %w(test), true)
c776080 @josevalim Allow cache to be temporarily disabled through lookup_context.
josevalim authored
183 end
184 assert @lookup_context.cache
185
186 assert_not_equal template, old_template
187 end
37f448e @apotonick added :prefixes accessor to LookupContext.
apotonick authored
188
189 test "responds to #prefixes" do
b4bc2c5 @apotonick fixed test to assert initialization value.
apotonick authored
190 assert_equal [], @lookup_context.prefixes
37f448e @apotonick added :prefixes accessor to LookupContext.
apotonick authored
191 @lookup_context.prefixes = ["foo"]
192 assert_equal ["foo"], @lookup_context.prefixes
193 end
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
194 end
195
196 class LookupContextWithFalseCaching < ActiveSupport::TestCase
197 def setup
198 @resolver = ActionView::FixtureResolver.new("test/_foo.erb" => ["Foo", Time.utc(2000)])
199 @resolver.stubs(:caching?).returns(false)
200 @lookup_context = ActionView::LookupContext.new(@resolver, {})
201 end
202
203 test "templates are always found in the resolver but timestamp is checked before being compiled" do
6c5a3bb @artemave all tests pass
artemave authored
204 template = @lookup_context.find("foo", %w(test), true)
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
205 assert_equal "Foo", template.source
206
207 # Now we are going to change the template, but it won't change the returned template
208 # since the timestamp is the same.
209 @resolver.hash["test/_foo.erb"][0] = "Bar"
6c5a3bb @artemave all tests pass
artemave authored
210 template = @lookup_context.find("foo", %w(test), true)
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
211 assert_equal "Foo", template.source
212
213 # Now update the timestamp.
214 @resolver.hash["test/_foo.erb"][1] = Time.now.utc
6c5a3bb @artemave all tests pass
artemave authored
215 template = @lookup_context.find("foo", %w(test), true)
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
216 assert_equal "Bar", template.source
217 end
218
8624065 @josevalim Allow template handlers to store temp data.
josevalim authored
219 test "if no template was found in the second lookup, with no cache, raise error" do
6c5a3bb @artemave all tests pass
artemave authored
220 template = @lookup_context.find("foo", %w(test), true)
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
221 assert_equal "Foo", template.source
222
223 @resolver.hash.clear
224 assert_raise ActionView::MissingTemplate do
6c5a3bb @artemave all tests pass
artemave authored
225 @lookup_context.find("foo", %w(test), true)
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
226 end
227 end
228
8624065 @josevalim Allow template handlers to store temp data.
josevalim authored
229 test "if no template was cached in the first lookup, retrieval should work in the second call" do
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
230 @resolver.hash.clear
231 assert_raise ActionView::MissingTemplate do
6c5a3bb @artemave all tests pass
artemave authored
232 @lookup_context.find("foo", %w(test), true)
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
233 end
234
235 @resolver.hash["test/_foo.erb"] = ["Foo", Time.utc(2000)]
6c5a3bb @artemave all tests pass
artemave authored
236 template = @lookup_context.find("foo", %w(test), true)
38d78f9 @josevalim Resolvers now consider timestamps.
josevalim authored
237 assert_equal "Foo", template.source
238 end
6c5a3bb @artemave all tests pass
artemave authored
239 end
4c44f04 @apotonick added tests for the MissingTemplate exception message.
apotonick authored
240
241 class TestMissingTemplate < ActiveSupport::TestCase
242 def setup
243 @lookup_context = ActionView::LookupContext.new("/Path/to/views", {})
244 end
245
246 test "if no template was found we get a helpful error message including the inheritance chain" do
247 e = assert_raise ActionView::MissingTemplate do
248 @lookup_context.find("foo", %w(parent child))
249 end
8e5d910 @drogus Don't be so picky on MissingTemplate error details, this fails random…
drogus authored
250 assert_match %r{Missing template parent/foo, child/foo with .* Searched in:\n \* "/Path/to/views"\n}, e.message
4c44f04 @apotonick added tests for the MissingTemplate exception message.
apotonick authored
251 end
8e5d910 @drogus Don't be so picky on MissingTemplate error details, this fails random…
drogus authored
252
4c44f04 @apotonick added tests for the MissingTemplate exception message.
apotonick authored
253 test "if no partial was found we get a helpful error message including the inheritance chain" do
254 e = assert_raise ActionView::MissingTemplate do
255 @lookup_context.find("foo", %w(parent child), true)
256 end
8e5d910 @drogus Don't be so picky on MissingTemplate error details, this fails random…
drogus authored
257 assert_match %r{Missing partial parent/foo, child/foo with .* Searched in:\n \* "/Path/to/views"\n}, e.message
4c44f04 @apotonick added tests for the MissingTemplate exception message.
apotonick authored
258 end
156039c Added a test for MissingTemplate change, and changed to use Array.wra…
Burke Libbey authored
259
260 test "if a single prefix is passed as a string and the lookup fails, MissingTemplate accepts it" do
261 e = assert_raise ActionView::MissingTemplate do
262 details = {:handlers=>[], :formats=>[], :locale=>[]}
263 @lookup_context.view_paths.find("foo", "parent", true, details)
264 end
265 assert_match %r{Missing partial parent/foo with .* Searched in:\n \* "/Path/to/views"\n}, e.message
266 end
267
4c44f04 @apotonick added tests for the MissingTemplate exception message.
apotonick authored
268 end
Something went wrong with that request. Please try again.