fix warning related to keywoard argument (#45)

* add workaround to avoid warnings related to keyword arguments (refs: #44)
This commit is contained in:
Taichi Ishitani 2020-12-18 02:42:10 +09:00 committed by GitHub
parent eec6ac6c4d
commit be7a98833e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 9 deletions

View File

@ -82,17 +82,25 @@ module Docile
# Proxy all methods, excluding {NON_PROXIED_METHODS}, first to `receiver`
# and then to `fallback` if not found.
def method_missing(method, *args, &block)
if @__receiver__.respond_to?(method.to_sym)
@__receiver__.__send__(method.to_sym, *args, &block)
args_string =
if RUBY_VERSION >= "2.7.0"
"*args, **kwargs, &block"
else
begin
@__fallback__.__send__(method.to_sym, *args, &block)
rescue NoMethodError => e
e.extend(BacktraceFilter)
raise e
"*args, &block"
end
class_eval(<<-METHOD)
def method_missing(method, #{args_string})
if @__receiver__.respond_to?(method.to_sym)
@__receiver__.__send__(method.to_sym, #{args_string})
else
begin
@__fallback__.__send__(method.to_sym, #{args_string})
rescue NoMethodError => e
e.extend(BacktraceFilter)
raise e
end
end
end
end
METHOD
end
end

View File

@ -435,6 +435,36 @@ describe Docile do
end
end
end
if RUBY_VERSION >= "2.0.0"
context "when a DSL method has a keyword argument" do
class DSLMethodWithKeywordArgument
attr_reader :v0, :v1, :v2
class_eval(<<-METHOD)
def set(v0, v1:, v2:)
@v0 = v0
@v1 = v1
@v2 = v2
end
METHOD
end
let(:dsl) do
DSLMethodWithKeywordArgument.new
end
it "calls such DSL methods with no stderr output" do
# This is to check warnings related to keyword argument is not output.
# See: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
expect { Docile.dsl_eval(dsl) { set(0, v2: 2, v1: 1) } }.
not_to output.to_stderr
expect(dsl.v0).to eq 0
expect(dsl.v1).to eq 1
expect(dsl.v2).to eq 2
end
end
end
end
describe ".dsl_eval_with_block_return" do