lib/rubanok/rspec.rb in rubanok-0.1.3 vs lib/rubanok/rspec.rb in rubanok-0.2.0

- old
+ new

@@ -1,56 +1,70 @@ # frozen_string_literal: true require "rspec/mocks" module Rubanok - class HavePlanished < RSpec::Matchers::BuiltIn::BaseMatcher + class HaveProcessed < RSpec::Matchers::BuiltIn::BaseMatcher include RSpec::Mocks::ExampleMethods - attr_reader :data_class, :plane, :matcher + attr_reader :data_class, :processor, :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) + @name = "have_rubanok_processed" end - def with(plane) - @plane = plane + def as(alias_name) + @name = alias_name self end + def with(processor) + @processor = processor + self + end + def supports_block_expectations? true end def matches?(proc) - raise ArgumentError, "have_planished only supports block expectations" unless Proc === proc + raise ArgumentError, "#{name} only supports block expectations" unless Proc === proc - raise ArgumentError, "Plane class is required. Please, specify it using `.with` modifier" if plane.nil? + raise ArgumentError, "Processor class is required. Please, specify it using `.with` modifier" if processor.nil? - allow(plane).to receive(:call).and_call_original + allow(processor).to receive(:call).and_call_original proc.call matcher.with(an_instance_of(data_class), anything) if data_class - matcher.matches?(plane) + matcher.matches?(processor) end def failure_message - "expected to use #{plane.name}#{data_class ? " for #{data_class.name}" : ""}, but didn't" + "expected to use #{processor.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" + "expected not to use #{processor.name}#{data_class ? " for #{data_class.name} " : ""}, but have used" end + + private + + attr_reader :name end end RSpec.configure do |config| config.include(Module.new do + def have_rubanok_processed(*args) + Rubanok::HaveProcessed.new(*args) + end + def have_planished(*args) - Rubanok::HavePlanished.new(*args) + Rubanok::HaveProcessed.new(*args).as("have_planished") end end) end