$:.unshift(File.dirname(__FILE__) + '/../lib') require 'htmlentities' require 'test/unit' $KCODE = 'u' class HTMLEntities::EntitiesTest < Test::Unit::TestCase attr_reader :xhtml1_entities, :html4_entities def setup @xhtml1_entities = HTMLEntities.new('xhtml1') @html4_entities = HTMLEntities.new('html4') end class PseudoString def initialize(string) @string = string end def to_s @string end end def test_should_raise_exception_when_unknown_flavor_specified assert_raises(HTMLEntities::UnknownFlavor) do HTMLEntities.new('foo') end end def test_should_allow_symbol_for_flavor assert_nothing_raised do HTMLEntities.new(:xhtml1) end end def test_should_allow_upper_case_flavor assert_nothing_raised do HTMLEntities.new('XHTML1') end end def test_should_decode_basic_entities assert_decode('&', '&') assert_decode('<', '<') assert_decode('"', '"') end def test_should_encode_basic_entities assert_encode('&', '&', :basic) assert_encode('"', '"') assert_encode('<', '<', :basic) assert_encode('<', '<') end def test_should_encode_basic_entities_to_decimal assert_encode('&', '&', :decimal) assert_encode('"', '"', :decimal) assert_encode('<', '<', :decimal) assert_encode('>', '>', :decimal) assert_encode(''', "'", :decimal) end def test_should_encode_basic_entities_to_hexadecimal assert_encode('&', '&', :hexadecimal) assert_encode('"', '"', :hexadecimal) assert_encode('<', '<', :hexadecimal) assert_encode('>', '>', :hexadecimal) assert_encode(''', "'", :hexadecimal) end def test_should_decode_extended_named_entities assert_decode('±', '±') assert_decode('ð', 'ð') assert_decode('Œ', 'Œ') assert_decode('œ', 'œ') end def test_should_encode_extended_named_entities assert_encode('±', '±', :named) assert_encode('ð', 'ð', :named) assert_encode('Œ', 'Œ', :named) assert_encode('œ', 'œ', :named) end def test_should_decode_decimal_entities assert_decode('“', '“') assert_decode('…', '…') assert_decode(' ', ' ') end def test_should_encode_decimal_entities assert_encode('“', '“', :decimal) assert_encode('…', '…', :decimal) end def test_should_decode_hexadecimal_entities assert_decode('−', '−') assert_decode('—', '—') assert_decode('`', '`') assert_decode('`', '`') end def test_should_encode_hexadecimal_entities assert_encode('−', '−', :hexadecimal) assert_encode('—', '—', :hexadecimal) 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_encode_text_using_mix_of_entities assert_encode( '"bientôt" & 文字', '"bientôt" & 文字', :basic, :named, :hexadecimal ) assert_encode( '"bientôt" & 文字', '"bientôt" & 文字', :basic, :named, :decimal ) end def test_should_sort_commands_when_encoding_using_mix_of_entities assert_encode( '"bientôt" & 文字', '"bientôt" & 文字', :named, :hexadecimal, :basic ) assert_encode( '"bientôt" & 文字', '"bientôt" & 文字', :decimal, :named, :basic ) end def test_should_detect_illegal_encoding_command assert_raise(HTMLEntities::InstructionError) { HTMLEntities.encode_entities('foo', :bar, :baz) } 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 def test_should_not_encode_normal_ASCII assert_encode('`', '`') assert_encode(' ', ' ') end def test_should_double_encode_existing_entity assert_encode('&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 def test_should_ducktype_parameter_to_string_before_encoding pseudo_string = PseudoString.new('foo') assert_decode('foo', pseudo_string) end def test_should_ducktype_parameter_to_string_before_decoding pseudo_string = PseudoString.new('foo') assert_encode('foo', pseudo_string) end def assert_decode(expected, input) [xhtml1_entities, html4_entities].each do |coder| assert_equal(expected, coder.decode(input)) end end def assert_encode(expected, input, *args) [xhtml1_entities, html4_entities].each do |coder| assert_equal(expected, coder.encode(input, *args)) end end end