# 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 '&', '&', :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_encode_extended_named_entities assert_encode '±', '±', :named assert_encode 'ð', 'ð', :named assert_encode 'Œ', 'Œ', :named assert_encode 'œ', 'œ', :named end def test_should_encode_decimal_entities assert_encode '“', '“', :decimal assert_encode '…', '…', :decimal end def test_should_encode_hexadecimal_entities assert_encode '−', '−', :hexadecimal assert_encode '—', '—', :hexadecimal 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 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;', '&' 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