1# -*- coding: utf-8 -*-
2# frozen_string_literal: false
3require 'test/unit'
4require 'erb'
5
6class TestERBEncoding < Test::Unit::TestCase
7  def test_result_encoding
8    erb = ERB.new("hello")
9    assert_equal __ENCODING__, erb.result.encoding
10
11    erb = ERB.new("こんにちは".encode("EUC-JP"))
12    assert_equal Encoding::EUC_JP, erb.result.encoding
13
14    erb = ERB.new("\xC4\xE3\xBA\xC3".force_encoding("EUC-CN"))
15    assert_equal Encoding::EUC_CN, erb.result.encoding
16
17    erb = ERB.new("γεια σας".encode("ISO-8859-7"))
18    assert_equal Encoding::ISO_8859_7, erb.result.encoding
19
20    assert_raise(ArgumentError, /ASCII compatible/) {
21      ERB.new("こんにちは".force_encoding("ISO-2022-JP")) # dummy encoding
22    }
23  end
24
25  def test_generate_magic_comment
26    erb = ERB.new("hello")
27    assert_match(/#coding:UTF-8/, erb.src)
28
29    erb = ERB.new("hello".force_encoding("EUC-JP"))
30    assert_match(/#coding:EUC-JP/, erb.src)
31
32    erb = ERB.new("hello".force_encoding("ISO-8859-9"))
33    assert_match(/#coding:ISO-8859-9/, erb.src)
34  end
35
36  def test_literal_encoding
37    erb = ERB.new("literal encoding is <%= 'hello'.encoding  %>")
38    assert_match(/literal encoding is UTF-8/, erb.result)
39
40    erb = ERB.new("literal encoding is <%= 'こんにちは'.encoding  %>".encode("EUC-JP"))
41    assert_match(/literal encoding is EUC-JP/, erb.result)
42
43    erb = ERB.new("literal encoding is <%= '\xC4\xE3\xBA\xC3'.encoding %>".force_encoding("EUC-CN"))
44    assert_match(/literal encoding is GB2312/, erb.result)
45  end
46
47  def test___ENCODING__
48    erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>")
49    assert_match(/__ENCODING__ is UTF-8/, erb.result)
50
51    erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>".force_encoding("EUC-JP"))
52    assert_match(/__ENCODING__ is EUC-JP/, erb.result)
53
54    erb = ERB.new("__ENCODING__ is <%= __ENCODING__ %>".force_encoding("Big5"))
55    assert_match(/__ENCODING__ is Big5/, erb.result)
56  end
57
58  def test_recognize_magic_comment
59    erb = ERB.new(<<-EOS.encode("EUC-KR"))
60<%# -*- coding: EUC-KR -*- %>
61안녕하세요
62    EOS
63    assert_match(/#coding:EUC-KR/, erb.src)
64    assert_equal Encoding::EUC_KR, erb.result.encoding
65
66    erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("ASCII-8BIT"))
67<%#-*- coding: EUC-KR -*-%>
68안녕하세요
69    EOS
70    assert_match(/#coding:EUC-KR/, erb.src)
71    assert_equal Encoding::EUC_KR, erb.result.encoding
72
73    erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("ASCII-8BIT"))
74<%# vim: tabsize=8 encoding=EUC-KR shiftwidth=2 expandtab %>
75안녕하세요
76    EOS
77    assert_match(/#coding:EUC-KR/, erb.src)
78    assert_equal Encoding::EUC_KR, erb.result.encoding
79
80    erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("ASCII-8BIT"))
81<%#coding:EUC-KR %>
82안녕하세요
83    EOS
84    assert_match(/#coding:EUC-KR/, erb.src)
85    assert_equal Encoding::EUC_KR, erb.result.encoding
86
87    erb = ERB.new(<<-EOS.encode("EUC-KR").force_encoding("EUC-JP"))
88<%#coding:EUC-KR %>
89안녕하세요
90    EOS
91    assert_match(/#coding:EUC-KR/, erb.src)
92    assert_equal Encoding::EUC_KR, erb.result.encoding
93  end
94
95  module M; end
96  def test_method_with_encoding
97    obj = Object.new
98    obj.extend(M)
99
100    erb = ERB.new(<<-EOS.encode("EUC-JP").force_encoding("ASCII-8BIT"))
101<%#coding:EUC-JP %>
102literal encoding is <%= 'こんにちは'.encoding %>
103__ENCODING__ is <%= __ENCODING__ %>
104    EOS
105    erb.def_method(M, :m_from_magic_comment)
106
107    result = obj.m_from_magic_comment
108    assert_equal Encoding::EUC_JP, result.encoding
109    assert_match(/literal encoding is EUC-JP/, result)
110    assert_match(/__ENCODING__ is EUC-JP/, result)
111
112    erb = ERB.new(<<-EOS.encode("EUC-KR"))
113literal encoding is <%= '안녕하세요'.encoding %>
114__ENCODING__ is <%= __ENCODING__ %>
115EOS
116    erb.def_method(M, :m_from_eval_encoding)
117    result = obj.m_from_eval_encoding
118    assert_equal Encoding::EUC_KR, result.encoding
119    assert_match(/literal encoding is EUC-KR/, result)
120    assert_match(/__ENCODING__ is EUC-KR/, result)
121  end
122end
123
124# vim:fileencoding=UTF-8
125