2010-03-11 06:45:05 -05:00
|
|
|
require "abstract_unit"
|
|
|
|
require "abstract_controller/rendering"
|
|
|
|
|
|
|
|
class LookupContextTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@lookup_context = ActionView::LookupContext.new(FIXTURE_LOAD_PATH, {})
|
2011-12-14 03:23:34 -05:00
|
|
|
ActionView::LookupContext::DetailsKey.clear
|
2010-03-11 06:45:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
I18n.locale = :en
|
|
|
|
end
|
|
|
|
|
2012-06-15 14:36:09 -04:00
|
|
|
test "allows to override default_formats with ActionView::Base.default_formats" do
|
|
|
|
begin
|
|
|
|
formats = ActionView::Base.default_formats
|
|
|
|
ActionView::Base.default_formats = [:foo, :bar]
|
|
|
|
|
|
|
|
assert_equal [:foo, :bar], ActionView::LookupContext.new([]).default_formats
|
|
|
|
ensure
|
|
|
|
ActionView::Base.default_formats = formats
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-11 06:45:05 -05:00
|
|
|
test "process view paths on initialization" do
|
|
|
|
assert_kind_of ActionView::PathSet, @lookup_context.view_paths
|
|
|
|
end
|
|
|
|
|
|
|
|
test "normalizes details on initialization" do
|
2015-09-21 17:55:34 -04:00
|
|
|
assert_equal Mime::SET.to_a, @lookup_context.formats
|
2010-03-19 12:20:15 -04:00
|
|
|
assert_equal :en, @lookup_context.locale
|
2010-03-11 06:45:05 -05:00
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "allows me to freeze and retrieve frozen formats" do
|
|
|
|
@lookup_context.formats.freeze
|
|
|
|
assert @lookup_context.formats.frozen?
|
2010-03-11 06:45:05 -05:00
|
|
|
end
|
|
|
|
|
2013-12-03 05:17:01 -05:00
|
|
|
test "provides getters and setters for variants" do
|
|
|
|
@lookup_context.variants = [:mobile]
|
|
|
|
assert_equal [:mobile], @lookup_context.variants
|
|
|
|
end
|
|
|
|
|
2010-03-11 06:45:05 -05:00
|
|
|
test "provides getters and setters for formats" do
|
2010-06-07 04:13:41 -04:00
|
|
|
@lookup_context.formats = [:html]
|
2010-03-11 06:45:05 -05:00
|
|
|
assert_equal [:html], @lookup_context.formats
|
|
|
|
end
|
|
|
|
|
|
|
|
test "handles */* formats" do
|
2010-11-28 16:40:32 -05:00
|
|
|
@lookup_context.formats = ["*/*"]
|
2015-09-21 17:55:34 -04:00
|
|
|
assert_equal Mime::SET.to_a, @lookup_context.formats
|
2010-03-11 06:45:05 -05:00
|
|
|
end
|
|
|
|
|
2011-02-26 19:52:08 -05:00
|
|
|
test "handles explicitly defined */* formats fallback to :js" do
|
2015-10-05 01:14:04 -04:00
|
|
|
@lookup_context.formats = [:js, Mime::ALL]
|
2011-05-07 17:43:25 -04:00
|
|
|
assert_equal [:js, *Mime::SET.symbols], @lookup_context.formats
|
2011-02-26 19:52:08 -05:00
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "adds :html fallback to :js formats" do
|
|
|
|
@lookup_context.formats = [:js]
|
|
|
|
assert_equal [:js, :html], @lookup_context.formats
|
|
|
|
end
|
|
|
|
|
2010-03-11 06:45:05 -05:00
|
|
|
test "provides getters and setters for locale" do
|
|
|
|
@lookup_context.locale = :pt
|
|
|
|
assert_equal :pt, @lookup_context.locale
|
|
|
|
end
|
|
|
|
|
|
|
|
test "changing lookup_context locale, changes I18n.locale" do
|
|
|
|
@lookup_context.locale = :pt
|
|
|
|
assert_equal :pt, I18n.locale
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delegates changing the locale to the I18n configuration object if it contains a lookup_context object" do
|
|
|
|
begin
|
2013-06-27 08:15:43 -04:00
|
|
|
I18n.config = ActionView::I18nProxy.new(I18n.config, @lookup_context)
|
2010-03-11 06:45:05 -05:00
|
|
|
@lookup_context.locale = :pt
|
|
|
|
assert_equal :pt, I18n.locale
|
|
|
|
assert_equal :pt, @lookup_context.locale
|
|
|
|
ensure
|
2011-01-19 17:42:10 -05:00
|
|
|
I18n.config = I18n.config.original_config
|
2010-03-11 06:45:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal :pt, I18n.locale
|
|
|
|
end
|
|
|
|
|
|
|
|
test "find templates using the given view paths and configured details" do
|
2010-12-12 15:40:40 -05:00
|
|
|
template = @lookup_context.find("hello_world", %w(test))
|
2010-03-11 06:45:05 -05:00
|
|
|
assert_equal "Hello world!", template.source
|
|
|
|
|
|
|
|
@lookup_context.locale = :da
|
2010-12-12 15:40:40 -05:00
|
|
|
template = @lookup_context.find("hello_world", %w(test))
|
2010-03-11 06:45:05 -05:00
|
|
|
assert_equal "Hey verden", template.source
|
|
|
|
end
|
|
|
|
|
2014-03-08 17:09:54 -05:00
|
|
|
test "find templates with given variants" do
|
|
|
|
@lookup_context.formats = [:html]
|
|
|
|
@lookup_context.variants = [:phone]
|
|
|
|
|
|
|
|
template = @lookup_context.find("hello_world", %w(test))
|
|
|
|
assert_equal "Hello phone!", template.source
|
|
|
|
|
|
|
|
@lookup_context.variants = [:phone]
|
|
|
|
@lookup_context.formats = [:text]
|
|
|
|
|
|
|
|
template = @lookup_context.find("hello_world", %w(test))
|
|
|
|
assert_equal "Hello texty phone!", template.source
|
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "found templates respects given formats if one cannot be found from template or handler" do
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_called(ActionView::Template::Handlers::Builder, :default_format, returns: nil) do
|
|
|
|
@lookup_context.formats = [:text]
|
|
|
|
template = @lookup_context.find("hello", %w(test))
|
|
|
|
assert_equal [:text], template.formats
|
|
|
|
end
|
2010-03-19 12:20:15 -04:00
|
|
|
end
|
|
|
|
|
2010-03-11 06:45:05 -05:00
|
|
|
test "adds fallbacks to view paths when required" do
|
|
|
|
assert_equal 1, @lookup_context.view_paths.size
|
|
|
|
|
|
|
|
@lookup_context.with_fallbacks do
|
|
|
|
assert_equal 3, @lookup_context.view_paths.size
|
2010-10-10 17:11:50 -04:00
|
|
|
assert @lookup_context.view_paths.include?(ActionView::FallbackFileSystemResolver.new(""))
|
|
|
|
assert @lookup_context.view_paths.include?(ActionView::FallbackFileSystemResolver.new("/"))
|
2010-03-11 06:45:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add fallbacks just once in nested fallbacks calls" do
|
|
|
|
@lookup_context.with_fallbacks do
|
|
|
|
@lookup_context.with_fallbacks do
|
|
|
|
assert_equal 3, @lookup_context.view_paths.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generates a new details key for each details hash" do
|
|
|
|
keys = []
|
|
|
|
keys << @lookup_context.details_key
|
|
|
|
assert_equal 1, keys.uniq.size
|
|
|
|
|
|
|
|
@lookup_context.locale = :da
|
|
|
|
keys << @lookup_context.details_key
|
|
|
|
assert_equal 2, keys.uniq.size
|
|
|
|
|
|
|
|
@lookup_context.locale = :en
|
|
|
|
keys << @lookup_context.details_key
|
|
|
|
assert_equal 2, keys.uniq.size
|
|
|
|
|
2010-06-07 04:13:41 -04:00
|
|
|
@lookup_context.formats = [:html]
|
2010-03-11 06:45:05 -05:00
|
|
|
keys << @lookup_context.details_key
|
|
|
|
assert_equal 3, keys.uniq.size
|
|
|
|
|
|
|
|
@lookup_context.formats = nil
|
|
|
|
keys << @lookup_context.details_key
|
|
|
|
assert_equal 3, keys.uniq.size
|
|
|
|
end
|
|
|
|
|
|
|
|
test "gives the key forward to the resolver, so it can be used as cache key" do
|
|
|
|
@lookup_context.view_paths = ActionView::FixtureResolver.new("test/_foo.erb" => "Foo")
|
2010-12-12 15:40:40 -05:00
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
2010-03-11 06:45:05 -05:00
|
|
|
assert_equal "Foo", template.source
|
|
|
|
|
|
|
|
# Now we are going to change the template, but it won't change the returned template
|
|
|
|
# since we will hit the cache.
|
|
|
|
@lookup_context.view_paths.first.hash["test/_foo.erb"] = "Bar"
|
2010-12-12 15:40:40 -05:00
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
2010-03-11 06:45:05 -05:00
|
|
|
assert_equal "Foo", template.source
|
|
|
|
|
|
|
|
# This time we will change the locale. The updated template should be picked since
|
|
|
|
# lookup_context generated a new key after we changed the locale.
|
|
|
|
@lookup_context.locale = :da
|
2010-12-12 15:40:40 -05:00
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
2010-03-11 06:45:05 -05:00
|
|
|
assert_equal "Bar", template.source
|
|
|
|
|
|
|
|
# Now we will change back the locale and it will still pick the old template.
|
|
|
|
# This is expected because lookup_context will reuse the previous key for :en locale.
|
|
|
|
@lookup_context.locale = :en
|
2010-12-12 15:40:40 -05:00
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
2010-03-11 06:45:05 -05:00
|
|
|
assert_equal "Foo", template.source
|
|
|
|
|
|
|
|
# Finally, we can expire the cache. And the expected template will be used.
|
|
|
|
@lookup_context.view_paths.first.clear_cache
|
2010-12-12 15:40:40 -05:00
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
2010-03-11 06:45:05 -05:00
|
|
|
assert_equal "Bar", template.source
|
|
|
|
end
|
2010-10-07 15:30:19 -04:00
|
|
|
|
|
|
|
test "can disable the cache on demand" do
|
|
|
|
@lookup_context.view_paths = ActionView::FixtureResolver.new("test/_foo.erb" => "Foo")
|
2010-12-12 15:40:40 -05:00
|
|
|
old_template = @lookup_context.find("foo", %w(test), true)
|
2010-10-07 15:30:19 -04:00
|
|
|
|
2010-12-12 15:40:40 -05:00
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
2010-10-07 15:30:19 -04:00
|
|
|
assert_equal template, old_template
|
|
|
|
|
|
|
|
assert @lookup_context.cache
|
|
|
|
template = @lookup_context.disable_cache do
|
|
|
|
assert !@lookup_context.cache
|
2010-12-12 15:40:40 -05:00
|
|
|
@lookup_context.find("foo", %w(test), true)
|
2010-10-07 15:30:19 -04:00
|
|
|
end
|
|
|
|
assert @lookup_context.cache
|
|
|
|
|
|
|
|
assert_not_equal template, old_template
|
|
|
|
end
|
2012-05-20 01:03:34 -04:00
|
|
|
|
2011-05-03 17:38:44 -04:00
|
|
|
test "responds to #prefixes" do
|
2011-05-03 18:11:34 -04:00
|
|
|
assert_equal [], @lookup_context.prefixes
|
2011-05-03 17:38:44 -04:00
|
|
|
@lookup_context.prefixes = ["foo"]
|
|
|
|
assert_equal ["foo"], @lookup_context.prefixes
|
|
|
|
end
|
2010-10-10 03:24:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class LookupContextWithFalseCaching < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@resolver = ActionView::FixtureResolver.new("test/_foo.erb" => ["Foo", Time.utc(2000)])
|
|
|
|
@lookup_context = ActionView::LookupContext.new(@resolver, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "templates are always found in the resolver but timestamp is checked before being compiled" do
|
2015-08-22 00:03:53 -04:00
|
|
|
ActionView::Resolver.stub(:caching?, false) do
|
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
|
|
|
assert_equal "Foo", template.source
|
|
|
|
|
|
|
|
# Now we are going to change the template, but it won't change the returned template
|
|
|
|
# since the timestamp is the same.
|
|
|
|
@resolver.hash["test/_foo.erb"][0] = "Bar"
|
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
|
|
|
assert_equal "Foo", template.source
|
|
|
|
|
|
|
|
# Now update the timestamp.
|
|
|
|
@resolver.hash["test/_foo.erb"][1] = Time.now.utc
|
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
|
|
|
assert_equal "Bar", template.source
|
|
|
|
end
|
2010-10-10 03:24:17 -04:00
|
|
|
end
|
|
|
|
|
2010-11-24 06:38:52 -05:00
|
|
|
test "if no template was found in the second lookup, with no cache, raise error" do
|
2015-08-22 00:03:53 -04:00
|
|
|
ActionView::Resolver.stub(:caching?, false) do
|
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
|
|
|
assert_equal "Foo", template.source
|
2010-10-10 03:24:17 -04:00
|
|
|
|
2015-08-22 00:03:53 -04:00
|
|
|
@resolver.hash.clear
|
|
|
|
assert_raise ActionView::MissingTemplate do
|
|
|
|
@lookup_context.find("foo", %w(test), true)
|
|
|
|
end
|
2010-10-10 03:24:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-24 06:38:52 -05:00
|
|
|
test "if no template was cached in the first lookup, retrieval should work in the second call" do
|
2015-08-22 00:03:53 -04:00
|
|
|
ActionView::Resolver.stub(:caching?, false) do
|
|
|
|
@resolver.hash.clear
|
|
|
|
assert_raise ActionView::MissingTemplate do
|
|
|
|
@lookup_context.find("foo", %w(test), true)
|
|
|
|
end
|
2010-10-10 03:24:17 -04:00
|
|
|
|
2015-08-22 00:03:53 -04:00
|
|
|
@resolver.hash["test/_foo.erb"] = ["Foo", Time.utc(2000)]
|
|
|
|
template = @lookup_context.find("foo", %w(test), true)
|
|
|
|
assert_equal "Foo", template.source
|
|
|
|
end
|
2010-10-10 03:24:17 -04:00
|
|
|
end
|
2010-12-12 15:40:40 -05:00
|
|
|
end
|
2010-12-30 08:11:14 -05:00
|
|
|
|
|
|
|
class TestMissingTemplate < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@lookup_context = ActionView::LookupContext.new("/Path/to/views", {})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "if no template was found we get a helpful error message including the inheritance chain" do
|
|
|
|
e = assert_raise ActionView::MissingTemplate do
|
|
|
|
@lookup_context.find("foo", %w(parent child))
|
|
|
|
end
|
2010-12-31 05:17:37 -05:00
|
|
|
assert_match %r{Missing template parent/foo, child/foo with .* Searched in:\n \* "/Path/to/views"\n}, e.message
|
2010-12-30 08:11:14 -05:00
|
|
|
end
|
2010-12-31 05:17:37 -05:00
|
|
|
|
2010-12-30 08:11:14 -05:00
|
|
|
test "if no partial was found we get a helpful error message including the inheritance chain" do
|
|
|
|
e = assert_raise ActionView::MissingTemplate do
|
|
|
|
@lookup_context.find("foo", %w(parent child), true)
|
|
|
|
end
|
2013-11-22 08:14:41 -05:00
|
|
|
assert_match %r{Missing partial parent/_foo, child/_foo with .* Searched in:\n \* "/Path/to/views"\n}, e.message
|
2010-12-30 08:11:14 -05:00
|
|
|
end
|
2011-05-06 15:02:31 -04:00
|
|
|
|
|
|
|
test "if a single prefix is passed as a string and the lookup fails, MissingTemplate accepts it" do
|
|
|
|
e = assert_raise ActionView::MissingTemplate do
|
2016-08-06 13:36:34 -04:00
|
|
|
details = {handlers: [], formats: [], variants: [], locale: []}
|
2011-05-06 15:02:31 -04:00
|
|
|
@lookup_context.view_paths.find("foo", "parent", true, details)
|
|
|
|
end
|
2013-11-22 08:14:41 -05:00
|
|
|
assert_match %r{Missing partial parent/_foo with .* Searched in:\n \* "/Path/to/views"\n}, e.message
|
2012-05-20 01:03:34 -04:00
|
|
|
end
|
2010-12-30 08:11:14 -05:00
|
|
|
end
|