libxml-ruby 1.1.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. data/{CHANGES → HISTORY} +35 -0
  2. data/LICENSE +1 -0
  3. data/MANIFEST +165 -0
  4. data/{README → README.rdoc} +2 -2
  5. data/Rakefile +47 -147
  6. data/ext/libxml/libxml.c +17 -1
  7. data/ext/libxml/ruby_libxml.h +9 -1
  8. data/ext/libxml/ruby_xml_attr.c +3 -3
  9. data/ext/libxml/ruby_xml_attr_decl.c +32 -32
  10. data/ext/libxml/ruby_xml_attributes.c +1 -1
  11. data/ext/libxml/ruby_xml_cbg.c +1 -2
  12. data/ext/libxml/ruby_xml_document.c +26 -4
  13. data/ext/libxml/ruby_xml_dtd.c +3 -3
  14. data/ext/libxml/ruby_xml_encoding.c +92 -5
  15. data/ext/libxml/ruby_xml_encoding.h +4 -0
  16. data/ext/libxml/ruby_xml_namespace.c +2 -2
  17. data/ext/libxml/ruby_xml_node.c +14 -14
  18. data/ext/libxml/ruby_xml_parser_context.c +8 -8
  19. data/ext/libxml/ruby_xml_reader.c +98 -43
  20. data/ext/libxml/ruby_xml_sax2_handler.c +13 -1
  21. data/ext/libxml/ruby_xml_sax_parser.c +4 -5
  22. data/ext/libxml/ruby_xml_version.h +5 -5
  23. data/ext/libxml/ruby_xml_xpath_context.c +9 -6
  24. data/ext/libxml/ruby_xml_xpath_object.c +1 -1
  25. data/ext/vc/libxml_ruby.sln +4 -4
  26. data/lib/libxml.rb +29 -29
  27. data/lib/libxml/attr.rb +112 -110
  28. data/lib/libxml/attr_decl.rb +2 -0
  29. data/lib/libxml/attributes.rb +13 -11
  30. data/lib/libxml/document.rb +192 -190
  31. data/lib/libxml/error.rb +89 -87
  32. data/lib/libxml/hpricot.rb +77 -75
  33. data/lib/libxml/html_parser.rb +96 -94
  34. data/lib/libxml/namespace.rb +61 -59
  35. data/lib/libxml/namespaces.rb +37 -35
  36. data/lib/libxml/node.rb +398 -384
  37. data/lib/libxml/ns.rb +21 -19
  38. data/lib/libxml/parser.rb +366 -364
  39. data/lib/libxml/properties.rb +22 -20
  40. data/lib/libxml/reader.rb +2 -0
  41. data/lib/libxml/sax_callbacks.rb +179 -177
  42. data/lib/libxml/sax_parser.rb +57 -55
  43. data/lib/libxml/tree.rb +28 -26
  44. data/lib/libxml/xpath_object.rb +15 -13
  45. data/lib/xml.rb +16 -14
  46. data/lib/xml/libxml.rb +10 -8
  47. data/libxml-ruby.gemspec +50 -0
  48. data/script/benchmark/depixelate +634 -0
  49. data/script/benchmark/hamlet.xml +9055 -0
  50. data/script/benchmark/parsecount +170 -0
  51. data/script/benchmark/sock_entries.xml +507 -0
  52. data/script/benchmark/throughput +41 -0
  53. data/script/test +6 -0
  54. data/test/etc_doc_to_s.rb +21 -19
  55. data/test/ets_doc_file.rb +17 -15
  56. data/test/ets_doc_to_s.rb +23 -21
  57. data/test/ets_gpx.rb +28 -26
  58. data/test/ets_node_gc.rb +23 -21
  59. data/test/ets_tsr.rb +11 -9
  60. data/test/model/bands.iso-8859-1.xml +5 -0
  61. data/test/model/bands.utf-8.xml +5 -0
  62. data/test/rb-magic-comment.rb +33 -0
  63. data/test/tc_attr.rb +181 -170
  64. data/test/tc_attr_decl.rb +3 -1
  65. data/test/tc_attributes.rb +134 -132
  66. data/test/tc_deprecated_require.rb +13 -11
  67. data/test/tc_document.rb +119 -113
  68. data/test/tc_document_write.rb +186 -117
  69. data/test/tc_dtd.rb +125 -123
  70. data/test/tc_error.rb +3 -1
  71. data/test/tc_html_parser.rb +139 -137
  72. data/test/tc_namespace.rb +61 -58
  73. data/test/tc_namespaces.rb +176 -173
  74. data/test/tc_node.rb +257 -180
  75. data/test/tc_node_cdata.rb +51 -49
  76. data/test/tc_node_comment.rb +33 -30
  77. data/test/tc_node_copy.rb +42 -40
  78. data/test/tc_node_edit.rb +159 -157
  79. data/test/tc_node_text.rb +71 -69
  80. data/test/tc_node_write.rb +41 -16
  81. data/test/tc_node_xlink.rb +29 -26
  82. data/test/tc_parser.rb +335 -329
  83. data/test/tc_parser_context.rb +188 -185
  84. data/test/tc_properties.rb +39 -36
  85. data/test/tc_reader.rb +297 -283
  86. data/test/tc_relaxng.rb +54 -51
  87. data/test/tc_sax_parser.rb +275 -273
  88. data/test/tc_schema.rb +53 -51
  89. data/test/tc_traversal.rb +222 -220
  90. data/test/tc_xinclude.rb +21 -19
  91. data/test/tc_xml.rb +3 -1
  92. data/test/tc_xpath.rb +195 -193
  93. data/test/tc_xpath_context.rb +80 -78
  94. data/test/tc_xpath_expression.rb +38 -35
  95. data/test/tc_xpointer.rb +74 -72
  96. data/test/test_helper.rb +14 -0
  97. data/test/test_suite.rb +39 -33
  98. metadata +65 -105
  99. data/doc/css/normal.css +0 -182
  100. data/doc/img/raze-tiny.png +0 -0
  101. data/doc/img/red-cube.jpg +0 -0
  102. data/doc/img/xml-ruby.png +0 -0
  103. data/doc/index.xml +0 -43
  104. data/doc/install.xml +0 -77
  105. data/doc/layout.rhtml +0 -38
  106. data/doc/layout.xsl +0 -67
  107. data/doc/license.xml +0 -32
  108. data/doc/log/changelog.xml +0 -1324
  109. data/doc/log/changelog.xsl +0 -42
  110. data/ext/libxml/Makefile +0 -156
  111. data/ext/libxml/extconf.h +0 -5
  112. data/ext/libxml/libxml-ruby.so.a +0 -0
  113. data/ext/libxml/libxml.o +0 -0
  114. data/ext/libxml/libxml_ruby.so +0 -0
  115. data/ext/libxml/mkmf.log +0 -129
  116. data/ext/libxml/ruby_xml.o +0 -0
  117. data/ext/libxml/ruby_xml_attr.o +0 -0
  118. data/ext/libxml/ruby_xml_attr_decl.o +0 -0
  119. data/ext/libxml/ruby_xml_attributes.o +0 -0
  120. data/ext/libxml/ruby_xml_cbg.o +0 -0
  121. data/ext/libxml/ruby_xml_document.o +0 -0
  122. data/ext/libxml/ruby_xml_dtd.o +0 -0
  123. data/ext/libxml/ruby_xml_encoding.o +0 -0
  124. data/ext/libxml/ruby_xml_error.o +0 -0
  125. data/ext/libxml/ruby_xml_html_parser.o +0 -0
  126. data/ext/libxml/ruby_xml_html_parser_context.o +0 -0
  127. data/ext/libxml/ruby_xml_html_parser_options.o +0 -0
  128. data/ext/libxml/ruby_xml_input_cbg.o +0 -0
  129. data/ext/libxml/ruby_xml_io.o +0 -0
  130. data/ext/libxml/ruby_xml_namespace.o +0 -0
  131. data/ext/libxml/ruby_xml_namespaces.o +0 -0
  132. data/ext/libxml/ruby_xml_node.o +0 -0
  133. data/ext/libxml/ruby_xml_parser.o +0 -0
  134. data/ext/libxml/ruby_xml_parser_context.o +0 -0
  135. data/ext/libxml/ruby_xml_parser_options.o +0 -0
  136. data/ext/libxml/ruby_xml_reader.o +0 -0
  137. data/ext/libxml/ruby_xml_relaxng.o +0 -0
  138. data/ext/libxml/ruby_xml_sax2_handler.o +0 -0
  139. data/ext/libxml/ruby_xml_sax_parser.o +0 -0
  140. data/ext/libxml/ruby_xml_schema.o +0 -0
  141. data/ext/libxml/ruby_xml_xinclude.o +0 -0
  142. data/ext/libxml/ruby_xml_xpath.o +0 -0
  143. data/ext/libxml/ruby_xml_xpath_context.o +0 -0
  144. data/ext/libxml/ruby_xml_xpath_expression.o +0 -0
  145. data/ext/libxml/ruby_xml_xpath_object.o +0 -0
  146. data/ext/libxml/ruby_xml_xpointer.o +0 -0
data/test/tc_xinclude.rb CHANGED
@@ -1,19 +1,21 @@
1
- require 'xml'
2
- require 'test/unit'
3
-
4
- class TestXInclude < Test::Unit::TestCase
5
- def setup
6
- @doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/xinclude.xml'))
7
- assert_instance_of(XML::Document, @doc)
8
- end
9
-
10
- def teardown
11
- @doc = nil
12
- end
13
-
14
- def test_ruby_xml_xinclude
15
- assert_equal(1, @doc.xinclude)
16
- assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<document xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n <p>This libxml2 binding has the following project information:\n <code>This is some text to include in an xml file via XInclude.</code></p>\n</document>\n",
17
- @doc.to_s)
18
- end
19
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class TestXInclude < Test::Unit::TestCase
7
+ def setup
8
+ @doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/xinclude.xml'))
9
+ assert_instance_of(XML::Document, @doc)
10
+ end
11
+
12
+ def teardown
13
+ @doc = nil
14
+ end
15
+
16
+ def test_ruby_xml_xinclude
17
+ assert_equal(1, @doc.xinclude)
18
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<document xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n <p>This libxml2 binding has the following project information:\n <code>This is some text to include in an xml file via XInclude.</code></p>\n</document>\n",
19
+ @doc.to_s)
20
+ end
21
+ end
data/test/tc_xml.rb CHANGED
@@ -1,4 +1,6 @@
1
- require 'xml'
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
2
4
  require 'test/unit'
3
5
  require 'stringio'
4
6
 
data/test/tc_xpath.rb CHANGED
@@ -1,193 +1,195 @@
1
- require 'xml'
2
- require 'tempfile'
3
- require 'test/unit'
4
-
5
- class TestXPath < Test::Unit::TestCase
6
- def setup
7
- @doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
8
- end
9
-
10
- def teardown
11
- @doc = nil
12
- end
13
-
14
- def test_doc_find
15
- nodes = @doc.find('/soap:Envelope')
16
- assert_instance_of(XML::XPath::Object, nodes)
17
- assert_equal(1, nodes.length)
18
- assert_equal(nodes.xpath_type, XML::XPath::NODESET)
19
- end
20
-
21
- def test_doc_find_first
22
- node = @doc.find_first('/soap:Envelope/soap:Body')
23
- assert_instance_of(XML::Node, node)
24
- end
25
-
26
- def test_ns
27
- nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
28
- assert_equal(3, nodes.length)
29
- end
30
-
31
- def test_ns_array
32
- nodes = @doc.find('//ns1:IdAndName', ['ns1:http://domain.somewhere.com'])
33
- assert_equal(3, nodes.length)
34
- end
35
-
36
- def test_default_ns1
37
- # Find all nodes with http://services.somewhere.com namespace
38
- nodes = @doc.find('//*[namespace-uri()="http://services.somewhere.com"]')
39
- assert_equal(2, nodes.length)
40
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
41
- assert_equal('IDAndNameList', nodes[1].name)
42
- end
43
-
44
- def test_default_ns2
45
- # Find all nodes with http://services.somewhere.com namespace
46
- nodes = @doc.find('//ns:*', 'ns:http://services.somewhere.com')
47
- assert_equal(2, nodes.length)
48
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
49
- assert_equal('IDAndNameList', nodes[1].name)
50
-
51
- # Get getManufacturerNamesResponse node
52
- nodes = @doc.find('//ns:getManufacturerNamesResponse', 'ns:http://services.somewhere.com')
53
- assert_equal(1, nodes.length)
54
-
55
- # Get IdAndName node
56
- nodes = @doc.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse/ns0:IDAndNameList/ns1:IdAndName',
57
- ['ns0:http://services.somewhere.com', 'ns1:http://domain.somewhere.com'])
58
- assert_equal(3, nodes.length)
59
- end
60
-
61
- def test_default_ns3
62
- # Find all nodes with http://services.somewhere.com namespace
63
- nodes = @doc.find('//ns:*', 'ns' => 'http://services.somewhere.com')
64
- assert_equal(2, nodes.length)
65
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
66
- assert_equal('IDAndNameList', nodes[1].name)
67
- end
68
-
69
- def test_default_ns4
70
- # Find all nodes with http://services.somewhere.com namespace
71
- nodes = @doc.find('//ns:*', :ns => 'http://services.somewhere.com')
72
- assert_equal(2, nodes.length)
73
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
74
- assert_equal('IDAndNameList', nodes[1].name)
75
- end
76
-
77
- def test_default_ns5
78
- # Find all nodes with http://services.somewhere.com namespace
79
- XML::Namespace.new(@doc.root, 'ns', 'http://services.somewhere.com')
80
- nodes = @doc.find('//ns:*')
81
- assert_equal(2, nodes.length)
82
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
83
- assert_equal('IDAndNameList', nodes[1].name)
84
- end
85
-
86
- def test_attribute_ns
87
- # Pull all nodes with http://services.somewhere.com namespace
88
- nodes = @doc.find('@soap:encodingStyle')
89
- assert_equal(1, nodes.length)
90
- assert_equal('encodingStyle', nodes.first.name)
91
- assert_equal('http://www.w3.org/2001/12/soap-encoding', nodes.first.value)
92
- end
93
-
94
- def test_register_default_ns
95
- doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/atom.xml'))
96
-
97
- # No namespace has been yet defined
98
- assert_raise(XML::Error) do
99
- node = doc.find("atom:title")
100
- end
101
-
102
- node = doc.find('atom:title', 'atom:http://www.w3.org/2005/Atom')
103
- assert_not_nil(node)
104
-
105
- # Register namespace
106
- doc.root.namespaces.default_prefix = 'atom'
107
- node = doc.find("atom:title")
108
- assert_not_nil(node)
109
- end
110
-
111
- def test_node_find
112
- nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
113
- node = nodes.first
114
-
115
- # Since we are searching on the node, don't have to register namespace
116
- nodes = node.find('ns1:name')
117
- assert_equal(1, nodes.length)
118
- assert_equal(nodes.first.object_id, nodes.last.object_id, 'First and last should be the same')
119
- assert_equal('name', nodes.first.name)
120
- assert_equal('man1', nodes.first.content)
121
- end
122
-
123
- def test_node_find_first
124
- node = @doc.find_first('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
125
-
126
- # Since we are searching on the node, don't have to register namespace
127
- node = node.find_first('ns1:name')
128
- assert_equal('name', node.name)
129
- assert_equal('man1', node.content)
130
- end
131
-
132
- def test_node_no_doc
133
- node = XML::Node.new('header', 'some content')
134
- assert_raise(TypeError) do
135
- node = node.find_first('/header')
136
- end
137
- end
138
-
139
- def test_memory
140
- # This sometimes causes a segmentation fault because
141
- # an xml document is sometimes freed before the
142
- # xpath_object used to query it. When the xpath_object
143
- # is free, it iterates over its results which are pointers
144
- # to the document's nodes. A segmentation fault then happens.
145
-
146
- 1000.times do
147
- doc = XML::Document.new('1.0')
148
- doc.root = XML::Node.new("header")
149
-
150
- 1000.times do
151
- doc.root << XML::Node.new("footer")
152
- end
153
-
154
- nodes = doc.find('/header/footer')
155
- end
156
- end
157
-
158
- # Test that document doesn't get freed before nodes
159
- def test_xpath_free
160
- doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
161
- nodes = doc.find('//*')
162
- GC.start
163
- assert_equal('Envelope', nodes.first.name)
164
- end
165
-
166
- def test_xpath_namespace_nodes
167
- doc = XML::Document.string('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml"><entry/></feed>')
168
- nodes = doc.find('//atom:entry|namespace::*', :atom => "http://www.w3.org/2005/Atom")
169
- assert_equal(4, nodes.length)
170
-
171
- node = nodes[0]
172
- assert_equal(XML::Node::ELEMENT_NODE, node.node_type)
173
-
174
- node = nodes[1]
175
- assert_equal(XML::Node::NAMESPACE_DECL, node.node_type)
176
-
177
- node = nodes[2]
178
- assert_equal(XML::Node::NAMESPACE_DECL, node.node_type)
179
-
180
- node = nodes[3]
181
- assert_equal(XML::Node::NAMESPACE_DECL, node.node_type)
182
- end
183
-
184
- # Test to make sure we don't get nil on empty results.
185
- # This is also to test that we don't segfault due to our C code getting a NULL pointer
186
- # and not handling it properly.
187
- def test_xpath_empty_result
188
- doc = XML::Document.string('<html><body><p>Welcome to XHTML land!</p></body></html>')
189
- nodes = doc.find("//object/param[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'wmode']")
190
- assert_not_nil nodes
191
- end
192
-
193
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'tempfile'
5
+ require 'test/unit'
6
+
7
+ class TestXPath < Test::Unit::TestCase
8
+ def setup
9
+ @doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
10
+ end
11
+
12
+ def teardown
13
+ @doc = nil
14
+ end
15
+
16
+ def test_doc_find
17
+ nodes = @doc.find('/soap:Envelope')
18
+ assert_instance_of(XML::XPath::Object, nodes)
19
+ assert_equal(1, nodes.length)
20
+ assert_equal(nodes.xpath_type, XML::XPath::NODESET)
21
+ end
22
+
23
+ def test_doc_find_first
24
+ node = @doc.find_first('/soap:Envelope/soap:Body')
25
+ assert_instance_of(XML::Node, node)
26
+ end
27
+
28
+ def test_ns
29
+ nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
30
+ assert_equal(3, nodes.length)
31
+ end
32
+
33
+ def test_ns_array
34
+ nodes = @doc.find('//ns1:IdAndName', ['ns1:http://domain.somewhere.com'])
35
+ assert_equal(3, nodes.length)
36
+ end
37
+
38
+ def test_default_ns1
39
+ # Find all nodes with http://services.somewhere.com namespace
40
+ nodes = @doc.find('//*[namespace-uri()="http://services.somewhere.com"]')
41
+ assert_equal(2, nodes.length)
42
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
43
+ assert_equal('IDAndNameList', nodes[1].name)
44
+ end
45
+
46
+ def test_default_ns2
47
+ # Find all nodes with http://services.somewhere.com namespace
48
+ nodes = @doc.find('//ns:*', 'ns:http://services.somewhere.com')
49
+ assert_equal(2, nodes.length)
50
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
51
+ assert_equal('IDAndNameList', nodes[1].name)
52
+
53
+ # Get getManufacturerNamesResponse node
54
+ nodes = @doc.find('//ns:getManufacturerNamesResponse', 'ns:http://services.somewhere.com')
55
+ assert_equal(1, nodes.length)
56
+
57
+ # Get IdAndName node
58
+ nodes = @doc.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse/ns0:IDAndNameList/ns1:IdAndName',
59
+ ['ns0:http://services.somewhere.com', 'ns1:http://domain.somewhere.com'])
60
+ assert_equal(3, nodes.length)
61
+ end
62
+
63
+ def test_default_ns3
64
+ # Find all nodes with http://services.somewhere.com namespace
65
+ nodes = @doc.find('//ns:*', 'ns' => 'http://services.somewhere.com')
66
+ assert_equal(2, nodes.length)
67
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
68
+ assert_equal('IDAndNameList', nodes[1].name)
69
+ end
70
+
71
+ def test_default_ns4
72
+ # Find all nodes with http://services.somewhere.com namespace
73
+ nodes = @doc.find('//ns:*', :ns => 'http://services.somewhere.com')
74
+ assert_equal(2, nodes.length)
75
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
76
+ assert_equal('IDAndNameList', nodes[1].name)
77
+ end
78
+
79
+ def test_default_ns5
80
+ # Find all nodes with http://services.somewhere.com namespace
81
+ XML::Namespace.new(@doc.root, 'ns', 'http://services.somewhere.com')
82
+ nodes = @doc.find('//ns:*')
83
+ assert_equal(2, nodes.length)
84
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
85
+ assert_equal('IDAndNameList', nodes[1].name)
86
+ end
87
+
88
+ def test_attribute_ns
89
+ # Pull all nodes with http://services.somewhere.com namespace
90
+ nodes = @doc.find('@soap:encodingStyle')
91
+ assert_equal(1, nodes.length)
92
+ assert_equal('encodingStyle', nodes.first.name)
93
+ assert_equal('http://www.w3.org/2001/12/soap-encoding', nodes.first.value)
94
+ end
95
+
96
+ def test_register_default_ns
97
+ doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/atom.xml'))
98
+
99
+ # No namespace has been yet defined
100
+ assert_raise(XML::Error) do
101
+ node = doc.find("atom:title")
102
+ end
103
+
104
+ node = doc.find('atom:title', 'atom:http://www.w3.org/2005/Atom')
105
+ assert_not_nil(node)
106
+
107
+ # Register namespace
108
+ doc.root.namespaces.default_prefix = 'atom'
109
+ node = doc.find("atom:title")
110
+ assert_not_nil(node)
111
+ end
112
+
113
+ def test_node_find
114
+ nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
115
+ node = nodes.first
116
+
117
+ # Since we are searching on the node, don't have to register namespace
118
+ nodes = node.find('ns1:name')
119
+ assert_equal(1, nodes.length)
120
+ assert_equal(nodes.first.object_id, nodes.last.object_id, 'First and last should be the same')
121
+ assert_equal('name', nodes.first.name)
122
+ assert_equal('man1', nodes.first.content)
123
+ end
124
+
125
+ def test_node_find_first
126
+ node = @doc.find_first('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
127
+
128
+ # Since we are searching on the node, don't have to register namespace
129
+ node = node.find_first('ns1:name')
130
+ assert_equal('name', node.name)
131
+ assert_equal('man1', node.content)
132
+ end
133
+
134
+ def test_node_no_doc
135
+ node = XML::Node.new('header', 'some content')
136
+ assert_raise(TypeError) do
137
+ node = node.find_first('/header')
138
+ end
139
+ end
140
+
141
+ def test_memory
142
+ # This sometimes causes a segmentation fault because
143
+ # an xml document is sometimes freed before the
144
+ # xpath_object used to query it. When the xpath_object
145
+ # is free, it iterates over its results which are pointers
146
+ # to the document's nodes. A segmentation fault then happens.
147
+
148
+ 1000.times do
149
+ doc = XML::Document.new('1.0')
150
+ doc.root = XML::Node.new("header")
151
+
152
+ 1000.times do
153
+ doc.root << XML::Node.new("footer")
154
+ end
155
+
156
+ nodes = doc.find('/header/footer')
157
+ end
158
+ end
159
+
160
+ # Test that document doesn't get freed before nodes
161
+ def test_xpath_free
162
+ doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
163
+ nodes = doc.find('//*')
164
+ GC.start
165
+ assert_equal('Envelope', nodes.first.name)
166
+ end
167
+
168
+ def test_xpath_namespace_nodes
169
+ doc = XML::Document.string('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml"><entry/></feed>')
170
+ nodes = doc.find('//atom:entry|namespace::*', :atom => "http://www.w3.org/2005/Atom")
171
+ assert_equal(4, nodes.length)
172
+
173
+ node = nodes[0]
174
+ assert_equal(XML::Node::ELEMENT_NODE, node.node_type)
175
+
176
+ node = nodes[1]
177
+ assert_equal(XML::Node::NAMESPACE_DECL, node.node_type)
178
+
179
+ node = nodes[2]
180
+ assert_equal(XML::Node::NAMESPACE_DECL, node.node_type)
181
+
182
+ node = nodes[3]
183
+ assert_equal(XML::Node::NAMESPACE_DECL, node.node_type)
184
+ end
185
+
186
+ # Test to make sure we don't get nil on empty results.
187
+ # This is also to test that we don't segfault due to our C code getting a NULL pointer
188
+ # and not handling it properly.
189
+ def test_xpath_empty_result
190
+ doc = XML::Document.string('<html><body><p>Welcome to XHTML land!</p></body></html>')
191
+ nodes = doc.find("//object/param[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'wmode']")
192
+ assert_not_nil nodes
193
+ end
194
+
195
+ end
@@ -1,78 +1,80 @@
1
- require 'xml'
2
- require "tempfile"
3
- require "test/unit"
4
-
5
- class TestXPathContext < Test::Unit::TestCase
6
- SOAP_PREFIX = 'soap'
7
- SOAP_URI = 'http://schemas.xmlsoap.org/soap/envelope/'
8
-
9
- NS0_PREFIX = 'ns0'
10
- NS0_URI = 'http://services.somewhere.com'
11
-
12
- def setup
13
- doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
14
- @context = XML::XPath::Context.new(doc)
15
- end
16
-
17
- def teardown()
18
- @context = nil
19
- end
20
-
21
- #def test_no_ns
22
- #assert_raise(XML::XPath::InvalidPath) do
23
- #@context.find('/soap:Envelope')
24
- #end
25
- #end
26
-
27
- def test_ns_register
28
- @context.register_namespace(SOAP_PREFIX, SOAP_URI)
29
- @context.register_namespace(NS0_PREFIX, NS0_URI)
30
- nodes = @context.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse')
31
- assert_equal(1, nodes.length)
32
- end
33
-
34
- def test_ns_register_string
35
- @context.register_namespaces("#{SOAP_PREFIX}:#{SOAP_URI}")
36
- @context.register_namespaces("#{NS0_PREFIX}:#{NS0_URI}")
37
- nodes = @context.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse')
38
- assert_equal(1, nodes.length)
39
- end
40
-
41
- def test_ns_register_array
42
- @context.register_namespaces(["#{SOAP_PREFIX}:#{SOAP_URI}", "#{NS0_PREFIX}:#{NS0_URI}"])
43
- nodes = @context.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse')
44
- assert_equal(1, nodes.length)
45
- end
46
-
47
- def test_ns_register_hash
48
- @context.register_namespaces(SOAP_PREFIX => SOAP_URI,
49
- NS0_PREFIX => NS0_URI)
50
-
51
- nodes = @context.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse')
52
- assert_equal(1, nodes.length)
53
- end
54
-
55
- def test_ns_register_node
56
- @context.register_namespaces_from_node(@context.doc.root)
57
- nodes = @context.find('/soap:Envelope')
58
- assert_equal(1, nodes.length)
59
- end
60
-
61
- def test_node
62
- @context.register_namespaces_from_node(@context.doc.root)
63
-
64
- nodes = @context.find('soap:Body')
65
- assert_equal(0, nodes.length)
66
-
67
-
68
- @context.node = @context.doc.root.child.next
69
- nodes = @context.find('soap:Body')
70
- assert_equal(0, nodes.length)
71
- end
72
-
73
- def test_cache
74
- @context.enable_cache
75
- @context.enable_cache(10)
76
- @context.disable_cache
77
- end
78
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require "tempfile"
5
+ require "test/unit"
6
+
7
+ class TestXPathContext < Test::Unit::TestCase
8
+ SOAP_PREFIX = 'soap'
9
+ SOAP_URI = 'http://schemas.xmlsoap.org/soap/envelope/'
10
+
11
+ NS0_PREFIX = 'ns0'
12
+ NS0_URI = 'http://services.somewhere.com'
13
+
14
+ def setup
15
+ doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
16
+ @context = XML::XPath::Context.new(doc)
17
+ end
18
+
19
+ def teardown()
20
+ @context = nil
21
+ end
22
+
23
+ #def test_no_ns
24
+ #assert_raise(XML::XPath::InvalidPath) do
25
+ #@context.find('/soap:Envelope')
26
+ #end
27
+ #end
28
+
29
+ def test_ns_register
30
+ @context.register_namespace(SOAP_PREFIX, SOAP_URI)
31
+ @context.register_namespace(NS0_PREFIX, NS0_URI)
32
+ nodes = @context.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse')
33
+ assert_equal(1, nodes.length)
34
+ end
35
+
36
+ def test_ns_register_string
37
+ @context.register_namespaces("#{SOAP_PREFIX}:#{SOAP_URI}")
38
+ @context.register_namespaces("#{NS0_PREFIX}:#{NS0_URI}")
39
+ nodes = @context.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse')
40
+ assert_equal(1, nodes.length)
41
+ end
42
+
43
+ def test_ns_register_array
44
+ @context.register_namespaces(["#{SOAP_PREFIX}:#{SOAP_URI}", "#{NS0_PREFIX}:#{NS0_URI}"])
45
+ nodes = @context.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse')
46
+ assert_equal(1, nodes.length)
47
+ end
48
+
49
+ def test_ns_register_hash
50
+ @context.register_namespaces(SOAP_PREFIX => SOAP_URI,
51
+ NS0_PREFIX => NS0_URI)
52
+
53
+ nodes = @context.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse')
54
+ assert_equal(1, nodes.length)
55
+ end
56
+
57
+ def test_ns_register_node
58
+ @context.register_namespaces_from_node(@context.doc.root)
59
+ nodes = @context.find('/soap:Envelope')
60
+ assert_equal(1, nodes.length)
61
+ end
62
+
63
+ def test_node
64
+ @context.register_namespaces_from_node(@context.doc.root)
65
+
66
+ nodes = @context.find('soap:Body')
67
+ assert_equal(0, nodes.length)
68
+
69
+
70
+ @context.node = @context.doc.root.child.next
71
+ nodes = @context.find('soap:Body')
72
+ assert_equal(0, nodes.length)
73
+ end
74
+
75
+ def test_cache
76
+ @context.enable_cache
77
+ @context.enable_cache(10)
78
+ @context.disable_cache
79
+ end
80
+ end