nokogiri 1.8.5 → 1.9.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -1
  3. data/ext/nokogiri/extconf.rb +10 -6
  4. data/ext/nokogiri/xml_attr.c +26 -21
  5. data/ext/nokogiri/xml_document.c +4 -1
  6. data/ext/nokogiri/xml_namespace.c +3 -3
  7. data/ext/nokogiri/xml_namespace.h +1 -2
  8. data/ext/nokogiri/xml_node.c +55 -15
  9. data/lib/nokogiri/css/parser.rb +61 -61
  10. data/lib/nokogiri/version.rb +1 -1
  11. data/lib/nokogiri/xml/document_fragment.rb +11 -0
  12. data/lib/nokogiri/xml/node.rb +12 -0
  13. data/lib/nokogiri/xml/node_set.rb +4 -8
  14. metadata +17 -153
  15. data/.autotest +0 -22
  16. data/.cross_rubies +0 -8
  17. data/.editorconfig +0 -17
  18. data/.gemtest +0 -0
  19. data/.travis.yml +0 -63
  20. data/CHANGELOG.md +0 -1368
  21. data/CONTRIBUTING.md +0 -42
  22. data/C_CODING_STYLE.rdoc +0 -33
  23. data/Gemfile +0 -23
  24. data/Gemfile-libxml-ruby +0 -3
  25. data/Manifest.txt +0 -370
  26. data/ROADMAP.md +0 -111
  27. data/Rakefile +0 -348
  28. data/SECURITY.md +0 -19
  29. data/STANDARD_RESPONSES.md +0 -47
  30. data/Y_U_NO_GEMSPEC.md +0 -155
  31. data/appveyor.yml +0 -29
  32. data/build_all +0 -44
  33. data/patches/sort-patches-by-date +0 -25
  34. data/suppressions/README.txt +0 -1
  35. data/suppressions/nokogiri_ruby-2.supp +0 -10
  36. data/tasks/test.rb +0 -100
  37. data/test/css/test_nthiness.rb +0 -226
  38. data/test/css/test_parser.rb +0 -386
  39. data/test/css/test_tokenizer.rb +0 -215
  40. data/test/css/test_xpath_visitor.rb +0 -96
  41. data/test/decorators/test_slop.rb +0 -23
  42. data/test/files/2ch.html +0 -108
  43. data/test/files/GH_1042.html +0 -18
  44. data/test/files/address_book.rlx +0 -12
  45. data/test/files/address_book.xml +0 -10
  46. data/test/files/atom.xml +0 -344
  47. data/test/files/bar/bar.xsd +0 -4
  48. data/test/files/bogus.xml +0 -0
  49. data/test/files/dont_hurt_em_why.xml +0 -422
  50. data/test/files/encoding.html +0 -82
  51. data/test/files/encoding.xhtml +0 -84
  52. data/test/files/exslt.xml +0 -8
  53. data/test/files/exslt.xslt +0 -35
  54. data/test/files/foo/foo.xsd +0 -4
  55. data/test/files/metacharset.html +0 -10
  56. data/test/files/namespace_pressure_test.xml +0 -1684
  57. data/test/files/noencoding.html +0 -47
  58. data/test/files/po.xml +0 -32
  59. data/test/files/po.xsd +0 -66
  60. data/test/files/saml/saml20assertion_schema.xsd +0 -283
  61. data/test/files/saml/saml20protocol_schema.xsd +0 -302
  62. data/test/files/saml/xenc_schema.xsd +0 -146
  63. data/test/files/saml/xmldsig_schema.xsd +0 -318
  64. data/test/files/shift_jis.html +0 -10
  65. data/test/files/shift_jis.xml +0 -5
  66. data/test/files/shift_jis_no_charset.html +0 -9
  67. data/test/files/slow-xpath.xml +0 -25509
  68. data/test/files/snuggles.xml +0 -3
  69. data/test/files/staff.dtd +0 -10
  70. data/test/files/staff.xml +0 -59
  71. data/test/files/staff.xslt +0 -32
  72. data/test/files/test_document_url/bar.xml +0 -2
  73. data/test/files/test_document_url/document.dtd +0 -4
  74. data/test/files/test_document_url/document.xml +0 -6
  75. data/test/files/tlm.html +0 -851
  76. data/test/files/to_be_xincluded.xml +0 -2
  77. data/test/files/valid_bar.xml +0 -2
  78. data/test/files/xinclude.xml +0 -4
  79. data/test/helper.rb +0 -271
  80. data/test/html/sax/test_parser.rb +0 -168
  81. data/test/html/sax/test_parser_context.rb +0 -46
  82. data/test/html/sax/test_parser_text.rb +0 -163
  83. data/test/html/sax/test_push_parser.rb +0 -87
  84. data/test/html/test_attributes.rb +0 -85
  85. data/test/html/test_builder.rb +0 -164
  86. data/test/html/test_document.rb +0 -712
  87. data/test/html/test_document_encoding.rb +0 -143
  88. data/test/html/test_document_fragment.rb +0 -310
  89. data/test/html/test_element_description.rb +0 -105
  90. data/test/html/test_named_characters.rb +0 -14
  91. data/test/html/test_node.rb +0 -212
  92. data/test/html/test_node_encoding.rb +0 -91
  93. data/test/namespaces/test_additional_namespaces_in_builder_doc.rb +0 -14
  94. data/test/namespaces/test_namespaces_aliased_default.rb +0 -24
  95. data/test/namespaces/test_namespaces_in_builder_doc.rb +0 -75
  96. data/test/namespaces/test_namespaces_in_cloned_doc.rb +0 -31
  97. data/test/namespaces/test_namespaces_in_created_doc.rb +0 -75
  98. data/test/namespaces/test_namespaces_in_parsed_doc.rb +0 -80
  99. data/test/namespaces/test_namespaces_preservation.rb +0 -31
  100. data/test/test_convert_xpath.rb +0 -135
  101. data/test/test_css_cache.rb +0 -47
  102. data/test/test_encoding_handler.rb +0 -48
  103. data/test/test_memory_leak.rb +0 -156
  104. data/test/test_nokogiri.rb +0 -138
  105. data/test/test_soap4r_sax.rb +0 -52
  106. data/test/test_xslt_transforms.rb +0 -314
  107. data/test/xml/node/test_save_options.rb +0 -28
  108. data/test/xml/node/test_subclass.rb +0 -44
  109. data/test/xml/sax/test_parser.rb +0 -402
  110. data/test/xml/sax/test_parser_context.rb +0 -115
  111. data/test/xml/sax/test_parser_text.rb +0 -202
  112. data/test/xml/sax/test_push_parser.rb +0 -265
  113. data/test/xml/test_attr.rb +0 -74
  114. data/test/xml/test_attribute_decl.rb +0 -86
  115. data/test/xml/test_builder.rb +0 -341
  116. data/test/xml/test_c14n.rb +0 -180
  117. data/test/xml/test_cdata.rb +0 -54
  118. data/test/xml/test_comment.rb +0 -40
  119. data/test/xml/test_document.rb +0 -982
  120. data/test/xml/test_document_encoding.rb +0 -31
  121. data/test/xml/test_document_fragment.rb +0 -298
  122. data/test/xml/test_dtd.rb +0 -187
  123. data/test/xml/test_dtd_encoding.rb +0 -31
  124. data/test/xml/test_element_content.rb +0 -56
  125. data/test/xml/test_element_decl.rb +0 -73
  126. data/test/xml/test_entity_decl.rb +0 -122
  127. data/test/xml/test_entity_reference.rb +0 -262
  128. data/test/xml/test_namespace.rb +0 -96
  129. data/test/xml/test_node.rb +0 -1325
  130. data/test/xml/test_node_attributes.rb +0 -115
  131. data/test/xml/test_node_encoding.rb +0 -75
  132. data/test/xml/test_node_inheritance.rb +0 -32
  133. data/test/xml/test_node_reparenting.rb +0 -592
  134. data/test/xml/test_node_set.rb +0 -809
  135. data/test/xml/test_parse_options.rb +0 -64
  136. data/test/xml/test_processing_instruction.rb +0 -30
  137. data/test/xml/test_reader.rb +0 -620
  138. data/test/xml/test_reader_encoding.rb +0 -134
  139. data/test/xml/test_relax_ng.rb +0 -60
  140. data/test/xml/test_schema.rb +0 -142
  141. data/test/xml/test_syntax_error.rb +0 -36
  142. data/test/xml/test_text.rb +0 -60
  143. data/test/xml/test_unparented_node.rb +0 -483
  144. data/test/xml/test_xinclude.rb +0 -83
  145. data/test/xml/test_xpath.rb +0 -470
  146. data/test/xslt/test_custom_functions.rb +0 -133
  147. data/test/xslt/test_exception_handling.rb +0 -37
@@ -1,809 +0,0 @@
1
- require "helper"
2
-
3
- module Nokogiri
4
- module XML
5
- class TestNodeSet < Nokogiri::TestCase
6
- class TestNodeSetNamespaces < Nokogiri::TestCase
7
- def setup
8
- super
9
- @xml = Nokogiri.XML('<foo xmlns:n0="http://example.com" />')
10
- @list = @xml.xpath('//namespace::*')
11
- end
12
-
13
- def test_include?
14
- assert @list.include?(@list.first), 'list should have item'
15
- end
16
-
17
- def test_push
18
- @list.push @list.first
19
- end
20
-
21
- def test_delete
22
- @list.push @list.first
23
- @list.delete @list.first
24
- end
25
-
26
- def test_reference_after_delete
27
- first = @list.first
28
- @list.delete(first)
29
- assert_equal 'http://www.w3.org/XML/1998/namespace', first.href
30
- end
31
- end
32
-
33
- def setup
34
- super
35
- @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
36
- @list = @xml.css('employee')
37
- end
38
-
39
- def test_break_works
40
- assert_equal 7, @xml.root.elements.each { |x| break 7 }
41
- end
42
-
43
- def test_filter
44
- list = @xml.css('address').filter('*[domestic="Yes"]')
45
- assert_equal(%w{ Yes } * 4, list.map { |n| n['domestic'] })
46
- end
47
-
48
- def test_remove_attr
49
- @list.each { |x| x['class'] = 'blah' }
50
- assert_equal @list, @list.remove_attr('class')
51
- @list.each { |x| assert_nil x['class'] }
52
- end
53
-
54
- def test_remove_attribute
55
- @list.each { |x| x['class'] = 'blah' }
56
- assert_equal @list, @list.remove_attribute('class')
57
- @list.each { |x| assert_nil x['class'] }
58
- end
59
-
60
- def test_add_class
61
- assert_equal @list, @list.add_class('bar')
62
- @list.each { |x| assert_equal 'bar', x['class'] }
63
-
64
- @list.add_class('bar')
65
- @list.each { |x| assert_equal 'bar', x['class'] }
66
-
67
- @list.add_class('baz')
68
- @list.each { |x| assert_equal 'bar baz', x['class'] }
69
- end
70
-
71
- def test_append_class
72
- assert_equal @list, @list.append_class('bar')
73
- @list.each { |x| assert_equal 'bar', x['class'] }
74
-
75
- @list.append_class('bar')
76
- @list.each { |x| assert_equal 'bar bar', x['class'] }
77
-
78
- @list.append_class('baz')
79
- @list.each { |x| assert_equal 'bar bar baz', x['class'] }
80
- end
81
-
82
- def test_remove_class_with_no_class
83
- assert_equal @list, @list.remove_class('bar')
84
- @list.each { |e| assert_nil e['class'] }
85
-
86
- @list.each { |e| e['class'] = '' }
87
- assert_equal @list, @list.remove_class('bar')
88
- @list.each { |e| assert_nil e['class'] }
89
- end
90
-
91
- def test_remove_class_single
92
- @list.each { |e| e['class'] = 'foo bar' }
93
-
94
- assert_equal @list, @list.remove_class('bar')
95
- @list.each { |e| assert_equal 'foo', e['class'] }
96
- end
97
-
98
- def test_remove_class_completely
99
- @list.each { |e| e['class'] = 'foo' }
100
-
101
- assert_equal @list, @list.remove_class
102
- @list.each { |e| assert_nil e['class'] }
103
- end
104
-
105
- def test_attribute_set
106
- @list.each { |e| assert_nil e['foo'] }
107
-
108
- [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
109
- @list.send(t.first.to_sym, 'foo', t.last)
110
- @list.each { |e| assert_equal t.last, e['foo'] }
111
- end
112
- end
113
-
114
- def test_attribute_set_with_block
115
- @list.each { |e| assert_nil e['foo'] }
116
-
117
- [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
118
- @list.send(t.first.to_sym, 'foo') { |x| t.last }
119
- @list.each { |e| assert_equal t.last, e['foo'] }
120
- end
121
- end
122
-
123
- def test_attribute_set_with_hash
124
- @list.each { |e| assert_nil e['foo'] }
125
-
126
- [ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
127
- @list.send(t.first.to_sym, 'foo' => t.last)
128
- @list.each { |e| assert_equal t.last, e['foo'] }
129
- end
130
- end
131
-
132
- def test_attribute_no_args
133
- @list.first['foo'] = 'bar'
134
- assert_equal @list.first.attribute('foo'), @list.attribute('foo')
135
- end
136
-
137
- def test_search_empty_node_set
138
- set = Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new)
139
- assert_equal 0, set.css('foo').length
140
- assert_equal 0, set.xpath('.//foo').length
141
- assert_equal 0, set.search('foo').length
142
- end
143
-
144
- def test_node_set_search_with_multiple_queries
145
- xml = '<document>
146
- <thing>
147
- <div class="title">important thing</div>
148
- </thing>
149
- <thing>
150
- <div class="content">stuff</div>
151
- </thing>
152
- <thing>
153
- <p class="blah">more stuff</div>
154
- </thing>
155
- </document>'
156
- set = Nokogiri::XML(xml).xpath(".//thing")
157
- assert_kind_of Nokogiri::XML::NodeSet, set
158
-
159
- assert_equal 3, set.xpath('./div', './p').length
160
- assert_equal 3, set.css('.title', '.content', 'p').length
161
- assert_equal 3, set.search('./div', 'p.blah').length
162
- end
163
-
164
- def test_search_with_custom_selector
165
- set = @xml.xpath('//staff')
166
-
167
- [
168
- [:xpath, '//*[awesome(.)]'],
169
- [:search, '//*[awesome(.)]'],
170
- [:css, '*:awesome'],
171
- [:search, '*:awesome']
172
- ].each do |method, query|
173
- custom_employees = set.send(method, query, Class.new {
174
- def awesome ns
175
- ns.select { |n| n.name == 'employee' }
176
- end
177
- }.new)
178
-
179
- assert_equal(@xml.xpath('//employee'), custom_employees,
180
- "using #{method} with custom selector '#{query}'")
181
- end
182
- end
183
-
184
- def test_search_with_variable_bindings
185
- set = @xml.xpath('//staff')
186
-
187
- assert_equal(4, set.xpath('//address[@domestic=$value]', nil, :value => 'Yes').length,
188
- "using #xpath with variable binding")
189
-
190
- assert_equal(4, set.search('//address[@domestic=$value]', nil, :value => 'Yes').length,
191
- "using #search with variable binding")
192
- end
193
-
194
- def test_search_self
195
- set = @xml.xpath('//staff')
196
- assert_equal set.to_a, set.search('.').to_a
197
- end
198
-
199
- def test_css_searches_match_self
200
- html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
201
- set = html.xpath("/html/body/div")
202
- assert_equal set.first, set.css(".a").first
203
- assert_equal set.first, set.search(".a").first
204
- end
205
-
206
- def test_css_search_with_namespace
207
- fragment = Nokogiri::XML.fragment(<<-eoxml)
208
- <html xmlns="http://www.w3.org/1999/xhtml">
209
- <head></head>
210
- <body></body>
211
- </html>
212
- eoxml
213
- assert fragment.children.search( 'body', { 'xmlns' => 'http://www.w3.org/1999/xhtml' })
214
- end
215
-
216
- def test_double_equal
217
- assert node_set_one = @xml.xpath('//employee')
218
- assert node_set_two = @xml.xpath('//employee')
219
-
220
- assert_not_equal node_set_one.object_id, node_set_two.object_id
221
-
222
- assert_equal node_set_one, node_set_two
223
- end
224
-
225
- def test_node_set_not_equal_to_string
226
- node_set_one = @xml.xpath('//employee')
227
- assert_not_equal node_set_one, "asdfadsf"
228
- end
229
-
230
- def test_out_of_order_not_equal
231
- one = @xml.xpath('//employee')
232
- two = @xml.xpath('//employee')
233
- two.push two.shift
234
- assert_not_equal one, two
235
- end
236
-
237
- def test_shorter_is_not_equal
238
- node_set_one = @xml.xpath('//employee')
239
- node_set_two = @xml.xpath('//employee')
240
- node_set_two.delete(node_set_two.first)
241
-
242
- assert_not_equal node_set_one, node_set_two
243
- end
244
-
245
- def test_pop
246
- set = @xml.xpath('//employee')
247
- last = set.last
248
- assert_equal last, set.pop
249
- end
250
-
251
- def test_shift
252
- set = @xml.xpath('//employee')
253
- first = set.first
254
- assert_equal first, set.shift
255
- end
256
-
257
- def test_shift_empty
258
- set = Nokogiri::XML::NodeSet.new(@xml)
259
- assert_nil set.shift
260
- end
261
-
262
- def test_pop_empty
263
- set = Nokogiri::XML::NodeSet.new(@xml)
264
- assert_nil set.pop
265
- end
266
-
267
- def test_first_takes_arguments
268
- assert node_set = @xml.xpath('//employee')
269
- assert_equal 2, node_set.first(2).length
270
- end
271
-
272
- def test_first_clamps_arguments
273
- assert node_set = @xml.xpath('//employee[position() < 3]')
274
- assert_equal 2, node_set.first(5).length
275
- end
276
-
277
- [:dup, :clone].each do |method_name|
278
- define_method "test_#{method_name}" do
279
- assert node_set = @xml.xpath('//employee')
280
- duplicate = node_set.send(method_name)
281
- assert_equal node_set.length, duplicate.length
282
- node_set.zip(duplicate).each do |a,b|
283
- assert_equal a, b
284
- end
285
- end
286
- end
287
-
288
- def test_dup_on_empty_set
289
- empty_set = Nokogiri::XML::NodeSet.new @xml, []
290
- assert_equal 0, empty_set.dup.length # this shouldn't raise null pointer exception
291
- end
292
-
293
- def test_xmlns_is_automatically_registered
294
- doc = Nokogiri::XML(<<-eoxml)
295
- <root xmlns="http://tenderlovemaking.com/">
296
- <foo>
297
- <bar/>
298
- </foo>
299
- </root>
300
- eoxml
301
- set = doc.css('foo')
302
- assert_equal 1, set.css('xmlns|bar').length
303
- assert_equal 0, set.css('|bar').length
304
- assert_equal 1, set.xpath('//xmlns:bar').length
305
- assert_equal 1, set.search('xmlns|bar').length
306
- assert_equal 1, set.search('//xmlns:bar').length
307
- assert set.at('//xmlns:bar')
308
- assert set.at('xmlns|bar')
309
- assert set.at('bar')
310
- end
311
-
312
- def test_children_has_document
313
- set = @xml.root.children
314
- assert_instance_of(NodeSet, set)
315
- assert_equal @xml, set.document
316
- end
317
-
318
- def test_length_size
319
- assert node_set = @xml.search('//employee')
320
- assert_equal node_set.length, node_set.size
321
- end
322
-
323
- def test_to_xml
324
- assert node_set = @xml.search('//employee')
325
- assert node_set.to_xml
326
- end
327
-
328
- def test_inner_html
329
- doc = Nokogiri::HTML(<<-eohtml)
330
- <html>
331
- <body>
332
- <div>
333
- <a>one</a>
334
- </div>
335
- <div>
336
- <a>two</a>
337
- </div>
338
- </body>
339
- </html>
340
- eohtml
341
- assert html = doc.css('div').inner_html
342
- assert_match '<a>', html
343
- end
344
-
345
- def test_gt_string_arg
346
- assert node_set = @xml.search('//employee')
347
- assert_equal node_set.xpath('./employeeId'), (node_set > 'employeeId')
348
- end
349
-
350
- def test_at_performs_a_search_with_css
351
- assert node_set = @xml.search('//employee')
352
- assert_equal node_set.first.first_element_child, node_set.at('employeeId')
353
- assert_equal node_set.first.first_element_child, node_set.%('employeeId')
354
- end
355
-
356
- def test_at_performs_a_search_with_xpath
357
- assert node_set = @xml.search('//employee')
358
- assert_equal node_set.first.first_element_child, node_set.at('./employeeId')
359
- assert_equal node_set.first.first_element_child, node_set.%('./employeeId')
360
- end
361
-
362
- def test_at_with_integer_index
363
- assert node_set = @xml.search('//employee')
364
- assert_equal node_set.first, node_set.at(0)
365
- assert_equal node_set.first, node_set % 0
366
- end
367
-
368
- def test_at_xpath
369
- assert node_set = @xml.search('//employee')
370
- assert_equal node_set.first.first_element_child, node_set.at_xpath('./employeeId')
371
- end
372
-
373
- def test_at_css
374
- assert node_set = @xml.search('//employee')
375
- assert_equal node_set.first.first_element_child, node_set.at_css('employeeId')
376
- end
377
-
378
- def test_to_ary
379
- assert node_set = @xml.search('//employee')
380
- foo = []
381
- foo += node_set
382
- assert_equal node_set.length, foo.length
383
- end
384
-
385
- def test_push
386
- node = Nokogiri::XML::Node.new('foo', @xml)
387
- node.content = 'bar'
388
-
389
- assert node_set = @xml.search('//employee')
390
- node_set.push(node)
391
-
392
- assert node_set.include?(node)
393
- end
394
-
395
- def test_delete_with_invalid_argument
396
- employees = @xml.search("//employee")
397
- positions = @xml.search("//position")
398
-
399
- assert_raises(ArgumentError) { employees.delete(positions) }
400
- end
401
-
402
- def test_delete_when_present
403
- employees = @xml.search("//employee")
404
- wally = employees.first
405
- assert employees.include?(wally) # testing setup
406
- length = employees.length
407
-
408
- result = employees.delete(wally)
409
- assert_equal result, wally
410
- assert ! employees.include?(wally)
411
- assert length-1, employees.length
412
- end
413
-
414
- def test_delete_when_not_present
415
- employees = @xml.search("//employee")
416
- phb = @xml.search("//position").first
417
- assert ! employees.include?(phb) # testing setup
418
- length = employees.length
419
-
420
- result = employees.delete(phb)
421
- assert_nil result
422
- assert length, employees.length
423
- end
424
-
425
- def test_delete_on_empty_set
426
- empty_set = Nokogiri::XML::NodeSet.new @xml, []
427
- employee = @xml.at_xpath("//employee")
428
- assert_nil empty_set.delete(employee)
429
- end
430
-
431
- def test_unlink
432
- xml = Nokogiri::XML.parse(<<-eoxml)
433
- <root>
434
- <a class='foo bar'>Bar</a>
435
- <a class='bar foo'>Bar</a>
436
- <a class='bar'>Bar</a>
437
- <a>Hello world</a>
438
- <a class='baz bar foo'>Bar</a>
439
- <a class='bazbarfoo'>Awesome</a>
440
- <a class='bazbar'>Awesome</a>
441
- </root>
442
- eoxml
443
- set = xml.xpath('//a')
444
- set.unlink
445
- set.each do |node|
446
- assert !node.parent
447
- #assert !node.document
448
- assert !node.previous_sibling
449
- assert !node.next_sibling
450
- end
451
- assert_no_match(/Hello world/, xml.to_s)
452
- end
453
-
454
- def test_nodeset_search_takes_namespace
455
- @xml = Nokogiri::XML.parse(<<-eoxml)
456
- <root>
457
- <car xmlns:part="http://general-motors.com/">
458
- <part:tire>Michelin Model XGV</part:tire>
459
- </car>
460
- <bicycle xmlns:part="http://schwinn.com/">
461
- <part:tire>I'm a bicycle tire!</part:tire>
462
- </bicycle>
463
- </root>
464
- eoxml
465
- set = @xml/'root'
466
- assert_equal 1, set.length
467
- bike_tire = set.search('//bike:tire', 'bike' => "http://schwinn.com/")
468
- assert_equal 1, bike_tire.length
469
- end
470
-
471
- def test_new_nodeset
472
- node_set = Nokogiri::XML::NodeSet.new(@xml)
473
- assert_equal(0, node_set.length)
474
- node = Nokogiri::XML::Node.new('form', @xml)
475
- node_set << node
476
- assert_equal(1, node_set.length)
477
- assert_equal(node, node_set.last)
478
- end
479
-
480
- def test_search_on_nodeset
481
- assert node_set = @xml.search('//employee')
482
- assert sub_set = node_set.search('.//name')
483
- assert_equal(node_set.length, sub_set.length)
484
- end
485
-
486
- def test_negative_index_works
487
- assert node_set = @xml.search('//employee')
488
- assert_equal node_set.last, node_set[-1]
489
- end
490
-
491
- def test_large_negative_index_returns_nil
492
- assert node_set = @xml.search('//employee')
493
- assert_nil(node_set[-1 * (node_set.length + 1)])
494
- end
495
-
496
- def test_node_set_fetches_private_data
497
- assert node_set = @xml.search('//employee')
498
-
499
- set = node_set
500
- assert_equal(set[0], set[0])
501
- end
502
-
503
- def test_node_set_returns_0
504
- assert node_set = @xml.search('//asdkfjhasdlkfjhaldskfh')
505
- assert_equal(0, node_set.length)
506
- end
507
-
508
- def test_wrap
509
- employees = (@xml/"//employee").wrap("<wrapper/>")
510
- assert_equal 'wrapper', employees[0].parent.name
511
- assert_equal 'employee', @xml.search("//wrapper").first.children[0].name
512
- end
513
-
514
- def test_wrap_a_fragment
515
- frag = Nokogiri::XML::DocumentFragment.parse <<-EOXML
516
- <employees>
517
- <employee>hello</employee>
518
- <employee>goodbye</employee>
519
- </employees>
520
- EOXML
521
- employees = frag.xpath ".//employee"
522
- employees.wrap("<wrapper/>")
523
- assert_equal 'wrapper', employees[0].parent.name
524
- assert_equal 'employee', frag.at(".//wrapper").children.first.name
525
- end
526
-
527
- def test_wrap_preserves_document_structure
528
- assert_equal "employeeId",
529
- @xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
530
- @xml.xpath("//employeeId[text()='EMP0001']").wrap("<wrapper/>")
531
- assert_equal "wrapper",
532
- @xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
533
- end
534
-
535
- def test_plus_operator
536
- names = @xml.search("name")
537
- positions = @xml.search("position")
538
-
539
- names_len = names.length
540
- positions_len = positions.length
541
-
542
- assert_raises(ArgumentError) { names + positions.first }
543
-
544
- result = names + positions
545
- assert_equal names_len, names.length
546
- assert_equal positions_len, positions.length
547
- assert_equal names.length + positions.length, result.length
548
-
549
- names += positions
550
- assert_equal result.length, names.length
551
- end
552
-
553
- def test_union
554
- names = @xml.search("name")
555
-
556
- assert_equal(names.length, (names | @xml.search("name")).length)
557
- end
558
-
559
- def test_minus_operator
560
- employees = @xml.search("//employee")
561
- females = @xml.search("//employee[gender[text()='Female']]")
562
-
563
- employees_len = employees.length
564
- females_len = females.length
565
-
566
- assert_raises(ArgumentError) { employees - females.first }
567
-
568
- result = employees - females
569
- assert_equal employees_len, employees.length
570
- assert_equal females_len, females.length
571
- assert_equal employees.length - females.length, result.length
572
-
573
- employees -= females
574
- assert_equal result.length, employees.length
575
- end
576
-
577
- def test_array_index
578
- employees = @xml.search("//employee")
579
- other = @xml.search("//position").first
580
-
581
- assert_equal 3, employees.index(employees[3])
582
- assert_nil employees.index(other)
583
- assert_equal 3, employees.index {|employee| employee.search("employeeId/text()").to_s == "EMP0004" }
584
- assert_nil employees.index {|employee| employee.search("employeeId/text()").to_s == "EMP0000" }
585
- end
586
-
587
- def test_slice_too_far
588
- employees = @xml.search("//employee")
589
- assert_equal employees.length, employees[0, employees.length + 1].length
590
- assert_equal employees.length, employees[0, employees.length].length
591
- end
592
-
593
- def test_slice_on_empty_node_set
594
- empty_set = Nokogiri::XML::NodeSet.new @xml, []
595
- assert_nil empty_set[99]
596
- assert_nil empty_set[99..101]
597
- assert_nil empty_set[99,2]
598
- end
599
-
600
- def test_slice_waaaaaay_off_the_end
601
- xml = Nokogiri::XML::Builder.new {
602
- root { 100.times { div } }
603
- }.doc
604
- nodes = xml.css "div"
605
- assert_equal 1, nodes.slice(99, 100_000).length
606
- assert_equal 0, nodes.slice(100, 100_000).length
607
- end
608
-
609
- def test_array_slice_with_start_and_end
610
- employees = @xml.search("//employee")
611
- assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
612
- end
613
-
614
- def test_array_index_bracket_equivalence
615
- employees = @xml.search("//employee")
616
- assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
617
- assert_equal [employees[1], employees[2], employees[3]], employees.slice(1,3).to_a
618
- end
619
-
620
- def test_array_slice_with_negative_start
621
- employees = @xml.search("//employee")
622
- assert_equal [employees[2]], employees[-3,1].to_a
623
- assert_equal [employees[2], employees[3]], employees[-3,2].to_a
624
- end
625
-
626
- def test_array_slice_with_invalid_args
627
- employees = @xml.search("//employee")
628
- assert_nil employees[99, 1] # large start
629
- assert_nil employees[1, -1] # negative len
630
- assert_equal [], employees[1, 0].to_a # zero len
631
- end
632
-
633
- def test_array_slice_with_range
634
- employees = @xml.search("//employee")
635
- assert_equal [employees[1], employees[2], employees[3]], employees[1..3].to_a
636
- assert_equal [employees[0], employees[1], employees[2], employees[3]], employees[0..3].to_a
637
- end
638
-
639
- def test_intersection_with_no_overlap
640
- employees = @xml.search("//employee")
641
- positions = @xml.search("//position")
642
-
643
- assert_equal [], (employees & positions).to_a
644
- end
645
-
646
- def test_intersection
647
- employees = @xml.search("//employee")
648
- first_set = employees[0..2]
649
- second_set = employees[2..4]
650
-
651
- assert_equal [employees[2]], (first_set & second_set).to_a
652
- end
653
-
654
- def test_intersection_on_empty_set
655
- empty_set = Nokogiri::XML::NodeSet.new @xml
656
- employees = @xml.search("//employee")
657
- assert_equal 0, (empty_set & employees).length
658
- end
659
-
660
- def test_include?
661
- employees = @xml.search("//employee")
662
- yes = employees.first
663
- no = @xml.search("//position").first
664
-
665
- assert employees.include?(yes)
666
- assert ! employees.include?(no)
667
- end
668
-
669
- def test_include_on_empty_node_set
670
- empty_set = Nokogiri::XML::NodeSet.new @xml, []
671
- employee = @xml.at_xpath("//employee")
672
- assert ! empty_set.include?(employee)
673
- end
674
-
675
- def test_each
676
- employees = @xml.search("//employee")
677
- enum = employees.each
678
- assert_instance_of Enumerator, enum
679
- assert_equal enum.next, employees[0]
680
- assert_equal enum.next, employees[1]
681
- end
682
-
683
- def test_children
684
- employees = @xml.search("//employee")
685
- count = 0
686
- employees.each do |employee|
687
- count += employee.children.length
688
- end
689
- set = employees.children
690
- assert_equal count, set.length
691
- end
692
-
693
- def test_inspect
694
- employees = @xml.search("//employee")
695
- inspected = employees.inspect
696
-
697
- assert_equal "[#{employees.map(&:inspect).join(', ')}]",
698
- inspected
699
- end
700
-
701
- def test_should_not_splode_when_accessing_namespace_declarations_in_a_node_set
702
- 2.times do
703
- xml = Nokogiri::XML "<foo></foo>"
704
- node_set = xml.xpath("//namespace::*")
705
- assert_equal 1, node_set.size
706
- node = node_set.first
707
- node.to_s # segfaults in 1.4.0 and earlier
708
-
709
- # if we haven't segfaulted, let's make sure we handled it correctly
710
- assert_instance_of Nokogiri::XML::Namespace, node
711
- end
712
- end
713
-
714
- def test_should_not_splode_when_arrayifying_node_set_containing_namespace_declarations
715
- xml = Nokogiri::XML "<foo></foo>"
716
- node_set = xml.xpath("//namespace::*")
717
- assert_equal 1, node_set.size
718
-
719
- node_array = node_set.to_a
720
- node = node_array.first
721
- node.to_s # segfaults in 1.4.0 and earlier
722
-
723
- # if we haven't segfaulted, let's make sure we handled it correctly
724
- assert_instance_of Nokogiri::XML::Namespace, node
725
- end
726
-
727
- def test_should_not_splode_when_unlinking_node_set_containing_namespace_declarations
728
- xml = Nokogiri::XML "<foo></foo>"
729
- node_set = xml.xpath("//namespace::*")
730
- assert_equal 1, node_set.size
731
-
732
- node_set.unlink
733
- end
734
-
735
- def test_reverse
736
- xml = Nokogiri::XML "<root><a />b<c />d<e /></root>"
737
- children = xml.root.children
738
- assert_instance_of Nokogiri::XML::NodeSet, children
739
-
740
- reversed = children.reverse
741
- assert_equal reversed[0], children[4]
742
- assert_equal reversed[1], children[3]
743
- assert_equal reversed[2], children[2]
744
- assert_equal reversed[3], children[1]
745
- assert_equal reversed[4], children[0]
746
-
747
- assert_equal children, children.reverse.reverse
748
- end
749
-
750
- def test_node_set_dup_result_has_document_and_is_decorated
751
- x = Module.new do
752
- def awesome! ; end
753
- end
754
- util_decorate(@xml, x)
755
- node_set = @xml.css("address")
756
- new_set = node_set.dup
757
- assert_equal node_set.document, new_set.document
758
- assert new_set.respond_to?(:awesome!)
759
- end
760
-
761
- def test_node_set_union_result_has_document_and_is_decorated
762
- x = Module.new do
763
- def awesome! ; end
764
- end
765
- util_decorate(@xml, x)
766
- node_set1 = @xml.css("address")
767
- node_set2 = @xml.css("address")
768
- new_set = node_set1 | node_set2
769
- assert_equal node_set1.document, new_set.document
770
- assert new_set.respond_to?(:awesome!)
771
- end
772
-
773
- def test_node_set_intersection_result_has_document_and_is_decorated
774
- x = Module.new do
775
- def awesome! ; end
776
- end
777
- util_decorate(@xml, x)
778
- node_set1 = @xml.css("address")
779
- node_set2 = @xml.css("address")
780
- new_set = node_set1 & node_set2
781
- assert_equal node_set1.document, new_set.document
782
- assert new_set.respond_to?(:awesome!)
783
- end
784
-
785
- def test_node_set_difference_result_has_document_and_is_decorated
786
- x = Module.new do
787
- def awesome! ; end
788
- end
789
- util_decorate(@xml, x)
790
- node_set1 = @xml.css("address")
791
- node_set2 = @xml.css("address")
792
- new_set = node_set1 - node_set2
793
- assert_equal node_set1.document, new_set.document
794
- assert new_set.respond_to?(:awesome!)
795
- end
796
-
797
- def test_node_set_slice_result_has_document_and_is_decorated
798
- x = Module.new do
799
- def awesome! ; end
800
- end
801
- util_decorate(@xml, x)
802
- node_set = @xml.css("address")
803
- new_set = node_set[0..-1]
804
- assert_equal node_set.document, new_set.document
805
- assert new_set.respond_to?(:awesome!)
806
- end
807
- end
808
- end
809
- end