提交 ba106986 编写于 作者: R Ryuta Kamizono

Fix `with_options` to allow string key options

上级 fcb5e9d5
......@@ -24,9 +24,20 @@ def method_missing(method, *arguments, &block)
options = @options
end
if options
@context.__send__(method, *arguments, **options, &block)
else
invoke_method(method, arguments, options, &block)
end
if RUBY_VERSION >= "2.7"
def invoke_method(method, arguments, options, &block)
if options
@context.__send__(method, *arguments, **options, &block)
else
@context.__send__(method, *arguments, &block)
end
end
else
def invoke_method(method, arguments, options, &block)
arguments << options if options
@context.__send__(method, *arguments, &block)
end
end
......
......@@ -8,13 +8,21 @@ def setup
@options = { hello: "world" }
end
def test_method_with_options_merges_string_options
local_options = { "cool" => true }
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
end
end
def test_method_with_options_merges_options_when_options_are_present
local_options = { cool: true }
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options),
o.method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_kwargs(local_options)
assert_equal @options.merge(local_options), o.method_with_kwargs_only(local_options)
end
......@@ -35,13 +43,11 @@ def test_method_with_options_allows_to_overwrite_options
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options),
o.method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
assert_equal local_options, o.method_with_options(local_options)
end
with_options(local_options) do |o|
assert_equal local_options.merge(@options),
o.method_with_options(@options)
assert_equal local_options.merge(@options), o.method_with_options(@options)
end
end
......@@ -75,8 +81,7 @@ def test_nested_method_with_options_containing_hashes_going_deep
def test_nested_method_with_options_using_lambda
local_lambda = lambda { { lambda: true } }
with_options(@options) do |o|
assert_equal @options.merge(local_lambda.call),
o.method_with_options(local_lambda).call
assert_equal @options.merge(local_lambda.call), o.method_with_options(local_lambda).call
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册