2012-11-12 21:17:06 -05:00
|
|
|
# encoding: US-ASCII
|
2010-05-16 02:25:55 -04:00
|
|
|
require "abstract_unit"
|
2010-10-10 07:17:01 -04:00
|
|
|
require "logger"
|
2010-05-16 02:25:55 -04:00
|
|
|
|
|
|
|
class TestERBTemplate < ActiveSupport::TestCase
|
2010-10-09 19:14:50 -04:00
|
|
|
ERBHandler = ActionView::Template::Handlers::ERB.new
|
2010-05-16 02:25:55 -04:00
|
|
|
|
2010-10-10 08:47:11 -04:00
|
|
|
class LookupContext
|
|
|
|
def disable_cache
|
|
|
|
yield
|
2010-10-10 07:17:01 -04:00
|
|
|
end
|
2011-11-03 12:09:57 -04:00
|
|
|
|
|
|
|
def find_template(*args)
|
|
|
|
end
|
2012-02-20 15:10:07 -05:00
|
|
|
|
2012-02-21 12:57:24 -05:00
|
|
|
attr_accessor :formats
|
2010-10-10 08:47:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Context
|
2010-05-16 02:25:55 -04:00
|
|
|
def initialize
|
|
|
|
@output_buffer = "original"
|
2011-05-01 05:14:38 -04:00
|
|
|
@virtual_path = nil
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def hello
|
|
|
|
"Hello"
|
|
|
|
end
|
|
|
|
|
2012-11-16 03:11:43 -05:00
|
|
|
def apostrophe
|
|
|
|
"l'apostrophe"
|
|
|
|
end
|
|
|
|
|
2010-05-16 02:25:55 -04:00
|
|
|
def partial
|
|
|
|
ActionView::Template.new(
|
2011-05-01 05:14:38 -04:00
|
|
|
"<%= @virtual_path %>",
|
2010-05-16 02:25:55 -04:00
|
|
|
"partial",
|
|
|
|
ERBHandler,
|
|
|
|
:virtual_path => "partial"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-10-10 07:17:01 -04:00
|
|
|
def lookup_context
|
|
|
|
@lookup_context ||= LookupContext.new
|
|
|
|
end
|
|
|
|
|
2010-05-16 02:25:55 -04:00
|
|
|
def logger
|
2012-01-04 13:52:24 -05:00
|
|
|
ActiveSupport::Logger.new(STDERR)
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def my_buffer
|
|
|
|
@output_buffer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-16 06:23:59 -05:00
|
|
|
def new_template(body = "<%= hello %>", details = { format: :html })
|
2012-05-13 20:22:29 -04:00
|
|
|
ActionView::Template.new(body, "hello template", details.fetch(:handler) { ERBHandler }, {:virtual_path => "hello"}.merge!(details))
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render(locals = {})
|
2010-10-10 07:17:01 -04:00
|
|
|
@template.render(@context, locals)
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
2010-10-10 07:17:01 -04:00
|
|
|
@context = Context.new
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_basic_template
|
|
|
|
@template = new_template
|
|
|
|
assert_equal "Hello", render
|
|
|
|
end
|
|
|
|
|
2012-11-16 03:11:43 -05:00
|
|
|
def test_basic_template_does_html_escape
|
|
|
|
@template = new_template("<%= apostrophe %>")
|
|
|
|
assert_equal "l'apostrophe", render
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_text_template_does_not_html_escape
|
2012-12-03 12:30:58 -05:00
|
|
|
@template = new_template("<%= apostrophe %> <%== apostrophe %>", format: :text)
|
|
|
|
assert_equal "l'apostrophe l'apostrophe", render
|
2012-11-16 03:11:43 -05:00
|
|
|
end
|
|
|
|
|
2012-05-13 20:22:29 -04:00
|
|
|
def test_raw_template
|
|
|
|
@template = new_template("<%= hello %>", :handler => ActionView::Template::Handlers::Raw.new)
|
|
|
|
assert_equal "<%= hello %>", render
|
|
|
|
end
|
|
|
|
|
2010-10-09 17:56:08 -04:00
|
|
|
def test_template_loses_its_source_after_rendering
|
|
|
|
@template = new_template
|
|
|
|
render
|
|
|
|
assert_nil @template.source
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_template_does_not_lose_its_source_after_rendering_if_it_does_not_have_a_virtual_path
|
|
|
|
@template = new_template("Hello", :virtual_path => nil)
|
|
|
|
render
|
|
|
|
assert_equal "Hello", @template.source
|
|
|
|
end
|
|
|
|
|
2010-05-16 02:25:55 -04:00
|
|
|
def test_locals
|
|
|
|
@template = new_template("<%= my_local %>")
|
2010-10-07 09:50:20 -04:00
|
|
|
@template.locals = [:my_local]
|
2012-07-31 21:25:54 -04:00
|
|
|
assert_equal "I am a local", render(:my_local => "I am a local")
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_restores_buffer
|
|
|
|
@template = new_template
|
|
|
|
assert_equal "Hello", render
|
2010-10-10 07:17:01 -04:00
|
|
|
assert_equal "original", @context.my_buffer
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_virtual_path
|
2011-05-01 05:14:38 -04:00
|
|
|
@template = new_template("<%= @virtual_path %>" \
|
2010-05-16 02:25:55 -04:00
|
|
|
"<%= partial.render(self, {}) %>" \
|
2011-05-01 05:14:38 -04:00
|
|
|
"<%= @virtual_path %>")
|
2010-05-16 02:25:55 -04:00
|
|
|
assert_equal "hellopartialhello", render
|
|
|
|
end
|
|
|
|
|
2010-10-10 07:43:32 -04:00
|
|
|
def test_refresh_with_templates
|
2010-10-14 23:11:40 -04:00
|
|
|
@template = new_template("Hello", :virtual_path => "test/foo/bar")
|
2010-10-10 07:17:01 -04:00
|
|
|
@template.locals = [:key]
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_called_with(@context.lookup_context, :find_template,["bar", %w(test/foo), false, [:key]], returns: "template") do
|
|
|
|
assert_equal "template", @template.refresh(@context)
|
|
|
|
end
|
2010-10-10 07:17:01 -04:00
|
|
|
end
|
|
|
|
|
2010-10-10 07:43:32 -04:00
|
|
|
def test_refresh_with_partials
|
|
|
|
@template = new_template("Hello", :virtual_path => "test/_foo")
|
|
|
|
@template.locals = [:key]
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_called_with(@context.lookup_context, :find_template,[ "foo", %w(test), true, [:key]], returns: "partial") do
|
|
|
|
assert_equal "partial", @template.refresh(@context)
|
|
|
|
end
|
2010-10-10 07:43:32 -04:00
|
|
|
end
|
|
|
|
|
2010-10-10 07:17:01 -04:00
|
|
|
def test_refresh_raises_an_error_without_virtual_path
|
|
|
|
@template = new_template("Hello", :virtual_path => nil)
|
2010-10-10 07:43:32 -04:00
|
|
|
assert_raise RuntimeError do
|
2010-10-10 07:17:01 -04:00
|
|
|
@template.refresh(@context)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
def test_resulting_string_is_utf8
|
|
|
|
@template = new_template
|
|
|
|
assert_equal Encoding::UTF_8, render.encoding
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_magic_comment_word_with_utf_8
|
|
|
|
@template = new_template("hello \u{fc}mlat")
|
|
|
|
assert_equal Encoding::UTF_8, render.encoding
|
|
|
|
assert_equal "hello \u{fc}mlat", render
|
|
|
|
end
|
2010-05-16 02:25:55 -04:00
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
# This test ensures that if the default_external
|
|
|
|
# is set to something other than UTF-8, we don't
|
|
|
|
# get any errors and get back a UTF-8 String.
|
|
|
|
def test_default_external_works
|
|
|
|
with_external_encoding "ISO-8859-1" do
|
|
|
|
@template = new_template("hello \xFCmlat")
|
2010-05-16 02:25:55 -04:00
|
|
|
assert_equal Encoding::UTF_8, render.encoding
|
|
|
|
assert_equal "hello \u{fc}mlat", render
|
|
|
|
end
|
2011-12-24 07:57:54 -05:00
|
|
|
end
|
2010-05-16 02:25:55 -04:00
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
def test_encoding_can_be_specified_with_magic_comment
|
|
|
|
@template = new_template("# encoding: ISO-8859-1\nhello \xFCmlat")
|
|
|
|
assert_equal Encoding::UTF_8, render.encoding
|
|
|
|
assert_equal "\nhello \u{fc}mlat", render
|
|
|
|
end
|
2010-05-16 02:25:55 -04:00
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
# TODO: This is currently handled inside ERB. The case of explicitly
|
|
|
|
# lying about encodings via the normal Rails API should be handled
|
|
|
|
# inside Rails.
|
|
|
|
def test_lying_with_magic_comment
|
|
|
|
assert_raises(ActionView::Template::Error) do
|
|
|
|
@template = new_template("# encoding: UTF-8\nhello \xFCmlat", :virtual_path => nil)
|
|
|
|
render
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
2011-12-24 07:57:54 -05:00
|
|
|
end
|
2010-05-16 02:25:55 -04:00
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
def test_encoding_can_be_specified_with_magic_comment_in_erb
|
|
|
|
with_external_encoding Encoding::UTF_8 do
|
|
|
|
@template = new_template("<%# encoding: ISO-8859-1 %>hello \xFCmlat", :virtual_path => nil)
|
|
|
|
assert_equal Encoding::UTF_8, render.encoding
|
|
|
|
assert_equal "hello \u{fc}mlat", render
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
2011-12-24 07:57:54 -05:00
|
|
|
end
|
2010-05-16 02:25:55 -04:00
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
def test_error_when_template_isnt_valid_utf8
|
2014-11-07 21:27:18 -05:00
|
|
|
e = assert_raises ActionView::Template::Error do
|
2011-12-24 07:57:54 -05:00
|
|
|
@template = new_template("hello \xFCmlat", :virtual_path => nil)
|
|
|
|
render
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
2014-11-07 21:27:18 -05:00
|
|
|
assert_match(/\xFC/, e.message)
|
2011-12-24 07:57:54 -05:00
|
|
|
end
|
2010-05-30 05:07:34 -04:00
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
def with_external_encoding(encoding)
|
|
|
|
old = Encoding.default_external
|
|
|
|
Encoding::Converter.new old, encoding if old != encoding
|
|
|
|
silence_warnings { Encoding.default_external = encoding }
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
silence_warnings { Encoding.default_external = old }
|
2010-05-16 02:25:55 -04:00
|
|
|
end
|
2010-08-21 21:36:51 -04:00
|
|
|
end
|