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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
# -*- coding: utf-8 -*-
# frozen_string_literal: true
require "helper"
module Nokogiri
module XML
module SAX
class TestParserContext < Nokogiri::SAX::TestCase
def setup
super
@xml = <<~EOF
<hello>
world
<inter>
<net>
</net>
</inter>
</hello>
EOF
end
class Counter < Nokogiri::XML::SAX::Document
attr_accessor :context, :lines, :columns
def initialize
super
@context = nil
@lines = []
@columns = []
end
def start_element(name, attrs = [])
@lines << [name, context.line]
@columns << [name, context.column]
end
end
def test_line_numbers
sax_handler = Counter.new
parser = Nokogiri::XML::SAX::Parser.new(sax_handler)
parser.parse(@xml) do |ctx|
sax_handler.context = ctx
end
assert_equal([["hello", 1], ["inter", 4], ["net", 5]],
sax_handler.lines)
end
def test_column_numbers
sax_handler = Counter.new
parser = Nokogiri::XML::SAX::Parser.new(sax_handler)
parser.parse(@xml) do |ctx|
sax_handler.context = ctx
end
assert_equal([["hello", 7], ["inter", 7], ["net", 9]],
sax_handler.columns)
end
def test_replace_entities
pc = ParserContext.new(StringIO.new("<root />"), "UTF-8")
pc.replace_entities = false
refute(pc.replace_entities)
pc.replace_entities = true
assert(pc.replace_entities)
end
def test_recovery
pc = ParserContext.new(StringIO.new("<root />"), "UTF-8")
pc.recovery = false
refute(pc.recovery)
pc.recovery = true
assert(pc.recovery)
end
def test_graceful_handling_of_invalid_types
assert_raises(TypeError) { ParserContext.new(0xcafecafe) }
assert_raises(TypeError) { ParserContext.memory(0xcafecafe) }
assert_raises(TypeError) { ParserContext.io(0xcafecafe, 1) }
assert_raises(TypeError) { ParserContext.io(StringIO.new("asdf"), "should be an index into ENCODINGS") }
end
def test_from_io
ctx = ParserContext.new(StringIO.new("fo"), "UTF-8")
assert(ctx)
end
def test_from_string
assert(ParserContext.new("blah blah"))
end
def test_parse_with
ctx = ParserContext.new("blah")
assert_raises(ArgumentError) do
ctx.parse_with(nil)
end
end
def test_parse_with_sax_parser
xml = "<root />"
ctx = ParserContext.new(xml)
parser = Parser.new(Doc.new)
assert_nil(ctx.parse_with(parser))
end
def test_from_file
ctx = ParserContext.file(XML_FILE)
parser = Parser.new(Doc.new)
assert_nil(ctx.parse_with(parser))
end
def test_parse_with_returns_nil
xml = "<root />"
ctx = ParserContext.new(xml)
parser = Parser.new(Doc.new)
assert_nil(ctx.parse_with(parser))
end
end
end
end
end
|