File: test_document_error.rb

package info (click to toggle)
ruby-nokogiri 1.13.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,416 kB
  • sloc: ansic: 38,198; xml: 28,086; ruby: 22,271; java: 15,517; cpp: 7,037; yacc: 244; sh: 148; makefile: 136
file content (45 lines) | stat: -rw-r--r-- 1,286 bytes parent folder | download
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
# frozen_string_literal: true

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 test_error_throwing_document_raises_exception
          error_parser = Parser.new(ThrowingErrorDocument.new)
          e = assert_raises(StandardError) do
            error_parser.parse("<xml>") # no closing element
          end
          assert_match(/parsing did not complete/, e.message)
        end

        def test_warning_document_encounters_error_but_terminates_normally
          warning_parser = Parser.new(WarningErrorDocument.new)
          warning_parser.parse("<xml>")
          refute_empty(warning_parser.document.errors, "error collector did not collect an error")
        end
      end
    end
  end
end