Sha256: f5d163e5611e86ede2a232163a6c22f5a8f26284775f361ccd05e33909f4c135

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Webrat
  
  module HaveTagMatcher

    class HaveTag < ::Webrat::Matchers::HaveSelector #:nodoc:

      # ==== 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 = {}, &block)
      HaveTag.new([name, attributes], &block)
    end
    alias_method :match_tag, :have_tag

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
webrat-0.3.3 lib/webrat/core/matchers/have_tag.rb
webrat-0.3.4 lib/webrat/core/matchers/have_tag.rb
webrat-0.3.2 lib/webrat/core/matchers/have_tag.rb