require "helper" module Nokogiri module XML class TestDocumentFragment < Nokogiri::TestCase def setup super @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) end def test_new fragment = Nokogiri::XML::DocumentFragment.new(@xml) end def test_fragment_should_have_document fragment = Nokogiri::XML::DocumentFragment.new(@xml) assert_equal @xml, fragment.document end def test_name fragment = Nokogiri::XML::DocumentFragment.new(@xml) assert_equal '#document-fragment', fragment.name end def test_static_method fragment = Nokogiri::XML::DocumentFragment.parse("
a
") assert_instance_of Nokogiri::XML::DocumentFragment, fragment end def test_static_method_with_namespaces # follows different path in FragmentHandler#start_element which blew up after 597195ff fragment = Nokogiri::XML::DocumentFragment.parse("a") assert_instance_of Nokogiri::XML::DocumentFragment, fragment end def test_many_fragments 100.times { Nokogiri::XML::DocumentFragment.new(@xml) } end def test_subclass klass = Class.new(Nokogiri::XML::DocumentFragment) fragment = klass.new(@xml, "
a
") assert_instance_of klass, fragment end def test_subclass_parse klass = Class.new(Nokogiri::XML::DocumentFragment) doc = klass.parse("
a
") assert_instance_of klass, doc end def test_xml_fragment fragment = Nokogiri::XML.fragment("
a
") assert_equal "
a
", fragment.to_s end def test_xml_fragment_has_multiple_toplevel_children doc = "
b
e
" fragment = Nokogiri::XML::Document.new.fragment(doc) assert_equal "
b
e
", fragment.to_s end def test_xml_fragment_has_outer_text # this test is descriptive, not prescriptive. doc = "a
b
" fragment = Nokogiri::XML::Document.new.fragment(doc) assert_equal "a
b
", fragment.to_s doc = "
b
c" fragment = Nokogiri::XML::Document.new.fragment(doc) assert_equal "
b
c", fragment.to_s end def test_xml_fragment_case_sensitivity doc = "b" fragment = Nokogiri::XML::Document.new.fragment(doc) assert_equal "b", fragment.to_s end def test_xml_fragment_with_leading_whitespace doc = "
b
" fragment = Nokogiri::XML::Document.new.fragment(doc) assert_equal "
b
", fragment.to_s end def test_xml_fragment_with_leading_whitespace_and_newline doc = " \n
b
" fragment = Nokogiri::XML::Document.new.fragment(doc) assert_equal "
b
", fragment.to_s end def test_fragment_children_search fragment = Nokogiri::XML::Document.new.fragment( '

hi

' ) css = fragment.children.css('p') xpath = fragment.children.xpath('.//p') assert_equal css, xpath end def test_fragment_search frag = Nokogiri::XML::Document.new.fragment '

hi

' p_tag = frag.css('#content').first assert_equal 'p', p_tag.name assert_equal p_tag, frag.xpath('./*[@id = \'content\']').first end def test_fragment_without_a_namespace_does_not_get_a_namespace doc = Nokogiri::XML <<-EOX EOX frag = doc.fragment "" assert_nil frag.namespace end def test_fragment_namespace_resolves_against_document_root doc = Nokogiri::XML <<-EOX EOX ns = doc.root.namespace_definitions.detect { |x| x.prefix == "bar" } frag = doc.fragment "" assert frag.children.first.namespace assert_equal ns, frag.children.first.namespace end def test_fragment_invalid_namespace_is_silently_ignored doc = Nokogiri::XML <<-EOX EOX frag = doc.fragment "" assert_nil frag.children.first.namespace end end end end