Sha256: c6b8346053d523a5e5e77006539cbc44e1ad89012da182fb672c8610dfb59f3b

Contents?: true

Size: 1.95 KB

Versions: 20

Compression:

Stored size: 1.95 KB

Contents

module Webrat
  module Matchers
  
    class HasContent #:nodoc:
      def initialize(content)
        @content = content
      end
      
      def matches?(stringlike)
        if Webrat.configuration.parse_with_nokogiri?
          @document = Webrat.nokogiri_document(stringlike)
        else
          @document = Webrat.hpricot_document(stringlike)
        end
        
        @element = Webrat::XML.inner_text(@document)
      
        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

20 entries across 20 versions & 8 rubygems

Version Path
diabolo-webrat-0.4.2 lib/webrat/core/matchers/have_content.rb
hectoregm-webrat-0.4.2 lib/webrat/core/matchers/have_content.rb
jferris-webrat-0.4.3.0.1238640835 lib/webrat/core/matchers/have_content.rb
jferris-webrat-0.4.3 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.2.1 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.2.2 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.2.3 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.2.4 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.2.5 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.2 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.3.2 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.3.3 lib/webrat/core/matchers/have_content.rb
jwilger-webrat-0.4.3.4 lib/webrat/core/matchers/have_content.rb
mattwynne-cucover-0.1.0 examples/self_test/rails/vendor/gems/webrat-0.4.4/lib/webrat/core/matchers/have_content.rb
mattwynne-cucover-0.1.1 examples/self_test/rails/vendor/gems/webrat-0.4.4/lib/webrat/core/matchers/have_content.rb
mmurphy-webrat-0.4.4 lib/webrat/core/matchers/have_content.rb
integrity-0.1.9 vendor/webrat/lib/webrat/core/matchers/have_content.rb
webrat-0.4.2 lib/webrat/core/matchers/have_content.rb
webrat-0.4.4 lib/webrat/core/matchers/have_content.rb
webrat-0.4.3 lib/webrat/core/matchers/have_content.rb