Adding :count option to have_tag

This commit is contained in:
Bryan Helmkamp 2009-02-15 19:41:44 -05:00
parent 932fdab884
commit 17a8bc7b66
3 changed files with 42 additions and 17 deletions

View File

@ -15,6 +15,19 @@ module Webrat
"expected following output to omit a #{tag_inspect}:\n#{@document}" "expected following output to omit a #{tag_inspect}:\n#{@document}"
end end
def matches?(stringlike, &block)
@block ||= block
matched = matches(stringlike)
options = @expected.last.dup
if options[:count]
matched.size == options[:count] && (!@block || @block.call(matched))
else
matched.any? && (!@block || @block.call(matched))
end
end
def tag_inspect def tag_inspect
options = @expected.last.dup options = @expected.last.dup
content = options.delete(:content) content = options.delete(:content)
@ -38,7 +51,7 @@ module Webrat
selector = @expected.first.to_s selector = @expected.first.to_s
options.each do |key, value| options.each do |key, value|
next if key == :content next if [:content, :count].include?(key)
selector << "[#{key}='#{value}']" selector << "[#{key}='#{value}']"
end end
@ -56,6 +69,7 @@ module Webrat
elsif options[:content] elsif options[:content]
q << "[contains(., '#{options[:content]}')]" q << "[contains(., '#{options[:content]}')]"
end end
q q
end end
end end

View File

@ -12,15 +12,19 @@ module Webrat
def matches?(stringlike, &block) def matches?(stringlike, &block)
@block ||= block @block ||= block
matched = matches(stringlike)
matched.any? && (!@block || @block.call(matched))
end
def matches(stringlike)
if Webrat.configuration.parse_with_nokogiri? if Webrat.configuration.parse_with_nokogiri?
matches_nokogiri?(stringlike) nokogiri_matches(stringlike)
else else
matches_rexml?(stringlike) rexml_matches(stringlike)
end end
end end
def matches_rexml?(stringlike) def rexml_matches(stringlike)
if REXML::Node === stringlike || Array === stringlike if REXML::Node === stringlike || Array === stringlike
@query = query.map { |q| q.gsub(%r'//', './') } @query = query.map { |q| q.gsub(%r'//', './') }
else else
@ -29,18 +33,16 @@ module Webrat
@document = Webrat.rexml_document(stringlike) @document = Webrat.rexml_document(stringlike)
matched = @query.map do |q| @query.map do |q|
if @document.is_a?(Array) if @document.is_a?(Array)
@document.map { |d| REXML::XPath.match(d, q) } @document.map { |d| REXML::XPath.match(d, q) }
else else
REXML::XPath.match(@document, q) REXML::XPath.match(@document, q)
end end
end.flatten.compact end.flatten.compact
matched.any? && (!@block || @block.call(matched))
end end
def matches_nokogiri?(stringlike) def nokogiri_matches(stringlike)
if Nokogiri::XML::NodeSet === stringlike if Nokogiri::XML::NodeSet === stringlike
@query = query.map { |q| q.gsub(%r'//', './') } @query = query.map { |q| q.gsub(%r'//', './') }
else else
@ -48,8 +50,7 @@ module Webrat
end end
@document = Webrat::XML.document(stringlike) @document = Webrat::XML.document(stringlike)
matched = @document.xpath(*@query) @document.xpath(*@query)
matched.any? && (!@block || @block.call(matched))
end end
def query def query

View File

@ -193,6 +193,16 @@ describe Webrat::Matchers do
@body.should have_tag("h4", :content => "Welcome 'Bryan\"") @body.should have_tag("h4", :content => "Welcome 'Bryan\"")
end end
it "should be able to specify the number of occurences of the tag" do
@body.should have_tag("li", :count => 2)
end
it "should not match if the count is wrong" do
lambda {
@body.should have_tag("li", :count => 3)
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should be able to specify the attributes of the tag" do it "should be able to specify the attributes of the tag" do
@body.should have_tag("div", :class => "inner") @body.should have_tag("div", :class => "inner")
end end
@ -226,10 +236,10 @@ describe Webrat::Matchers do
end end
it "should work with items that have multiple child nodes" do it "should work with items that have multiple child nodes" do
@body.should have_tag("ul") { |n| @body.should have_tag("ul") do |n|
n.should have_tag("li", :content => "First") n.should have_tag("li", :content => "First")
n.should have_tag("li", :content => "Second") n.should have_tag("li", :content => "Second")
} end
end end
describe "asserts for tags," do describe "asserts for tags," do