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
|
# encoding: UTF-8
require_relative "./test_helper"
class HTMLEntities::DecodingTest < Test::Unit::TestCase
def setup
@entities = [:xhtml1, :html4, :expanded].map{ |a| HTMLEntities.new(a) }
end
def assert_decode(expected, input)
@entities.each do |coder|
assert_equal expected, coder.decode(input)
end
end
def test_should_decode_basic_entities
assert_decode '&', '&'
assert_decode '<', '<'
assert_decode '"', '"'
end
def test_should_decode_extended_named_entities
assert_decode '±', '±'
assert_decode 'ð', 'ð'
assert_decode 'Œ', 'Œ'
assert_decode 'œ', 'œ'
end
def test_should_decode_decimal_entities
assert_decode '“', '“'
assert_decode '…', '…'
assert_decode ' ', ' '
end
def test_should_decode_hexadecimal_entities
assert_decode '−', '−'
assert_decode '—', '—'
assert_decode '`', '`'
assert_decode '`', '`'
end
def test_should_not_mutate_string_being_decoded
original = "<£"
input = original.dup
HTMLEntities.new.decode(input)
assert_equal original, input
end
def test_should_decode_text_with_mix_of_entities
# Just a random headline - I needed something with accented letters.
assert_decode(
'Le tabac pourrait bientôt être banni dans tous les lieux publics en France',
'Le tabac pourrait bientôt être banni dans tous les lieux publics en France'
)
assert_decode(
'"bientôt" & 文字',
'"bientôt" & 文字'
)
end
def test_should_decode_empty_string
assert_decode '', ''
end
def test_should_skip_unknown_entity
assert_decode '&bogus;', '&bogus;'
end
def test_should_decode_double_encoded_entity_once
assert_decode '&', '&amp;'
end
# Faults found and patched by Moonwolf
def test_should_decode_full_hexadecimal_range
(0..127).each do |codepoint|
assert_decode [codepoint].pack('U'), "&\#x#{codepoint.to_s(16)};"
end
end
# Reported by Dallas DeVries and Johan Duflost
def test_should_decode_named_entities_reported_as_missing_in_3_0_1
assert_decode [178].pack('U'), '²'
assert_decode [8226].pack('U'), '•'
assert_decode [948].pack('U'), 'δ'
end
# Reported by ckruse
def test_should_decode_only_first_element_in_masked_entities
input = '&#3346;'
expected = 'ഒ'
assert_decode expected, input
end
def test_should_ducktype_parameter_to_string_before_encoding
obj = Object.new
def obj.to_s; "foo"; end
assert_decode "foo", obj
end
end
|