<commit_before># This is a wrapper of assert_select for rspec.

module Spec # :nodoc:
  module Rails
    module Matchers

      class AssertSelect #:nodoc:

        attr_reader :options

        def initialize(assertion, spec_scope, *args, &block)

          extracted = extract_options(args)
          @options = extracted[:options]
          @args = extracted[:args]

          @spec_scope = spec_scope
          @assertion = assertion
          @block = block
        end
        
        def matches?(response_or_text, &block)
          @block = block if block

          if doc = doc_from(response_or_text)
            @args.unshift(doc)
          end

          begin
            @spec_scope.__send__(@assertion, *@args, &@block)
            true
          rescue ::Test::Unit::AssertionFailedError => @error
            false
          end
        end
        
        def failure_message_for_should; @error.message; end
        def failure_message_for_should_not; "should not #{description}, but did"; end

        def description
          {
            :assert_select => "have tag#{format_args(*@args)}",
            :assert_select_email => "send email#{format_args(*@args)}",
          }[@assertion]
        end

      private

        module TestResponseOrString
          def test_response?
            ActionController::TestResponse === self and
                                               !self.headers['Content-Type'].blank? and
                                               self.headers['Content-Type'].to_sym == :xml
          end
        
          def string?
            String === self
          end
        end

        def doc_from(response_or_text)
          response_or_text.extend TestResponseOrString
          if response_or_text.test_response?
            HTML::Document.new(response_or_text.body, @options[:strict], @options[:xml]).root
          elsif response_or_text.string?
            HTML::Document.new(response_or_text, @options[:strict], @options[:xml]).root
           end
        end

        def format_args(*args)
          args.empty? ? "" : "(#{arg_list(*args)})"
        end

        def arg_list(*args)
          args.map do |arg|
            arg.respond_to?(:description) ? arg.description : arg.inspect
          end.join(", ")
        end
        
        def extract_options(args)
          opts = {:xml => false, :strict => false}
          if args.last.is_a?(::Hash)
            opts[:strict] = args.last.delete(:strict) unless args.last[:strict].nil?
            opts[:xml] = args.last.delete(:xml) unless args.last[:xml].nil?
            args.pop if args.last.empty?
          end
          {:args => args, :options => opts}
        end
        
      end
      
      # :call-seq:
      #   response.should have_tag(*args, &block)
      #   string.should have_tag(*args, &block)
      #
      # wrapper for assert_select with additional support for using
      # css selectors to set expectation on Strings. Use this in
      # helper specs, for example, to set expectations on the results
      # of helper methods. Also allow specification of how the 
      # response is parsed using the options :xml and :strict options.
      # By default, these options are set to false.
      #
      # == Examples
      #
      #   # in a controller spec
      #   response.should have_tag("div", "some text")
      #
      #   # to force xml and/or strict parsing of the response
      #   response.should have_tag("div", "some text", :xml => true)
      #   response.should have_tag("div", "some text", :strict => true)
      #   response.should have_tag("div", "some text", :xml => true, :strict => false)<commit_msg>align w/ other parts of rspec<commit_after># This is a wrapper of assert_select for rspec.

module Spec # :nodoc:
  module Rails
    module Matchers

      class AssertSelect #:nodoc:
        attr_reader :options

        def initialize(selector_assertion, spec_scope, *args, &block)
          @args, @options = args_and_options(args)
          @spec_scope = spec_scope
          @selector_assertion = selector_assertion
          @block = block
        end
        
        def matches?(response_or_text, &block)
          @block = block if block

          if doc = doc_from(response_or_text)
            @args.unshift(doc)
          end

          begin
            @spec_scope.__send__(@selector_assertion, *@args, &@block)
            true
          rescue ::Test::Unit::AssertionFailedError => @error
            false
          end
        end
        
        def failure_message_for_should; @error.message; end
        def failure_message_for_should_not; "should not #{description}, but did"; end

        def description
          {
            :assert_select => "have tag#{format_args(*@args)}",
            :assert_select_email => "send email#{format_args(*@args)}",
          }[@selector_assertion]
        end

      private

        module TestResponseOrString
          def test_response?
            ActionController::TestResponse === self and
                                               !self.headers['Content-Type'].blank? and
                                               self.headers['Content-Type'].to_sym == :xml
          end
        
          def string?
            String === self
          end
        end

        def doc_from(response_or_text)
          response_or_text.extend TestResponseOrString
          if response_or_text.test_response?
            HTML::Document.new(response_or_text.body, @options[:strict], @options[:xml]).root
          elsif response_or_text.string?
            HTML::Document.new(response_or_text, @options[:strict], @options[:xml]).root
           end
        end

        def format_args(*args)
          args.empty? ? "" : "(#{arg_list(*args)})"
        end

        def arg_list(*args)
          args.map do |arg|
            arg.respond_to?(:description) ? arg.description : arg.inspect
          end.join(", ")
        end
        
        def args_and_options(args)
          opts = {:xml => false, :strict => false}
          if args.last.is_a?(::Hash)
            opts[:strict] = args.last.delete(:strict) unless args.last[:strict].nil?
            opts[:xml]    = args.last.delete(:xml)    unless args.last[:xml].nil?
            args.pop if args.last.empty?
          end
          return [args, opts]
        end
        
      end
      
      # :call-seq:
      #   response.should have_tag(*args, &block)
      #   string.should have_tag(*args, &block)
      #
      # wrapper for assert_select with additional support for using
      # css selectors to set expectation on Strings. Use this in
      # helper specs, for example, to set expectations on the results
      # of helper methods. Also allow specification of how the 
      # response is parsed using the options :xml and :strict options.
      # By default, these options are set to false.
      #
      # == Examples
      #
      #   # in a controller spec
      #   response.should have_tag("div", "some text")
      #
      #   # to force xml and/or strict parsing of the response
      #   response.should have_tag("div", "some text", :xml => true)
      #   response.should have_tag("div", "some text", :strict => true)
      #   response.should have_tag("div", "some text", :xml => true, :strict => false)
{'fraction_non_alphanumeric': 0.10294920808301475, 'fraction_numerical': 0.0, 'mean_word_length': 1.843555900621118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2353046', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1967, 'n_words': 522}