# encoding: UTF-8 require './test_helper' require 'test/unit' class TestNode < Test::Unit::TestCase def setup load_encoding("utf-8") end def teardown XML.default_keep_blanks = true @doc = nil end def load_encoding(name) @encoding = Encoding.find(name) if defined?(Encoding) @file_name = "model/bands.#{name.downcase}.xml" # Strip spaces to make testing easier XML.default_keep_blanks = false file = File.join(File.dirname(__FILE__), @file_name) @doc = XML::Document.file(file) end def nodes # Find all nodes with a country attributes @doc.find('*[@country]') end def test_doc_class assert_instance_of(XML::Document, @doc) end def test_doc_node_type assert_equal XML::Node::DOCUMENT_NODE, @doc.node_type end def test_root_class assert_instance_of(XML::Node, @doc.root) end def test_root_node_type assert_equal XML::Node::ELEMENT_NODE, @doc.root.node_type end def test_node_class for n in nodes assert_instance_of(XML::Node, n) end end def test_context node = @doc.root context = node.context assert_instance_of(XML::XPath::Context, context) end def test_find assert_instance_of(XML::XPath::Object, self.nodes) end def test_node_child_get assert_instance_of(TrueClass, @doc.root.child?) assert_instance_of(XML::Node, @doc.root.child) if defined?(Encoding) assert_equal(@encoding, @doc.root.child.name.encoding) assert_equal("m\u00F6tley_cr\u00FCe", @doc.root.child.name) else assert_equal("m\303\266tley_cr\303\274e", @doc.root.child.name) end end def test_node_doc for n in nodes assert_instance_of(XML::Document, n.doc) if n.document? end end def test_name if defined?(Encoding) assert_equal(@encoding, nodes[0].name.encoding) assert_equal("m\u00F6tley_cr\u00FCe", nodes[0].name) else assert_equal("m\303\266tley_cr\303\274e", nodes[0].name) end assert_equal("iron_maiden", nodes[1].name) end def test_name_iso_8859_1 load_encoding("iso-8859-1") if defined?(Encoding) assert_equal(@encoding, nodes[0].name.encoding) assert_equal("m\303\266tley_cr\303\274e".force_encoding(@encoding), nodes[0].name) else assert_equal("m\303\266tley_cr\303\274e", nodes[0].name) end assert_equal("iron_maiden", nodes[1].name) end def test_node_find nodes = @doc.root.find('./fixnum') for node in nodes assert_instance_of(XML::Node, node) end end def test_equality node_a = @doc.find_first('*[@country]') node_b = @doc.root.child assert(node_a == node_b) assert(node_a.eql?(node_b)) assert(node_a.equal?(node_b)) file = File.join(File.dirname(__FILE__), @file_name) doc2 = XML::Document.file(file) node_a2 = doc2.find_first('*[@country]') assert_equal(node_a.to_s, node_a2.to_s) assert_equal(node_a, node_a2) assert(node_a.eql?(node_a2)) assert(!node_a.equal?(node_a2)) end def test_equality_nil node = @doc.root assert(node != nil) end def test_equality_wrong_type node = @doc.root assert_raise(TypeError) do assert(node != 'abc') end end def test_content if defined?(Encoding) assert_equal(@encoding, @doc.root.content.encoding) assert_equal("M\u00F6tley Cr\u00FCe is an American heavy metal band formed in Los Angeles, California in 1981.Iron Maiden is a British heavy metal band formed in 1975.", @doc.root.content) first = @doc.root.child assert_equal("M\u00F6tley Cr\u00FCe is an American heavy metal band formed in Los Angeles, California in 1981.", first.content) assert_equal("Iron Maiden is a British heavy metal band formed in 1975.", first.next.content) else assert_equal("M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.Iron Maiden is a British heavy metal band formed in 1975.", @doc.root.content) first = @doc.root.child assert_equal("M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.", first.content) assert_equal("Iron Maiden is a British heavy metal band formed in 1975.", first.next.content) end end def test_content_iso_8859_1 load_encoding("iso-8859-1") if defined?(Encoding) assert_equal(@encoding, @doc.root.content.encoding) assert_equal("M\xC3\xB6tley Cr\xC3\xBCe is an American heavy metal band formed in Los Angeles, California in 1981.Iron Maiden is a British heavy metal band formed in 1975.".force_encoding(@encoding), @doc.root.content) first = @doc.root.child assert_equal("M\xC3\xB6tley Cr\xC3\xBCe is an American heavy metal band formed in Los Angeles, California in 1981.".force_encoding(@encoding), first.content) assert_equal("Iron Maiden is a British heavy metal band formed in 1975.", first.next.content) else assert_equal("M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.Iron Maiden is a British heavy metal band formed in 1975.", @doc.root.content) first = @doc.root.child assert_equal("M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.", first.content) assert_equal("Iron Maiden is a British heavy metal band formed in 1975.", first.next.content) end end def test_base doc = XML::Parser.string('').parse assert_nil(doc.root.base_uri) end # We use the same facility that libXSLT does here to disable output escaping. # This lets you specify that the node's content should be rendered unaltered # whenever it is being output. This is useful for things like