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 '&', '&', :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
|