lib/much-stub.rb in much-stub-0.1.1 vs lib/much-stub.rb in much-stub-0.1.2

- old
+ new

@@ -1,9 +1,8 @@ require "much-stub/version" module MuchStub - def self.stubs @stubs ||= {} end def self.stub_key(obj, meth) @@ -35,12 +34,19 @@ raise NotStubbedError, "`#{meth}` not stubbed.", orig_caller.map(&:to_s) end stub.call_method(args, &block) end - class Stub + def self.tap(obj, meth, &tap_block) + self.stub(obj, meth) { |*args, &block| + self.stub_send(obj, meth, *args, &block).tap { |value| + tap_block.call(value, *args, &block) if tap_block + } + } + end + class Stub def self.key(object, method_name) "--#{object.object_id}--#{method_name}--" end attr_reader :method_name, :name, :ivar_name, :do @@ -98,11 +104,11 @@ @metaclass.send(:alias_method, @method_name, @name) @metaclass.send(:undef_method, @name) end def inspect - "#<#{self.class}:#{'0x0%x' % (object_id << 1)}" \ + "#<#{self.class}:#{"0x0%x" % (object_id << 1)}" \ " @method_name=#{@method_name.inspect}" \ ">" end private @@ -157,21 +163,20 @@ def inspect_lookup_stubs @lookup.keys.map{ |args| " - #{inspect_call(args)}" }.join("\n") end def inspect_call(args) - "`#{@method_name}(#{args.map(&:inspect).join(',')})`" + "`#{@method_name}(#{args.map(&:inspect).join(",")})`" end def number_of_args(arity) if arity < 0 "at least #{(arity + 1).abs}" else arity end end - end StubError = Class.new(ArgumentError) NotStubbedError = Class.new(StubError) StubArityError = Class.new(StubError) @@ -179,19 +184,18 @@ NullStub = Class.new do def teardown; end # no-op end module ParameterList + LETTERS = ("a".."z").to_a.freeze - LETTERS = ('a'..'z').to_a.freeze - def self.new(object, method_name) arity = get_arity(object, method_name) params = build_params_from_arity(arity) - params << '*args' if arity < 0 - params << '&block' - params.join(', ') + params << "*args" if arity < 0 + params << "&block" + params.join(", ") end private def self.get_arity(object, method_name) @@ -208,12 +212,10 @@ def self.get_param_name(param_index) param_index += LETTERS.size # avoid getting 0 for the number of letters number_of_letters, letter_index = param_index.divmod(LETTERS.size) LETTERS[letter_index] * number_of_letters end - end - end # Kernel#caller_locations polyfill for pre ruby 2.0.0 if RUBY_VERSION =~ /\A1\..+/ && !Kernel.respond_to?(:caller_locations) module Kernel