From f13dec013c630166aa1434cfacb4d4e6b6295310 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 6 Nov 2008 16:58:53 -0500 Subject: [PATCH] Split have_tag matcher into a separate module --- lib/webrat/core/matchers.rb | 112 +++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 54 deletions(-) diff --git a/lib/webrat/core/matchers.rb b/lib/webrat/core/matchers.rb index 1518c19..f6c4f11 100644 --- a/lib/webrat/core/matchers.rb +++ b/lib/webrat/core/matchers.rb @@ -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}" - 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}" + 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