require "test/unit"
require "HTML/Encoder"
class TestHTML_Encoder < Test::Unit::TestCase
def test_lower
encoder = HTML::Encoder.new()
assert_equal(
'&<>"'',
encoder.encode( '&<>"\'' ),
'default chars encoded correctly'
)
assert_equal(
'&<>"\'',
encoder.encode( '&<>"\'', '' ),
'no encodes when chars is empty'
)
assert_equal(
'hello',
encoder.encode( 'hello', 'e' ),
'requested chars encoded correctly'
)
assert_equal(
'hello',
encoder.encode( 'hello' ),
'no encodes when default chars requested'
)
assert_equal(
'hello',
encoder.encode( 'hello', '' ),
'no chars encoded when no chars requested'
)
deadbeef = 222.chr + 173.chr + 190.chr + 239.chr
assert_equal(
'Þ¾ï',
encoder.encode( deadbeef, deadbeef ),
'hex codes encoded correctly'
)
assert_equal(
deadbeef,
encoder.encode( deadbeef ),
'no encodes when default chars requested'
)
assert_equal(
deadbeef,
encoder.encode( deadbeef, '' ),
'no encodes when no chars requested'
)
end
def test_numeric
encoder = HTML::Encoder.new()
assert_equal(
'&',
encoder.num_entity( '&' ),
'char encoded numerically correctly'
)
assert_equal(
'&<>"'',
encoder.encode_hex( '&<>"\'' ),
'default chars encoded numerically correctly'
)
assert_equal(
'&<>"'',
encoder.encode( '&<>"\'' ),
'does not impact default chars encoded correctly'
)
assert_equal(
'hello',
encoder.encode_hex( 'hello', '' ),
'no chars encoded when no chars requested'
)
assert_equal(
'hello',
encoder.encode_hex( 'hello', 'e' ),
'requested chars encoded correctly'
)
assert_equal(
'hello',
encoder.encode( 'hello', 'e' ),
'requested chars encoded correctly'
)
end
def test_upper
return unless RUBY_VERSION > '1.8.7'
encoder = HTML::Encoder.new()
str = eval( '8224.chr(Encoding::UTF_8)' )
assert_equal(
'†',
encoder.encode( str, str ),
'unicode char encoded correctly'
)
str = eval( '9824.chr(Encoding::UTF_8)+9827.chr(Encoding::UTF_8)+9829.chr(Encoding::UTF_8)+9830.chr(Encoding::UTF_8)' )
assert_equal(
'♠♣♥♦',
encoder.encode( str, str ),
'unicode chars encoded correctly'
)
end
end