Split have_tag matcher into a separate module

This commit is contained in:
Bryan Helmkamp 2008-11-06 16:58:53 -05:00
parent a8e0e7578a
commit f13dec013c
1 changed files with 58 additions and 54 deletions

View File

@ -102,53 +102,6 @@ module Webrat
end
end
class HaveTag < HaveSelector
# ==== Returns
# String:: The failure message.
def failure_message
"expected following output to contain a #{tag_inspect} tag:\n#{@document}"
end
# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected following output to omit a #{tag_inspect}:\n#{@document}"
end
def tag_inspect
options = @expected.last.dup
content = options.delete(:content)
html = "<#{@expected.first}"
options.each do |k,v|
html << " #{k}='#{v}'"
end
if content
html << ">#{content}</#{@expected.first}>"
else
html << "/>"
end
html
end
def query
options = @expected.last.dup
selector = @expected.first.to_s
selector << ":contains('#{options.delete(:content)}')" if options[:content]
options.each do |key, value|
selector << "[#{key}='#{value}']"
end
Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }
end
end
class HasContent
def initialize(content)
@ -161,9 +114,9 @@ module Webrat
case @content
when String
@element.contains?(@content)
@element.include?(@content)
when Regexp
@element.matches?(@content)
@element.match(@content)
end
end
@ -217,11 +170,6 @@ module Webrat
end
alias_method :match_xpath, :have_xpath
def have_tag(name, attributes = {})
HaveTag.new([name, attributes])
end
alias_method :match_tag, :have_tag
# Matches the contents of an HTML document with
# whatever string is supplied
#
@ -232,4 +180,60 @@ module Webrat
end
end
module HaveTagMatcher
class HaveTag < ::Webrat::Matchers::HaveSelector
# ==== Returns
# String:: The failure message.
def failure_message
"expected following output to contain a #{tag_inspect} tag:\n#{@document}"
end
# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected following output to omit a #{tag_inspect}:\n#{@document}"
end
def tag_inspect
options = @expected.last.dup
content = options.delete(:content)
html = "<#{@expected.first}"
options.each do |k,v|
html << " #{k}='#{v}'"
end
if content
html << ">#{content}</#{@expected.first}>"
else
html << "/>"
end
html
end
def query
options = @expected.last.dup
selector = @expected.first.to_s
selector << ":contains('#{options.delete(:content)}')" if options[:content]
options.each do |key, value|
selector << "[#{key}='#{value}']"
end
Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }
end
end
def have_tag(name, attributes = {})
HaveTag.new([name, attributes])
end
alias_method :match_tag, :have_tag
end
end