Sha256: 1cded370ce686d7c6cf2d77bab4943e0dca9748c2c60cfa7e4e686cd8cba16f0

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 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
    
    # Asserts that the body of the response contains
    # the supplied tag with the associated selectors
    def assert_have_tag(name, attributes = {})
      ht = HaveTag.new([name, attributes])
      assert ht.matches?(response_body), ht.failure_message
    end
    
    # Asserts that the body of the response
    # does not contain the supplied string or regepx
    def assert_have_no_tag(name, attributes = {})
      ht = HaveTag.new([name, attributes])
      assert !ht.matches?(response_body), ht.negative_failure_message
    end

  end
end

Version data entries

18 entries across 18 versions & 4 rubygems

Version Path
adva-0.3.2 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.3.1 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.3.0 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.2.4 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.2.3 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.2.2 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.2.1 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.2.0 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.1.4 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.1.3 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.1.2 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.1.1 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.1.0 test/webrat/lib/webrat/core/matchers/have_tag.rb
adva-0.0.1 test/webrat/lib/webrat/core/matchers/have_tag.rb
auxesis-webrat-0.4.1 lib/webrat/core/matchers/have_tag.rb
brynary-webrat-0.4.0 lib/webrat/core/matchers/have_tag.rb
webrat-0.4.0 lib/webrat/core/matchers/have_tag.rb
webrat-0.4.1 lib/webrat/core/matchers/have_tag.rb