Skip to content

Commit

Permalink
Merge pull request #26 from doudou/pass_thru_block_handling_3_0
Browse files Browse the repository at this point in the history
fix: handling of blocks in pass_thru (v3.0)
  • Loading branch information
doudou authored Sep 9, 2024
2 parents 94ea6cc + 958849b commit f9cddbb
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
- "3.2"
- "3.1"
- "3.0"
- "2.7"

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion lib/flexmock/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def flexmock_calls

# Invocke the original non-mocked functionality for the given
# symbol.
def flexmock_invoke_original(method_name, args, kw = {})
def flexmock_invoke_original(method_name, args, kw = {}, orig_block = nil)
return FlexMock.undefined
end

Expand Down
4 changes: 2 additions & 2 deletions lib/flexmock/expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ def and_throw(sym, value=nil)

def pass_thru(&block)
block ||= lambda { |value| value }
and_return { |*args, **kw|
and_return { |*args, **kw, &orig_block|
begin
block.call(@mock.flexmock_invoke_original(@sym, args, kw))
block.call(@mock.flexmock_invoke_original(@sym, args, kw, orig_block))
rescue NoMethodError => e
if e.name == @sym
raise e, "#{e.message} while performing #pass_thru in expectation object #{self}"
Expand Down
13 changes: 3 additions & 10 deletions lib/flexmock/partial_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ def should_expect(*args)
#
# Usually called in a #and_return statement
def invoke_original(m, *args, **kw, &block)
if block
args << block
end
flexmock_invoke_original(m, args, kw)
flexmock_invoke_original(m, args, kw, block)
end

# Whether the given method's original definition has been stored
Expand Down Expand Up @@ -336,7 +333,7 @@ def initialize_stub_remove
# (3) Apply any recorded expecations
#
def create_new_mocked_object(allocate_method, args, kw, recorder, block)
new_obj = flexmock_invoke_original(allocate_method, args, kw)
new_obj = flexmock_invoke_original(allocate_method, args, kw, nil)
mock = flexmock_container.flexmock(new_obj)
block.call(mock) unless block.nil?
recorder.apply(mock)
Expand All @@ -346,12 +343,8 @@ def create_new_mocked_object(allocate_method, args, kw, recorder, block)

# Invoke the original definition of method on the object supported by
# the stub.
def flexmock_invoke_original(method, args, kw)
def flexmock_invoke_original(method, args, kw, block)
if (original_method = find_original_method(method))
if Proc === args.last
block = args.last
args = args[0..-2]
end
original_method.call(*args, **kw, &block)
else
@obj.__send__(:method_missing, method, *args, **kw, &block)
Expand Down
14 changes: 14 additions & 0 deletions test/partial_mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,20 @@ def call(value); @value += 1 end
assert_equal 1, obj.value
end

def test_pass_thru_forwards_a_given_block
obj = Class.new do
attr_reader :block
def mocked_method(&block)
@block = block
end
end.new
flexmock(obj).should_receive(:mocked_method).with_block.pass_thru

block = obj.mocked_method { 42 }
assert_kind_of Proc, block
assert_equal block, obj.block
end

def test_should_expect
flexmock(obj = Dog.new).should_expect do |e|
e.bark
Expand Down

0 comments on commit f9cddbb

Please sign in to comment.