Sha256: 7913ce15b42e28c3fb4f4fe727d0753910517e54da908a7c577fc71f168e705b

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module RSpec
  module LiveControllers
    module Matchers
      class Base
        attr_reader :actual_matcher, :response_body, :target_response, :list_of_matchers

        def initialize(actual_matcher)
          @actual_matcher = actual_matcher
        end

        def matches?(target_response)
          @target_response = target_response
          @response_body = extract_response_body(target_response)
          row = target_response_row(actual_matcher)
          json = target_response_json(actual_matcher)
          string = target_response_string(actual_matcher)
          check_matches?(response_body, row, json, string)
        end

        def failure_message
          raise 'Not implemented'
        end

        def failure_message_when_negated
          raise 'Not implemented'
        end

        protected

        def target_response_json(actual_matcher)
          return unless actual_matcher.respond_to?(:to_json)
          actual_matcher.to_json
        end

        def target_response_string(actual_matcher)
          actual_matcher.to_s
        end

        def target_response_row(actual_matcher)
          return actual_matcher if actual_matcher.is_a?(String) || actual_matcher.is_a?(Regexp)
        end

        def extract_response_body(target_response)
          if target_response.is_a?(ActionDispatch::Response::Buffer)
            target_response.instance_variable_get(:@buf).join('')
          end
        end

        def regexp(reg)
          raise 'Not implemented'
        end

        def check_matches?(response_body, *matchers)
          @list_of_matchers = matchers.compact
          list_of_matchers.reduce(false) do |acc, value|
            reg = Regexp.quote(value)
            acc ||= response_body.match?(regexp(reg))
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-live_controllers-0.0.1 lib/rspec/live_controllers/matchers/base.rb