/
test_parser_context.rb
116 lines (94 loc) · 2.76 KB
/
test_parser_context.rb
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
# -*- coding: utf-8 -*-
require "helper"
module Nokogiri
module XML
module SAX
class TestParserContext < Nokogiri::SAX::TestCase
def setup
super
@xml = '<hello>
world
<inter>
<net>
</net>
</inter>
</hello>'
end
class Counter < Nokogiri::XML::SAX::Document
attr_accessor :context, :lines, :columns
def initialize
@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
assert_equal false, pc.replace_entities
pc.replace_entities = true
assert_equal true, pc.replace_entities
end
def test_recovery
pc = ParserContext.new StringIO.new('<root />'), 'UTF-8'
pc.recovery = false
assert_equal false, pc.recovery
pc.recovery = true
assert_equal true, pc.recovery
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