Sha256: 9202e6292cc970ef4486ef4e8d3707343899ac5135a12ffbb4a3c55b40aeb11e

Contents?: true

Size: 1.74 KB

Versions: 12

Compression:

Stored size: 1.74 KB

Contents

module Webrat
  module Matchers

    class HasContent #:nodoc:
      def initialize(content)
        @content = content
      end

      def matches?(stringlike)
        @document = Webrat::XML.document(stringlike)
        @element = @document.inner_text

        case @content
        when String
          @element.include?(@content)
        when Regexp
          @element.match(@content)
        end
      end

      # ==== Returns
      # String:: The failure message.
      def failure_message
        "expected the following element's content to #{content_message}:\n#{squeeze_space(@element)}"
      end

      # ==== Returns
      # String:: The failure message to be displayed in negative matches.
      def negative_failure_message
        "expected the following element's content to not #{content_message}:\n#{squeeze_space(@element)}"
      end

      def squeeze_space(inner_text)
        inner_text.gsub(/^\s*$/, "").squeeze("\n")
      end

      def content_message
        case @content
        when String
          "include \"#{@content}\""
        when Regexp
          "match #{@content.inspect}"
        end
      end
    end

    # Matches the contents of an HTML document with
    # whatever string is supplied
    def contain(content)
      HasContent.new(content)
    end

    # Asserts that the body of the response contain
    # the supplied string or regexp
    def assert_contain(content)
      hc = HasContent.new(content)
      assert hc.matches?(response_body), hc.failure_message
    end

    # Asserts that the body of the response
    # does not contain the supplied string or regepx
    def assert_not_contain(content)
      hc = HasContent.new(content)
      assert !hc.matches?(response_body), hc.negative_failure_message
    end

  end
end

Version data entries

12 entries across 12 versions & 8 rubygems

Version Path
diabolo-webrat-0.5.1 lib/webrat/core/matchers/have_content.rb
honkster-webrat-0.6.0.10 lib/webrat/core/matchers/have_content.rb
honkster-webrat-0.6.0.9 lib/webrat/core/matchers/have_content.rb
darkofabijan-webrat-0.6.1 lib/webrat/core/matchers/have_content.rb
kbaum-webrat-0.5.1 lib/webrat/core/matchers/have_content.rb
kbaum-webrat-0.6.1.pre lib/webrat/core/matchers/have_content.rb
cynergy-webrat-0.6.0 lib/webrat/core/matchers/have_content.rb
webrat-0.6.0 lib/webrat/core/matchers/have_content.rb
radar-webrat-0.5.1.1 lib/webrat/core/matchers/have_content.rb
radar-webrat-0.5.1 lib/webrat/core/matchers/have_content.rb
mkuklis-webrat-0.5.1 lib/webrat/core/matchers/have_content.rb
honkster-webrat-0.6.0 lib/webrat/core/matchers/have_content.rb