2017-07-23 15:36:41 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 16:50:17 +00:00
|
|
|
require "abstract_unit"
|
2008-07-16 13:26:23 +00:00
|
|
|
|
2012-01-06 01:01:45 +00:00
|
|
|
class CompiledTemplatesTest < ActiveSupport::TestCase
|
2019-01-23 22:19:50 +00:00
|
|
|
attr_reader :view_class
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
2019-01-24 00:11:54 +00:00
|
|
|
view_paths = ActionController::Base.view_paths
|
2019-01-23 22:19:50 +00:00
|
|
|
view_paths.each(&:clear_cache)
|
|
|
|
@view_class = ActionView::Base.with_empty_template_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
2011-12-14 08:23:34 +00:00
|
|
|
ActionView::LookupContext::DetailsKey.clear
|
2009-02-04 02:25:37 +00:00
|
|
|
end
|
2010-08-14 05:13:00 +00:00
|
|
|
|
2014-09-15 00:11:04 +00:00
|
|
|
def test_template_with_nil_erb_return
|
2016-08-06 17:36:34 +00:00
|
|
|
assert_equal "This is nil: \n", render(template: "test/nil_return")
|
2014-09-15 00:11:04 +00:00
|
|
|
end
|
|
|
|
|
2016-09-30 17:55:38 +00:00
|
|
|
def test_template_with_ruby_keyword_locals
|
|
|
|
assert_equal "The class is foo",
|
2019-03-19 18:56:53 +00:00
|
|
|
render(template: "test/render_file_with_ruby_keyword_locals", locals: { class: "foo" })
|
2016-09-30 17:55:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_template_with_invalid_identifier_locals
|
|
|
|
locals = {
|
|
|
|
foo: "bar",
|
|
|
|
Foo: "bar",
|
|
|
|
"d-a-s-h-e-s": "",
|
|
|
|
"white space": "",
|
|
|
|
}
|
2019-03-19 18:56:53 +00:00
|
|
|
assert_equal locals.inspect, render(template: "test/render_file_inspect_local_assigns", locals: locals)
|
2016-09-30 17:55:38 +00:00
|
|
|
end
|
|
|
|
|
2016-12-08 19:55:21 +00:00
|
|
|
def test_template_with_delegation_reserved_keywords
|
|
|
|
locals = {
|
|
|
|
_: "one",
|
|
|
|
arg: "two",
|
|
|
|
args: "three",
|
|
|
|
block: "four",
|
|
|
|
}
|
2019-03-19 18:56:53 +00:00
|
|
|
assert_equal "one two three four", render(template: "test/test_template_with_delegation_reserved_keywords", locals: locals)
|
2016-12-08 19:55:21 +00:00
|
|
|
end
|
|
|
|
|
2016-09-30 17:55:38 +00:00
|
|
|
def test_template_with_unicode_identifier
|
2019-03-19 18:56:53 +00:00
|
|
|
assert_equal "🎂", render(template: "test/render_file_unicode_local", locals: { 🎃: "🎂" })
|
2016-09-30 17:55:38 +00:00
|
|
|
end
|
|
|
|
|
2017-01-15 13:41:39 +00:00
|
|
|
def test_template_with_instance_variable_identifier
|
2021-02-17 08:41:37 +00:00
|
|
|
expected_deprecation = "In Rails 7.1, @foo will be ignored."
|
2021-02-16 18:20:21 +00:00
|
|
|
assert_deprecated(expected_deprecation) do
|
|
|
|
assert_equal "bar", render(template: "test/render_file_instance_variable", locals: { "@foo": "bar" })
|
|
|
|
end
|
2017-01-15 13:41:39 +00:00
|
|
|
end
|
|
|
|
|
2009-02-04 02:25:37 +00:00
|
|
|
def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
|
2019-03-19 18:56:53 +00:00
|
|
|
assert_equal "one", render(template: "test/render_file_with_locals_and_default")
|
|
|
|
assert_equal "two", render(template: "test/render_file_with_locals_and_default", locals: { secret: "two" })
|
2009-02-20 02:55:56 +00:00
|
|
|
end
|
2008-11-28 20:31:54 +00:00
|
|
|
|
2009-02-04 02:25:37 +00:00
|
|
|
private
|
|
|
|
def render(*args)
|
2019-06-06 01:22:04 +00:00
|
|
|
ActionController::Base.render(*args)
|
2009-02-04 02:25:37 +00:00
|
|
|
end
|
2008-07-16 13:26:23 +00:00
|
|
|
end
|