Skip to content

Commit

Permalink
Tests demonstrating the issue from #572
Browse files Browse the repository at this point in the history
and #454 and #370 and #213.
  • Loading branch information
flavorjones committed Jan 2, 2015
1 parent a674db0 commit ab26d27
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/xml/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,59 @@ def test_can_be_closed
Nokogiri::XML f
f.close
end

describe "css pseudoselector :last" do
attr_reader :html, :document

before do
@html = '<p>Lorem ipsum</p><p>Second paragraph</p><div>Not paragraph</div>'
@document = Nokogiri::XML html
end

it "works in #css" do
assert_equal(1, document.css('p:last').size)
end

it "works in #search" do
assert_equal(1, document.search('p:last').size)
end
end

describe "flavors of xpath search" do
# https://github.com/sparklemotion/nokogiri/issues/572
attr_reader :xml, :document

before do
@xml = <<-EOXML.strip
<root name="root_1">
<descendant name="desc_1_1"/>
<descendant name="desc_1_2"/>
</root>
EOXML
@document = Nokogiri::XML::Document.parse xml
end

[
['root', 1],
['./root', 1],
['.//root', 1],
['.//*[@name="root_1"]', 1],
['/root', 1],
['/*[@name="root_1"]', 1],
['//root', 1],
['//*[@name="root_1"]', 1],
['./root/descendant', 2],
['.//descendant', 2],
].each do |xpath, expected_n|

it "returns expected results" do
actual_n = document.xpath(xpath).size
assert_equal(expected_n, actual_n,
"expected #{expected_n} results when searching with '#{xpath}', got #{actual_n}")
end

end
end
end
end
end
72 changes: 72 additions & 0 deletions test/xml/test_document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,78 @@ def test_for_libxml_in_context_memory_badness_when_encountering_encoding_errors
doc = Nokogiri::HTML html
doc.at_css("div").replace("Bar")
end

describe "css pseudoselector :last" do
attr_reader :html, :fragment

before do
@html = '<p>Lorem ipsum</p><p>Second paragraph</p><div>Not paragraph</div>'
@fragment = Nokogiri::XML.fragment html
end

it "works in #css" do
assert_equal(1, fragment.css('p:last').size)
end
it "works in #search" do
assert_equal(1, fragment.search('p:last').size)
end
end

describe "xpath search" do
# https://github.com/sparklemotion/nokogiri/issues/572
attr_reader :xml, :fragment

before do
@xml = <<-EOXML.strip
<root name="root_1">
<descendant name="desc_1_1"/>
<descendant name="desc_1_2"/>
</root>
<root name="root_2">
<descendant name="desc_2_1"/>
<descendant name="desc_2_2"/>
</root>
EOXML
end

{
"on a fragment parsed in a node context" => lambda do |xml|
Nokogiri::XML::Document.new.fragment xml
end,
"on a bare fragment" => lambda do |xml|
Nokogiri::XML::Document.parse("<context></context>").at_css("context").parse xml
end
}.each do |desc, factory|

describe desc do
before do
@fragment = factory.call(xml)
end

[
['root', 2],
['./root', 2],
['.//root', 2],
['.//*[@name="root_1"]', 1],
['/root', 2],
['/*[@name="root_1"]', 1],
['//root', 2],
['//*[@name="root_1"]', 1],
['./root/descendant', 4],
['.//descendant', 4],
].each do |xpath, expected_n|

it "returns expected results" do
actual_n = fragment.xpath(xpath).length
assert_equal(expected_n, actual_n,
"expected #{expected_n} results when searching with '#{xpath}', got #{actual_n}")
end

end
end # describe desc

end
end
end
end
end

0 comments on commit ab26d27

Please sign in to comment.