1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
require "helper"
module Nokogiri
module XML
module SAX
# raises an exception when underlying parser
# encounters an XML parsing error
class ThrowingErrorDocument < Document
def error(msg)
raise(StandardError, "parsing did not complete: #{msg}")
end
end
# only warns when underlying parser encounters
# an XML parsing error
class WarningErrorDocument < Document
def error(msg)
errors << msg
end
def errors
@errors ||= []
end
end
class TestErrorHandling < Nokogiri::SAX::TestCase
def setup
super
@error_parser = Parser.new(ThrowingErrorDocument.new)
@warning_parser = Parser.new(WarningErrorDocument.new)
end
def test_error_throwing_document_raises_exception
begin
@error_parser.parse("<xml>") # no closing element
fail "#parse should not complete successfully when document #error throws exception"
rescue StandardError => e
assert_match(/parsing did not complete/, e.message)
end
end
def test_warning_document_encounters_error_but_terminates_normally
begin
@warning_parser.parse("<xml>")
assert !@warning_parser.document.errors.empty?, "error collector did not collect an error"
rescue StandardError => e
warn(e)
fail '#parse should complete successfully unless document #error throws exception (#{e}'
end
end
end
end
end
end
|