2014-07-25 03:55:58 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2008-12-29 04:26:34 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'erb'
|
|
|
|
|
2018-03-13 02:29:02 -04:00
|
|
|
class TestERBEncoding < Test::Unit::TestCase
|
2008-12-29 04:26:34 -05:00
|
|
|
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
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_raise(ArgumentError, /ASCII compatible/) {
|
2008-12-29 04:26:34 -05:00
|
|
|
ERB.new("こんにちは".force_encoding("ISO-2022-JP")) # dummy encoding
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_generate_magic_comment
|
|
|
|
erb = ERB.new("hello")
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/#coding:UTF-8/, erb.src)
|
2008-12-29 04:26:34 -05:00
|
|
|
|
|
|
|
erb = ERB.new("hello".force_encoding("EUC-JP"))
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/#coding:EUC-JP/, erb.src)
|
2008-12-29 04:26:34 -05:00
|
|
|
|
|
|
|
erb = ERB.new("hello".force_encoding("ISO-8859-9"))
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/#coding:ISO-8859-9/, erb.src)
|
2008-12-29 04:26:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_literal_encoding
|
|
|
|
erb = ERB.new("literal encoding is <%= 'hello'.encoding %>")
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/literal encoding is UTF-8/, erb.result)
|
2008-12-29 04:26:34 -05:00
|
|
|
|
|
|
|
erb = ERB.new("literal encoding is <%= 'こんにちは'.encoding %>".encode("EUC-JP"))
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/literal encoding is EUC-JP/, erb.result)
|
2008-12-29 04:26:34 -05:00
|
|
|
|
|
|
|
erb = ERB.new("literal encoding is <%= '\xC4\xE3\xBA\xC3'.encoding %>".force_encoding("EUC-CN"))
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/literal encoding is GB2312/, erb.result)
|
2008-12-29 04:26:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test___ENCODING__
|
|
|
|
erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>")
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/__ENCODING__ is UTF-8/, erb.result)
|
2008-12-29 04:26:34 -05:00
|
|
|
|
|
|
|
erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>".force_encoding("EUC-JP"))
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/__ENCODING__ is EUC-JP/, erb.result)
|
2008-12-29 04:26:34 -05:00
|
|
|
|
|
|
|
erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>".force_encoding("Big5"))
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/__ENCODING__ is Big5/, erb.result)
|
2008-12-29 04:26:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_recognize_magic_comment
|
|
|
|
erb = ERB.new(<<-EOS.encode("EUC-KR"))
|
|
|
|
<%# -*- coding: EUC-KR -*- %>
|
|
|
|
안녕하세요
|
|
|
|
EOS
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/#coding:EUC-KR/, erb.src)
|
2008-12-29 04:26:34 -05:00
|
|
|
assert_equal Encoding::EUC_KR, erb.result.encoding
|
|
|
|
|
|
|
|
erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("ASCII-8BIT"))
|
|
|
|
<%#-*- coding: EUC-KR -*-%>
|
|
|
|
안녕하세요
|
|
|
|
EOS
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/#coding:EUC-KR/, erb.src)
|
2008-12-29 04:26:34 -05:00
|
|
|
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
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/#coding:EUC-KR/, erb.src)
|
2008-12-29 04:26:34 -05:00
|
|
|
assert_equal Encoding::EUC_KR, erb.result.encoding
|
|
|
|
|
|
|
|
erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("ASCII-8BIT"))
|
|
|
|
<%#coding:EUC-KR %>
|
|
|
|
안녕하세요
|
|
|
|
EOS
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/#coding:EUC-KR/, erb.src)
|
2008-12-29 04:26:34 -05:00
|
|
|
assert_equal Encoding::EUC_KR, erb.result.encoding
|
|
|
|
|
|
|
|
erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("EUC-JP"))
|
|
|
|
<%#coding:EUC-KR %>
|
|
|
|
안녕하세요
|
|
|
|
EOS
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/#coding:EUC-KR/, erb.src)
|
2008-12-29 04:26:34 -05:00
|
|
|
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
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/literal encoding is EUC-JP/, result)
|
|
|
|
assert_match(/__ENCODING__ is EUC-JP/, result)
|
2008-12-29 04:26:34 -05:00
|
|
|
|
|
|
|
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
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/literal encoding is EUC-KR/, result)
|
|
|
|
assert_match(/__ENCODING__ is EUC-KR/, result)
|
2008-12-29 04:26:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim:fileencoding=UTF-8
|