forked from xml4r/libxml-ruby
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtc_parser.rb
356 lines (287 loc) · 10.3 KB
/
tc_parser.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# encoding: UTF-8
require './test_helper'
require 'test/unit'
require 'stringio'
class TestParser < Test::Unit::TestCase
def setup
XML::Error.set_handler(&XML::Error::QUIET_HANDLER)
end
def teardown
GC.start
GC.start
GC.start
end
# ----- Sources -------
def test_document
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
parser = XML::Parser.file(file)
doc = parser.parse
parser = XML::Parser.document(doc)
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
def test_nil_document
error = assert_raise(TypeError) do
XML::Parser.document(nil)
end
assert_equal("Must pass an XML::Document object", error.to_s)
end
def test_file
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
parser = XML::Parser.file(file)
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
def test_noexistent_file
error = assert_raise(XML::Error) do
XML::Parser.file('i_dont_exist.xml')
end
assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
end
def test_nil_file
error = assert_raise(TypeError) do
XML::Parser.file(nil)
end
assert_equal("can't convert nil into String", error.to_s)
end
def test_file_encoding
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
parser = XML::Parser.file(file, :encoding => XML::Encoding::ISO_8859_1)
error = assert_raise(XML::Error) do
doc = parser.parse
end
assert(error.to_s.match(/Fatal error: Extra content at the end of the document/))
parser = XML::Parser.file(file, :encoding => XML::Encoding::UTF_8)
doc = parser.parse
assert_not_nil(doc)
end
def test_file_base_uri
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
parser = XML::Parser.file(file)
doc = parser.parse
assert(doc.child.base_uri.match(/test\/model\/bands.utf-8.xml/))
parser = XML::Parser.file(file, :base_uri => "http://libxml.org")
doc = parser.parse
assert(doc.child.base_uri.match(/test\/model\/bands.utf-8.xml/))
end
def test_io
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
parser = XML::Parser.io(io)
assert_instance_of(XML::Parser, parser)
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
end
def test_io_gc
# Test that the reader keeps a reference
# to the io object
file = File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
parser = XML::Parser.io(file)
file = nil
GC.start
assert(parser.parse)
end
def test_nil_io
error = assert_raise(TypeError) do
XML::Parser.io(nil)
end
assert_equal("Must pass in an IO object", error.to_s)
end
def test_string_io
data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
string_io = StringIO.new(data)
parser = XML::Parser.io(string_io)
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
def test_string_io_thread
thread = Thread.new do
data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
string_io = StringIO.new(data)
parser = XML::Parser.io(string_io)
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
thread.join
assert(true)
end
def test_string
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
parser = XML::Parser.string(str)
assert_instance_of(XML::Parser, parser)
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
def test_nil_string
error = assert_raise(TypeError) do
XML::Parser.string(nil)
end
assert_equal("wrong argument type nil (expected String)", error.to_s)
end
def test_string_options
xml = <<-EOS
<!DOCTYPE foo [<!ENTITY foo 'bar'>]>
<test>
<cdata><![CDATA[something]]></cdata>
<entity>&foo;</entity>
</test>
EOS
XML::default_substitute_entities = false
# Parse normally
parser = XML::Parser.string(xml)
doc = parser.parse
assert_nil(doc.child.base_uri)
# Cdata section should be cdata nodes
node = doc.find_first('/test/cdata').child
assert_equal(XML::Node::CDATA_SECTION_NODE, node.node_type)
# Entities should not be subtituted
node = doc.find_first('/test/entity')
assert_equal('&foo;', node.child.to_s)
# Parse with options
parser = XML::Parser.string(xml, :base_uri => 'http://libxml.rubyforge.org',
:options => XML::Parser::Options::NOCDATA | XML::Parser::Options::NOENT)
doc = parser.parse
assert_equal(doc.child.base_uri, 'http://libxml.rubyforge.org')
# Cdata section should be text nodes
node = doc.find_first('/test/cdata').child
assert_equal(XML::Node::TEXT_NODE, node.node_type)
# Entities should be subtituted
node = doc.find_first('/test/entity')
assert_equal('bar', node.child.to_s)
end
def test_string_encoding
# ISO_8859_1:
# ö - f6 in hex, \366 in octal
# ü - fc in hex, \374 in octal
xml = <<-EOS
<bands>
<metal>m\366tley_cr\374e</metal>
</bands>
EOS
# Parse as UTF_8
parser = XML::Parser.string(xml, :encoding => XML::Encoding::UTF_8)
error = assert_raise(XML::Error) do
doc = parser.parse
end
assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
error.to_s)
# Parse as ISO_8859_1:
parser = XML::Parser.string(xml, :encoding => XML::Encoding::ISO_8859_1)
doc = parser.parse
node = doc.find_first('//metal')
if defined?(Encoding)
assert_equal(Encoding::UTF_8, node.content.encoding)
assert_equal("m\303\266tley_cr\303\274e", node.content)
else
assert_equal("m\303\266tley_cr\303\274e", node.content)
end
end
def test_fd_gc
# Test opening # of documents up to the file limit for the OS.
# Ideally it should run until libxml emits a warning,
# thereby knowing we've done a GC sweep. For the time being,
# re-open the same doc `limit descriptors` times.
# If we make it to the end, then we've succeeded,
# otherwise an exception will be thrown.
XML::Error.set_handler {|error|}
max_fd = if RUBY_PLATFORM.match(/mswin32|mingw/i)
500
else
(`ulimit -n`.chomp.to_i) + 1
end
file = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
max_fd.times do
XML::Parser.file(file).parse
end
XML::Error.reset_handler {|error|}
end
def test_open_many_files
1000.times do
doc = XML::Parser.file('model/atom.xml').parse
end
end
# ----- Errors ------
def test_error
error = assert_raise(XML::Error) do
XML::Parser.string('<foo><bar/></foz>').parse
end
assert_not_nil(error)
assert_kind_of(XML::Error, error)
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", error.message)
assert_equal(XML::Error::PARSER, error.domain)
assert_equal(XML::Error::TAG_NAME_MISMATCH, error.code)
assert_equal(XML::Error::FATAL, error.level)
assert_nil(error.file)
assert_equal(1, error.line)
assert_equal('foo', error.str1)
assert_equal('foz', error.str2)
assert_nil(error.str3)
assert_equal(1, error.int1)
assert_equal(20, error.int2)
assert_nil(error.node)
end
def test_bad_xml
parser = XML::Parser.string('<ruby_array uga="booga" foo="bar"<fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
error = assert_raise(XML::Error) do
assert_not_nil(parser.parse)
end
assert_not_nil(error)
assert_kind_of(XML::Error, error)
assert_equal("Fatal error: Extra content at the end of the document at :1.", error.message)
assert_equal(XML::Error::PARSER, error.domain)
assert_equal(XML::Error::DOCUMENT_END, error.code)
assert_equal(XML::Error::FATAL, error.level)
assert_nil(error.file)
assert_equal(1, error.line)
assert_nil(error.str1)
assert_nil(error.str2)
assert_nil(error.str3)
assert_equal(0, error.int1)
assert_equal(20, error.int2)
assert_nil(error.node)
end
# Deprecated methods
def test_document_deprecated
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
parser = XML::Parser.file(file)
doc = parser.parse
parser = XML::Parser.new
parser.document = doc
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
def test_file_deprecated
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
parser = XML::Parser.new
parser.file = file
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
def test_io_deprecated
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
parser = XML::Parser.new
assert_instance_of(XML::Parser, parser)
parser.io = io
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
end
def test_string_deprecated
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
parser = XML::Parser.new
parser.string = str
assert_instance_of(XML::Parser, parser)
doc = parser.parse
assert_instance_of(XML::Document, doc)
assert_instance_of(XML::Parser::Context, parser.context)
end
end