Sha256: e95ea84151afffc70fef20785eaca32ceaffe425c888afe4df0e2e27cc65cec5

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 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)
      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

1 entries across 1 versions & 1 rubygems

Version Path
rubanok-0.1.0 lib/rubanok/rspec.rb