libxml-ruby 5.0.6 → 6.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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/{HISTORY → CHANGELOG.md} +875 -930
  3. data/LICENSE +20 -20
  4. data/README.md +67 -0
  5. data/Rakefile +13 -34
  6. data/ext/libxml/extconf.rb +34 -20
  7. data/ext/libxml/libxml.c +2 -14
  8. data/ext/libxml/ruby_libxml.h +1 -1
  9. data/ext/libxml/ruby_xml_attr.c +27 -22
  10. data/ext/libxml/ruby_xml_attr.h +1 -0
  11. data/ext/libxml/ruby_xml_attr_decl.c +19 -12
  12. data/ext/libxml/ruby_xml_attributes.c +18 -9
  13. data/ext/libxml/ruby_xml_document.c +111 -98
  14. data/ext/libxml/ruby_xml_document.h +1 -0
  15. data/ext/libxml/ruby_xml_dtd.c +49 -19
  16. data/ext/libxml/ruby_xml_dtd.h +1 -0
  17. data/ext/libxml/ruby_xml_html_parser.c +1 -1
  18. data/ext/libxml/ruby_xml_html_parser_context.c +19 -6
  19. data/ext/libxml/ruby_xml_html_parser_context.h +1 -0
  20. data/ext/libxml/ruby_xml_input_cbg.c +45 -17
  21. data/ext/libxml/ruby_xml_io.c +3 -1
  22. data/ext/libxml/ruby_xml_namespace.c +20 -12
  23. data/ext/libxml/ruby_xml_namespace.h +1 -0
  24. data/ext/libxml/ruby_xml_namespaces.c +40 -20
  25. data/ext/libxml/ruby_xml_node.c +71 -32
  26. data/ext/libxml/ruby_xml_node.h +2 -0
  27. data/ext/libxml/ruby_xml_parser.c +1 -1
  28. data/ext/libxml/ruby_xml_parser_context.c +54 -47
  29. data/ext/libxml/ruby_xml_parser_context.h +1 -0
  30. data/ext/libxml/ruby_xml_reader.c +25 -15
  31. data/ext/libxml/ruby_xml_registry.c +31 -0
  32. data/ext/libxml/ruby_xml_registry.h +22 -0
  33. data/ext/libxml/ruby_xml_relaxng.c +21 -5
  34. data/ext/libxml/ruby_xml_relaxng.h +1 -0
  35. data/ext/libxml/ruby_xml_sax_parser.c +1 -1
  36. data/ext/libxml/ruby_xml_schema.c +18 -11
  37. data/ext/libxml/ruby_xml_schema.h +1 -0
  38. data/ext/libxml/ruby_xml_schema_attribute.c +7 -7
  39. data/ext/libxml/ruby_xml_schema_element.c +8 -8
  40. data/ext/libxml/ruby_xml_schema_facet.c +7 -7
  41. data/ext/libxml/ruby_xml_schema_type.c +12 -19
  42. data/ext/libxml/ruby_xml_version.h +4 -4
  43. data/ext/libxml/ruby_xml_writer.c +18 -6
  44. data/ext/libxml/ruby_xml_xpath.c +2 -2
  45. data/ext/libxml/ruby_xml_xpath.h +1 -1
  46. data/ext/libxml/ruby_xml_xpath_context.c +72 -26
  47. data/ext/libxml/ruby_xml_xpath_expression.c +11 -5
  48. data/ext/libxml/ruby_xml_xpath_expression.h +1 -0
  49. data/ext/libxml/ruby_xml_xpath_object.c +69 -54
  50. data/ext/libxml/ruby_xml_xpath_object.h +3 -1
  51. data/ext/vc/libxml_ruby/libxml_ruby.vcxproj +271 -0
  52. data/ext/xcode/libxml-ruby.xcodeproj/project.pbxproj +633 -0
  53. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  54. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  55. data/ext/xcode/libxml-ruby.xcodeproj/xcshareddata/xcschemes/libxml-ruby.xcscheme +80 -0
  56. data/lib/libxml/document.rb +0 -29
  57. data/lib/libxml/error.rb +30 -3
  58. data/lib/libxml/html_parser.rb +0 -16
  59. data/lib/libxml/node.rb +1 -3
  60. data/lib/libxml/parser.rb +0 -8
  61. data/lib/libxml/schema/attribute.rb +27 -19
  62. data/lib/libxml/schema/element.rb +20 -0
  63. data/lib/libxml/schema/type.rb +44 -21
  64. data/lib/libxml/schema.rb +47 -47
  65. data/lib/libxml-ruby.rb +30 -30
  66. data/libxml-ruby.gemspec +12 -16
  67. data/test/c14n/result/1-1-without-comments/example-1 +3 -3
  68. data/test/c14n/result/1-1-without-comments/example-2 +10 -10
  69. data/test/c14n/result/1-1-without-comments/example-3 +13 -13
  70. data/test/c14n/result/1-1-without-comments/example-4 +8 -8
  71. data/test/c14n/result/1-1-without-comments/example-5 +2 -2
  72. data/test/c14n/result/with-comments/example-1 +5 -5
  73. data/test/c14n/result/with-comments/example-2 +10 -10
  74. data/test/c14n/result/with-comments/example-3 +13 -13
  75. data/test/c14n/result/with-comments/example-4 +8 -8
  76. data/test/c14n/result/with-comments/example-5 +3 -3
  77. data/test/c14n/result/without-comments/example-1 +3 -3
  78. data/test/c14n/result/without-comments/example-2 +10 -10
  79. data/test/c14n/result/without-comments/example-3 +13 -13
  80. data/test/c14n/result/without-comments/example-4 +8 -8
  81. data/test/c14n/result/without-comments/example-5 +2 -2
  82. data/test/test_attr.rb +179 -180
  83. data/test/test_attr_decl.rb +131 -131
  84. data/test/test_attributes.rb +135 -135
  85. data/test/test_canonicalize.rb +122 -120
  86. data/test/test_document.rb +2 -4
  87. data/test/test_dtd.rb +12 -4
  88. data/test/test_encoding_sax.rb +114 -114
  89. data/test/test_error.rb +1 -1
  90. data/test/test_html_parser.rb +6 -2
  91. data/test/test_html_parser_context.rb +22 -22
  92. data/test/test_input_callbacks.rb +36 -0
  93. data/test/test_namespace.rb +1 -1
  94. data/test/test_namespaces.rb +200 -200
  95. data/test/test_node.rb +16 -0
  96. data/test/test_node_cdata.rb +50 -50
  97. data/test/test_node_comment.rb +32 -32
  98. data/test/test_node_copy.rb +40 -40
  99. data/test/test_node_edit.rb +176 -158
  100. data/test/test_node_pi.rb +37 -37
  101. data/test/test_node_text.rb +69 -69
  102. data/test/test_node_xlink.rb +28 -28
  103. data/test/test_parser.rb +5 -41
  104. data/test/test_parser_context.rb +198 -198
  105. data/test/test_properties.rb +38 -38
  106. data/test/test_reader.rb +55 -6
  107. data/test/test_relaxng.rb +59 -53
  108. data/test/test_sax_parser.rb +345 -345
  109. data/test/test_schema.rb +28 -0
  110. data/test/test_traversal.rb +152 -152
  111. data/test/test_writer.rb +0 -31
  112. data/test/test_xinclude.rb +20 -20
  113. data/test/test_xml.rb +3 -7
  114. data/test/test_xpath.rb +244 -244
  115. data/test/test_xpath_context.rb +87 -87
  116. data/test/test_xpath_expression.rb +37 -37
  117. metadata +32 -25
  118. data/README.rdoc +0 -208
  119. data/ext/libxml/extconf.h +0 -4
  120. data/ext/libxml/ruby_xml_cbg.c +0 -85
  121. data/lib/libxml/hpricot.rb +0 -78
  122. data/lib/libxml.rb +0 -5
  123. data/lib/xml/libxml.rb +0 -10
  124. data/lib/xml.rb +0 -14
  125. data/script/benchmark/depixelate +0 -634
  126. data/script/benchmark/hamlet.xml +0 -9055
  127. data/script/benchmark/parsecount +0 -170
  128. data/script/benchmark/sock_entries.xml +0 -507
  129. data/script/benchmark/throughput +0 -41
  130. data/script/test +0 -6
  131. data/test/test_deprecated_require.rb +0 -12
data/test/test_xpath.rb CHANGED
@@ -1,244 +1,244 @@
1
- # encoding: UTF-8
2
-
3
- require_relative './test_helper'
4
- require 'tempfile'
5
-
6
- class TestXPath < Minitest::Test
7
- def setup
8
- @doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
9
- end
10
-
11
- def teardown
12
- @doc = nil
13
- end
14
-
15
- def test_doc_find
16
- nodes = @doc.find('/soap:Envelope')
17
- assert_instance_of(LibXML::XML::XPath::Object, nodes)
18
- assert_equal(1, nodes.length)
19
- assert_equal(nodes.xpath_type, LibXML::XML::XPath::NODESET)
20
- end
21
-
22
- def test_doc_find_first
23
- node = @doc.find_first('/soap:Envelope/soap:Body')
24
- assert_instance_of(LibXML::XML::Node, node)
25
- end
26
-
27
- def test_ns
28
- nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
29
- assert_equal(3, nodes.length)
30
- end
31
-
32
- def test_ns_gc
33
- _stress = GC.stress
34
- GC.stress = true
35
-
36
- doc = LibXML::XML::Document.string('<foo xmlns="http://bar.com" />')
37
- node = doc.root
38
- # This line segfaults on prior versions of libxml-ruby
39
- node.find("namespace::*")
40
-
41
- GC.stress = _stress
42
- end
43
-
44
- def test_ns_array
45
- nodes = @doc.find('//ns1:IdAndName', ['ns1:http://domain.somewhere.com'])
46
- assert_equal(3, nodes.length)
47
- end
48
-
49
- def test_default_ns1
50
- # Find all nodes with http://services.somewhere.com namespace
51
- nodes = @doc.find('//*[namespace-uri()="http://services.somewhere.com"]')
52
- assert_equal(2, nodes.length)
53
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
54
- assert_equal('IDAndNameList', nodes[1].name)
55
- end
56
-
57
- def test_default_ns2
58
- # Find all nodes with http://services.somewhere.com namespace
59
- nodes = @doc.find('//ns:*', 'ns:http://services.somewhere.com')
60
- assert_equal(2, nodes.length)
61
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
62
- assert_equal('IDAndNameList', nodes[1].name)
63
-
64
- # Get getManufacturerNamesResponse node
65
- nodes = @doc.find('//ns:getManufacturerNamesResponse', 'ns:http://services.somewhere.com')
66
- assert_equal(1, nodes.length)
67
-
68
- # Get IdAndName node
69
- nodes = @doc.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse/ns0:IDAndNameList/ns1:IdAndName',
70
- ['ns0:http://services.somewhere.com', 'ns1:http://domain.somewhere.com'])
71
- assert_equal(3, nodes.length)
72
- end
73
-
74
- def test_default_ns3
75
- # Find all nodes with http://services.somewhere.com namespace
76
- nodes = @doc.find('//ns:*', 'ns' => 'http://services.somewhere.com')
77
- assert_equal(2, nodes.length)
78
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
79
- assert_equal('IDAndNameList', nodes[1].name)
80
- end
81
-
82
- def test_default_ns4
83
- # Find all nodes with http://services.somewhere.com namespace
84
- nodes = @doc.find('//ns:*', :ns => 'http://services.somewhere.com')
85
- assert_equal(2, nodes.length)
86
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
87
- assert_equal('IDAndNameList', nodes[1].name)
88
- end
89
-
90
- def test_default_ns5
91
- # Find all nodes with http://services.somewhere.com namespace
92
- LibXML::XML::Namespace.new(@doc.root, 'ns', 'http://services.somewhere.com')
93
- nodes = @doc.find('//ns:*')
94
- assert_equal(2, nodes.length)
95
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
96
- assert_equal('IDAndNameList', nodes[1].name)
97
- end
98
-
99
- def test_attribute_ns
100
- # Pull all nodes with http://services.somewhere.com namespace
101
- nodes = @doc.find('@soap:encodingStyle')
102
- assert_equal(1, nodes.length)
103
- assert_equal('encodingStyle', nodes.first.name)
104
- assert_equal('http://www.w3.org/2001/12/soap-encoding', nodes.first.value)
105
- end
106
-
107
- def test_register_default_ns
108
- doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/atom.xml'))
109
-
110
- # No namespace has been yet defined
111
- assert_raises(LibXML::XML::Error) do
112
- doc.find("atom:title")
113
- end
114
-
115
- node = doc.find('atom:title', 'atom:http://www.w3.org/2005/Atom')
116
- refute_nil(node)
117
-
118
- # Register namespace
119
- doc.root.namespaces.default_prefix = 'atom'
120
- node = doc.find("atom:title")
121
- refute_nil(node)
122
- end
123
-
124
- def test_node_find
125
- nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
126
- node = nodes.first
127
-
128
- # Since we are searching on the node, don't have to register namespace
129
- nodes = node.find('ns1:name')
130
- assert_equal(1, nodes.length)
131
- refute_equal(nodes.first.object_id, nodes.last.object_id)
132
- assert_equal('name', nodes.first.name)
133
- assert_equal('man1', nodes.first.content)
134
- end
135
-
136
- def test_node_find_first
137
- node = @doc.find_first('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
138
-
139
- # Since we are searching on the node, don't have to register namespace
140
- node = node.find_first('ns1:name')
141
- assert_equal('name', node.name)
142
- assert_equal('man1', node.content)
143
- end
144
-
145
- def test_node_no_doc
146
- node = LibXML::XML::Node.new('header', 'some content')
147
- assert_raises(TypeError) do
148
- node = node.find_first('/header')
149
- end
150
- end
151
-
152
- def test_memory
153
- # This sometimes causes a segmentation fault because
154
- # an xml document is sometimes freed before the
155
- # xpath_object used to query it. When the xpath_object
156
- # is free, it iterates over its results which are pointers
157
- # to the document's nodes. A segmentation fault then happens.
158
-
159
- 1000.times do
160
- doc = LibXML::XML::Document.new('1.0')
161
- doc.root = LibXML::XML::Node.new("header")
162
-
163
- 1000.times do
164
- doc.root << LibXML::XML::Node.new("footer")
165
- end
166
-
167
- doc.find('/header/footer')
168
- end
169
- end
170
-
171
- # Test that document doesn't get freed before nodes
172
- def test_xpath_free
173
- doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
174
- nodes = doc.find('//*')
175
- GC.start
176
- assert_equal('Envelope', nodes.first.name)
177
- end
178
-
179
- def test_xpath_namespace_nodes
180
- doc = LibXML::XML::Document.string('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml"><entry/></feed>')
181
- nodes = doc.find('//atom:entry|namespace::*', :atom => "http://www.w3.org/2005/Atom")
182
- assert_equal(4, nodes.length)
183
-
184
- node = nodes[0]
185
- assert_equal(LibXML::XML::Node::ELEMENT_NODE, node.node_type)
186
-
187
- node = nodes[1]
188
- assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
189
-
190
- node = nodes[2]
191
- assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
192
-
193
- node = nodes[3]
194
- assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
195
- end
196
-
197
- # Test to make sure we don't get nil on empty results.
198
- # This is also to test that we don't segfault due to our C code getting a NULL pointer
199
- # and not handling it properly.
200
- def test_xpath_empty_result
201
- doc = LibXML::XML::Document.string('<html><body><p>Welcome to XHTML land!</p></body></html>')
202
- nodes = doc.find("//object/param[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'wmode']")
203
- refute_nil nodes
204
- end
205
-
206
- def test_invalid_expression
207
- xml = LibXML::XML::Document.string('<a></a>')
208
-
209
- # Using the expression twice used to cause a Segmentation Fault
210
- error = assert_raises(LibXML::XML::Error) do
211
- xml.find('//a/')
212
- end
213
- assert_equal("Error: Invalid expression.", error.to_s)
214
-
215
- # Try again - this used to cause a Segmentation Fault
216
- error = assert_raises(LibXML::XML::Error) do
217
- xml.find('//a/')
218
- end
219
- assert_equal("Error: Invalid expression.", error.to_s)
220
- end
221
-
222
- def test_find_cdata
223
- doc = LibXML::XML::Document.string('<root>hi there <![CDATA[ mycdata ]]> bye!</root>')
224
-
225
- nodes = doc.find('/root/text()')
226
- assert_equal(3, nodes.length)
227
- assert_equal(nodes[0].node_type, LibXML::XML::Node::TEXT_NODE)
228
- assert_equal(nodes[0].content, 'hi there ')
229
-
230
- assert_equal(nodes[1].node_type, LibXML::XML::Node::CDATA_SECTION_NODE)
231
- assert_equal(nodes[1].content, ' mycdata ')
232
-
233
- assert_equal(nodes[2].node_type, LibXML::XML::Node::TEXT_NODE)
234
- assert_equal(nodes[2].content, ' bye!')
235
- end
236
-
237
- def test_find_comment
238
- doc = LibXML::XML::Document.string('<root>hi there <!-- my comment --> bye!</root>')
239
-
240
- nodes = doc.find('//comment()')
241
- assert_equal(1, nodes.length)
242
- assert_equal(nodes[0].content, ' my comment ')
243
- end
244
- end
1
+ # encoding: UTF-8
2
+
3
+ require_relative './test_helper'
4
+ require 'tempfile'
5
+
6
+ class TestXPath < Minitest::Test
7
+ def setup
8
+ @doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
9
+ end
10
+
11
+ def teardown
12
+ @doc = nil
13
+ end
14
+
15
+ def test_doc_find
16
+ nodes = @doc.find('/soap:Envelope')
17
+ assert_instance_of(LibXML::XML::XPath::Object, nodes)
18
+ assert_equal(1, nodes.length)
19
+ assert_equal(nodes.xpath_type, LibXML::XML::XPath::NODESET)
20
+ end
21
+
22
+ def test_doc_find_first
23
+ node = @doc.find_first('/soap:Envelope/soap:Body')
24
+ assert_instance_of(LibXML::XML::Node, node)
25
+ end
26
+
27
+ def test_ns
28
+ nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
29
+ assert_equal(3, nodes.length)
30
+ end
31
+
32
+ def test_ns_gc
33
+ _stress = GC.stress
34
+ GC.stress = true
35
+
36
+ doc = LibXML::XML::Document.string('<foo xmlns="http://bar.com" />')
37
+ node = doc.root
38
+ # This line segfaults on prior versions of libxml-ruby
39
+ node.find("namespace::*")
40
+
41
+ GC.stress = _stress
42
+ end
43
+
44
+ def test_ns_array
45
+ nodes = @doc.find('//ns1:IdAndName', ['ns1:http://domain.somewhere.com'])
46
+ assert_equal(3, nodes.length)
47
+ end
48
+
49
+ def test_default_ns1
50
+ # Find all nodes with http://services.somewhere.com namespace
51
+ nodes = @doc.find('//*[namespace-uri()="http://services.somewhere.com"]')
52
+ assert_equal(2, nodes.length)
53
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
54
+ assert_equal('IDAndNameList', nodes[1].name)
55
+ end
56
+
57
+ def test_default_ns2
58
+ # Find all nodes with http://services.somewhere.com namespace
59
+ nodes = @doc.find('//ns:*', 'ns:http://services.somewhere.com')
60
+ assert_equal(2, nodes.length)
61
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
62
+ assert_equal('IDAndNameList', nodes[1].name)
63
+
64
+ # Get getManufacturerNamesResponse node
65
+ nodes = @doc.find('//ns:getManufacturerNamesResponse', 'ns:http://services.somewhere.com')
66
+ assert_equal(1, nodes.length)
67
+
68
+ # Get IdAndName node
69
+ nodes = @doc.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse/ns0:IDAndNameList/ns1:IdAndName',
70
+ ['ns0:http://services.somewhere.com', 'ns1:http://domain.somewhere.com'])
71
+ assert_equal(3, nodes.length)
72
+ end
73
+
74
+ def test_default_ns3
75
+ # Find all nodes with http://services.somewhere.com namespace
76
+ nodes = @doc.find('//ns:*', 'ns' => 'http://services.somewhere.com')
77
+ assert_equal(2, nodes.length)
78
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
79
+ assert_equal('IDAndNameList', nodes[1].name)
80
+ end
81
+
82
+ def test_default_ns4
83
+ # Find all nodes with http://services.somewhere.com namespace
84
+ nodes = @doc.find('//ns:*', :ns => 'http://services.somewhere.com')
85
+ assert_equal(2, nodes.length)
86
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
87
+ assert_equal('IDAndNameList', nodes[1].name)
88
+ end
89
+
90
+ def test_default_ns5
91
+ # Find all nodes with http://services.somewhere.com namespace
92
+ LibXML::XML::Namespace.new(@doc.root, 'ns', 'http://services.somewhere.com')
93
+ nodes = @doc.find('//ns:*')
94
+ assert_equal(2, nodes.length)
95
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
96
+ assert_equal('IDAndNameList', nodes[1].name)
97
+ end
98
+
99
+ def test_attribute_ns
100
+ # Pull all nodes with http://services.somewhere.com namespace
101
+ nodes = @doc.find('@soap:encodingStyle')
102
+ assert_equal(1, nodes.length)
103
+ assert_equal('encodingStyle', nodes.first.name)
104
+ assert_equal('http://www.w3.org/2001/12/soap-encoding', nodes.first.value)
105
+ end
106
+
107
+ def test_register_default_ns
108
+ doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/atom.xml'))
109
+
110
+ # No namespace has been yet defined
111
+ assert_raises(LibXML::XML::Error) do
112
+ doc.find("atom:title")
113
+ end
114
+
115
+ node = doc.find('atom:title', 'atom:http://www.w3.org/2005/Atom')
116
+ refute_nil(node)
117
+
118
+ # Register namespace
119
+ doc.root.namespaces.default_prefix = 'atom'
120
+ node = doc.find("atom:title")
121
+ refute_nil(node)
122
+ end
123
+
124
+ def test_node_find
125
+ nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
126
+ node = nodes.first
127
+
128
+ # Since we are searching on the node, don't have to register namespace
129
+ nodes = node.find('ns1:name')
130
+ assert_equal(1, nodes.length)
131
+ refute_equal(nodes.first.object_id, nodes.last.object_id)
132
+ assert_equal('name', nodes.first.name)
133
+ assert_equal('man1', nodes.first.content)
134
+ end
135
+
136
+ def test_node_find_first
137
+ node = @doc.find_first('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
138
+
139
+ # Since we are searching on the node, don't have to register namespace
140
+ node = node.find_first('ns1:name')
141
+ assert_equal('name', node.name)
142
+ assert_equal('man1', node.content)
143
+ end
144
+
145
+ def test_node_no_doc
146
+ node = LibXML::XML::Node.new('header', 'some content')
147
+ assert_raises(TypeError) do
148
+ node = node.find_first('/header')
149
+ end
150
+ end
151
+
152
+ def test_memory
153
+ # This sometimes causes a segmentation fault because
154
+ # an xml document is sometimes freed before the
155
+ # xpath_object used to query it. When the xpath_object
156
+ # is free, it iterates over its results which are pointers
157
+ # to the document's nodes. A segmentation fault then happens.
158
+
159
+ 1000.times do
160
+ doc = LibXML::XML::Document.new('1.0')
161
+ doc.root = LibXML::XML::Node.new("header")
162
+
163
+ 1000.times do
164
+ doc.root << LibXML::XML::Node.new("footer")
165
+ end
166
+
167
+ doc.find('/header/footer')
168
+ end
169
+ end
170
+
171
+ # Test that document doesn't get freed before nodes
172
+ def test_xpath_free
173
+ doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
174
+ nodes = doc.find('//*')
175
+ GC.start
176
+ assert_equal('Envelope', nodes.first.name)
177
+ end
178
+
179
+ def test_xpath_namespace_nodes
180
+ doc = LibXML::XML::Document.string('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml"><entry/></feed>')
181
+ nodes = doc.find('//atom:entry|namespace::*', :atom => "http://www.w3.org/2005/Atom")
182
+ assert_equal(4, nodes.length)
183
+
184
+ node = nodes[0]
185
+ assert_equal(LibXML::XML::Node::ELEMENT_NODE, node.node_type)
186
+
187
+ node = nodes[1]
188
+ assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
189
+
190
+ node = nodes[2]
191
+ assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
192
+
193
+ node = nodes[3]
194
+ assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
195
+ end
196
+
197
+ # Test to make sure we don't get nil on empty results.
198
+ # This is also to test that we don't segfault due to our C code getting a NULL pointer
199
+ # and not handling it properly.
200
+ def test_xpath_empty_result
201
+ doc = LibXML::XML::Document.string('<html><body><p>Welcome to XHTML land!</p></body></html>')
202
+ nodes = doc.find("//object/param[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'wmode']")
203
+ refute_nil nodes
204
+ end
205
+
206
+ def test_invalid_expression
207
+ xml = LibXML::XML::Document.string('<a></a>')
208
+
209
+ # Using the expression twice used to cause a Segmentation Fault
210
+ error = assert_raises(LibXML::XML::Error) do
211
+ xml.find('//a/')
212
+ end
213
+ assert_equal("Error: Invalid expression.", error.to_s)
214
+
215
+ # Try again - this used to cause a Segmentation Fault
216
+ error = assert_raises(LibXML::XML::Error) do
217
+ xml.find('//a/')
218
+ end
219
+ assert_equal("Error: Invalid expression.", error.to_s)
220
+ end
221
+
222
+ def test_find_cdata
223
+ doc = LibXML::XML::Document.string('<root>hi there <![CDATA[ mycdata ]]> bye!</root>')
224
+
225
+ nodes = doc.find('/root/text()')
226
+ assert_equal(3, nodes.length)
227
+ assert_equal(nodes[0].node_type, LibXML::XML::Node::TEXT_NODE)
228
+ assert_equal(nodes[0].content, 'hi there ')
229
+
230
+ assert_equal(nodes[1].node_type, LibXML::XML::Node::CDATA_SECTION_NODE)
231
+ assert_equal(nodes[1].content, ' mycdata ')
232
+
233
+ assert_equal(nodes[2].node_type, LibXML::XML::Node::TEXT_NODE)
234
+ assert_equal(nodes[2].content, ' bye!')
235
+ end
236
+
237
+ def test_find_comment
238
+ doc = LibXML::XML::Document.string('<root>hi there <!-- my comment --> bye!</root>')
239
+
240
+ nodes = doc.find('//comment()')
241
+ assert_equal(1, nodes.length)
242
+ assert_equal(nodes[0].content, ' my comment ')
243
+ end
244
+ end
@@ -1,88 +1,88 @@
1
- # encoding: UTF-8
2
-
3
- require_relative './test_helper'
4
- require "tempfile"
5
-
6
- class TestXPathContext < Minitest::Test
7
- SOAP_PREFIX = 'soap'
8
- SOAP_URI = 'http://schemas.xmlsoap.org/soap/envelope/'
9
-
10
- NS0_PREFIX = 'ns0'
11
- NS0_URI = 'http://services.somewhere.com'
12
-
13
- def setup
14
- doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
15
- @context = LibXML::XML::XPath::Context.new(doc)
16
- end
17
-
18
- def teardown()
19
- @context = nil
20
- end
21
-
22
- def test_no_ns
23
- error = assert_raises(LibXML::XML::Error) do
24
- @context.find('/soap:Envelope')
25
- end
26
- assert_equal("Error: Undefined namespace prefix.", error.to_s)
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
-
81
- def test_require_doc
82
- doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
83
- error = assert_raises(TypeError) do
84
- @context = LibXML::XML::XPath::Context.new(doc.root)
85
- end
86
- assert_equal("Supplied argument must be a document or node.", error.to_s)
87
- end
1
+ # encoding: UTF-8
2
+
3
+ require_relative './test_helper'
4
+ require "tempfile"
5
+
6
+ class TestXPathContext < Minitest::Test
7
+ SOAP_PREFIX = 'soap'
8
+ SOAP_URI = 'http://schemas.xmlsoap.org/soap/envelope/'
9
+
10
+ NS0_PREFIX = 'ns0'
11
+ NS0_URI = 'http://services.somewhere.com'
12
+
13
+ def setup
14
+ doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
15
+ @context = LibXML::XML::XPath::Context.new(doc)
16
+ end
17
+
18
+ def teardown()
19
+ @context = nil
20
+ end
21
+
22
+ def test_no_ns
23
+ error = assert_raises(LibXML::XML::Error) do
24
+ @context.find('/soap:Envelope')
25
+ end
26
+ assert_match(/Error: Undefined namespace prefix/, error.to_s)
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
+
81
+ def test_require_doc
82
+ doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
83
+ error = assert_raises(TypeError) do
84
+ @context = LibXML::XML::XPath::Context.new(doc.root)
85
+ end
86
+ assert_equal("Supplied argument must be a document or node.", error.to_s)
87
+ end
88
88
  end