Skip to content

Instantly share code, notes, and snippets.

@fabrizioc1
Created February 28, 2011 20:38
Show Gist options
  • Save fabrizioc1/847984 to your computer and use it in GitHub Desktop.
Save fabrizioc1/847984 to your computer and use it in GitHub Desktop.
A test case to check how libxml-ruby handles ruby 1.9 string encodings
# encoding: UTF-8
require 'xml'
require 'test/unit'
class EncodingTest < Test::Unit::TestCase
def setup()
@xml = <<-EOS
<?xml version='1.0' encoding='UTF-8'?>
<test>
<ascii>Hello World!</ascii>
<unicode>Fran\u1e09ais</unicode>
</test>
EOS
xp = XML::Parser.string(@xml, :encoding=>XML::Encoding::UTF_8)
@doc = xp.parse
end
def teardown
@doc = nil
end
def test_document_encoding_is_utf8
assert_equal(@doc.encoding,XML::Encoding::UTF_8,"Document encoding should be UTF_8")
end
def test_input_xml_is_utf8
assert_equal(@xml.encoding.name,'UTF-8',"Input XML string encoding should be UTF-8")
assert(@xml.valid_encoding?,"Input XML string encoding should be valid")
end
def test_parsed_unicode_element_is_utf8
unicode_string = @doc.find('/test/unicode').find.first.content
assert_equal(unicode_string.encoding.name,'UTF-8',"Parsed 'unicode' element content should be UTF-8")
end
end
@fabrizioc1
Copy link
Author

Here is the output when I run the test case:

Loaded suite test/tc_encoding
Started
..F
Finished in 0.001214 seconds.

  1) Failure:
test_parsed_unicode_element_is_utf8(EncodingTest) [test/tc_encoding.rb:34]:
Parsed 'unicode' element content should be UTF-8.
<"ASCII-8BIT"> expected but was
<"UTF-8">.

3 tests, 4 assertions, 1 failures, 0 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment