Pulling :count option up from have_selector to have_xpath

This commit is contained in:
Bryan Helmkamp 2009-02-23 20:18:36 -05:00
parent 66d39b851c
commit 9761b64b60
2 changed files with 15 additions and 23 deletions

View File

@ -15,31 +15,18 @@ module Webrat
def negative_failure_message def negative_failure_message
"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 = @options.dup
content = options.delete(:content) content = options.delete(:content)
html = "<#{@expected.first}" html = "<#{@expected}"
options.each do |k,v| options.each do |k,v|
html << " #{k}='#{v}'" html << " #{k}='#{v}'"
end end
if content if content
html << ">#{content}</#{@expected.first}>" html << ">#{content}</#{@expected}>"
else else
html << "/>" html << "/>"
end end
@ -48,8 +35,8 @@ module Webrat
end end
def query def query
options = @expected.last.dup options = @options.dup
selector = @expected.first.to_s selector = @expected.to_s
options.each do |key, value| options.each do |key, value|
next if [:content, :count].include?(key) next if [:content, :count].include?(key)
@ -84,7 +71,7 @@ module Webrat
# ==== Returns # ==== Returns
# HaveSelector:: A new have selector matcher. # HaveSelector:: A new have selector matcher.
def have_selector(name, attributes = {}, &block) def have_selector(name, attributes = {}, &block)
HaveSelector.new([name, attributes], &block) HaveSelector.new(name, attributes, &block)
end end
alias_method :match_selector, :have_selector alias_method :match_selector, :have_selector
@ -92,14 +79,14 @@ module Webrat
# Asserts that the body of the response contains # Asserts that the body of the response contains
# the supplied selector # the supplied selector
def assert_have_selector(name, attributes = {}, &block) def assert_have_selector(name, attributes = {}, &block)
matcher = HaveSelector.new([name, attributes], &block) matcher = HaveSelector.new(name, attributes, &block)
assert matcher.matches?(response_body), matcher.failure_message assert matcher.matches?(response_body), matcher.failure_message
end end
# Asserts that the body of the response # Asserts that the body of the response
# does not contain the supplied string or regepx # does not contain the supplied string or regepx
def assert_have_no_selector(name, attributes = {}, &block) def assert_have_no_selector(name, attributes = {}, &block)
matcher = HaveSelector.new([name, attributes], &block) matcher = HaveSelector.new(name, attributes, &block)
assert !matcher.matches?(response_body), matcher.negative_failure_message assert !matcher.matches?(response_body), matcher.negative_failure_message
end end

View File

@ -7,14 +7,19 @@ module Webrat
class HaveXpath #:nodoc: class HaveXpath #:nodoc:
def initialize(expected, options = {}, &block) def initialize(expected, options = {}, &block)
@expected = expected @expected = expected
@options = {} @options = options
@block = block @block = block
end end
def matches?(stringlike, &block) def matches?(stringlike, &block)
@block ||= block @block ||= block
matched = matches(stringlike) matched = matches(stringlike)
matched.any? && (!@block || @block.call(matched))
if @options[:count]
matched.size == @options[:count] && (!@block || @block.call(matched))
else
matched.any? && (!@block || @block.call(matched))
end
end end
def matches(stringlike) def matches(stringlike)