aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template/template_test.rb
diff options
context:
space:
mode:
authorwycats <wycats@gmail.com>2010-05-16 10:25:55 +0400
committerwycats <wycats@gmail.com>2010-05-16 22:44:43 +0400
commit64d109e3539ad600f58536d3ecabd2f87b67fd1c (patch)
tree4b1deedbd5e33dd5410b1a37e9895f7a254c751a /actionpack/test/template/template_test.rb
parentaf0d1a88157942c6e6398dbf73891cff1e152405 (diff)
downloadrails-64d109e3539ad600f58536d3ecabd2f87b67fd1c.tar.gz
rails-64d109e3539ad600f58536d3ecabd2f87b67fd1c.tar.bz2
rails-64d109e3539ad600f58536d3ecabd2f87b67fd1c.zip
Significantly improved internal encoding heuristics and support.
* Default Encoding.default_internal to UTF-8 * Eliminated the use of file-wide magic comments to coerce code evaluated inside the file * Read templates as BINARY, use default_external or template-wide magic comments inside the Template to set the initial encoding * This means that template handlers in Ruby 1.9 will receive Strings encoded in default_internal (UTF-8 by default) * Create a better Exception for encoding issues, and use it when the template source has bytes that are not compatible with the specified encoding * Allow template handlers to opt-into handling BINARY. If they do so, they need to do some of their own manual encoding work * Added a "Configuration Gotchas" section to the intro Rails Guide instructing users to use UTF-8 for everything * Use config.encoding= in Ruby 1.8, and raise if a value that is an invalid $KCODE value is used Also: * Fixed a few tests that were assert() rather than assert_equal() and were caught by Minitest requiring a String for the message * Fixed a test where an assert_select was misformed, also caught by Minitest being more restrictive * Fixed a test where a Rack response was returning a String rather than an Enumerable
Diffstat (limited to 'actionpack/test/template/template_test.rb')
-rw-r--r--actionpack/test/template/template_test.rb128
1 files changed, 128 insertions, 0 deletions
diff --git a/actionpack/test/template/template_test.rb b/actionpack/test/template/template_test.rb
new file mode 100644
index 0000000000..c4a65d84fc
--- /dev/null
+++ b/actionpack/test/template/template_test.rb
@@ -0,0 +1,128 @@
+require "abstract_unit"
+
+# These are the normal settings that will be set up by Railties
+# TODO: Have these tests support other combinations of these values
+Encoding.default_internal = "UTF-8"
+Encoding.default_external = "UTF-8"
+
+class TestERBTemplate < ActiveSupport::TestCase
+ ERBHandler = ActionView::Template::Handlers::ERB
+
+ class Context
+ def initialize
+ @output_buffer = "original"
+ end
+
+ def hello
+ "Hello"
+ end
+
+ def partial
+ ActionView::Template.new(
+ "<%= @_virtual_path %>",
+ "partial",
+ ERBHandler,
+ :virtual_path => "partial"
+ )
+ end
+
+ def logger
+ require "logger"
+ Logger.new(STDERR)
+ end
+
+ def my_buffer
+ @output_buffer
+ end
+ end
+
+ def new_template(body = "<%= hello %>", handler = ERBHandler, details = {})
+ ActionView::Template.new(body, "hello template", ERBHandler, {:virtual_path => "hello"})
+ end
+
+ def render(locals = {})
+ @template.render(@obj, locals)
+ end
+
+ def setup
+ @obj = Context.new
+ end
+
+ def test_basic_template
+ @template = new_template
+ assert_equal "Hello", render
+ end
+
+ def test_locals
+ @template = new_template("<%= my_local %>")
+ assert_equal "I'm a local", render(:my_local => "I'm a local")
+ end
+
+ def test_restores_buffer
+ @template = new_template
+ assert_equal "Hello", render
+ assert_equal "original", @obj.my_buffer
+ end
+
+ def test_virtual_path
+ @template = new_template("<%= @_virtual_path %>" \
+ "<%= partial.render(self, {}) %>" \
+ "<%= @_virtual_path %>")
+ assert_equal "hellopartialhello", render
+ end
+
+ if "ruby".encoding_aware?
+ 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
+
+ # 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
+ Encoding.default_external = "ISO-8859-1"
+ @template = new_template("hello \xFCmlat")
+ assert_equal Encoding::UTF_8, render.encoding
+ assert_equal "hello \u{fc}mlat", render
+ ensure
+ Encoding.default_external = "UTF-8"
+ end
+
+ 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
+
+ # 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")
+ render
+ end
+ end
+
+ def test_encoding_can_be_specified_with_magic_comment_in_erb
+ @template = new_template("<%# encoding: ISO-8859-1 %>hello \xFCmlat")
+ result = render
+ assert_equal Encoding::UTF_8, render.encoding
+ assert_equal "hello \u{fc}mlat", render
+ end
+
+ def test_error_when_template_isnt_valid_utf8
+ assert_raises(ActionView::Template::Error, /\xFC/) do
+ @template = new_template("hello \xFCmlat")
+ render
+ end
+ end
+ end
+end \ No newline at end of file