Skip to content

Commit

Permalink
- Added MT_KWARGS_HACK kludge for stub to deal with ruby 2.7 kwargs n…
Browse files Browse the repository at this point in the history
…astiness. (tsugimoto)

[git-p4: depot-paths = "//src/minitest/dev/": change = 13458]
  • Loading branch information
zenspider committed Jun 25, 2022
1 parent 6bd249b commit b8ddc4f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
31 changes: 21 additions & 10 deletions lib/minitest/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,33 @@ def stub name, val_or_callable, *block_args, **block_kwargs, &block

metaclass.send :alias_method, new_name, name

metaclass.send :define_method, name do |*args, **kwargs, &blk|
if val_or_callable.respond_to? :call then
if kwargs.empty? then # FIX: drop this after 2.7 dead
if ENV["MT_KWARGS_HAC\K"] then
metaclass.send :define_method, name do |*args, &blk|
if val_or_callable.respond_to? :call then
val_or_callable.call(*args, &blk)
else
val_or_callable.call(*args, **kwargs, &blk)
blk.call(*block_args, **block_kwargs) if blk
val_or_callable
end
else
if blk then
if block_kwargs.empty? then # FIX: drop this after 2.7 dead
blk.call(*block_args)
end
else
metaclass.send :define_method, name do |*args, **kwargs, &blk|
if val_or_callable.respond_to? :call then
if kwargs.empty? then # FIX: drop this after 2.7 dead
val_or_callable.call(*args, &blk)
else
blk.call(*block_args, **block_kwargs)
val_or_callable.call(*args, **kwargs, &blk)
end
else
if blk then
if block_kwargs.empty? then # FIX: drop this after 2.7 dead
blk.call(*block_args)
else
blk.call(*block_args, **block_kwargs)
end
end
val_or_callable
end
val_or_callable
end
end

Expand Down
36 changes: 28 additions & 8 deletions test/minitest/test_minitest_mock.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
require "minitest/autorun"

def with_kwargs_env
ENV["MT_KWARGS_HAC\K"] = "1"

yield
ensure
ENV.delete "MT_KWARGS_HAC\K"
end

class TestMinitestMock < Minitest::Test
parallelize_me!

Expand Down Expand Up @@ -363,14 +371,6 @@ def test_mock_block_is_passed_keyword_args__args
assert_mock mock
end

def with_kwargs_env
ENV["MT_KWARGS_HAC\K"] = "1"

yield
ensure
ENV.delete "MT_KWARGS_HAC\K"
end

def test_mock_allow_all_kwargs__old_style_env
with_kwargs_env do
mock = Minitest::Mock.new
Expand Down Expand Up @@ -838,6 +838,26 @@ def test_stub_callable_keyword_args
end
end

def test_stub__hash_as_last_real_arg__FUCK
with_kwargs_env do
token = Object.new
def token.create_with_retry u, p; raise "shouldn't see this"; end

controller = Object.new
controller.define_singleton_method :create do |u, p|
token.create_with_retry u, p
end

params = Object.new
def params.to_hash; raise "nah"; end

token.stub(:create_with_retry, ->(u, p) { 42 }) do
act = controller.create :u, params
@tc.assert_equal 42, act
end
end
end

def test_stub_callable_block_5 # from tenderlove
@assertion_count += 1
Foo.stub5 :blocking, Bar.new do
Expand Down

0 comments on commit b8ddc4f

Please sign in to comment.