File: test_document_error.rb

package info (click to toggle)
ruby-nokogiri 1.11.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,576 kB
  • sloc: xml: 28,086; ruby: 18,456; java: 13,067; ansic: 5,138; yacc: 265; sh: 208; makefile: 27
file content (55 lines) | stat: -rw-r--r-- 1,605 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
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