Sha256: 324be05827b855a6b43f1eee7ed1163366c08b8b36712bd2b286d1293cfa8873

Contents?: true

Size: 1.91 KB

Versions: 26

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module RSpec
  module CustomMatchers
    class AddMethod < Matchers::BuiltIn::BaseMatcher
      attr_reader :method

      def initialize(method = nil)
        @method = method
      end

      def to(instance = nil, &block)
        AddMethodTo.new(instance, method, &block)
      end
    end

    class AddMethodTo < Matchers::BuiltIn::BaseMatcher
      attr_reader :method, :instance, :block

      def initialize(instance, method, &block)
        @instance = instance
        @method = method
        @block = block
      end

      def description
        "Adds method #{method} to #{instance_class}"
      end

      def failure_message
        "expected #{method} to be added to #{instance_class} but " \
          "#{@initial_state ? 'it already existed' : "it didn't"}"
      end

      def matches?(event_proc)
        @event_proc = event_proc
        return false unless event_proc.is_a?(Proc)

        raise_block_syntax_error if block_given?
        perform_change(event_proc)
        changed?
      end

      def supports_block_expectations?
        true
      end

      private

      def changed?
        !@initial_state && @final_state
      end

      def perform_change(event_proc)
        @initial_state = evaluated_instance.respond_to?(method)
        @evaluated_instance = nil
        event_proc.call
        @final_state = evaluated_instance.respond_to?(method)
      end

      def evaluated_instance
        @evaluated_instance ||= instance || block.call
      end

      def instance_class
        @instance_class ||= evaluated_instance.class
      end

      def raise_block_syntax_error
        raise SyntaxError, 'Block not received by the' \
        '`add_method_to` matcher. ' \
        'Perhaps you want to use `{ ... }` instead of do/end?'
      end
    end
  end
end

module RSpec
  module Matchers
    def add_method(_method)
      CustomMatchers::AddMethod.new(:index)
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
azeroth-0.3.0 spec/support/matchers/add_method.rb
azeroth-0.2.0 spec/support/matchers/add_method.rb
azeroth-0.1.0 spec/support/matchers/add_method.rb
azeroth-0.0.7 spec/support/matchers/add_method.rb
azeroth-0.0.6 spec/support/matchers/add_method.rb
azeroth-0.0.5 spec/support/matchers/add_method.rb