File: test-encoder.rb

package info (click to toggle)
ruby-htree 0.8%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 520 kB
  • sloc: ruby: 5,928; makefile: 23
file content (58 lines) | stat: -rw-r--r-- 2,062 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'test/unit'
require 'htree/encoder'

class TestEncoder < Test::Unit::TestCase
  EUC_JISX0212_CH = "\217\260\241" # cannot encode with Shift_JIS.
  EUC_JISX0208_CH = "\260\241"
  if EUC_JISX0212_CH.respond_to? :force_encoding
    EUC_JISX0212_CH.force_encoding("EUC-JP")
    EUC_JISX0208_CH.force_encoding("EUC-JP")
  end

  def test_minimal_charset
    out = HTree::Encoder.new('Shift_JIS', 'EUC-JP')
    assert_equal("US-ASCII", out.minimal_charset)
    out.output_text("a")
    assert_equal("US-ASCII", out.minimal_charset)
    out.output_text(EUC_JISX0212_CH)
    assert_equal("US-ASCII", out.minimal_charset)
    out.output_text("b")
    assert_equal("US-ASCII", out.minimal_charset)
    assert_match(/\Aa&#19970;b\z|\Aa&#x4E02;b\z/, out.finish)
  end

  def test_minimal_charset_2
    return if defined?(Encoding) # Ruby 1.9 doesn't support ISO-2022-JP-2 conversion.
    out = HTree::Encoder.new('ISO-2022-JP-2', 'EUC-JP')
    assert_equal("US-ASCII", out.minimal_charset)
    out.output_text("a")
    assert_equal("US-ASCII", out.minimal_charset)
    out.output_text(EUC_JISX0208_CH)
    assert_equal("ISO-2022-JP", out.minimal_charset)
    out.output_text("b")
    assert_equal("ISO-2022-JP", out.minimal_charset)
    out.output_text(EUC_JISX0212_CH)
    assert_equal("ISO-2022-JP-2", out.minimal_charset)
    assert_equal("a\e$B0!\e(Bb\e$(D0!\e(B", out.finish)
  end

  def test_minimal_charset_u
    out = HTree::Encoder.new('UTF-16BE', 'EUC-JP')
    assert_equal("UTF-16BE", out.minimal_charset)
    out.output_text("a")
    assert_equal("UTF-16BE", out.minimal_charset)
    expected = "\000a"
    expected.force_encoding("UTF-16BE") if expected.respond_to? :force_encoding
    assert_equal(expected, out.finish)
  end

  def test_close
    out = HTree::Encoder.new('ISO-2022-JP', 'EUC-JP')
    out.output_string(EUC_JISX0208_CH)
    assert_equal("ISO-2022-JP", out.minimal_charset)
    expected = "\e$B0!\e(B"
    expected.force_encoding("ISO-2022-JP") if expected.respond_to? :force_encoding
    assert_equal(expected, out.finish)
  end

end