File: encoding_test.rb

package info (click to toggle)
ruby-htmlentities 4.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 316 kB
  • sloc: ruby: 2,235; makefile: 3
file content (106 lines) | stat: -rw-r--r-- 3,009 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# encoding: UTF-8
require_relative "./test_helper"

class HTMLEntities::EncodingTest < Test::Unit::TestCase

  def setup
    @entities = [:xhtml1, :html4, :expanded].map{ |a| HTMLEntities.new(a) }
  end

  def assert_encode(expected, input, *args)
    @entities.each do |coder|
      assert_equal expected, coder.encode(input, *args)
    end
  end

  def test_should_encode_basic_entities
    assert_encode '&amp;',  '&', :basic
    assert_encode '&quot;', '"'
    assert_encode '&lt;',   '<', :basic
    assert_encode '&lt;',   '<'
  end

  def test_should_encode_basic_entities_to_decimal
    assert_encode '&#38;', '&', :decimal
    assert_encode '&#34;', '"', :decimal
    assert_encode '&#60;', '<', :decimal
    assert_encode '&#62;', '>', :decimal
    assert_encode '&#39;', "'", :decimal
  end

  def test_should_encode_basic_entities_to_hexadecimal
    assert_encode '&#x26;', '&', :hexadecimal
    assert_encode '&#x22;', '"', :hexadecimal
    assert_encode '&#x3c;', '<', :hexadecimal
    assert_encode '&#x3e;', '>', :hexadecimal
    assert_encode '&#x27;', "'", :hexadecimal
  end

  def test_should_encode_extended_named_entities
    assert_encode '&plusmn;', '±', :named
    assert_encode '&eth;',    'ð', :named
    assert_encode '&OElig;',  'Œ', :named
    assert_encode '&oelig;',  'œ', :named
  end

  def test_should_encode_decimal_entities
    assert_encode '&#8220;', '“', :decimal
    assert_encode '&#8230;', '…', :decimal
  end

  def test_should_encode_hexadecimal_entities
    assert_encode '&#x2212;', '−', :hexadecimal
    assert_encode '&#x2014;', '—', :hexadecimal
  end

  def test_should_encode_text_using_mix_of_entities
    assert_encode(
      '&quot;bient&ocirc;t&quot; &amp; &#x6587;&#x5b57;',
      '"bientôt" & 文字', :basic, :named, :hexadecimal
    )
    assert_encode(
      '&quot;bient&ocirc;t&quot; &amp; &#25991;&#23383;',
      '"bientôt" & 文字', :basic, :named, :decimal
    )
  end

  def test_should_sort_commands_when_encoding_using_mix_of_entities
    assert_encode(
      '&quot;bient&ocirc;t&quot; &amp; &#x6587;&#x5b57;',
      '"bientôt" & 文字', :named, :hexadecimal, :basic
    )
    assert_encode(
      '&quot;bient&ocirc;t&quot; &amp; &#25991;&#23383;',
      '"bientôt" & 文字', :decimal, :named, :basic
    )
  end

  def test_should_detect_illegal_encoding_command
    assert_raise HTMLEntities::InstructionError do
      HTMLEntities.new.encode('foo', :bar, :baz)
    end
  end

  def test_should_not_encode_normal_ASCII
    assert_encode '`', '`'
    assert_encode ' ', ' '
  end

  def test_should_double_encode_existing_entity
    assert_encode '&amp;amp;', '&amp;'
  end

  def test_should_not_mutate_string_being_encoded
    original = "<£"
    input = original.dup
    HTMLEntities.new.encode(input, :basic, :decimal)

    assert_equal original, input
  end

  def test_should_ducktype_parameter_to_string_before_encoding
    obj = Object.new
    def obj.to_s; "foo"; end
    assert_encode "foo", obj
  end
end