libxml-ruby 0.8.3 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +18 -0
- data/RAKEFILE +15 -39
- data/README +48 -47
- data/ext/libxml/libxml.c +847 -22
- data/ext/libxml/ruby_libxml.h +71 -95
- data/ext/libxml/ruby_xml_attr.c +500 -500
- data/ext/libxml/ruby_xml_attributes.c +1 -1
- data/ext/libxml/ruby_xml_document.c +1144 -1135
- data/ext/libxml/ruby_xml_document.h +4 -11
- data/ext/libxml/ruby_xml_dtd.c +27 -0
- data/ext/libxml/ruby_xml_encoding.c +164 -0
- data/ext/libxml/ruby_xml_encoding.h +13 -0
- data/ext/libxml/ruby_xml_error.c +941 -0
- data/ext/libxml/ruby_xml_error.h +13 -0
- data/ext/libxml/ruby_xml_html_parser.c +71 -387
- data/ext/libxml/ruby_xml_html_parser.h +1 -17
- data/ext/libxml/ruby_xml_input.c +179 -0
- data/ext/libxml/ruby_xml_input.h +18 -0
- data/ext/libxml/ruby_xml_input_cbg.c +17 -3
- data/ext/libxml/ruby_xml_node.c +1566 -1582
- data/ext/libxml/ruby_xml_node.h +1 -4
- data/ext/libxml/ruby_xml_ns.c +14 -3
- data/ext/libxml/ruby_xml_parser.c +164 -1398
- data/ext/libxml/ruby_xml_parser.h +5 -17
- data/ext/libxml/ruby_xml_parser_context.c +131 -169
- data/ext/libxml/ruby_xml_parser_context.h +2 -9
- data/ext/libxml/ruby_xml_reader.c +910 -945
- data/ext/libxml/ruby_xml_relaxng.c +32 -3
- data/ext/libxml/ruby_xml_sax_parser.c +106 -364
- data/ext/libxml/ruby_xml_sax_parser.h +1 -37
- data/ext/libxml/ruby_xml_schema.c +174 -145
- data/ext/libxml/ruby_xml_xinclude.c +9 -5
- data/ext/libxml/ruby_xml_xpath.c +25 -6
- data/ext/libxml/ruby_xml_xpath.h +1 -2
- data/ext/libxml/ruby_xml_xpath_context.c +17 -19
- data/ext/libxml/ruby_xml_xpath_object.c +60 -56
- data/ext/libxml/ruby_xml_xpointer.c +11 -5
- data/ext/libxml/sax_parser_callbacks.inc +42 -37
- data/ext/libxml/version.h +3 -3
- data/ext/mingw/Rakefile +20 -27
- data/ext/mingw/build.rake +41 -0
- data/ext/vc/libxml_ruby.vcproj +23 -15
- data/lib/libxml.rb +8 -2
- data/lib/libxml/document.rb +16 -4
- data/lib/libxml/error.rb +84 -0
- data/lib/libxml/hpricot.rb +76 -0
- data/lib/libxml/html_parser.rb +61 -0
- data/lib/libxml/node.rb +36 -25
- data/lib/libxml/parser.rb +312 -33
- data/lib/libxml/parser_context.rb +17 -0
- data/lib/libxml/properties.rb +15 -2
- data/lib/libxml/reader.rb +15 -0
- data/lib/libxml/sax_callbacks.rb +179 -0
- data/lib/libxml/sax_parser.rb +42 -0
- data/lib/libxml/tree.rb +1 -2
- data/lib/libxml/xpath_object.rb +12 -0
- data/test/model/atom.xml +4 -0
- data/test/tc_attributes.rb +43 -19
- data/test/tc_document.rb +1 -1
- data/test/tc_document_write.rb +15 -8
- data/test/tc_dtd.rb +36 -20
- data/test/tc_encoding.rb +13 -0
- data/test/tc_error.rb +136 -0
- data/test/tc_node.rb +2 -3
- data/test/tc_node_copy.rb +1 -1
- data/test/tc_node_edit.rb +6 -0
- data/test/tc_ns.rb +18 -0
- data/test/tc_parser.rb +113 -228
- data/test/tc_parser_context.rb +1 -2
- data/test/tc_reader.rb +24 -14
- data/test/tc_relaxng.rb +18 -6
- data/test/tc_sax_parser.rb +48 -13
- data/test/tc_schema.rb +20 -8
- data/test/tc_well_formed.rb +2 -1
- data/test/tc_xml.rb +212 -0
- data/test/tc_xpath.rb +60 -46
- data/test/tc_xpointer.rb +7 -11
- data/test/test_suite.rb +4 -3
- metadata +26 -109
- data/doc/rdoc/classes/LibXML.html +0 -241
- data/doc/rdoc/classes/LibXML/XML.html +0 -185
- data/doc/rdoc/classes/LibXML/XML/Attr.html +0 -1010
- data/doc/rdoc/classes/LibXML/XML/Attributes.html +0 -526
- data/doc/rdoc/classes/LibXML/XML/Document.html +0 -1489
- data/doc/rdoc/classes/LibXML/XML/Dtd.html +0 -213
- data/doc/rdoc/classes/LibXML/XML/Error.html +0 -117
- data/doc/rdoc/classes/LibXML/XML/HTMLParser.html +0 -348
- data/doc/rdoc/classes/LibXML/XML/InputCallbacks.html +0 -160
- data/doc/rdoc/classes/LibXML/XML/NS.html +0 -381
- data/doc/rdoc/classes/LibXML/XML/Node.html +0 -3396
- data/doc/rdoc/classes/LibXML/XML/Node/FailedModify.html +0 -123
- data/doc/rdoc/classes/LibXML/XML/Node/Set.html +0 -440
- data/doc/rdoc/classes/LibXML/XML/Node/SetNamespace.html +0 -123
- data/doc/rdoc/classes/LibXML/XML/Node/UnknownType.html +0 -123
- data/doc/rdoc/classes/LibXML/XML/Parser.html +0 -2239
- data/doc/rdoc/classes/LibXML/XML/Parser/Context.html +0 -1255
- data/doc/rdoc/classes/LibXML/XML/Parser/ParseError.html +0 -123
- data/doc/rdoc/classes/LibXML/XML/Reader.html +0 -2264
- data/doc/rdoc/classes/LibXML/XML/RelaxNG.html +0 -237
- data/doc/rdoc/classes/LibXML/XML/SaxParser.html +0 -415
- data/doc/rdoc/classes/LibXML/XML/Schema.html +0 -308
- data/doc/rdoc/classes/LibXML/XML/State.html +0 -124
- data/doc/rdoc/classes/LibXML/XML/Tree.html +0 -111
- data/doc/rdoc/classes/LibXML/XML/XInclude.html +0 -123
- data/doc/rdoc/classes/LibXML/XML/XInclude/Error.html +0 -117
- data/doc/rdoc/classes/LibXML/XML/XMLParserOptions.html +0 -198
- data/doc/rdoc/classes/LibXML/XML/XPath.html +0 -184
- data/doc/rdoc/classes/LibXML/XML/XPath/Context.html +0 -404
- data/doc/rdoc/classes/LibXML/XML/XPath/InvalidPath.html +0 -172
- data/doc/rdoc/classes/LibXML/XML/XPath/Object.html +0 -627
- data/doc/rdoc/classes/LibXML/XML/XPointer.html +0 -170
- data/doc/rdoc/classes/LibXML/XML/XPointer/Context.html +0 -123
- data/doc/rdoc/classes/LibXML/XML/XPointer/Context/InvalidPath.html +0 -117
- data/doc/rdoc/classes/LibXML/XML/XPointer/InvalidExpression.html +0 -124
- data/doc/rdoc/classes/singleton.html +0 -114
- data/doc/rdoc/created.rid +0 -1
- data/doc/rdoc/files/CHANGES.html +0 -442
- data/doc/rdoc/files/LICENSE.html +0 -133
- data/doc/rdoc/files/README.html +0 -388
- data/doc/rdoc/files/VERSION.html +0 -107
- data/doc/rdoc/files/ext/libxml/cbg_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/libxml_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_attr_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_attributes_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_document_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_dtd_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_html_parser_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_input_cbg_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_node_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_node_set_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_ns_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_parser_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_parser_context_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_reader_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_relaxng_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_sax_parser_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_schema_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_state_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_xinclude_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_xpath_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_xpath_context_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_xpath_object_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_xpointer_c.html +0 -101
- data/doc/rdoc/files/ext/libxml/ruby_xml_xpointer_context_c.html +0 -101
- data/doc/rdoc/files/lib/libxml/attr_rb.html +0 -108
- data/doc/rdoc/files/lib/libxml/attributes_rb.html +0 -108
- data/doc/rdoc/files/lib/libxml/document_rb.html +0 -108
- data/doc/rdoc/files/lib/libxml/node_rb.html +0 -108
- data/doc/rdoc/files/lib/libxml/node_set_rb.html +0 -108
- data/doc/rdoc/files/lib/libxml/parser_options_rb.html +0 -107
- data/doc/rdoc/files/lib/libxml/parser_rb.html +0 -101
- data/doc/rdoc/files/lib/libxml/properties_rb.html +0 -108
- data/doc/rdoc/files/lib/libxml/tree_rb.html +0 -107
- data/doc/rdoc/files/lib/libxml_rb.html +0 -124
- data/doc/rdoc/files/lib/xml/libxml_rb.html +0 -124
- data/doc/rdoc/files/lib/xml_rb.html +0 -134
- data/doc/rdoc/fr_class_index.html +0 -62
- data/doc/rdoc/fr_file_index.html +0 -66
- data/doc/rdoc/fr_method_index.html +0 -392
- data/doc/rdoc/index.html +0 -24
- data/doc/rdoc/rdoc-style.css +0 -208
- data/ext/libxml/ruby_xml_node_set.c +0 -172
- data/ext/libxml/ruby_xml_node_set.h +0 -20
- data/ext/libxml/ruby_xml_xpointer_context.c +0 -22
- data/ext/libxml/ruby_xml_xpointer_context.h +0 -18
- data/lib/libxml/node_set.rb +0 -27
- data/test/tc_node_set.rb +0 -24
- data/test/tc_node_set2.rb +0 -37
data/test/tc_document_write.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "xml"
|
2
2
|
require 'test/unit'
|
3
3
|
|
4
|
-
class
|
4
|
+
class TestDocumentWrite < Test::Unit::TestCase
|
5
5
|
def setup
|
6
6
|
@doc = XML::Document.new('1.0')
|
7
7
|
end
|
@@ -35,13 +35,6 @@ class TC_XML_Document_Write < Test::Unit::TestCase
|
|
35
35
|
assert_equal("<?xml version=\"1.0\"?>\n<rubynet/>\n", @doc.to_s(false))
|
36
36
|
end
|
37
37
|
|
38
|
-
def test_encoding
|
39
|
-
@doc.root = XML::Node.new('rubynet')
|
40
|
-
@doc.encoding = 'UTF-8'
|
41
|
-
assert_instance_of(XML::Node, @doc.root)
|
42
|
-
assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rubynet/>\n", @doc.to_s)
|
43
|
-
end
|
44
|
-
|
45
38
|
def test_child()
|
46
39
|
xml = "<?xml version=\"1.0\"?>\n<rubynet>\n <pkg/>\n</rubynet>\n"
|
47
40
|
@doc.root = XML::Node.new('rubynet')
|
@@ -136,4 +129,18 @@ class TC_XML_Document_Write < Test::Unit::TestCase
|
|
136
129
|
pkgname = meta.child = XML::Node.new('pkgname', 'libxml')
|
137
130
|
assert_equal(xml, @doc.to_s)
|
138
131
|
end
|
132
|
+
|
133
|
+
def test_encoding_utf
|
134
|
+
@doc.root = XML::Node.new('rubynet')
|
135
|
+
@doc.encoding = XML::Encoding.encoding_to_s(XML::Encoding::UTF8)
|
136
|
+
assert_instance_of(XML::Node, @doc.root)
|
137
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rubynet/>\n", @doc.to_s)
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_encoding_latin1
|
141
|
+
@doc.root = XML::Node.new('rubynet')
|
142
|
+
@doc.encoding = XML::Encoding.encoding_to_s(XML::Encoding::ISO_8859_1)
|
143
|
+
assert_instance_of(XML::Node, @doc.root)
|
144
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<rubynet/>\n", @doc.to_s)
|
145
|
+
end
|
139
146
|
end
|
data/test/tc_dtd.rb
CHANGED
@@ -31,20 +31,32 @@ class TestDtd < Test::Unit::TestCase
|
|
31
31
|
def test_valid
|
32
32
|
assert(@doc.validate(dtd))
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def test_invalid
|
36
36
|
new_node = XML::Node.new('invalid', 'this will mess up validation')
|
37
37
|
@doc.root.child_add(new_node)
|
38
|
-
|
38
|
+
|
39
39
|
messages = Hash.new
|
40
|
-
|
41
|
-
|
42
|
-
end
|
40
|
+
error = assert_raise(XML::Error) do
|
41
|
+
@doc.validate(dtd)
|
42
|
+
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
assert_equal(
|
44
|
+
# Check the error worked
|
45
|
+
assert_not_nil(error)
|
46
|
+
assert_kind_of(XML::Error, error)
|
47
|
+
assert_equal("Error: No declaration for element invalid at :0.", error.message)
|
48
|
+
assert_equal(XML::Error::VALID, error.domain)
|
49
|
+
assert_equal(XML::Error::DTD_UNKNOWN_ELEM, error.code)
|
50
|
+
assert_equal(XML::Error::ERROR, error.level)
|
51
|
+
assert_nil(error.file)
|
52
|
+
assert_nil(error.line)
|
53
|
+
assert_equal('invalid', error.str1)
|
54
|
+
assert_equal('invalid', error.str2)
|
55
|
+
assert_nil(error.str3)
|
56
|
+
assert_equal(0, error.int1)
|
57
|
+
assert_equal(0, error.int2)
|
58
|
+
assert_not_nil(error.node)
|
59
|
+
assert_equal('invalid', error.node.name)
|
48
60
|
end
|
49
61
|
|
50
62
|
def test_external_dtd
|
@@ -54,17 +66,21 @@ class TestDtd < Test::Unit::TestCase
|
|
54
66
|
<title>T1</title>
|
55
67
|
</test>
|
56
68
|
EOS
|
57
|
-
|
58
|
-
|
59
|
-
XML::
|
60
|
-
|
61
|
-
|
69
|
+
|
70
|
+
errors = Array.new
|
71
|
+
XML::Error.set_handler do |error|
|
72
|
+
errors << error
|
73
|
+
end
|
74
|
+
|
75
|
+
XML.default_load_external_dtd = false
|
62
76
|
doc = XML::Parser.string(xml).parse
|
63
|
-
assert_equal(Array.new,
|
64
|
-
|
65
|
-
XML
|
77
|
+
assert_equal(Array.new, errors)
|
78
|
+
|
79
|
+
XML.default_load_external_dtd = true
|
66
80
|
doc = XML::Parser.string(xml).parse
|
67
|
-
assert_equal(
|
68
|
-
|
81
|
+
assert_equal("Warning: failed to load external entity \"test.dtd\" at :1.",
|
82
|
+
errors.map do |error|
|
83
|
+
error.to_s
|
84
|
+
end.join(' '))
|
69
85
|
end
|
70
|
-
end
|
86
|
+
end
|
data/test/tc_encoding.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'xml'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestEncoding < Test::Unit::TestCase
|
5
|
+
def test_latin1
|
6
|
+
assert_equal(XML::Encoding::ISO_8859_1, 10)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_latin1_to_s
|
10
|
+
encoding_str = XML::Encoding.encoding_to_s(XML::Encoding::ISO_8859_1)
|
11
|
+
assert_equal('ISO-8859-1', encoding_str)
|
12
|
+
end
|
13
|
+
end
|
data/test/tc_error.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'xml'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class TestError < Test::Unit::TestCase
|
6
|
+
def test_invalid_handler
|
7
|
+
assert_raises(RuntimeError) do
|
8
|
+
XML::Error.set_handler
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_handler
|
13
|
+
exception = nil
|
14
|
+
XML::Error.set_handler do |error|
|
15
|
+
exception = error
|
16
|
+
end
|
17
|
+
|
18
|
+
# Raise the error
|
19
|
+
XML::Reader.new('<foo').read
|
20
|
+
|
21
|
+
# Check the handler worked
|
22
|
+
assert_not_nil(exception)
|
23
|
+
assert_kind_of(XML::Error, exception)
|
24
|
+
assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", exception.message)
|
25
|
+
assert_equal(XML::Error::XML_FROM_PARSER, exception.domain)
|
26
|
+
assert_equal(XML::Error::XML_ERR_GT_REQUIRED, exception.code)
|
27
|
+
assert_equal(XML::Error::XML_ERR_FATAL, exception.level)
|
28
|
+
assert_nil(exception.file)
|
29
|
+
assert_equal(1, exception.line)
|
30
|
+
assert_equal('foo', exception.str1)
|
31
|
+
assert_nil(exception.str2)
|
32
|
+
assert_nil(exception.str3)
|
33
|
+
assert_equal(0, exception.int1)
|
34
|
+
assert_equal(5, exception.int2)
|
35
|
+
assert_nil(exception.node)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_reset_handler
|
39
|
+
exception = nil
|
40
|
+
XML::Error.set_handler do |error|
|
41
|
+
exception = error
|
42
|
+
end
|
43
|
+
|
44
|
+
XML::Error.reset_handler
|
45
|
+
reader = XML::Reader.new('<foo')
|
46
|
+
assert_nil(exception)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_verbose_handler
|
50
|
+
XML::Error.set_handler(&XML::Error::VERBOSE_HANDLER)
|
51
|
+
output = StringIO.new
|
52
|
+
original_stderr = Object::STDERR
|
53
|
+
|
54
|
+
Object.const_set(:STDERR, output)
|
55
|
+
begin
|
56
|
+
assert_raise(XML::Error) do
|
57
|
+
XML::Parser.string('<foo><bar/></foz>').parse
|
58
|
+
end
|
59
|
+
ensure
|
60
|
+
Object.const_set(:STDERR, original_stderr)
|
61
|
+
end
|
62
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.\n", output.string)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_no_hanlder
|
66
|
+
XML::Error.reset_handler
|
67
|
+
output = StringIO.new
|
68
|
+
original_stderr = Object::STDERR
|
69
|
+
|
70
|
+
Object.const_set(:STDERR, output)
|
71
|
+
begin
|
72
|
+
assert_raise(XML::Error) do
|
73
|
+
XML::Parser.string('<foo><bar/></foz>').parse
|
74
|
+
end
|
75
|
+
ensure
|
76
|
+
Object.const_set(:STDERR, original_stderr)
|
77
|
+
end
|
78
|
+
assert_equal('', output.string)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_parse_error
|
82
|
+
exception = assert_raise(XML::Error) do
|
83
|
+
XML::Parser.string('<foo><bar/></foz>').parse
|
84
|
+
end
|
85
|
+
|
86
|
+
assert_instance_of(XML::Error, exception)
|
87
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", exception.message)
|
88
|
+
assert_equal(XML::Error::XML_FROM_PARSER, exception.domain)
|
89
|
+
assert_equal(XML::Error::XML_ERR_TAG_NAME_MISMATCH, exception.code)
|
90
|
+
assert_equal(XML::Error::XML_ERR_FATAL, exception.level)
|
91
|
+
assert_nil(exception.file)
|
92
|
+
assert_equal(1, exception.line)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_xpath_error
|
96
|
+
doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
|
97
|
+
|
98
|
+
exception = assert_raise(XML::Error) do
|
99
|
+
elements = doc.find('/foo[bar=test')
|
100
|
+
end
|
101
|
+
|
102
|
+
assert_instance_of(XML::Error, exception)
|
103
|
+
assert_equal("Error: Invalid predicate at :0.", exception.message)
|
104
|
+
assert_equal(XML::Error::XML_FROM_XPATH, exception.domain)
|
105
|
+
assert_equal(XML::Error::XML_XPATH_INVALID_PREDICATE_ERROR, exception.code)
|
106
|
+
assert_equal(XML::Error::XML_ERR_ERROR, exception.level)
|
107
|
+
assert_nil(exception.file)
|
108
|
+
assert_nil(nil)
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_double_parse
|
112
|
+
XML::Parser.register_error_handler(lambda {|msg| nil })
|
113
|
+
parser = XML::Parser.string("<test>something</test>")
|
114
|
+
doc = parser.parse
|
115
|
+
|
116
|
+
error = assert_raise(RuntimeError) do
|
117
|
+
# Try parsing a second time
|
118
|
+
parser.parse
|
119
|
+
end
|
120
|
+
|
121
|
+
assert_equal("You cannot parse a data source twice", error.to_s)
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_libxml_parser_empty_string
|
125
|
+
xp = XML::Parser.new
|
126
|
+
|
127
|
+
assert_raise(TypeError) do
|
128
|
+
xp.string = nil
|
129
|
+
end
|
130
|
+
|
131
|
+
xp.string = ''
|
132
|
+
assert_raise(XML::Error) do
|
133
|
+
xp.parse
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
data/test/tc_node.rb
CHANGED
@@ -54,9 +54,8 @@ class TestNode < Test::Unit::TestCase
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def test_node_find
|
57
|
-
|
58
|
-
|
59
|
-
for node in set
|
57
|
+
nodes = @doc.root.find('./fixnum')
|
58
|
+
for node in nodes
|
60
59
|
assert_instance_of(XML::Node, node)
|
61
60
|
end
|
62
61
|
end
|
data/test/tc_node_copy.rb
CHANGED
data/test/tc_node_edit.rb
CHANGED
data/test/tc_ns.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "xml"
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestNS < Test::Unit::TestCase
|
5
|
+
def test_ns
|
6
|
+
node = XML::Node.new('foo')
|
7
|
+
ns = XML::NS.new(node, 'http://www.mynamespace.com', 'my_namepace')
|
8
|
+
assert_equal(ns.prefix, 'my_namepace')
|
9
|
+
assert_equal(ns.href, 'http://www.mynamespace.com')
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_default_ns
|
13
|
+
node = XML::Node.new('foo')
|
14
|
+
ns = XML::NS.new(node, 'http://www.mynamespace.com', nil)
|
15
|
+
assert_equal(ns.prefix, nil)
|
16
|
+
assert_equal(ns.href, 'http://www.mynamespace.com')
|
17
|
+
end
|
18
|
+
end
|
data/test/tc_parser.rb
CHANGED
@@ -1,294 +1,179 @@
|
|
1
|
-
require
|
1
|
+
require 'xml'
|
2
2
|
require 'test/unit'
|
3
|
+
require 'stringio'
|
3
4
|
|
4
5
|
class TestParser < Test::Unit::TestCase
|
5
6
|
def setup
|
6
|
-
XML::
|
7
|
+
XML::Error.set_handler(&XML::Error::QUIET_HANDLER)
|
7
8
|
@xp = XML::Parser.new
|
8
9
|
end
|
9
10
|
|
10
11
|
def teardown
|
11
12
|
@xp = nil
|
12
|
-
|
13
|
-
|
14
|
-
# the test_fd_gc test
|
13
|
+
GC.start
|
14
|
+
GC.start
|
15
15
|
GC.start
|
16
16
|
end
|
17
17
|
|
18
|
-
# -----
|
19
|
-
def
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_debug_entities
|
24
|
-
XML::Parser::debug_entities = false
|
25
|
-
assert(!XML::Parser::debug_entities)
|
26
|
-
|
27
|
-
XML::Parser::debug_entities = true
|
28
|
-
assert(XML::Parser::debug_entities)
|
29
|
-
|
30
|
-
XML::Parser::debug_entities = false
|
31
|
-
assert(!XML::Parser::debug_entities)
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_default_compression
|
35
|
-
return unless XML::Parser::default_compression
|
36
|
-
|
37
|
-
0.upto(9) do |i|
|
38
|
-
XML::Parser::default_compression = i
|
39
|
-
assert_equal(i, XML::Parser::default_compression)
|
40
|
-
end
|
41
|
-
|
42
|
-
9.downto(0) do |i|
|
43
|
-
assert_equal(i, XML::Parser::default_compression = i)
|
44
|
-
assert_equal(i, XML::Parser::default_compression)
|
45
|
-
end
|
46
|
-
|
47
|
-
0.downto(-10) do |i|
|
48
|
-
assert_equal(i, XML::Parser::default_compression = i)
|
49
|
-
assert_equal(0, XML::Parser::default_compression)
|
50
|
-
end
|
51
|
-
|
52
|
-
10.upto(20) do |i|
|
53
|
-
assert_equal(i, XML::Parser::default_compression = i)
|
54
|
-
assert_equal(9, XML::Parser::default_compression)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_default_keep_blanks
|
59
|
-
XML::Parser::default_keep_blanks = false
|
60
|
-
assert(!XML::Parser::default_keep_blanks)
|
61
|
-
|
62
|
-
XML::Parser::default_keep_blanks = true
|
63
|
-
assert(XML::Parser::default_keep_blanks)
|
64
|
-
|
65
|
-
XML::Parser::default_keep_blanks = false
|
66
|
-
assert(!XML::Parser::default_keep_blanks)
|
67
|
-
|
68
|
-
# other tests depend on keeping blanks by default,
|
69
|
-
# which is the default default behaviour anyway.
|
70
|
-
XML::Parser::default_keep_blanks = true
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_default_line_numbers
|
74
|
-
XML::Parser::default_line_numbers = false
|
75
|
-
assert(!XML::Parser::default_line_numbers)
|
76
|
-
|
77
|
-
XML::Parser::default_line_numbers = true
|
78
|
-
assert(XML::Parser::default_line_numbers)
|
79
|
-
|
80
|
-
XML::Parser::default_line_numbers = false
|
81
|
-
assert(!XML::Parser::default_line_numbers)
|
82
|
-
end
|
18
|
+
# ----- Sources ------
|
19
|
+
def test_file
|
20
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
83
21
|
|
84
|
-
|
85
|
-
|
86
|
-
|
22
|
+
@xp.file = file
|
23
|
+
assert_equal(file, @xp.file)
|
24
|
+
assert_equal(file, @xp.input.file)
|
87
25
|
|
88
|
-
|
89
|
-
|
26
|
+
doc = @xp.parse
|
27
|
+
assert_instance_of(XML::Document, doc)
|
28
|
+
assert_instance_of(XML::Parser::Context, @xp.context)
|
29
|
+
GC.start
|
30
|
+
GC.start
|
31
|
+
GC.start
|
32
|
+
end
|
90
33
|
|
91
|
-
|
92
|
-
|
93
|
-
end
|
34
|
+
def test_file_class
|
35
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
94
36
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
assert_equal(
|
99
|
-
XML::Parser::default_tree_indent_string = 'uga'
|
100
|
-
s = XML::Parser::default_tree_indent_string
|
101
|
-
assert_instance_of(String, s)
|
102
|
-
assert_equal('uga', s)
|
103
|
-
XML::Parser::default_tree_indent_string = ' '
|
104
|
-
s = XML::Parser::default_tree_indent_string
|
105
|
-
assert_instance_of(String, s)
|
106
|
-
assert_equal(' ', s)
|
37
|
+
xp = XML::Parser.file(file)
|
38
|
+
assert_instance_of(XML::Parser, xp)
|
39
|
+
assert_equal(file, xp.file)
|
40
|
+
assert_equal(file, xp.input.file)
|
107
41
|
end
|
108
42
|
|
109
|
-
def
|
110
|
-
|
111
|
-
assert(!XML::Parser::default_validity_checking)
|
43
|
+
def test_string
|
44
|
+
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
112
45
|
|
113
|
-
|
114
|
-
|
46
|
+
@xp.string = str
|
47
|
+
assert_equal(str, @xp.string)
|
48
|
+
assert_equal(str, @xp.input.string)
|
115
49
|
|
116
|
-
|
117
|
-
|
50
|
+
doc = @xp.parse
|
51
|
+
assert_instance_of(XML::Document, doc)
|
52
|
+
assert_instance_of(XML::Parser::Context, @xp.context)
|
118
53
|
end
|
119
54
|
|
120
|
-
def
|
121
|
-
XML::
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
assert(XML::Parser::default_warnings)
|
55
|
+
def test_string_empty
|
56
|
+
assert_raise(XML::Error) do
|
57
|
+
@xp.string = ''
|
58
|
+
@xp.parse
|
59
|
+
end
|
126
60
|
|
127
|
-
|
128
|
-
|
61
|
+
assert_raise(TypeError) do
|
62
|
+
@xp.string = nil
|
63
|
+
end
|
129
64
|
end
|
130
65
|
|
131
|
-
def
|
132
|
-
|
133
|
-
|
134
|
-
XML::Parser::indent_tree_output = false
|
135
|
-
assert(FalseClass, XML::Parser::indent_tree_output)
|
66
|
+
def test_string_class
|
67
|
+
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
136
68
|
|
137
|
-
XML::Parser
|
138
|
-
|
69
|
+
xp = XML::Parser.string(str)
|
70
|
+
assert_instance_of(XML::Parser, xp)
|
71
|
+
assert_equal(str, xp.string)
|
72
|
+
assert_equal(str, xp.input.string)
|
139
73
|
end
|
140
74
|
|
141
|
-
def
|
142
|
-
|
143
|
-
|
75
|
+
def test_io
|
76
|
+
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
|
77
|
+
@xp.io = io
|
78
|
+
assert_equal(io, @xp.io)
|
79
|
+
assert_equal(io, @xp.input.io)
|
144
80
|
|
145
|
-
|
146
|
-
|
81
|
+
doc = @xp.parse
|
82
|
+
assert_instance_of(XML::Document, doc)
|
83
|
+
assert_instance_of(XML::Parser::Context, @xp.context)
|
84
|
+
end
|
147
85
|
end
|
148
86
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
def test_context
|
157
|
-
@xp = XML::Parser.string('<rubynet><testing>uga</testing><uga>foo</uga></rubynet>')
|
158
|
-
doc = @xp.parse
|
159
|
-
assert_instance_of(XML::Document, doc)
|
160
|
-
assert_instance_of(XML::Parser::Context, @xp.context)
|
87
|
+
def test_io_class
|
88
|
+
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
|
89
|
+
xp = XML::Parser.io(io)
|
90
|
+
assert_instance_of(XML::Parser, xp)
|
91
|
+
assert_equal(io, xp.io)
|
92
|
+
assert_equal(io, xp.input.io)
|
93
|
+
end
|
161
94
|
end
|
162
95
|
|
163
|
-
def
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
doc = @xp.parse
|
170
|
-
assert_instance_of(XML::Document, doc)
|
96
|
+
def test_string_io
|
97
|
+
data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
98
|
+
string_io = StringIO.new(data)
|
99
|
+
assert_raises(TypeError) do
|
100
|
+
@xp.io = string_io
|
101
|
+
end
|
171
102
|
end
|
172
103
|
|
173
|
-
def test_io
|
174
|
-
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |f|
|
175
|
-
assert_kind_of(IO, f)
|
176
|
-
assert_kind_of(IO, @xp.io = f)
|
177
|
-
assert_instance_of(XML::Document, @xp.parse)
|
178
|
-
end # File.open
|
179
|
-
end # def test_libxml_parser_io
|
180
|
-
|
181
104
|
def test_fd_gc
|
182
105
|
# Test opening # of documents up to the file limit for the OS.
|
183
|
-
# Ideally it should run until libxml emits a warning,
|
106
|
+
# Ideally it should run until libxml emits a warning,
|
184
107
|
# thereby knowing we've done a GC sweep. For the time being,
|
185
|
-
# re-open the same doc `limit descriptors` times.
|
186
|
-
# If we make it to the end, then we've succeeded,
|
108
|
+
# re-open the same doc `limit descriptors` times.
|
109
|
+
# If we make it to the end, then we've succeeded,
|
187
110
|
# otherwise an exception will be thrown.
|
188
|
-
XML::
|
189
|
-
|
111
|
+
XML::Error.set_handler {|error|}
|
112
|
+
|
190
113
|
max_fd = if RUBY_PLATFORM.match(/mswin32/i)
|
191
114
|
500
|
192
115
|
else
|
193
116
|
(`ulimit -n`.chomp.to_i) + 1
|
194
117
|
end
|
195
|
-
|
196
|
-
|
118
|
+
|
119
|
+
file = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
|
197
120
|
max_fd.times do
|
198
|
-
XML::
|
121
|
+
XML::Parser.file(file).parse
|
199
122
|
end
|
123
|
+
XML::Error.reset_handler {|error|}
|
200
124
|
end
|
201
125
|
|
202
|
-
def test_libxml_parser_features
|
203
|
-
assert_instance_of(Array, XML::Parser::features)
|
204
|
-
end
|
205
126
|
|
206
127
|
# ----- Errors ------
|
207
|
-
def
|
208
|
-
assert_raise(XML::
|
128
|
+
def test_error
|
129
|
+
error = assert_raise(XML::Error) do
|
209
130
|
XML::Parser.string('<foo><bar/></foz>').parse
|
210
131
|
end
|
211
132
|
|
212
|
-
|
213
|
-
XML::
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
"<foo><bar/></foz>\n",
|
227
|
-
" ^\n"], ary)
|
228
|
-
|
229
|
-
assert_instance_of(Proc, XML::Parser.register_error_handler(nil))
|
230
|
-
|
231
|
-
# this will go to stderr again
|
232
|
-
assert_raise(XML::Parser::ParseError) do
|
233
|
-
d = XML::Parser.string('<foo><bar/></foz>').parse
|
234
|
-
end
|
133
|
+
assert_not_nil(error)
|
134
|
+
assert_kind_of(XML::Error, error)
|
135
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", error.message)
|
136
|
+
assert_equal(XML::Error::PARSER, error.domain)
|
137
|
+
assert_equal(XML::Error::TAG_NAME_MISMATCH, error.code)
|
138
|
+
assert_equal(XML::Error::FATAL, error.level)
|
139
|
+
assert_nil(error.file)
|
140
|
+
assert_equal(1, error.line)
|
141
|
+
assert_equal('foo', error.str1)
|
142
|
+
assert_equal('foz', error.str2)
|
143
|
+
assert_nil(error.str3)
|
144
|
+
assert_equal(1, error.int1)
|
145
|
+
assert_equal(20, error.int2)
|
146
|
+
assert_nil(error.node)
|
235
147
|
end
|
236
|
-
|
237
|
-
def test_error_handler_lambda
|
238
|
-
XML::Parser.register_error_handler(nil)
|
239
|
-
assert_raise(XML::Parser::ParseError) do
|
240
|
-
XML::Parser.string('<foo><bar/></foz>').parse
|
241
|
-
end
|
242
148
|
|
243
|
-
ary = []
|
244
|
-
assert_nil XML::Parser.register_error_handler(lambda { |msg| ary << msg })
|
245
|
-
|
246
|
-
# this will use our error handler
|
247
|
-
assert_raise(XML::Parser::ParseError) do
|
248
|
-
XML::Parser.string('<foo><bar/></foz>').parse
|
249
|
-
end
|
250
|
-
|
251
|
-
assert_equal(["Entity: line 1: ",
|
252
|
-
"parser ",
|
253
|
-
"error : ",
|
254
|
-
"Opening and ending tag mismatch: foo line 1 and foz\n",
|
255
|
-
"<foo><bar/></foz>\n",
|
256
|
-
" ^\n"], ary)
|
257
|
-
|
258
|
-
assert_instance_of(Proc, XML::Parser.register_error_handler(nil))
|
259
|
-
|
260
|
-
# this will go to stderr again
|
261
|
-
assert_raise(XML::Parser::ParseError) do
|
262
|
-
d = XML::Parser.string('<foo><bar/></foz>').parse
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
149
|
def test_bad_xml
|
267
150
|
@xp.string = '<ruby_array uga="booga" foo="bar"<fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
268
|
-
XML::
|
269
|
-
assert_raise(XML::Parser::ParseError) do
|
151
|
+
error = assert_raise(XML::Error) do
|
270
152
|
assert_not_nil(@xp.parse)
|
271
153
|
end
|
154
|
+
|
155
|
+
assert_not_nil(error)
|
156
|
+
assert_kind_of(XML::Error, error)
|
157
|
+
assert_equal("Fatal error: Extra content at the end of the document at :1.", error.message)
|
158
|
+
assert_equal(XML::Error::PARSER, error.domain)
|
159
|
+
assert_equal(XML::Error::DOCUMENT_END, error.code)
|
160
|
+
assert_equal(XML::Error::FATAL, error.level)
|
161
|
+
assert_nil(error.file)
|
162
|
+
assert_equal(1, error.line)
|
163
|
+
assert_nil(error.str1)
|
164
|
+
assert_nil(error.str2)
|
165
|
+
assert_nil(error.str3)
|
166
|
+
assert_equal(0, error.int1)
|
167
|
+
assert_equal(20, error.int2)
|
168
|
+
assert_nil(error.node)
|
272
169
|
end
|
273
|
-
|
170
|
+
|
274
171
|
def test_double_parse
|
275
|
-
XML::Parser.register_error_handler(lambda {|msg| nil })
|
276
172
|
parser = XML::Parser.string("<test>something</test>")
|
277
173
|
doc = parser.parse
|
278
|
-
|
279
|
-
assert_raise(
|
174
|
+
|
175
|
+
assert_raise(RuntimeError) do
|
280
176
|
parser.parse
|
281
177
|
end
|
282
178
|
end
|
283
|
-
|
284
|
-
def test_libxml_parser_empty_string
|
285
|
-
assert_raise(XML::Parser::ParseError) do
|
286
|
-
@xp.string = ''
|
287
|
-
@xp.parse
|
288
|
-
end
|
289
|
-
|
290
|
-
assert_raise(TypeError) do
|
291
|
-
@xp.string = nil
|
292
|
-
end
|
293
|
-
end
|
294
|
-
end
|
179
|
+
end
|