summaryrefslogtreecommitdiff
path: root/test/erb
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-12-29 09:26:34 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-12-29 09:26:34 +0000
commitaf91b4235416af1ec499afafcf4524d93437607b (patch)
tree66010749bd70f0fd5d3048a7c65f5eb7babc88c8 /test/erb
parentd33298e52459045ddde4bae619a8da3c3cf7dbd6 (diff)
* lib/erb.rb (ERB): m17n of ERB. adds rdoc.
fixes #712. c.f. [ruby-dev:37516]. * lib/erb.rb (ERB::Compiler#compile): recognizes magic comments. returns a pair of compiled script and its script encoding. * lib/erb.rb (ERB#set_eoutvar): make generated scripts return a string in correct encoding. * lib/erb.rb (ERB#def_method): use Kernel#eval for encoding-awareness of the evaluated string. * bin/erb.rb (ERB::Main.run): adds -E and -U options. String is no longer Enumerable. * man/erb.1: new manapage. * test/erb/test_erb_m17n.rb: new test case for m17n features. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21170 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/erb')
-rw-r--r--test/erb/test_erb_m17n.rb123
1 files changed, 123 insertions, 0 deletions
diff --git a/test/erb/test_erb_m17n.rb b/test/erb/test_erb_m17n.rb
new file mode 100644
index 0000000..432cb4f
--- /dev/null
+++ b/test/erb/test_erb_m17n.rb
@@ -0,0 +1,123 @@
+# -*- coding: UTF-8 -*-
+require 'test/unit'
+require 'erb'
+
+class TestERB < Test::Unit::TestCase
+ def test_result_encoding
+ erb = ERB.new("hello")
+ assert_equal __ENCODING__, erb.result.encoding
+
+ erb = ERB.new("こんにちは".encode("EUC-JP"))
+ assert_equal Encoding::EUC_JP, erb.result.encoding
+
+ erb = ERB.new("\xC4\xE3\xBA\xC3".force_encoding("EUC-CN"))
+ assert_equal Encoding::EUC_CN, erb.result.encoding
+
+ erb = ERB.new("γεια σας".encode("ISO-8859-7"))
+ assert_equal Encoding::ISO_8859_7, erb.result.encoding
+
+ assert_raise(ArgumentError, /ASCII compatible/) {
+ ERB.new("こんにちは".force_encoding("ISO-2022-JP")) # dummy encoding
+ }
+ end
+
+ def test_generate_magic_comment
+ erb = ERB.new("hello")
+ assert_match /#coding:UTF-8/, erb.src
+
+ erb = ERB.new("hello".force_encoding("EUC-JP"))
+ assert_match /#coding:EUC-JP/, erb.src
+
+ erb = ERB.new("hello".force_encoding("ISO-8859-9"))
+ assert_match /#coding:ISO-8859-9/, erb.src
+ end
+
+ def test_literal_encoding
+ erb = ERB.new("literal encoding is <%= 'hello'.encoding %>")
+ assert_match /literal encoding is UTF-8/, erb.result
+
+ erb = ERB.new("literal encoding is <%= 'こんにちは'.encoding %>".encode("EUC-JP"))
+ assert_match /literal encoding is EUC-JP/, erb.result
+
+ erb = ERB.new("literal encoding is <%= '\xC4\xE3\xBA\xC3'.encoding %>".force_encoding("EUC-CN"))
+ assert_match /literal encoding is GB2312/, erb.result
+ end
+
+ def test___ENCODING__
+ erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>")
+ assert_match /__ENCODING__ is UTF-8/, erb.result
+
+ erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>".force_encoding("EUC-JP"))
+ assert_match /__ENCODING__ is EUC-JP/, erb.result
+
+ erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>".force_encoding("Big5"))
+ assert_match /__ENCODING__ is Big5/, erb.result
+ end
+
+ def test_recognize_magic_comment
+ erb = ERB.new(<<-EOS.encode("EUC-KR"))
+<%# -*- coding: EUC-KR -*- %>
+안녕하세요
+ EOS
+ assert_match /#coding:EUC-KR/, erb.src
+ assert_equal Encoding::EUC_KR, erb.result.encoding
+
+ erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("ASCII-8BIT"))
+<%#-*- coding: EUC-KR -*-%>
+안녕하세요
+ EOS
+ assert_match /#coding:EUC-KR/, erb.src
+ assert_equal Encoding::EUC_KR, erb.result.encoding
+
+ erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("ASCII-8BIT"))
+<%# vim: tabsize=8 encoding=EUC-KR shiftwidth=2 expandtab %>
+안녕하세요
+ EOS
+ assert_match /#coding:EUC-KR/, erb.src
+ assert_equal Encoding::EUC_KR, erb.result.encoding
+
+ erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("ASCII-8BIT"))
+<%#coding:EUC-KR %>
+안녕하세요
+ EOS
+ assert_match /#coding:EUC-KR/, erb.src
+ assert_equal Encoding::EUC_KR, erb.result.encoding
+
+ erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("EUC-JP"))
+<%#coding:EUC-KR %>
+안녕하세요
+ EOS
+ assert_match /#coding:EUC-KR/, erb.src
+ assert_equal Encoding::EUC_KR, erb.result.encoding
+ end
+
+ module M; end
+ def test_method_with_encoding
+ obj = Object.new
+ obj.extend(M)
+
+ erb = ERB.new(<<-EOS.encode("EUC-JP").force_encoding("ASCII-8BIT"))
+<%#coding:EUC-JP %>
+literal encoding is <%= 'こんにちは'.encoding %>
+__ENCODING__ is <%= __ENCODING__ %>
+ EOS
+ erb.def_method(M, :m_from_magic_comment)
+
+ result = obj.m_from_magic_comment
+ assert_equal Encoding::EUC_JP, result.encoding
+ assert_match /literal encoding is EUC-JP/, result
+ assert_match /__ENCODING__ is EUC-JP/, result
+
+ erb = ERB.new(<<-EOS.encode("EUC-KR"))
+literal encoding is <%= '안녕하세요'.encoding %>
+__ENCODING__ is <%= __ENCODING__ %>
+EOS
+ erb.def_method(M, :m_from_eval_encoding)
+ result = obj.m_from_eval_encoding
+ assert_equal Encoding::EUC_KR, result.encoding
+ assert_match /literal encoding is EUC-KR/, result
+ assert_match /__ENCODING__ is EUC-KR/, result
+ end
+end
+
+# vim:fileencoding=UTF-8