2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
2008-07-16 09:26:23 -04:00
|
|
|
|
2012-01-05 20:01:45 -05:00
|
|
|
class CompiledTemplatesTest < ActiveSupport::TestCase
|
2014-06-14 03:42:51 -04:00
|
|
|
teardown do
|
2011-12-14 03:23:34 -05:00
|
|
|
ActionView::LookupContext::DetailsKey.clear
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2014-09-14 20:11:04 -04:00
|
|
|
def test_template_with_nil_erb_return
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "This is nil: \n", render(template: "test/nil_return")
|
2014-09-14 20:11:04 -04:00
|
|
|
end
|
|
|
|
|
2016-09-30 13:55:38 -04:00
|
|
|
def test_template_with_ruby_keyword_locals
|
|
|
|
assert_equal "The class is foo",
|
|
|
|
render(file: "test/render_file_with_ruby_keyword_locals", locals: { class: "foo" })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_template_with_invalid_identifier_locals
|
|
|
|
locals = {
|
|
|
|
foo: "bar",
|
|
|
|
Foo: "bar",
|
|
|
|
"d-a-s-h-e-s": "",
|
|
|
|
"white space": "",
|
|
|
|
}
|
|
|
|
assert_equal locals.inspect, render(file: "test/render_file_inspect_local_assigns", locals: locals)
|
|
|
|
end
|
|
|
|
|
2016-12-08 14:55:21 -05:00
|
|
|
def test_template_with_delegation_reserved_keywords
|
|
|
|
locals = {
|
|
|
|
_: "one",
|
|
|
|
arg: "two",
|
|
|
|
args: "three",
|
|
|
|
block: "four",
|
|
|
|
}
|
|
|
|
assert_equal "one two three four", render(file: "test/test_template_with_delegation_reserved_keywords", locals: locals)
|
|
|
|
end
|
|
|
|
|
2016-09-30 13:55:38 -04:00
|
|
|
def test_template_with_unicode_identifier
|
|
|
|
assert_equal "🎂", render(file: "test/render_file_unicode_local", locals: { 🎃: "🎂" })
|
|
|
|
end
|
|
|
|
|
2017-01-15 08:41:39 -05:00
|
|
|
def test_template_with_instance_variable_identifier
|
|
|
|
assert_equal "bar", render(file: "test/render_file_instance_variable", locals: { "@foo": "bar" })
|
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "one", render(file: "test/render_file_with_locals_and_default")
|
|
|
|
assert_equal "two", render(file: "test/render_file_with_locals_and_default", locals: { secret: "two" })
|
2009-02-19 21:55:56 -05:00
|
|
|
end
|
2008-11-28 15:31:54 -05:00
|
|
|
|
2009-04-13 18:18:45 -04:00
|
|
|
def test_template_changes_are_not_reflected_with_cached_templates
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Hello world!", render(file: "test/hello_world")
|
2009-04-13 18:18:45 -04:00
|
|
|
modify_template "test/hello_world.erb", "Goodbye world!" do
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Hello world!", render(file: "test/hello_world")
|
2008-11-28 15:31:54 -05:00
|
|
|
end
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Hello world!", render(file: "test/hello_world")
|
2009-02-19 21:55:56 -05:00
|
|
|
end
|
2008-11-28 15:31:54 -05:00
|
|
|
|
2009-04-13 18:18:45 -04:00
|
|
|
def test_template_changes_are_reflected_with_uncached_templates
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Hello world!", render_without_cache(file: "test/hello_world")
|
2009-04-13 18:18:45 -04:00
|
|
|
modify_template "test/hello_world.erb", "Goodbye world!" do
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Goodbye world!", render_without_cache(file: "test/hello_world")
|
2008-11-28 15:31:54 -05:00
|
|
|
end
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Hello world!", render_without_cache(file: "test/hello_world")
|
2009-02-19 21:55:56 -05:00
|
|
|
end
|
2008-11-28 15:31:54 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
private
|
|
|
|
def render(*args)
|
2009-04-13 18:18:45 -04:00
|
|
|
render_with_cache(*args)
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-11-28 15:31:54 -05:00
|
|
|
|
2009-04-13 18:18:45 -04:00
|
|
|
def render_with_cache(*args)
|
|
|
|
view_paths = ActionController::Base.view_paths
|
|
|
|
ActionView::Base.new(view_paths, {}).render(*args)
|
2009-02-12 13:35:14 -05:00
|
|
|
end
|
2008-11-28 15:31:54 -05:00
|
|
|
|
2009-04-13 18:18:45 -04:00
|
|
|
def render_without_cache(*args)
|
2010-03-08 10:32:40 -05:00
|
|
|
path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH)
|
2011-08-09 15:10:25 -04:00
|
|
|
view_paths = ActionView::PathSet.new([path])
|
2009-04-13 18:18:45 -04:00
|
|
|
ActionView::Base.new(view_paths, {}).render(*args)
|
2009-02-19 21:55:56 -05:00
|
|
|
end
|
2008-11-28 15:31:54 -05:00
|
|
|
|
2009-04-13 18:18:45 -04:00
|
|
|
def modify_template(template, content)
|
|
|
|
filename = "#{FIXTURE_LOAD_PATH}/#{template}"
|
2009-02-03 21:25:37 -05:00
|
|
|
old_content = File.read(filename)
|
|
|
|
begin
|
|
|
|
File.open(filename, "wb+") { |f| f.write(content) }
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
File.open(filename, "wb+") { |f| f.write(old_content) }
|
2008-07-22 11:27:32 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-07-16 09:26:23 -04:00
|
|
|
end
|