Sha256: f88fb2b6efb9c713e6346cd5784f56d1d301616ce4f9a896a96946317157fd4d

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require "rspec/mocks"

module Rubanok
  class HavePlanished < RSpec::Matchers::BuiltIn::BaseMatcher
    include RSpec::Mocks::ExampleMethods

    attr_reader :data_class, :plane, :matcher

    def initialize(data_class = nil)
      if data_class
        @data_class = data_class.is_a?(Module) ? data_class : data_class.class
      end
      @matcher = have_received(:call)
    end

    def with(plane)
      @plane = plane
      self
    end

    def supports_block_expectations?
      true
    end

    def matches?(proc)
      raise ArgumentError, "have_planished only supports block expectations" unless Proc === proc

      raise ArgumentError, "Plane class is required. Please, specify it using `.with` modifier" if plane.nil?

      allow(plane).to receive(:call).and_call_original
      proc.call

      matcher.with(an_instance_of(data_class), anything) if data_class

      matcher.matches?(plane)
    end

    def failure_message
      "expected to use #{plane.name}#{data_class ? " for #{data_class.name}" : ""}, but didn't"
    end

    def failure_message_when_negated
      "expected not to use #{plane.name}#{data_class ? " for #{data_class.name} " : ""}, but have used"
    end
  end
end

RSpec.configure do |config|
  config.include(Module.new do
    def have_planished(*args)
      Rubanok::HavePlanished.new(*args)
    end
  end)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubanok-0.1.3 lib/rubanok/rspec.rb
rubanok-0.1.1 lib/rubanok/rspec.rb