diff options
author | Jeremy Evans <[email protected]> | 2019-09-29 09:15:43 -0700 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2019-09-29 10:34:07 -0700 |
commit | 869e4f6e4c683bf8e76ae7db54a26b33fb925410 (patch) | |
tree | 4985302b04065eb0e42156006f60d8b191632e6b | |
parent | 070cbe22b70ec2bec36c7cfc84b726510afa306f (diff) |
Fix or suppress keyword argument separation warnings in util_spec
Some warnings are because the @o.rb_scan_args call doesn't
include keyword arguments, but the first argument is passed to
rb_scan_args may have a last hash treated as keywords. Those
should be handled using rb_scan_args_kw on Ruby 2.7.
Other warnings are for the deprecated rb_scan_args behavior to
split option hashes or treat a nil argument as an option hash.
Those warnings should just be suppressed.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/2504
-rw-r--r-- | spec/ruby/optional/capi/ext/util_spec.c | 9 | ||||
-rw-r--r-- | spec/ruby/optional/capi/util_spec.rb | 23 |
2 files changed, 24 insertions, 8 deletions
diff --git a/spec/ruby/optional/capi/ext/util_spec.c b/spec/ruby/optional/capi/ext/util_spec.c index 50a5fcb095..e579b5e0af 100644 --- a/spec/ruby/optional/capi/ext/util_spec.c +++ b/spec/ruby/optional/capi/ext/util_spec.c @@ -16,7 +16,14 @@ VALUE util_spec_rb_scan_args(VALUE self, VALUE argv, VALUE fmt, VALUE expected, args[i] = rb_ary_entry(argv, i); } - result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6); + if (*RSTRING_PTR(fmt) == 'k') { +#ifdef RB_SCAN_ARGS_KEYWORDS + result = rb_scan_args_kw(RB_SCAN_ARGS_KEYWORDS, argc, args, RSTRING_PTR(fmt)+1, &a1, &a2, &a3, &a4, &a5, &a6); +#endif + } + else { + result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6); + } switch(NUM2INT(expected)) { case 6: diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb index ee48e2e0ea..3556c8c010 100644 --- a/spec/ruby/optional/capi/util_spec.rb +++ b/spec/ruby/optional/capi/util_spec.rb @@ -11,6 +11,7 @@ describe "C-API Util function" do before :each do @prc = -> { 1 } @acc = [] + @keyword_prefix = 'k' if RUBY_VERSION >= '2.7' ScratchPad.record @acc end @@ -99,13 +100,13 @@ describe "C-API Util function" do it "assigns Hash arguments" do h = {a: 1, b: 2} - @o.rb_scan_args([h], "0:", 1, @acc).should == 0 + @o.rb_scan_args([h], "#{@keyword_prefix}0:", 1, @acc).should == 0 ScratchPad.recorded.should == [h] end it "assigns required and Hash arguments" do h = {a: 1, b: 2} - @o.rb_scan_args([1, h], "1:", 2, @acc).should == 1 + @o.rb_scan_args([1, h], "#{@keyword_prefix}1:", 2, @acc).should == 1 ScratchPad.recorded.should == [1, h] end @@ -115,7 +116,9 @@ describe "C-API Util function" do end it "assigns required and Hash arguments with nil Hash" do - @o.rb_scan_args([1, nil], "1:", 2, @acc).should == 1 + suppress_warning do + @o.rb_scan_args([1, nil], "1:", 2, @acc).should == 1 + end ScratchPad.recorded.should == [1, nil] end @@ -126,7 +129,7 @@ describe "C-API Util function" do it "assigns required, optional, splat, post-splat, Hash and block arguments" do h = {a: 1, b: 2} - @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 5 + @o.rb_scan_args([1, 2, 3, 4, 5, h], "#{@keyword_prefix}11*1:&", 6, @acc, &@prc).should == 5 ScratchPad.recorded.should == [1, 2, [3, 4], 5, h, @prc] end @@ -134,7 +137,9 @@ describe "C-API Util function" do it "rejects non-keyword arguments" do h = {1 => 2, 3 => 4} -> { - @o.rb_scan_args([h], "0:", 1, @acc) + suppress_warning do + @o.rb_scan_args([h], "#{@keyword_prefix}0:", 1, @acc) + end }.should raise_error(ArgumentError) ScratchPad.recorded.should == [] end @@ -142,14 +147,18 @@ describe "C-API Util function" do it "rejects required and non-keyword arguments" do h = {1 => 2, 3 => 4} -> { - @o.rb_scan_args([1, h], "1:", 2, @acc) + suppress_warning do + @o.rb_scan_args([1, h], "#{@keyword_prefix}1:", 2, @acc) + end }.should raise_error(ArgumentError) ScratchPad.recorded.should == [] end it "considers the hash as a post argument when there is a splat" do h = {1 => 2, 3 => 4} - @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 6 + suppress_warning do + @o.rb_scan_args([1, 2, 3, 4, 5, h], "#{@keyword_prefix}11*1:&", 6, @acc, &@prc).should == 6 + end ScratchPad.recorded.should == [1, 2, [3, 4, 5], h, nil, @prc] end end |