- Added MT_KWARGS_HACK kludge for stub to deal with ruby 2.7 kwargs nastiness. (tsugimoto)

[git-p4: depot-paths = "//src/minitest/dev/": change = 13458]
This commit is contained in:
Ryan Davis 2022-06-25 00:20:42 -08:00
parent 6bd249b73c
commit b8ddc4fa44
2 changed files with 49 additions and 18 deletions

View File

@ -284,22 +284,33 @@ class Object
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

View File

@ -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!
@ -363,14 +371,6 @@ class TestMinitestMock < Minitest::Test
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
@ -838,6 +838,26 @@ class TestMinitestStub < Minitest::Test
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