libxml-ruby 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +1 -1
- data/LICENSE +22 -22
- data/README +160 -160
- data/Rakefile +0 -9
- data/ext/libxml/Makefile +156 -0
- data/ext/libxml/extconf.h +5 -0
- data/ext/libxml/extconf.rb +4 -160
- data/ext/libxml/libxml-ruby.so.a +0 -0
- data/ext/libxml/libxml.o +0 -0
- data/ext/libxml/libxml_ruby.so +0 -0
- data/ext/libxml/mkmf.log +129 -0
- data/ext/libxml/ruby_xml.o +0 -0
- data/ext/libxml/ruby_xml_attr.c +1 -1
- data/ext/libxml/ruby_xml_attr.h +1 -1
- data/ext/libxml/ruby_xml_attr.o +0 -0
- data/ext/libxml/ruby_xml_attr_decl.o +0 -0
- data/ext/libxml/ruby_xml_attributes.o +0 -0
- data/ext/libxml/ruby_xml_cbg.o +0 -0
- data/ext/libxml/ruby_xml_document.c +936 -936
- data/ext/libxml/ruby_xml_document.h +1 -1
- data/ext/libxml/ruby_xml_document.o +0 -0
- data/ext/libxml/ruby_xml_dtd.o +0 -0
- data/ext/libxml/ruby_xml_encoding.o +0 -0
- data/ext/libxml/ruby_xml_error.o +0 -0
- data/ext/libxml/ruby_xml_html_parser.c +1 -1
- data/ext/libxml/ruby_xml_html_parser.h +1 -1
- data/ext/libxml/ruby_xml_html_parser.o +0 -0
- data/ext/libxml/ruby_xml_html_parser_context.o +0 -0
- data/ext/libxml/ruby_xml_html_parser_options.o +0 -0
- data/ext/libxml/ruby_xml_input_cbg.o +0 -0
- data/ext/libxml/ruby_xml_io.o +0 -0
- data/ext/libxml/ruby_xml_namespace.c +1 -1
- data/ext/libxml/ruby_xml_namespace.h +1 -1
- data/ext/libxml/ruby_xml_namespace.o +0 -0
- data/ext/libxml/ruby_xml_namespaces.o +0 -0
- data/ext/libxml/ruby_xml_node.c +1386 -1386
- data/ext/libxml/ruby_xml_node.h +1 -1
- data/ext/libxml/ruby_xml_node.o +0 -0
- data/ext/libxml/ruby_xml_parser.c +1 -1
- data/ext/libxml/ruby_xml_parser.h +1 -1
- data/ext/libxml/ruby_xml_parser.o +0 -0
- data/ext/libxml/ruby_xml_parser_context.c +1 -1
- data/ext/libxml/ruby_xml_parser_context.h +1 -1
- data/ext/libxml/ruby_xml_parser_context.o +0 -0
- data/ext/libxml/ruby_xml_parser_options.o +0 -0
- data/ext/libxml/ruby_xml_reader.o +0 -0
- data/ext/libxml/ruby_xml_relaxng.o +0 -0
- data/ext/libxml/ruby_xml_sax2_handler.o +0 -0
- data/ext/libxml/ruby_xml_sax_parser.c +1 -1
- data/ext/libxml/ruby_xml_sax_parser.h +1 -1
- data/ext/libxml/ruby_xml_sax_parser.o +0 -0
- data/ext/libxml/ruby_xml_schema.o +0 -0
- data/ext/libxml/ruby_xml_version.h +3 -3
- data/ext/libxml/ruby_xml_xinclude.c +1 -1
- data/ext/libxml/ruby_xml_xinclude.h +1 -1
- data/ext/libxml/ruby_xml_xinclude.o +0 -0
- data/ext/libxml/ruby_xml_xpath.c +1 -1
- data/ext/libxml/ruby_xml_xpath.h +1 -1
- data/ext/libxml/ruby_xml_xpath.o +0 -0
- data/ext/libxml/ruby_xml_xpath_context.c +1 -1
- data/ext/libxml/ruby_xml_xpath_context.h +1 -1
- data/ext/libxml/ruby_xml_xpath_context.o +0 -0
- data/ext/libxml/ruby_xml_xpath_expression.o +0 -0
- data/ext/libxml/ruby_xml_xpath_object.o +0 -0
- data/ext/libxml/ruby_xml_xpointer.c +1 -1
- data/ext/libxml/ruby_xml_xpointer.h +1 -1
- data/ext/libxml/ruby_xml_xpointer.o +0 -0
- data/ext/mingw/build.rake +3 -3
- data/lib/libxml.rb +29 -29
- data/test/model/merge_bug_data.xml +58 -58
- data/test/model/rubynet.xml +79 -79
- data/test/model/xinclude.xml +4 -4
- data/test/tc_attr.rb +170 -170
- data/test/tc_document.rb +113 -113
- data/test/tc_document_write.rb +117 -117
- data/test/tc_dtd.rb +123 -123
- data/test/tc_html_parser.rb +137 -137
- data/test/tc_node.rb +180 -180
- data/test/tc_node_cdata.rb +49 -49
- data/test/tc_node_comment.rb +30 -30
- data/test/tc_node_edit.rb +157 -157
- data/test/tc_node_xlink.rb +26 -26
- data/test/tc_parser.rb +329 -329
- data/test/tc_parser_context.rb +185 -185
- data/test/tc_reader.rb +283 -283
- data/test/tc_sax_parser.rb +273 -273
- data/test/tc_schema.rb +51 -51
- data/test/tc_xinclude.rb +19 -19
- data/test/tc_xpath.rb +193 -193
- data/test/tc_xpointer.rb +72 -72
- metadata +55 -14
- data/ext/libxml/build.log +0 -4
data/test/tc_node_xlink.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
|
-
# $Id
|
2
|
-
require "xml"
|
3
|
-
require 'test/unit'
|
4
|
-
|
5
|
-
class TC_XML_Node_XLink < Test::Unit::TestCase
|
6
|
-
def setup()
|
7
|
-
xp = XML::Parser.string('<ruby_array xmlns:xlink="http://www.w3.org/1999/xlink/namespace/"><fixnum xlink:type="simple">one</fixnum></ruby_array>')
|
8
|
-
doc = xp.parse
|
9
|
-
assert_instance_of(XML::Document, doc)
|
10
|
-
@root = doc.root
|
11
|
-
assert_instance_of(XML::Node, @root)
|
12
|
-
end
|
13
|
-
|
14
|
-
def teardown()
|
15
|
-
@root = nil
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_xml_node_xlink()
|
19
|
-
for elem in @root.find('fixnum')
|
20
|
-
assert_instance_of(XML::Node, elem)
|
21
|
-
assert_instance_of(TrueClass, elem.xlink?)
|
22
|
-
assert_equal("simple", elem.xlink_type_name)
|
23
|
-
assert_equal(XML::Node::XLINK_TYPE_SIMPLE, elem.xlink_type)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
1
|
+
# $Id$
|
2
|
+
require "xml"
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TC_XML_Node_XLink < Test::Unit::TestCase
|
6
|
+
def setup()
|
7
|
+
xp = XML::Parser.string('<ruby_array xmlns:xlink="http://www.w3.org/1999/xlink/namespace/"><fixnum xlink:type="simple">one</fixnum></ruby_array>')
|
8
|
+
doc = xp.parse
|
9
|
+
assert_instance_of(XML::Document, doc)
|
10
|
+
@root = doc.root
|
11
|
+
assert_instance_of(XML::Node, @root)
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown()
|
15
|
+
@root = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_xml_node_xlink()
|
19
|
+
for elem in @root.find('fixnum')
|
20
|
+
assert_instance_of(XML::Node, elem)
|
21
|
+
assert_instance_of(TrueClass, elem.xlink?)
|
22
|
+
assert_equal("simple", elem.xlink_type_name)
|
23
|
+
assert_equal(XML::Node::XLINK_TYPE_SIMPLE, elem.xlink_type)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/tc_parser.rb
CHANGED
@@ -1,330 +1,330 @@
|
|
1
|
-
require 'xml'
|
2
|
-
require 'test/unit'
|
3
|
-
require 'stringio'
|
4
|
-
|
5
|
-
class TestParser < Test::Unit::TestCase
|
6
|
-
def setup
|
7
|
-
XML::Error.set_handler(&XML::Error::QUIET_HANDLER)
|
8
|
-
end
|
9
|
-
|
10
|
-
def teardown
|
11
|
-
GC.start
|
12
|
-
GC.start
|
13
|
-
GC.start
|
14
|
-
end
|
15
|
-
|
16
|
-
# ----- Sources ------
|
17
|
-
def test_document
|
18
|
-
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
19
|
-
parser = XML::Parser.file(file)
|
20
|
-
doc = parser.parse
|
21
|
-
|
22
|
-
parser = XML::Parser.document(doc)
|
23
|
-
|
24
|
-
doc = parser.parse
|
25
|
-
|
26
|
-
assert_instance_of(XML::Document, doc)
|
27
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_nil_document
|
31
|
-
error = assert_raise(TypeError) do
|
32
|
-
XML::Parser.document(nil)
|
33
|
-
end
|
34
|
-
|
35
|
-
assert_equal("Must pass an XML::Document object", error.to_s)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_file
|
39
|
-
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
40
|
-
|
41
|
-
parser = XML::Parser.file(file)
|
42
|
-
doc = parser.parse
|
43
|
-
assert_instance_of(XML::Document, doc)
|
44
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_noexistent_file
|
48
|
-
error = assert_raise(XML::Error) do
|
49
|
-
XML::Parser.file('i_dont_exist.xml')
|
50
|
-
end
|
51
|
-
|
52
|
-
assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_nil_file
|
56
|
-
error = assert_raise(TypeError) do
|
57
|
-
XML::Parser.file(nil)
|
58
|
-
end
|
59
|
-
|
60
|
-
assert_equal("can't convert nil into String", error.to_s)
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_file_encoding
|
64
|
-
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
65
|
-
parser = XML::Parser.file(file, :encoding => XML::Encoding::ISO_8859_1)
|
66
|
-
|
67
|
-
error = assert_raise(XML::Error) do
|
68
|
-
doc = parser.parse
|
69
|
-
end
|
70
|
-
|
71
|
-
assert(error.to_s.match(/Fatal error: Extra content at the end of the document/))
|
72
|
-
|
73
|
-
parser = XML::Parser.file(file, :encoding => XML::Encoding::UTF_8)
|
74
|
-
doc = parser.parse
|
75
|
-
assert_not_nil(doc)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_file_base_uri
|
79
|
-
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
80
|
-
|
81
|
-
parser = XML::Parser.file(file)
|
82
|
-
doc = parser.parse
|
83
|
-
assert(doc.child.base.match(/test\/model\/bands.xml/))
|
84
|
-
|
85
|
-
parser = XML::Parser.file(file, :base_uri => "http://libxml.org")
|
86
|
-
doc = parser.parse
|
87
|
-
assert(doc.child.base.match(/test\/model\/bands.xml/))
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_io
|
91
|
-
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
|
92
|
-
parser = XML::Parser.io(io)
|
93
|
-
assert_instance_of(XML::Parser, parser)
|
94
|
-
|
95
|
-
doc = parser.parse
|
96
|
-
assert_instance_of(XML::Document, doc)
|
97
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_io_gc
|
102
|
-
# Test that the reader keeps a reference
|
103
|
-
# to the io object
|
104
|
-
file = File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
105
|
-
parser = XML::Parser.io(file)
|
106
|
-
file = nil
|
107
|
-
GC.start
|
108
|
-
assert(parser.parse)
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_nil_io
|
112
|
-
error = assert_raise(TypeError) do
|
113
|
-
XML::Parser.io(nil)
|
114
|
-
end
|
115
|
-
|
116
|
-
assert_equal("Must pass in an IO object", error.to_s)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_string_io
|
120
|
-
data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
121
|
-
string_io = StringIO.new(data)
|
122
|
-
parser = XML::Parser.io(string_io)
|
123
|
-
|
124
|
-
doc = parser.parse
|
125
|
-
assert_instance_of(XML::Document, doc)
|
126
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_string
|
130
|
-
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
131
|
-
|
132
|
-
parser = XML::Parser.string(str)
|
133
|
-
assert_instance_of(XML::Parser, parser)
|
134
|
-
|
135
|
-
doc = parser.parse
|
136
|
-
assert_instance_of(XML::Document, doc)
|
137
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_nil_string
|
141
|
-
error = assert_raise(TypeError) do
|
142
|
-
XML::Parser.string(nil)
|
143
|
-
end
|
144
|
-
|
145
|
-
assert_equal("wrong argument type nil (expected String)", error.to_s)
|
146
|
-
end
|
147
|
-
|
148
|
-
def test_string_options
|
149
|
-
xml = <<-EOS
|
150
|
-
<!DOCTYPE foo [<!ENTITY foo 'bar'>]>
|
151
|
-
<test>
|
152
|
-
<cdata><![CDATA[something]]></cdata>
|
153
|
-
<entity>&foo;</entity>
|
154
|
-
</test>
|
155
|
-
EOS
|
156
|
-
|
157
|
-
XML::default_substitute_entities = false
|
158
|
-
|
159
|
-
# Parse normally
|
160
|
-
parser = XML::Parser.string(xml)
|
161
|
-
doc = parser.parse
|
162
|
-
assert_nil(doc.child.base)
|
163
|
-
|
164
|
-
# Cdata section should be cdata nodes
|
165
|
-
node = doc.find_first('/test/cdata').child
|
166
|
-
assert_equal(XML::Node::CDATA_SECTION_NODE, node.node_type)
|
167
|
-
|
168
|
-
# Entities should not be subtituted
|
169
|
-
node = doc.find_first('/test/entity')
|
170
|
-
assert_equal('&foo;', node.child.to_s)
|
171
|
-
|
172
|
-
# Parse with options
|
173
|
-
parser = XML::Parser.string(xml, :base_uri => 'http://libxml.rubyforge.org',
|
174
|
-
:options => XML::Parser::Options::NOCDATA | XML::Parser::Options::NOENT)
|
175
|
-
doc = parser.parse
|
176
|
-
assert_equal(doc.child.base_uri, 'http://libxml.rubyforge.org')
|
177
|
-
|
178
|
-
# Cdata section should be text nodes
|
179
|
-
node = doc.find_first('/test/cdata').child
|
180
|
-
assert_equal(XML::Node::TEXT_NODE, node.node_type)
|
181
|
-
|
182
|
-
# Entities should be subtituted
|
183
|
-
node = doc.find_first('/test/entity')
|
184
|
-
assert_equal('bar', node.child.to_s)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_string_encoding
|
188
|
-
# ISO_8859_1:
|
189
|
-
# ö - f6 in hex, \366 in octal
|
190
|
-
# ü - fc in hex, \374 in octal
|
191
|
-
|
192
|
-
xml = <<-EOS
|
193
|
-
<bands>
|
194
|
-
<metal>m\366tley_cr\374e</metal>
|
195
|
-
</bands>
|
196
|
-
EOS
|
197
|
-
|
198
|
-
# Parse as UTF_8
|
199
|
-
parser = XML::Parser.string(xml)
|
200
|
-
|
201
|
-
error = assert_raise(XML::Error) do
|
202
|
-
doc = parser.parse
|
203
|
-
end
|
204
|
-
|
205
|
-
assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
|
206
|
-
error.to_s)
|
207
|
-
|
208
|
-
# Parse as ISO_8859_1:
|
209
|
-
parser = XML::Parser.string(xml, :encoding => XML::Encoding::ISO_8859_1)
|
210
|
-
doc = parser.parse
|
211
|
-
node = doc.find_first('//metal')
|
212
|
-
assert_equal("m\303\266tley_cr\303\274e", node.content)
|
213
|
-
end
|
214
|
-
|
215
|
-
|
216
|
-
def test_fd_gc
|
217
|
-
# Test opening # of documents up to the file limit for the OS.
|
218
|
-
# Ideally it should run until libxml emits a warning,
|
219
|
-
# thereby knowing we've done a GC sweep. For the time being,
|
220
|
-
# re-open the same doc `limit descriptors` times.
|
221
|
-
# If we make it to the end, then we've succeeded,
|
222
|
-
# otherwise an exception will be thrown.
|
223
|
-
XML::Error.set_handler {|error|}
|
224
|
-
|
225
|
-
max_fd = if RUBY_PLATFORM.match(/mswin32/i)
|
226
|
-
500
|
227
|
-
else
|
228
|
-
(`ulimit -n`.chomp.to_i) + 1
|
229
|
-
end
|
230
|
-
|
231
|
-
file = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
|
232
|
-
max_fd.times do
|
233
|
-
XML::Parser.file(file).parse
|
234
|
-
end
|
235
|
-
XML::Error.reset_handler {|error|}
|
236
|
-
end
|
237
|
-
|
238
|
-
|
239
|
-
# ----- Errors ------
|
240
|
-
def test_error
|
241
|
-
error = assert_raise(XML::Error) do
|
242
|
-
XML::Parser.string('<foo><bar/></foz>').parse
|
243
|
-
end
|
244
|
-
|
245
|
-
assert_not_nil(error)
|
246
|
-
assert_kind_of(XML::Error, error)
|
247
|
-
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", error.message)
|
248
|
-
assert_equal(XML::Error::PARSER, error.domain)
|
249
|
-
assert_equal(XML::Error::TAG_NAME_MISMATCH, error.code)
|
250
|
-
assert_equal(XML::Error::FATAL, error.level)
|
251
|
-
assert_nil(error.file)
|
252
|
-
assert_equal(1, error.line)
|
253
|
-
assert_equal('foo', error.str1)
|
254
|
-
assert_equal('foz', error.str2)
|
255
|
-
assert_nil(error.str3)
|
256
|
-
assert_equal(1, error.int1)
|
257
|
-
assert_equal(20, error.int2)
|
258
|
-
assert_nil(error.node)
|
259
|
-
end
|
260
|
-
|
261
|
-
def test_bad_xml
|
262
|
-
parser = XML::Parser.string('<ruby_array uga="booga" foo="bar"<fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
|
263
|
-
error = assert_raise(XML::Error) do
|
264
|
-
assert_not_nil(parser.parse)
|
265
|
-
end
|
266
|
-
|
267
|
-
assert_not_nil(error)
|
268
|
-
assert_kind_of(XML::Error, error)
|
269
|
-
assert_equal("Fatal error: Extra content at the end of the document at :1.", error.message)
|
270
|
-
assert_equal(XML::Error::PARSER, error.domain)
|
271
|
-
assert_equal(XML::Error::DOCUMENT_END, error.code)
|
272
|
-
assert_equal(XML::Error::FATAL, error.level)
|
273
|
-
assert_nil(error.file)
|
274
|
-
assert_equal(1, error.line)
|
275
|
-
assert_nil(error.str1)
|
276
|
-
assert_nil(error.str2)
|
277
|
-
assert_nil(error.str3)
|
278
|
-
assert_equal(0, error.int1)
|
279
|
-
assert_equal(20, error.int2)
|
280
|
-
assert_nil(error.node)
|
281
|
-
end
|
282
|
-
|
283
|
-
# Deprecated methods
|
284
|
-
def test_document_deprecated
|
285
|
-
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
286
|
-
parser = XML::Parser.file(file)
|
287
|
-
doc = parser.parse
|
288
|
-
|
289
|
-
parser = XML::Parser.new
|
290
|
-
parser.document = doc
|
291
|
-
doc = parser.parse
|
292
|
-
|
293
|
-
assert_instance_of(XML::Document, doc)
|
294
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
295
|
-
end
|
296
|
-
|
297
|
-
def test_file_deprecated
|
298
|
-
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
299
|
-
|
300
|
-
parser = XML::Parser.new
|
301
|
-
parser.file = file
|
302
|
-
doc = parser.parse
|
303
|
-
assert_instance_of(XML::Document, doc)
|
304
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
305
|
-
end
|
306
|
-
|
307
|
-
def test_io_deprecated
|
308
|
-
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
|
309
|
-
parser = XML::Parser.new
|
310
|
-
assert_instance_of(XML::Parser, parser)
|
311
|
-
parser.io = io
|
312
|
-
|
313
|
-
doc = parser.parse
|
314
|
-
assert_instance_of(XML::Document, doc)
|
315
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
316
|
-
end
|
317
|
-
end
|
318
|
-
|
319
|
-
def test_string_deprecated
|
320
|
-
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
321
|
-
|
322
|
-
parser = XML::Parser.new
|
323
|
-
parser.string = str
|
324
|
-
assert_instance_of(XML::Parser, parser)
|
325
|
-
|
326
|
-
doc = parser.parse
|
327
|
-
assert_instance_of(XML::Document, doc)
|
328
|
-
assert_instance_of(XML::Parser::Context, parser.context)
|
329
|
-
end
|
1
|
+
require 'xml'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class TestParser < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
XML::Error.set_handler(&XML::Error::QUIET_HANDLER)
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
GC.start
|
12
|
+
GC.start
|
13
|
+
GC.start
|
14
|
+
end
|
15
|
+
|
16
|
+
# ----- Sources ------
|
17
|
+
def test_document
|
18
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
19
|
+
parser = XML::Parser.file(file)
|
20
|
+
doc = parser.parse
|
21
|
+
|
22
|
+
parser = XML::Parser.document(doc)
|
23
|
+
|
24
|
+
doc = parser.parse
|
25
|
+
|
26
|
+
assert_instance_of(XML::Document, doc)
|
27
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_nil_document
|
31
|
+
error = assert_raise(TypeError) do
|
32
|
+
XML::Parser.document(nil)
|
33
|
+
end
|
34
|
+
|
35
|
+
assert_equal("Must pass an XML::Document object", error.to_s)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_file
|
39
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
40
|
+
|
41
|
+
parser = XML::Parser.file(file)
|
42
|
+
doc = parser.parse
|
43
|
+
assert_instance_of(XML::Document, doc)
|
44
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_noexistent_file
|
48
|
+
error = assert_raise(XML::Error) do
|
49
|
+
XML::Parser.file('i_dont_exist.xml')
|
50
|
+
end
|
51
|
+
|
52
|
+
assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_nil_file
|
56
|
+
error = assert_raise(TypeError) do
|
57
|
+
XML::Parser.file(nil)
|
58
|
+
end
|
59
|
+
|
60
|
+
assert_equal("can't convert nil into String", error.to_s)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_file_encoding
|
64
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
65
|
+
parser = XML::Parser.file(file, :encoding => XML::Encoding::ISO_8859_1)
|
66
|
+
|
67
|
+
error = assert_raise(XML::Error) do
|
68
|
+
doc = parser.parse
|
69
|
+
end
|
70
|
+
|
71
|
+
assert(error.to_s.match(/Fatal error: Extra content at the end of the document/))
|
72
|
+
|
73
|
+
parser = XML::Parser.file(file, :encoding => XML::Encoding::UTF_8)
|
74
|
+
doc = parser.parse
|
75
|
+
assert_not_nil(doc)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_file_base_uri
|
79
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
80
|
+
|
81
|
+
parser = XML::Parser.file(file)
|
82
|
+
doc = parser.parse
|
83
|
+
assert(doc.child.base.match(/test\/model\/bands.xml/))
|
84
|
+
|
85
|
+
parser = XML::Parser.file(file, :base_uri => "http://libxml.org")
|
86
|
+
doc = parser.parse
|
87
|
+
assert(doc.child.base.match(/test\/model\/bands.xml/))
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_io
|
91
|
+
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
|
92
|
+
parser = XML::Parser.io(io)
|
93
|
+
assert_instance_of(XML::Parser, parser)
|
94
|
+
|
95
|
+
doc = parser.parse
|
96
|
+
assert_instance_of(XML::Document, doc)
|
97
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_io_gc
|
102
|
+
# Test that the reader keeps a reference
|
103
|
+
# to the io object
|
104
|
+
file = File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
105
|
+
parser = XML::Parser.io(file)
|
106
|
+
file = nil
|
107
|
+
GC.start
|
108
|
+
assert(parser.parse)
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_nil_io
|
112
|
+
error = assert_raise(TypeError) do
|
113
|
+
XML::Parser.io(nil)
|
114
|
+
end
|
115
|
+
|
116
|
+
assert_equal("Must pass in an IO object", error.to_s)
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_string_io
|
120
|
+
data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
121
|
+
string_io = StringIO.new(data)
|
122
|
+
parser = XML::Parser.io(string_io)
|
123
|
+
|
124
|
+
doc = parser.parse
|
125
|
+
assert_instance_of(XML::Document, doc)
|
126
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_string
|
130
|
+
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
131
|
+
|
132
|
+
parser = XML::Parser.string(str)
|
133
|
+
assert_instance_of(XML::Parser, parser)
|
134
|
+
|
135
|
+
doc = parser.parse
|
136
|
+
assert_instance_of(XML::Document, doc)
|
137
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_nil_string
|
141
|
+
error = assert_raise(TypeError) do
|
142
|
+
XML::Parser.string(nil)
|
143
|
+
end
|
144
|
+
|
145
|
+
assert_equal("wrong argument type nil (expected String)", error.to_s)
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_string_options
|
149
|
+
xml = <<-EOS
|
150
|
+
<!DOCTYPE foo [<!ENTITY foo 'bar'>]>
|
151
|
+
<test>
|
152
|
+
<cdata><![CDATA[something]]></cdata>
|
153
|
+
<entity>&foo;</entity>
|
154
|
+
</test>
|
155
|
+
EOS
|
156
|
+
|
157
|
+
XML::default_substitute_entities = false
|
158
|
+
|
159
|
+
# Parse normally
|
160
|
+
parser = XML::Parser.string(xml)
|
161
|
+
doc = parser.parse
|
162
|
+
assert_nil(doc.child.base)
|
163
|
+
|
164
|
+
# Cdata section should be cdata nodes
|
165
|
+
node = doc.find_first('/test/cdata').child
|
166
|
+
assert_equal(XML::Node::CDATA_SECTION_NODE, node.node_type)
|
167
|
+
|
168
|
+
# Entities should not be subtituted
|
169
|
+
node = doc.find_first('/test/entity')
|
170
|
+
assert_equal('&foo;', node.child.to_s)
|
171
|
+
|
172
|
+
# Parse with options
|
173
|
+
parser = XML::Parser.string(xml, :base_uri => 'http://libxml.rubyforge.org',
|
174
|
+
:options => XML::Parser::Options::NOCDATA | XML::Parser::Options::NOENT)
|
175
|
+
doc = parser.parse
|
176
|
+
assert_equal(doc.child.base_uri, 'http://libxml.rubyforge.org')
|
177
|
+
|
178
|
+
# Cdata section should be text nodes
|
179
|
+
node = doc.find_first('/test/cdata').child
|
180
|
+
assert_equal(XML::Node::TEXT_NODE, node.node_type)
|
181
|
+
|
182
|
+
# Entities should be subtituted
|
183
|
+
node = doc.find_first('/test/entity')
|
184
|
+
assert_equal('bar', node.child.to_s)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_string_encoding
|
188
|
+
# ISO_8859_1:
|
189
|
+
# ö - f6 in hex, \366 in octal
|
190
|
+
# ü - fc in hex, \374 in octal
|
191
|
+
|
192
|
+
xml = <<-EOS
|
193
|
+
<bands>
|
194
|
+
<metal>m\366tley_cr\374e</metal>
|
195
|
+
</bands>
|
196
|
+
EOS
|
197
|
+
|
198
|
+
# Parse as UTF_8
|
199
|
+
parser = XML::Parser.string(xml)
|
200
|
+
|
201
|
+
error = assert_raise(XML::Error) do
|
202
|
+
doc = parser.parse
|
203
|
+
end
|
204
|
+
|
205
|
+
assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
|
206
|
+
error.to_s)
|
207
|
+
|
208
|
+
# Parse as ISO_8859_1:
|
209
|
+
parser = XML::Parser.string(xml, :encoding => XML::Encoding::ISO_8859_1)
|
210
|
+
doc = parser.parse
|
211
|
+
node = doc.find_first('//metal')
|
212
|
+
assert_equal("m\303\266tley_cr\303\274e", node.content)
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
def test_fd_gc
|
217
|
+
# Test opening # of documents up to the file limit for the OS.
|
218
|
+
# Ideally it should run until libxml emits a warning,
|
219
|
+
# thereby knowing we've done a GC sweep. For the time being,
|
220
|
+
# re-open the same doc `limit descriptors` times.
|
221
|
+
# If we make it to the end, then we've succeeded,
|
222
|
+
# otherwise an exception will be thrown.
|
223
|
+
XML::Error.set_handler {|error|}
|
224
|
+
|
225
|
+
max_fd = if RUBY_PLATFORM.match(/mswin32/i)
|
226
|
+
500
|
227
|
+
else
|
228
|
+
(`ulimit -n`.chomp.to_i) + 1
|
229
|
+
end
|
230
|
+
|
231
|
+
file = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
|
232
|
+
max_fd.times do
|
233
|
+
XML::Parser.file(file).parse
|
234
|
+
end
|
235
|
+
XML::Error.reset_handler {|error|}
|
236
|
+
end
|
237
|
+
|
238
|
+
|
239
|
+
# ----- Errors ------
|
240
|
+
def test_error
|
241
|
+
error = assert_raise(XML::Error) do
|
242
|
+
XML::Parser.string('<foo><bar/></foz>').parse
|
243
|
+
end
|
244
|
+
|
245
|
+
assert_not_nil(error)
|
246
|
+
assert_kind_of(XML::Error, error)
|
247
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", error.message)
|
248
|
+
assert_equal(XML::Error::PARSER, error.domain)
|
249
|
+
assert_equal(XML::Error::TAG_NAME_MISMATCH, error.code)
|
250
|
+
assert_equal(XML::Error::FATAL, error.level)
|
251
|
+
assert_nil(error.file)
|
252
|
+
assert_equal(1, error.line)
|
253
|
+
assert_equal('foo', error.str1)
|
254
|
+
assert_equal('foz', error.str2)
|
255
|
+
assert_nil(error.str3)
|
256
|
+
assert_equal(1, error.int1)
|
257
|
+
assert_equal(20, error.int2)
|
258
|
+
assert_nil(error.node)
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_bad_xml
|
262
|
+
parser = XML::Parser.string('<ruby_array uga="booga" foo="bar"<fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
|
263
|
+
error = assert_raise(XML::Error) do
|
264
|
+
assert_not_nil(parser.parse)
|
265
|
+
end
|
266
|
+
|
267
|
+
assert_not_nil(error)
|
268
|
+
assert_kind_of(XML::Error, error)
|
269
|
+
assert_equal("Fatal error: Extra content at the end of the document at :1.", error.message)
|
270
|
+
assert_equal(XML::Error::PARSER, error.domain)
|
271
|
+
assert_equal(XML::Error::DOCUMENT_END, error.code)
|
272
|
+
assert_equal(XML::Error::FATAL, error.level)
|
273
|
+
assert_nil(error.file)
|
274
|
+
assert_equal(1, error.line)
|
275
|
+
assert_nil(error.str1)
|
276
|
+
assert_nil(error.str2)
|
277
|
+
assert_nil(error.str3)
|
278
|
+
assert_equal(0, error.int1)
|
279
|
+
assert_equal(20, error.int2)
|
280
|
+
assert_nil(error.node)
|
281
|
+
end
|
282
|
+
|
283
|
+
# Deprecated methods
|
284
|
+
def test_document_deprecated
|
285
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
286
|
+
parser = XML::Parser.file(file)
|
287
|
+
doc = parser.parse
|
288
|
+
|
289
|
+
parser = XML::Parser.new
|
290
|
+
parser.document = doc
|
291
|
+
doc = parser.parse
|
292
|
+
|
293
|
+
assert_instance_of(XML::Document, doc)
|
294
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_file_deprecated
|
298
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
299
|
+
|
300
|
+
parser = XML::Parser.new
|
301
|
+
parser.file = file
|
302
|
+
doc = parser.parse
|
303
|
+
assert_instance_of(XML::Document, doc)
|
304
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_io_deprecated
|
308
|
+
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
|
309
|
+
parser = XML::Parser.new
|
310
|
+
assert_instance_of(XML::Parser, parser)
|
311
|
+
parser.io = io
|
312
|
+
|
313
|
+
doc = parser.parse
|
314
|
+
assert_instance_of(XML::Document, doc)
|
315
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_string_deprecated
|
320
|
+
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
321
|
+
|
322
|
+
parser = XML::Parser.new
|
323
|
+
parser.string = str
|
324
|
+
assert_instance_of(XML::Parser, parser)
|
325
|
+
|
326
|
+
doc = parser.parse
|
327
|
+
assert_instance_of(XML::Document, doc)
|
328
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
329
|
+
end
|
330
330
|
end
|