2019-04-16 04:05:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-18 15:03:39 -04:00
|
|
|
module ResolverSharedTests
|
|
|
|
attr_reader :tmpdir
|
|
|
|
|
|
|
|
def run(*args)
|
|
|
|
capture_exceptions do
|
|
|
|
Dir.mktmpdir(nil, __dir__) { |dir| @tmpdir = dir; super }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-16 04:05:50 -04:00
|
|
|
def with_file(filename, source = "File at #{filename}")
|
2019-03-18 15:03:39 -04:00
|
|
|
path = File.join(tmpdir, filename)
|
|
|
|
FileUtils.mkdir_p(File.dirname(path))
|
|
|
|
File.write(path, source)
|
|
|
|
end
|
|
|
|
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
def context
|
|
|
|
@context ||= ActionView::LookupContext.new(resolver)
|
|
|
|
end
|
|
|
|
|
2019-03-18 15:03:39 -04:00
|
|
|
def test_can_find_with_no_extensions
|
|
|
|
with_file "test/hello_world", "Hello default!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [:en], formats: [:html], variants: [:phone], handlers: [:erb])
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello default!", templates[0].source
|
|
|
|
assert_equal "test/hello_world", templates[0].virtual_path
|
|
|
|
assert_nil templates[0].format
|
|
|
|
assert_nil templates[0].variant
|
|
|
|
assert_kind_of ActionView::Template::Handlers::Raw, templates[0].handler
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_find_with_just_handler
|
|
|
|
with_file "test/hello_world.erb", "Hello erb!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [:en], formats: [:html], variants: [:phone], handlers: [:erb])
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello erb!", templates[0].source
|
|
|
|
assert_equal "test/hello_world", templates[0].virtual_path
|
|
|
|
assert_nil templates[0].format
|
|
|
|
assert_nil templates[0].variant
|
|
|
|
assert_kind_of ActionView::Template::Handlers::ERB, templates[0].handler
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_find_with_format_and_handler
|
|
|
|
with_file "test/hello_world.text.builder", "Hello plain text!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [:en], formats: [:html, :text], variants: [:phone], handlers: [:erb, :builder])
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello plain text!", templates[0].source
|
|
|
|
assert_equal "test/hello_world", templates[0].virtual_path
|
|
|
|
assert_equal :text, templates[0].format
|
|
|
|
assert_nil templates[0].variant
|
|
|
|
assert_kind_of ActionView::Template::Handlers::Builder, templates[0].handler
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_find_with_variant_format_and_handler
|
|
|
|
with_file "test/hello_world.html+phone.erb", "Hello plain text!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [:en], formats: [:html], variants: [:phone], handlers: [:erb])
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello plain text!", templates[0].source
|
|
|
|
assert_equal "test/hello_world", templates[0].virtual_path
|
|
|
|
assert_equal :html, templates[0].format
|
|
|
|
assert_equal "phone", templates[0].variant
|
|
|
|
assert_kind_of ActionView::Template::Handlers::ERB, templates[0].handler
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_find_with_any_variant_format_and_handler
|
|
|
|
with_file "test/hello_world.html+phone.erb", "Hello plain text!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [:en], formats: [:html], variants: :any, handlers: [:erb])
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello plain text!", templates[0].source
|
|
|
|
assert_equal "test/hello_world", templates[0].virtual_path
|
|
|
|
assert_equal :html, templates[0].format
|
|
|
|
assert_equal "phone", templates[0].variant
|
|
|
|
assert_kind_of ActionView::Template::Handlers::ERB, templates[0].handler
|
|
|
|
end
|
|
|
|
|
2019-09-03 16:36:37 -04:00
|
|
|
def test_can_find_when_special_chars_in_path
|
|
|
|
dir = "test +()[]{}"
|
|
|
|
with_file "#{dir}/hello_world", "Hello funky path!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", dir, false, locale: [:en], formats: [:html], variants: [:phone], handlers: [:erb])
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello funky path!", templates[0].source
|
|
|
|
assert_equal "#{dir}/hello_world", templates[0].virtual_path
|
|
|
|
end
|
|
|
|
|
2019-03-18 15:03:39 -04:00
|
|
|
def test_doesnt_find_template_with_wrong_details
|
|
|
|
with_file "test/hello_world.html.erb", "Hello plain text!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:xml], variants: :any, handlers: [:builder])
|
|
|
|
assert_equal 0, templates.size
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:xml], variants: :any, handlers: [:erb])
|
|
|
|
assert_equal 0, templates.size
|
|
|
|
end
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
|
|
|
|
def test_found_template_is_cached
|
|
|
|
with_file "test/hello_world.html.erb", "Hello HTML!"
|
|
|
|
|
|
|
|
a = context.find("hello_world", "test", false, [], {})
|
|
|
|
b = context.find("hello_world", "test", false, [], {})
|
|
|
|
assert_same a, b
|
|
|
|
end
|
|
|
|
|
2019-04-12 13:20:04 -04:00
|
|
|
def test_different_templates_when_cache_disabled
|
|
|
|
with_file "test/hello_world.html.erb", "Hello HTML!"
|
|
|
|
|
|
|
|
a = context.find("hello_world", "test", false, [], {})
|
|
|
|
b = context.disable_cache { context.find("hello_world", "test", false, [], {}) }
|
|
|
|
c = context.find("hello_world", "test", false, [], {})
|
|
|
|
|
|
|
|
# disable_cache should give us a new object
|
2019-04-16 04:05:50 -04:00
|
|
|
assert_not_same a, b
|
2019-04-12 13:20:04 -04:00
|
|
|
|
|
|
|
# but it should not clear the cache
|
|
|
|
assert_same a, c
|
|
|
|
end
|
|
|
|
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
def test_same_template_from_different_details_is_same_object
|
2019-04-12 19:08:30 -04:00
|
|
|
with_file "test/hello_world.html.erb", "Hello HTML!"
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
|
|
|
|
a = context.find("hello_world", "test", false, [], locale: [:en])
|
|
|
|
b = context.find("hello_world", "test", false, [], locale: [:fr])
|
|
|
|
assert_same a, b
|
|
|
|
end
|
|
|
|
|
2019-04-12 19:08:30 -04:00
|
|
|
def test_templates_with_optional_locale_shares_common_object
|
|
|
|
with_file "test/hello_world.text.erb", "Generic plain text!"
|
|
|
|
with_file "test/hello_world.fr.text.erb", "Texte en Francais!"
|
|
|
|
|
|
|
|
en = context.find_all("hello_world", "test", false, [], locale: [:en])
|
|
|
|
fr = context.find_all("hello_world", "test", false, [], locale: [:fr])
|
|
|
|
|
|
|
|
assert_equal 1, en.size
|
|
|
|
assert_equal 2, fr.size
|
|
|
|
|
|
|
|
assert_equal "Generic plain text!", en[0].source
|
|
|
|
assert_equal "Texte en Francais!", fr[0].source
|
|
|
|
assert_equal "Generic plain text!", fr[1].source
|
|
|
|
|
|
|
|
assert_same en[0], fr[1]
|
|
|
|
end
|
|
|
|
|
2019-10-24 16:51:49 -04:00
|
|
|
def test_templates_sort_by_formats_json_first
|
|
|
|
with_file "test/hello_world.html.erb", "Hello HTML!"
|
2020-02-17 02:31:43 -05:00
|
|
|
with_file "test/hello_world.json.builder", "Hello JSON!"
|
2019-10-24 16:51:49 -04:00
|
|
|
|
2020-02-17 02:31:43 -05:00
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:json, :html], variants: :any, handlers: [:erb, :builder])
|
2019-10-24 16:51:49 -04:00
|
|
|
|
|
|
|
assert_equal 2, templates.size
|
|
|
|
assert_equal "Hello JSON!", templates[0].source
|
2020-02-17 02:31:43 -05:00
|
|
|
assert_equal :json, templates[0].format
|
|
|
|
assert_equal "Hello HTML!", templates[1].source
|
|
|
|
assert_equal :html, templates[1].format
|
2019-10-24 16:51:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_templates_sort_by_formats_html_first
|
|
|
|
with_file "test/hello_world.html.erb", "Hello HTML!"
|
2020-02-17 02:31:43 -05:00
|
|
|
with_file "test/hello_world.json.builder", "Hello JSON!"
|
2019-10-24 16:51:49 -04:00
|
|
|
|
2020-02-17 02:31:43 -05:00
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :builder])
|
2019-10-24 16:51:49 -04:00
|
|
|
|
|
|
|
assert_equal 2, templates.size
|
|
|
|
assert_equal "Hello HTML!", templates[0].source
|
2020-02-17 02:31:43 -05:00
|
|
|
assert_equal :html, templates[0].format
|
|
|
|
assert_equal "Hello JSON!", templates[1].source
|
|
|
|
assert_equal :json, templates[1].format
|
2019-10-24 16:51:49 -04:00
|
|
|
end
|
|
|
|
|
2020-02-17 02:35:18 -05:00
|
|
|
def test_templates_with_variant
|
|
|
|
with_file "test/hello_world.html+mobile.erb", "Hello HTML!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :builder])
|
|
|
|
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello HTML!", templates[0].source
|
|
|
|
assert_kind_of ActionView::Template::Handlers::ERB, templates[0].handler
|
|
|
|
assert_equal :html, templates[0].format
|
|
|
|
assert_equal "mobile", templates[0].variant
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_finds_variants_in_order
|
|
|
|
with_file "test/hello_world.html+tricorder.erb", "Hello Spock!"
|
|
|
|
with_file "test/hello_world.html+lcars.erb", "Hello Geordi!"
|
|
|
|
|
|
|
|
tricorder = context.find("hello_world", "test", false, [], { variants: [:tricorder] })
|
|
|
|
lcars = context.find("hello_world", "test", false, [], { variants: [:lcars] })
|
|
|
|
|
|
|
|
assert_equal "Hello Spock!", tricorder.source
|
|
|
|
assert_equal "tricorder", tricorder.variant
|
|
|
|
assert_equal "Hello Geordi!", lcars.source
|
|
|
|
assert_equal "lcars", lcars.variant
|
|
|
|
|
|
|
|
templates = context.find_all("hello_world", "test", false, [], { variants: [:tricorder, :lcars] })
|
|
|
|
assert_equal [tricorder, lcars], templates
|
|
|
|
|
|
|
|
templates = context.find_all("hello_world", "test", false, [], { variants: [:lcars, :tricorder] })
|
|
|
|
assert_equal [lcars, tricorder], templates
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_templates_no_format_with_variant
|
|
|
|
with_file "test/hello_world+mobile.erb", "Hello HTML!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :builder])
|
|
|
|
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello HTML!", templates[0].source
|
|
|
|
assert_kind_of ActionView::Template::Handlers::ERB, templates[0].handler
|
|
|
|
assert_nil templates[0].format
|
|
|
|
assert_equal "mobile", templates[0].variant
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_templates_no_format_or_handler_with_variant
|
|
|
|
with_file "test/hello_world+mobile", "Hello HTML!"
|
|
|
|
|
|
|
|
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :builder])
|
|
|
|
|
|
|
|
assert_equal 1, templates.size
|
|
|
|
assert_equal "Hello HTML!", templates[0].source
|
|
|
|
assert_kind_of ActionView::Template::Handlers::Raw, templates[0].handler
|
|
|
|
assert_nil templates[0].format
|
|
|
|
assert_equal "mobile", templates[0].variant
|
|
|
|
end
|
|
|
|
|
2021-04-12 18:49:56 -04:00
|
|
|
def test_returns_no_results_with_dot
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
with_file "test/hello_world.html.erb", "Hello html!"
|
|
|
|
|
2021-04-12 18:49:56 -04:00
|
|
|
assert_empty context.find_all("hello_world.html", "test", false, [], {})
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
end
|
2019-03-18 15:03:39 -04:00
|
|
|
end
|