1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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.
This commit is contained in:
Jeremy Evans 2019-09-29 09:15:43 -07:00
parent 070cbe22b7
commit 869e4f6e4c
Notes: git 2019-09-30 02:34:32 +09:00
2 changed files with 24 additions and 8 deletions

View file

@ -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:

View file

@ -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