diff --git a/test/xml/sax/test_parser.rb b/test/xml/sax/test_parser.rb
index de078d6115..69f16d66d2 100644
--- a/test/xml/sax/test_parser.rb
+++ b/test/xml/sax/test_parser.rb
@@ -2,425 +2,429 @@
require "helper"
-module Nokogiri
- module XML
- module SAX
- class TestParser < Nokogiri::SAX::TestCase
- def setup
- super
- @parser = XML::SAX::Parser.new(Doc.new)
- end
-
- def test_parser_context_yielded_io
- doc = Doc.new
- parser = XML::SAX::Parser.new(doc)
- xml = ""
-
- block_called = false
- parser.parse(StringIO.new(xml)) do |ctx|
- block_called = true
- ctx.replace_entities = true
- end
-
- assert(block_called)
-
- assert_equal([["foo", [["a", "&b"]]]], doc.start_elements)
- end
-
- def test_parser_context_yielded_in_memory
- doc = Doc.new
- parser = XML::SAX::Parser.new(doc)
- xml = ""
-
- block_called = false
- parser.parse(xml) do |ctx|
- block_called = true
- ctx.replace_entities = true
- end
-
- assert(block_called)
-
- assert_equal([["foo", [["a", "&b"]]]], doc.start_elements)
- end
-
- def test_empty_decl
- parser = XML::SAX::Parser.new(Doc.new)
-
- xml = ""
- parser.parse(xml)
- assert(parser.document.start_document_called, xml)
- assert_nil(parser.document.xmldecls, xml)
- end
-
- def test_xml_decl
- [
- ['', ["1.0"]],
- ['', ["1.0", "UTF-8"]],
- ['', ["1.0", "yes"]],
- ['', ["1.0", "no"]],
- ['', ["1.0", "UTF-8", "no"]],
- ['', ["1.0", "ISO-8859-1", "yes"]],
- ].each do |decl, value|
- parser = XML::SAX::Parser.new(Doc.new)
-
- xml = "#{decl}\n"
- parser.parse(xml)
- assert(parser.document.start_document_called, xml)
- assert_equal(value, parser.document.xmldecls, xml)
- end
- end
-
- def test_parse_empty
- assert_raises(RuntimeError) do
- @parser.parse("")
- end
- end
-
- def test_namespace_declaration_order_is_saved
- @parser.parse(<<~eoxml)
-
-
-
- eoxml
- assert_equal(2, @parser.document.start_elements_namespace.length)
- el = @parser.document.start_elements_namespace.first
- namespaces = el.last
- assert_equal(["foo", "http://foo.example.com/"], namespaces.first)
- assert_equal([nil, "http://example.com/"], namespaces.last)
- end
-
- def test_bad_document_calls_error_handler
- @parser.parse("")
- assert(@parser.document.errors)
- assert(@parser.document.errors.length > 0)
- end
-
- def test_namespace_are_super_fun_to_parse
- @parser.parse(<<~eoxml)
-
-
-
-
-
- hello world
-
- eoxml
-
- assert(@parser.document.start_elements_namespace.length > 0)
- el = @parser.document.start_elements_namespace[1]
- assert_equal("a", el.first)
- assert_equal(1, el[1].length)
-
- attribute = el[1].first
- assert_equal("bar", attribute.localname)
- assert_equal("foo", attribute.prefix)
- assert_equal("hello", attribute.value)
- assert_equal("http://foo.example.com/", attribute.uri)
- end
-
- def test_sax_v1_namespace_attribute_declarations
- @parser.parse(<<~eoxml)
-
-
-
-
-
- hello world
-
- eoxml
- assert(@parser.document.start_elements.length > 0)
- elm = @parser.document.start_elements.first
- assert_equal("root", elm.first)
- assert(elm[1].include?(["xmlns:foo", "http://foo.example.com/"]))
- assert(elm[1].include?(["xmlns", "http://example.com/"]))
- end
-
- def test_sax_v1_namespace_nodes
- @parser.parse(<<~eoxml)
-
-
-
-
-
- hello world
-
- eoxml
- assert_equal(5, @parser.document.start_elements.length)
- assert(@parser.document.start_elements.map(&:first).include?("foo:bar"))
- assert(@parser.document.end_elements.map(&:first).include?("foo:bar"))
- end
-
- def test_start_is_called_without_namespace
- @parser.parse(<<~eoxml)
-
-
-
- eoxml
- assert_equal(["root", "foo:f", "bar"],
- @parser.document.start_elements.map(&:first))
- end
-
- def test_parser_sets_encoding
- parser = XML::SAX::Parser.new(Doc.new, "UTF-8")
- assert_equal("UTF-8", parser.encoding)
- end
-
- def test_errors_set_after_parsing_bad_dom
- doc = Nokogiri::XML("")
- assert(doc.errors)
-
- @parser.parse("")
- assert(@parser.document.errors)
- assert(@parser.document.errors.length > 0)
-
- doc.errors.each do |error|
- assert_equal("UTF-8", error.message.encoding.name)
- end
-
- # when using JRuby Nokogiri, more errors will be generated as the DOM
- # parser continue to parse an ill formed document, while the sax parser
- # will stop at the first error
- unless Nokogiri.jruby?
- assert_equal(doc.errors.length, @parser.document.errors.length)
- end
- end
-
- def test_parse_with_memory_argument
- @parser.parse(File.read(XML_FILE))
- assert(@parser.document.cdata_blocks.length > 0)
- end
-
- def test_parse_with_io_argument
- File.open(XML_FILE, "rb") do |f|
- @parser.parse(f)
- end
- assert(@parser.document.cdata_blocks.length > 0)
- end
-
- def test_parse_io
- call_parse_io_with_encoding("UTF-8")
- end
-
- # issue #828
- def test_parse_io_lower_case_encoding
- call_parse_io_with_encoding("utf-8")
- end
-
- def call_parse_io_with_encoding(encoding)
- File.open(XML_FILE, "rb") do |f|
- @parser.parse_io(f, encoding)
- end
- assert(@parser.document.cdata_blocks.length > 0)
-
- called = false
- @parser.document.start_elements.flatten.each do |thing|
- assert_equal("UTF-8", thing.encoding.name)
- called = true
- end
- assert(called)
-
- called = false
- @parser.document.end_elements.flatten.each do |thing|
- assert_equal("UTF-8", thing.encoding.name)
- called = true
- end
- assert(called)
-
- called = false
- @parser.document.data.each do |thing|
- assert_equal("UTF-8", thing.encoding.name)
- called = true
- end
- assert(called)
-
- called = false
- @parser.document.comments.flatten.each do |thing|
- assert_equal("UTF-8", thing.encoding.name)
- called = true
- end
- assert(called)
-
- called = false
- @parser.document.cdata_blocks.flatten.each do |thing|
- assert_equal("UTF-8", thing.encoding.name)
- called = true
- end
- assert(called)
- end
-
- def test_parse_file
- @parser.parse_file(XML_FILE)
-
- assert_raises(ArgumentError) do
- @parser.parse_file(nil)
- end
-
- assert_raises(Errno::ENOENT) do
- @parser.parse_file("")
- end
- assert_raises(Errno::EISDIR) do
- @parser.parse_file(File.expand_path(File.dirname(__FILE__)))
- end
- end
-
- def test_render_parse_nil_param
- assert_raises(ArgumentError) { @parser.parse_memory(nil) }
- end
-
- def test_bad_encoding_args
- assert_raises(ArgumentError) { XML::SAX::Parser.new(Doc.new, "not an encoding") }
- assert_raises(ArgumentError) { @parser.parse_io(StringIO.new(""), "not an encoding") }
- end
-
- def test_ctag
- @parser.parse_memory(<<~eoxml)
-
-
- Paragraph 1
-
- eoxml
- assert_equal([" This is a comment "], @parser.document.cdata_blocks)
- end
-
- def test_comment
- @parser.parse_memory(<<~eoxml)
-
-
- Paragraph 1
-
- eoxml
- assert_equal([" This is a comment "], @parser.document.comments)
- end
-
- def test_characters
- @parser.parse_memory(<<~eoxml)
- Paragraph 1
- eoxml
- assert_equal(["Paragraph 1"], @parser.document.data)
- end
-
- def test_end_document
- @parser.parse_memory(<<~eoxml)
- Paragraph 1
- eoxml
- assert(@parser.document.end_document_called)
- end
-
- def test_end_element
- @parser.parse_memory(<<~eoxml)
- Paragraph 1
- eoxml
- assert_equal([["p"]], @parser.document.end_elements)
- end
-
- def test_start_element_attrs
- @parser.parse_memory(<<~eoxml)
- Paragraph 1
- eoxml
- assert_equal([["p", [["id", "asdfasdf"]]]], @parser.document.start_elements)
- end
-
- def test_start_element_attrs_include_namespaces
- @parser.parse_memory(<<~eoxml)
- Paragraph 1
- eoxml
- assert_equal([["p", [["xmlns:foo", "http://foo.example.com/"]]]],
- @parser.document.start_elements)
- end
-
- def test_processing_instruction
- @parser.parse_memory(<<~eoxml)
-
-
- eoxml
- assert_equal([["xml-stylesheet", 'href="a.xsl" type="text/xsl"']],
- @parser.document.processing_instructions)
- end
-
- def test_parse_document
- skip_unless_libxml2("JRuby SAXParser only parses well-formed XML documents")
- @parser.parse_memory(<<~eoxml)
- Paragraph 1
- Paragraph 2
- eoxml
- end
-
- def test_parser_attributes
- xml = <<~eoxml
-
- eoxml
-
- block_called = false
- @parser.parse(xml) do |ctx|
- block_called = true
- ctx.replace_entities = true
- end
-
- assert(block_called)
-
- assert_equal([["root", []], ["foo", [["a", "&b"], ["c", ">d"]]]], @parser.document.start_elements)
- end
-
- def test_recovery_from_incorrect_xml
- xml = <<~eoxml
- - hey
- hey yourself
- eoxml
-
- block_called = false
- @parser.parse(xml) do |ctx|
- block_called = true
- ctx.recovery = true
- end
-
- assert(block_called)
-
- assert_equal([["Root", []], ["Data", []], ["Item", []], ["Data", []], ["Item", []]],
- @parser.document.start_elements)
- end
-
- def test_square_bracket_in_text # issue 1261
- xml = <<~eoxml
-
- en:#:home_page:#:stories:#:[6]:#:name
- Sandy S.
-
- eoxml
- @parser.parse(xml)
- assert_includes(@parser.document.data, "en:#:home_page:#:stories:#:[6]:#:name")
- end
-
- def test_large_cdata_is_handled
- # see #2132 and https://gitlab.gnome.org/GNOME/libxml2/-/issues/200
- skip("Upstream libxml2 <= 2.9.10 needs to be patched") if Nokogiri::VersionInfo.instance.libxml2_using_system?
-
- template = <<~EOF
-
-
-
-
- gorilla
- secret
-
-
-
-
-
-
-
-
- EOF
-
- factor = 10
- huge_data = "a" * (1024 * 1024 * factor)
- xml = StringIO.new(template % (huge_data))
-
- handler = Nokogiri::SAX::TestCase::Doc.new
- parser = Nokogiri::XML::SAX::Parser.new(handler)
- parser.parse(xml)
-
- assert_predicate(handler.errors, :empty?)
- end
+class Nokogiri::SAX::TestCase
+ describe Nokogiri::XML::SAX::Parser do
+ let(:parser) { Nokogiri::XML::SAX::Parser.new(Doc.new) }
+
+ it :test_parser_context_yielded_io do
+ doc = Doc.new
+ parser = Nokogiri::XML::SAX::Parser.new(doc)
+ xml = ""
+
+ block_called = false
+ parser.parse(StringIO.new(xml)) do |ctx|
+ block_called = true
+ ctx.replace_entities = true
end
+
+ assert(block_called)
+
+ assert_equal([["foo", [["a", "&b"]]]], doc.start_elements)
+ end
+
+ it :test_parser_context_yielded_in_memory do
+ doc = Doc.new
+ parser = Nokogiri::XML::SAX::Parser.new(doc)
+ xml = ""
+
+ block_called = false
+ parser.parse(xml) do |ctx|
+ block_called = true
+ ctx.replace_entities = true
+ end
+
+ assert(block_called)
+
+ assert_equal([["foo", [["a", "&b"]]]], doc.start_elements)
+ end
+
+ it :test_empty_decl do
+ parser = Nokogiri::XML::SAX::Parser.new(Doc.new)
+
+ xml = ""
+ parser.parse(xml)
+ assert(parser.document.start_document_called, xml)
+ assert_nil(parser.document.xmldecls, xml)
+ end
+
+ it :test_xml_decl do
+ [
+ ['', ["1.0"]],
+ ['', ["1.0", "UTF-8"]],
+ ['', ["1.0", "yes"]],
+ ['', ["1.0", "no"]],
+ ['', ["1.0", "UTF-8", "no"]],
+ ['', ["1.0", "ISO-8859-1", "yes"]],
+ ].each do |decl, value|
+ parser = Nokogiri::XML::SAX::Parser.new(Doc.new)
+
+ xml = "#{decl}\n"
+ parser.parse(xml)
+ assert(parser.document.start_document_called, xml)
+ assert_equal(value, parser.document.xmldecls, xml)
+ end
+ end
+
+ it :test_parse_empty do
+ assert_raises(RuntimeError) do
+ parser.parse("")
+ end
+ end
+
+ it :test_namespace_declaration_order_is_saved do
+ parser.parse(<<~EOF)
+
+
+
+ EOF
+ assert_equal(2, parser.document.start_elements_namespace.length)
+ el = parser.document.start_elements_namespace.first
+ namespaces = el.last
+ assert_equal(["foo", "http://foo.example.com/"], namespaces.first)
+ assert_equal([nil, "http://example.com/"], namespaces.last)
+ end
+
+ it :test_bad_document_calls_error_handler do
+ parser.parse("")
+ assert(parser.document.errors)
+ assert(parser.document.errors.length > 0)
+ end
+
+ it :test_namespace_are_super_fun_to_parse do
+ parser.parse(<<~EOF)
+
+
+
+
+
+ hello world
+
+ EOF
+
+ assert(parser.document.start_elements_namespace.length > 0)
+ el = parser.document.start_elements_namespace[1]
+ assert_equal("a", el.first)
+ assert_equal(1, el[1].length)
+
+ attribute = el[1].first
+ assert_equal("bar", attribute.localname)
+ assert_equal("foo", attribute.prefix)
+ assert_equal("hello", attribute.value)
+ assert_equal("http://foo.example.com/", attribute.uri)
+ end
+
+ it :test_sax_v1_namespace_attribute_declarations do
+ parser.parse(<<~EOF)
+
+
+
+
+
+ hello world
+
+ EOF
+ assert(parser.document.start_elements.length > 0)
+ elm = parser.document.start_elements.first
+ assert_equal("root", elm.first)
+ assert(elm[1].include?(["xmlns:foo", "http://foo.example.com/"]))
+ assert(elm[1].include?(["xmlns", "http://example.com/"]))
+ end
+
+ it :test_sax_v1_namespace_nodes do
+ parser.parse(<<~EOF)
+
+
+
+
+
+ hello world
+
+ EOF
+ assert_equal(5, parser.document.start_elements.length)
+ assert(parser.document.start_elements.map(&:first).include?("foo:bar"))
+ assert(parser.document.end_elements.map(&:first).include?("foo:bar"))
+ end
+
+ it :test_start_is_called_without_namespace do
+ parser.parse(<<~EOF)
+
+
+
+ EOF
+ assert_equal(
+ ["root", "foo:f", "bar"],
+ parser.document.start_elements.map(&:first)
+ )
+ end
+
+ it :test_parser_sets_encoding do
+ parser = Nokogiri::XML::SAX::Parser.new(Doc.new, "UTF-8")
+ assert_equal("UTF-8", parser.encoding)
+ end
+
+ it :test_errors_set_after_parsing_bad_dom do
+ doc = Nokogiri::XML("")
+ assert(doc.errors)
+
+ parser.parse("")
+ assert(parser.document.errors)
+ assert(parser.document.errors.length > 0)
+
+ doc.errors.each do |error|
+ assert_equal("UTF-8", error.message.encoding.name)
+ end
+
+ # when using JRuby Nokogiri, more errors will be generated as the DOM
+ # parser continue to parse an ill formed document, while the sax parser
+ # will stop at the first error
+ unless Nokogiri.jruby?
+ assert_equal(doc.errors.length, parser.document.errors.length)
+ end
+ end
+
+ it :test_parse_with_memory_argument do
+ parser.parse(File.read(XML_FILE))
+ assert(parser.document.cdata_blocks.length > 0)
+ end
+
+ it :test_parse_with_io_argument do
+ File.open(XML_FILE, "rb") do |f|
+ parser.parse(f)
+ end
+ assert(parser.document.cdata_blocks.length > 0)
+ end
+
+ it :test_parse_io do
+ call_parse_io_with_encoding("UTF-8")
+ end
+
+ # issue #828
+ it :test_parse_io_lower_case_encoding do
+ call_parse_io_with_encoding("utf-8")
+ end
+
+ def call_parse_io_with_encoding(encoding)
+ File.open(XML_FILE, "rb") do |f|
+ parser.parse_io(f, encoding)
+ end
+ assert(parser.document.cdata_blocks.length > 0)
+
+ called = false
+ parser.document.start_elements.flatten.each do |thing|
+ assert_equal("UTF-8", thing.encoding.name)
+ called = true
+ end
+ assert(called)
+
+ called = false
+ parser.document.end_elements.flatten.each do |thing|
+ assert_equal("UTF-8", thing.encoding.name)
+ called = true
+ end
+ assert(called)
+
+ called = false
+ parser.document.data.each do |thing|
+ assert_equal("UTF-8", thing.encoding.name)
+ called = true
+ end
+ assert(called)
+
+ called = false
+ parser.document.comments.flatten.each do |thing|
+ assert_equal("UTF-8", thing.encoding.name)
+ called = true
+ end
+ assert(called)
+
+ called = false
+ parser.document.cdata_blocks.flatten.each do |thing|
+ assert_equal("UTF-8", thing.encoding.name)
+ called = true
+ end
+ assert(called)
+ end
+
+ it :test_parse_file do
+ parser.parse_file(XML_FILE)
+
+ assert_raises(ArgumentError) do
+ parser.parse_file(nil)
+ end
+
+ assert_raises(Errno::ENOENT) do
+ parser.parse_file("")
+ end
+ assert_raises(Errno::EISDIR) do
+ parser.parse_file(File.expand_path(File.dirname(__FILE__)))
+ end
+ end
+
+ it :test_render_parse_nil_param do
+ assert_raises(ArgumentError) { parser.parse_memory(nil) }
+ end
+
+ it :test_bad_encoding_args do
+ assert_raises(ArgumentError) { Nokogiri::XML::SAX::Parser.new(Doc.new, "not an encoding") }
+ assert_raises(ArgumentError) { parser.parse_io(StringIO.new(""), "not an encoding") }
+ end
+
+ it :test_ctag do
+ parser.parse_memory(<<~EOF)
+
+
+ Paragraph 1
+
+ EOF
+ assert_equal([" This is a comment "], parser.document.cdata_blocks)
+ end
+
+ it :test_comment do
+ parser.parse_memory(<<~EOF)
+
+
+ Paragraph 1
+
+ EOF
+ assert_equal([" This is a comment "], parser.document.comments)
+ end
+
+ it :test_characters do
+ parser.parse_memory(<<~EOF)
+ Paragraph 1
+ EOF
+ assert_equal(["Paragraph 1"], parser.document.data)
+ end
+
+ it :test_end_document do
+ parser.parse_memory(<<~EOF)
+ Paragraph 1
+ EOF
+ assert(parser.document.end_document_called)
+ end
+
+ it :test_end_element do
+ parser.parse_memory(<<~EOF)
+ Paragraph 1
+ EOF
+ assert_equal([["p"]], parser.document.end_elements)
+ end
+
+ it :test_start_element_attrs do
+ parser.parse_memory(<<~EOF)
+ Paragraph 1
+ EOF
+ assert_equal([["p", [["id", "asdfasdf"]]]], parser.document.start_elements)
+ end
+
+ it :test_start_element_attrs_include_namespaces do
+ parser.parse_memory(<<~EOF)
+ Paragraph 1
+ EOF
+ assert_equal(
+ [["p", [["xmlns:foo", "http://foo.example.com/"]]]],
+ parser.document.start_elements
+ )
+ end
+
+ it :test_processing_instruction do
+ parser.parse_memory(<<~EOF)
+
+
+ EOF
+ assert_equal(
+ [["xml-stylesheet", 'href="a.xsl" type="text/xsl"']],
+ parser.document.processing_instructions
+ )
+ end
+
+ it :test_parse_document do
+ skip_unless_libxml2("JRuby SAXParser only parses well-formed XML documents")
+ parser.parse_memory(<<~EOF)
+ Paragraph 1
+ Paragraph 2
+ EOF
+ end
+
+ it :test_parser_attributes do
+ xml = <<~EOF
+
+ EOF
+
+ block_called = false
+ parser.parse(xml) do |ctx|
+ block_called = true
+ ctx.replace_entities = true
+ end
+
+ assert(block_called)
+
+ assert_equal(
+ [["root", []], ["foo", [["a", "&b"], ["c", ">d"]]]], parser.document.start_elements
+ )
+ end
+
+ it :test_recovery_from_incorrect_xml do
+ xml = <<~EOF
+ - hey
- hey yourself
+ EOF
+
+ block_called = false
+ parser.parse(xml) do |ctx|
+ block_called = true
+ ctx.recovery = true
+ end
+
+ assert(block_called)
+
+ assert_equal(
+ [["Root", []], ["Data", []], ["Item", []], ["Data", []], ["Item", []]],
+ parser.document.start_elements
+ )
+ end
+
+ it :test_square_bracket_in_text do
+ # issue 1261
+ xml = <<~EOF
+
+ en:#:home_page:#:stories:#:[6]:#:name
+ Sandy S.
+
+ EOF
+ parser.parse(xml)
+ assert_includes(parser.document.data, "en:#:home_page:#:stories:#:[6]:#:name")
+ end
+
+ it :test_large_cdata_is_handled do
+ # see #2132 and https://gitlab.gnome.org/GNOME/libxml2/-/issues/200
+ skip("Upstream libxml2 <= 2.9.10 needs to be patched") if Nokogiri::VersionInfo.instance.libxml2_using_system?
+
+ template = <<~EOF
+
+
+
+
+ gorilla
+ secret
+
+
+
+
+
+
+
+
+ EOF
+
+ factor = 10
+ huge_data = "a" * (1024 * 1024 * factor)
+ xml = StringIO.new(template % huge_data)
+
+ handler = Nokogiri::SAX::TestCase::Doc.new
+ parser = Nokogiri::XML::SAX::Parser.new(handler)
+ parser.parse(xml)
+
+ assert_predicate(handler.errors, :empty?)
end
end
end
diff --git a/test/xml/sax/test_push_parser.rb b/test/xml/sax/test_push_parser.rb
index 587bd54a1a..dfab1793fa 100644
--- a/test/xml/sax/test_push_parser.rb
+++ b/test/xml/sax/test_push_parser.rb
@@ -1,266 +1,243 @@
# -*- coding: utf-8 -*-
+# frozen_string_literal: true
require "helper"
-module Nokogiri
- module XML
- module SAX
- class TestPushParser < Nokogiri::SAX::TestCase
- def setup
- super
- @parser = XML::SAX::PushParser.new(Doc.new)
- end
-
- def test_exception
- assert_raises(SyntaxError) do
- @parser << ""
- end
-
- assert_raises(SyntaxError) do
- @parser << nil
- end
- end
+class Nokogiri::SAX::TestCase
+ describe Nokogiri::XML::SAX::PushParser do
+ let(:parser) { Nokogiri::XML::SAX::PushParser.new(Doc.new) }
- def test_early_finish
- @parser << ""
- assert_raises(SyntaxError) do
- @parser.finish
- end
- end
+ it :test_exception do
+ assert_raises(Nokogiri::XML::SyntaxError) do
+ parser << ""
+ end
- def test_write_last_chunk
- @parser << ""
- @parser.write "", true
- assert_equal [["foo", []]], @parser.document.start_elements
- assert_equal [["foo"]], @parser.document.end_elements
- end
+ assert_raises(Nokogiri::XML::SyntaxError) do
+ parser << nil
+ end
+ end
- def test_empty_doc
- @parser.options |= XML::ParseOptions::RECOVER
- @parser.write "", true
- assert_nil @parser.document.start_elements
- assert_nil @parser.document.end_elements
- end
+ it :test_early_finish do
+ parser << ""
+ assert_raises(Nokogiri::XML::SyntaxError) do
+ parser.finish
+ end
+ end
+ it :test_write_last_chunk do
+ parser << ""
+ parser.write("", true)
+ assert_equal [["foo", []]], parser.document.start_elements
+ assert_equal [["foo"]], parser.document.end_elements
+ end
- def test_finish_should_rethrow_last_error
- begin
- @parser << ""
- rescue => e
- expected = e
- end
+ it :test_empty_doc do
+ parser.options |= Nokogiri::XML::ParseOptions::RECOVER
+ parser.write("", true)
+ assert_nil parser.document.start_elements
+ assert_nil parser.document.end_elements
+ end
- begin
- @parser.finish
- rescue => e
- actual = e
- end
+ it :test_finish_should_rethrow_last_error do
+ expected = assert_raise(Nokogiri::XML::SyntaxError) { parser << "" }
+ actual = assert_raise(Nokogiri::XML::SyntaxError) { parser.finish }
+ assert_equal actual.message, expected.message
+ end
- assert_equal actual.message, expected.message
+ it :test_should_throw_error_returned_by_document do
+ doc = Doc.new
+ class << doc
+ def error(msg)
+ raise "parse error"
end
+ end
+ parser = Nokogiri::XML::SAX::PushParser.new(doc)
- def test_should_throw_error_returned_by_document
- doc = Doc.new
- class << doc
- def error msg
- raise "parse error"
- end
- end
-
- @parser = XML::SAX::PushParser.new(doc)
- begin
- @parser << ""
- rescue => e
- actual = e
- end
- fail 'PushParser should throw error when fed ill-formed data' if actual.nil?
-
- assert_equal actual.message, "parse error"
- end
+ exception = assert_raise(RuntimeError) { parser << "" }
+ assert_equal exception.message, "parse error"
+ end
- def test_writing_nil
- assert_equal @parser.write(nil), @parser
- end
+ it :test_writing_nil do
+ assert_equal parser.write(nil), parser
+ end
- def test_end_document_called
- @parser.<<(<<-eoxml)
-
-
- Paragraph 1
-
- eoxml
- assert ! @parser.document.end_document_called
- @parser.finish
- assert @parser.document.end_document_called
- end
+ it :test_end_document_called do
+ parser.<<(<<~EOF)
+
+
+ Paragraph 1
+
+ EOF
+ assert !parser.document.end_document_called
+ parser.finish
+ assert parser.document.end_document_called
+ end
- def test_start_element
- @parser.<<(<<-eoxml)
-
- eoxml
-
- assert_equal [["p", [["id", "asdfasdf"]]]],
- @parser.document.start_elements
-
- @parser.<<(<<-eoxml)
-
- Paragraph 1
-
- eoxml
- assert_equal [' This is a comment '], @parser.document.comments
- @parser.finish
- end
+ it :test_start_element do
+ parser.<<(<<~EOF)
+
+ EOF
+
+ assert_equal [["p", [["id", "asdfasdf"]]]],
+ parser.document.start_elements
+
+ parser.<<(<<~EOF)
+
+ Paragraph 1
+
+ EOF
+ assert_equal [" This is a comment "], parser.document.comments
+ parser.finish
+ end
- def test_start_element_with_namespaces
- @parser.<<(<<-eoxml)
-
- eoxml
-
- assert_equal [["p", [["xmlns:foo", "http://foo.example.com/"]]]],
- @parser.document.start_elements
-
- @parser.<<(<<-eoxml)
-
- Paragraph 1
-
- eoxml
- assert_equal [' This is a comment '], @parser.document.comments
- @parser.finish
- end
+ it :test_start_element_with_namespaces do
+ parser.<<(<<~EOF)
+
+ EOF
+
+ assert_equal [["p", [["xmlns:foo", "http://foo.example.com/"]]]],
+ parser.document.start_elements
+
+ parser.<<(<<~EOF)
+
+ Paragraph 1
+
+ EOF
+ assert_equal [" This is a comment "], parser.document.comments
+ parser.finish
+ end
- def test_start_element_ns
- @parser.<<(<<-eoxml)
-
- eoxml
+ it :test_start_element_ns do
+ parser.<<(<<~EOF)
+
+ EOF
- assert_equal 1, @parser.document.start_elements_namespace.length
- el = @parser.document.start_elements_namespace.first
+ assert_equal 1, parser.document.start_elements_namespace.length
+ el = parser.document.start_elements_namespace.first
- assert_equal 'stream', el.first
- assert_equal 2, el[1].length
- assert_equal [['version', '1.0'], ['size', 'large']],
- el[1].map { |x| [x.localname, x.value] }
+ assert_equal "stream", el.first
+ assert_equal 2, el[1].length
+ assert_equal [["version", "1.0"], ["size", "large"]],
+ el[1].map { |x| [x.localname, x.value] }
- assert_equal 'stream', el[2]
- assert_equal 'http://etherx.jabber.org/streams', el[3]
- @parser.finish
- end
+ assert_equal "stream", el[2]
+ assert_equal "http://etherx.jabber.org/streams", el[3]
+ parser.finish
+ end
- def test_end_element_ns
- @parser.<<(<<-eoxml)
-
- eoxml
+ it :test_end_element_ns do
+ parser.<<(<<~EOF)
+
+ EOF
- assert_equal [['stream', 'stream', 'http://etherx.jabber.org/streams']],
- @parser.document.end_elements_namespace
- @parser.finish
- end
+ assert_equal [["stream", "stream", "http://etherx.jabber.org/streams"]],
+ parser.document.end_elements_namespace
+ parser.finish
+ end
- def test_chevron_partial_xml
- @parser.<<(<<-eoxml)
-
- eoxml
-
- @parser.<<(<<-eoxml)
-
- Paragraph 1
-
- eoxml
- assert_equal [' This is a comment '], @parser.document.comments
- @parser.finish
- end
+ it :test_chevron_partial_xml do
+ parser.<<(<<~EOF)
+
+ EOF
+
+ parser.<<(<<~EOF)
+
+ Paragraph 1
+
+ EOF
+ assert_equal [" This is a comment "], parser.document.comments
+ parser.finish
+ end
- def test_chevron
- @parser.<<(<<-eoxml)
-
-
- Paragraph 1
-
- eoxml
- @parser.finish
- assert_equal [' This is a comment '], @parser.document.comments
- end
+ it :test_chevron do
+ parser.<<(<<~EOF)
+
+
+ Paragraph 1
+
+ EOF
+ parser.finish
+ assert_equal [" This is a comment "], parser.document.comments
+ end
- def test_default_options
- assert_equal 0, @parser.options
- end
+ it :test_default_options do
+ assert_equal 0, parser.options
+ end
- def test_recover
- @parser.options |= XML::ParseOptions::RECOVER
- @parser.<<(<<-eoxml)
-
- Foo
-
- Bar
-
- eoxml
- @parser.finish
- assert(@parser.document.errors.size >= 1)
- assert_equal [["p", []], ["bar", []]], @parser.document.start_elements
- assert_equal "FooBar", @parser.document.data.map { |x|
- x.gsub(/\s/, '')
- }.join
- end
+ it :test_recover do
+ parser.options |= Nokogiri::XML::ParseOptions::RECOVER
+ parser.<<(<<~EOF)
+
+ Foo
+
+ Bar
+
+ EOF
+ parser.finish
+ assert(parser.document.errors.size >= 1)
+ assert_equal [["p", []], ["bar", []]], parser.document.start_elements
+ assert_equal "FooBar", parser.document.data.map { |x|
+ x.gsub(/\s/, "")
+ }.join
+ end
- def test_broken_encoding
- skip_unless_libxml2("ultra hard to fix for pure Java version")
- @parser.options |= XML::ParseOptions::RECOVER
- # This is ISO_8859-1:
- @parser.<< "Gau\337"
- @parser.finish
- assert(@parser.document.errors.size >= 1)
- assert_equal "Gau\337", @parser.document.data.join
- assert_equal [["r"]], @parser.document.end_elements
- end
+ it :test_broken_encoding do
+ skip_unless_libxml2("ultra hard to fix for pure Java version")
+ parser.options |= Nokogiri::XML::ParseOptions::RECOVER
+ # This is ISO_8859-1:
+ parser.<< "Gau\337"
+ parser.finish
+ assert(parser.document.errors.size >= 1)
+ assert_equal "Gau\337", parser.document.data.join
+ assert_equal [["r"]], parser.document.end_elements
+ end
- def test_replace_entities_attribute_behavior
- if Nokogiri.uses_libxml?
- # initially false
- assert_equal false, @parser.replace_entities
-
- # can be set to true
- @parser.replace_entities = true
- assert_equal true, @parser.replace_entities
-
- # can be set to false
- @parser.replace_entities = false
- assert_equal false, @parser.replace_entities
- else
- # initially true
- assert_equal true, @parser.replace_entities
-
- # ignore attempts to set to false
- @parser.replace_entities = false # TODO: should we raise an exception here?
- assert_equal true, @parser.replace_entities
- end
- end
+ it :test_replace_entities_attribute_behavior do
+ if Nokogiri.uses_libxml?
+ # initially false
+ assert_equal false, parser.replace_entities
+
+ # can be set to true
+ parser.replace_entities = true
+ assert_equal true, parser.replace_entities
+
+ # can be set to false
+ parser.replace_entities = false
+ assert_equal false, parser.replace_entities
+ else
+ # initially true
+ assert_equal true, parser.replace_entities
+
+ # ignore attempts to set to false
+ parser.replace_entities = false # TODO: should we raise an exception here?
+ assert_equal true, parser.replace_entities
+ end
+ end
- def test_untouched_entities
- skip_unless_libxml2("entities are always replaced in pure Java version")
- @parser.<<(<<-eoxml)
-
-
- Paragraph 1 & 2
-
- eoxml
- @parser.finish
- assert_equal [["p", [["id", "asdf&asdf"]]]], @parser.document.start_elements
- assert_equal "Paragraph 1 & 2", @parser.document.data.join.strip
- end
+ it :test_untouched_entities do
+ skip_unless_libxml2("entities are always replaced in pure Java version")
+ parser.<<(<<~EOF)
+
+
+ Paragraph 1 & 2
+
+ EOF
+ parser.finish
+ assert_equal [["p", [["id", "asdf&asdf"]]]], parser.document.start_elements
+ assert_equal "Paragraph 1 & 2", parser.document.data.join.strip
+ end
- def test_replaced_entities
- @parser.replace_entities = true
- @parser.<<(<<-eoxml)
-
-
- Paragraph 1 & 2
-
- eoxml
- @parser.finish
- assert_equal [["p", [["id", "asdf&asdf"]]]], @parser.document.start_elements
- assert_equal "Paragraph 1 & 2", @parser.document.data.join.strip
- end
- end
+ it :test_replaced_entities do
+ parser.replace_entities = true
+ parser.<<(<<~EOF)
+
+
+ Paragraph 1 & 2
+
+ EOF
+ parser.finish
+ assert_equal [["p", [["id", "asdf&asdf"]]]], parser.document.start_elements
+ assert_equal "Paragraph 1 & 2", parser.document.data.join.strip
end
end
end