Skip to content

Commit

Permalink
match descendents in have_selector/have_xpath blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Jack committed Apr 29, 2009
1 parent 481bfe0 commit 9a3668b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/webrat/core/matchers/have_xpath.rb
Expand Up @@ -32,7 +32,7 @@ def matches(stringlike)

def rexml_matches(stringlike)
if REXML::Node === stringlike || Array === stringlike
@query = query.map { |q| q.gsub(%r'//', './') }
@query = query.map { |q| q.gsub(%r'^//', './/') }
else
@query = query
end
Expand All @@ -52,7 +52,7 @@ def rexml_matches(stringlike)

def nokogiri_matches(stringlike)
if Nokogiri::XML::NodeSet === stringlike
@query = query.gsub(%r'//', './')
@query = query.gsub(%r'^//', './/')
else
@query = query
end
Expand Down
11 changes: 9 additions & 2 deletions spec/public/matchers/have_selector_spec.rb
Expand Up @@ -13,6 +13,7 @@
<ul>
<li>First</li>
<li>Second</li>
<li><a href="http://example.org/">Third</a></li>
</ul>
</div>
HTML
Expand Down Expand Up @@ -52,12 +53,12 @@

describe "specifying counts" do
it "should be able to specify the number of occurences of the tag" do
@body.should have_selector("li", :count => 2)
@body.should have_selector("li", :count => 3)
end

it "should not match if the count is wrong" do
lambda {
@body.should have_selector("li", :count => 3)
@body.should have_selector("li", :count => 4)
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
end
Expand Down Expand Up @@ -97,6 +98,12 @@
n.should have_selector("li", :content => "Second")
end
end

it "should work with descendants of the matched elements" do
@body.should have_selector("ul") do |n|
n.should have_selector("a", :content => "Third")
end
end
end

describe "Test::Unit assertions" do
Expand Down
7 changes: 7 additions & 0 deletions spec/public/matchers/have_xpath_spec.rb
Expand Up @@ -13,6 +13,7 @@
<ul>
<li>First</li>
<li>Second</li>
<li><a href="http://example.org">Third</a></li>
</ul>
</div>
HTML
Expand Down Expand Up @@ -88,6 +89,12 @@
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end

it "should work with descendants of the matched elements" do
@body.should have_xpath("//ul") do |node|
node.should have_xpath("//a[@href='http://example.org']")
end
end

describe 'asserts for xpath' do
include Test::Unit::Assertions

Expand Down

0 comments on commit 9a3668b

Please sign in to comment.