aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-09-25 11:18:49 -0700
committerGitHub <noreply@github.com>2019-09-25 11:18:49 -0700
commit80b5a0ff2a7709367178f29d4ebe1c54122b1c27 (patch)
treeb621904ab5423349c9158a4f857d3f34064dcd2a /test
parent4755e23d2e43a0a16ae70be83097dcae050bc0c8 (diff)
downloadruby-80b5a0ff2a7709367178f29d4ebe1c54122b1c27.tar.gz
Make rb_scan_args handle keywords more similar to Ruby methods (#2460)
Cfuncs that use rb_scan_args with the : entry suffer similar keyword argument separation issues that Ruby methods suffer if the cfuncs accept optional or variable arguments. This makes the following changes to : handling. * Treats as **kw, prompting keyword argument separation warnings if called with a positional hash. * Do not look for an option hash if empty keywords are provided. For backwards compatibility, treat an empty keyword splat as a empty mandatory positional hash argument, but emit a a warning, as this behavior will be removed in Ruby 3. The argument number check needs to be moved lower so it can correctly handle an empty positional argument being added. * If the last argument is nil and it is necessary to treat it as an option hash in order to make sure all arguments are processed, continue to treat the last argument as the option hash. Emit a warning in this case, as this behavior will be removed in Ruby 3. * If splitting the keyword hash into two hashes, issue a warning, as we will not be splitting hashes in Ruby 3. * If the keyword argument is required to fill a mandatory positional argument, continue to do so, but emit a warning as this behavior will be going away in Ruby 3. * If keyword arguments are provided and the last argument is not a hash, that indicates something wrong. This can happen if a cfunc is calling rb_scan_args multiple times, and providing arguments that were not passed to it from Ruby. Callers need to switch to the new rb_scan_args_kw function, which allows passing of whether keywords were provided. This commit fixes all warnings caused by the changes above. It switches some function calls to *_kw versions with appropriate kw_splat flags. If delegating arguments, RB_PASS_CALLED_KEYWORDS is used. If creating new arguments, RB_PASS_KEYWORDS is used if the last argument is a hash to be treated as keywords. In open_key_args in io.c, use rb_scan_args_kw. In this case, the arguments provided come from another C function, not Ruby. The last argument may or may not be a hash, so we can't set keyword argument mode. However, if it is a hash, we don't want to warn when treating it as keywords. In Ruby files, make sure to appropriately use keyword splats or literal keywords when calling Cfuncs that now issue keyword argument separation warnings through rb_scan_args. Also, make sure not to pass nil in place of an option hash. Work around Kernel#warn warnings due to problems in the Rubygems override of the method. There is an open pull request to fix these issues in Rubygems, but part of the Rubygems tests for their override fail on ruby-head due to rb_scan_args not recognizing empty keyword splats, which this commit fixes. Implementation wise, adding rb_scan_args_kw is kind of a pain, because rb_scan_args takes a variable number of arguments. In order to not duplicate all the code, the function internals need to be split into two functions taking a va_list, and to avoid passing in a ton of arguments, a single struct argument is used to handle the variables previously local to the function.
Diffstat (limited to 'test')
-rw-r--r--test/-ext-/test_scan_args.rb137
-rw-r--r--test/pathname/test_pathname.rb2
-rw-r--r--test/reline/helper.rb2
-rw-r--r--test/ruby/test_dir_m17n.rb36
-rw-r--r--test/ruby/test_econv.rb7
-rw-r--r--test/ruby/test_exception.rb8
-rw-r--r--test/ruby/test_io.rb12
-rw-r--r--test/ruby/test_io_m17n.rb3
-rw-r--r--test/ruby/test_literal.rb4
-rw-r--r--test/ruby/test_numeric.rb7
-rw-r--r--test/ruby/test_string.rb4
11 files changed, 166 insertions, 56 deletions
diff --git a/test/-ext-/test_scan_args.rb b/test/-ext-/test_scan_args.rb
index cb2dab5760..949495dd0d 100644
--- a/test/-ext-/test_scan_args.rb
+++ b/test/-ext-/test_scan_args.rb
@@ -93,7 +93,14 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([1, "a", nil], Bug::ScanArgs.lead_hash("a"))
assert_raise(ArgumentError) {Bug::ScanArgs.lead_hash("a", "b")}
assert_equal([1, "a", {b: 1}], Bug::ScanArgs.lead_hash("a", b: 1))
- assert_equal([1, {b: 1}, nil], Bug::ScanArgs.lead_hash(b: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, {b: 1}, nil], Bug::ScanArgs.lead_hash(b: 1))
+ end
+ assert_equal([1, {"a"=>0, b: 1}, nil], Bug::ScanArgs.lead_hash({"a"=>0, b: 1}, **{}))
+ assert_raise(ArgumentError) {Bug::ScanArgs.lead_hash(1, {"a"=>0, b: 1}, **{})}
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, {}, nil], Bug::ScanArgs.lead_hash(**{}))
+ end
end
def test_opt_hash
@@ -102,7 +109,10 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([0, nil, {b: 1}], Bug::ScanArgs.opt_hash(b: 1))
assert_equal([1, "a", {b: 1}], Bug::ScanArgs.opt_hash("a", b: 1))
assert_raise(ArgumentError) {Bug::ScanArgs.opt_hash("a", "b")}
- assert_equal([1, {"a"=>0}, {b: 1}], Bug::ScanArgs.opt_hash("a"=>0, b: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([1, {"a"=>0}, {b: 1}], Bug::ScanArgs.opt_hash("a"=>0, b: 1))
+ end
+ assert_equal([1, {"a"=>0, b: 1}, nil], Bug::ScanArgs.opt_hash({"a"=>0, b: 1}, **{}))
end
def test_lead_opt_hash
@@ -110,9 +120,13 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, "a", "b", nil], Bug::ScanArgs.lead_opt_hash("a", "b"))
assert_equal([1, "a", nil, {c: 1}], Bug::ScanArgs.lead_opt_hash("a", c: 1))
assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.lead_opt_hash("a", "b", c: 1))
- assert_equal([1, {c: 1}, nil, nil], Bug::ScanArgs.lead_opt_hash(c: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, {c: 1}, nil, nil], Bug::ScanArgs.lead_opt_hash(c: 1))
+ end
assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_hash("a", "b", "c")}
- assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_hash("a", "b"=>0, c: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_hash("a", "b"=>0, c: 1))
+ end
end
def test_var_hash
@@ -120,7 +134,9 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([1, ["a"], nil], Bug::ScanArgs.var_hash("a"))
assert_equal([1, ["a"], {b: 1}], Bug::ScanArgs.var_hash("a", b: 1))
assert_equal([0, [], {b: 1}], Bug::ScanArgs.var_hash(b: 1))
- assert_equal([1, [{"a"=>0}], {b: 1}], Bug::ScanArgs.var_hash("a"=>0, b: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([1, [{"a"=>0}], {b: 1}], Bug::ScanArgs.var_hash("a"=>0, b: 1))
+ end
end
def test_lead_var_hash
@@ -129,9 +145,13 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, "a", ["b"], nil], Bug::ScanArgs.lead_var_hash("a", "b"))
assert_equal([2, "a", ["b"], {c: 1}], Bug::ScanArgs.lead_var_hash("a", "b", c: 1))
assert_equal([1, "a", [], {c: 1}], Bug::ScanArgs.lead_var_hash("a", c: 1))
- assert_equal([1, {c: 1}, [], nil], Bug::ScanArgs.lead_var_hash(c: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, {c: 1}, [], nil], Bug::ScanArgs.lead_var_hash(c: 1))
+ end
assert_equal([3, "a", ["b", "c"], nil], Bug::ScanArgs.lead_var_hash("a", "b", "c"))
- assert_equal([2, "a", [{"b"=>0}], {c: 1}], Bug::ScanArgs.lead_var_hash("a", "b"=>0, c: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([2, "a", [{"b"=>0}], {c: 1}], Bug::ScanArgs.lead_var_hash("a", "b"=>0, c: 1))
+ end
end
def test_opt_var_hash
@@ -142,7 +162,9 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([1, "a", [], {c: 1}], Bug::ScanArgs.opt_var_hash("a", c: 1))
assert_equal([0, nil, [], {c: 1}], Bug::ScanArgs.opt_var_hash(c: 1))
assert_equal([3, "a", ["b", "c"], nil], Bug::ScanArgs.opt_var_hash("a", "b", "c"))
- assert_equal([2, "a", [{"b"=>0}], {c: 1}], Bug::ScanArgs.opt_var_hash("a", "b"=>0, c: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([2, "a", [{"b"=>0}], {c: 1}], Bug::ScanArgs.opt_var_hash("a", "b"=>0, c: 1))
+ end
end
def test_lead_opt_var_hash
@@ -151,10 +173,14 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, "a", "b", [], nil], Bug::ScanArgs.lead_opt_var_hash("a", "b"))
assert_equal([2, "a", "b", [], {c: 1}], Bug::ScanArgs.lead_opt_var_hash("a", "b", c: 1))
assert_equal([1, "a", nil, [], {c: 1}], Bug::ScanArgs.lead_opt_var_hash("a", c: 1))
- assert_equal([1, {c: 1}, nil, [], nil], Bug::ScanArgs.lead_opt_var_hash(c: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, {c: 1}, nil, [], nil], Bug::ScanArgs.lead_opt_var_hash(c: 1))
+ end
assert_equal([3, "a", "b", ["c"], nil], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c"))
assert_equal([3, "a", "b", ["c"], {d: 1}], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c", d: 1))
- assert_equal([3, "a", "b", [{"c"=>0}], {d: 1}], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c"=>0, d: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([3, "a", "b", [{"c"=>0}], {d: 1}], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c"=>0, d: 1))
+ end
end
def test_opt_trail_hash
@@ -163,9 +189,13 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, "a", "b", nil], Bug::ScanArgs.opt_trail_hash("a", "b"))
assert_equal([1, nil, "a", {c: 1}], Bug::ScanArgs.opt_trail_hash("a", c: 1))
assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.opt_trail_hash("a", "b", c: 1))
- assert_equal([1, nil, {c: 1}, nil], Bug::ScanArgs.opt_trail_hash(c: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, nil, {c: 1}, nil], Bug::ScanArgs.opt_trail_hash(c: 1))
+ end
assert_raise(ArgumentError) {Bug::ScanArgs.opt_trail_hash("a", "b", "c")}
- assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.opt_trail_hash("a", "b"=>0, c: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.opt_trail_hash("a", "b"=>0, c: 1))
+ end
end
def test_lead_opt_trail_hash
@@ -173,12 +203,16 @@ class TestScanArgs < Test::Unit::TestCase
assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail_hash("a")}
assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail_hash(c: 1)}
assert_equal([2, "a", nil, "b", nil], Bug::ScanArgs.lead_opt_trail_hash("a", "b"))
- assert_equal([2, "a", nil, {c: 1}, nil], Bug::ScanArgs.lead_opt_trail_hash("a", c: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([2, "a", nil, {c: 1}, nil], Bug::ScanArgs.lead_opt_trail_hash("a", c: 1))
+ end
assert_equal([2, "a", nil, "b", {c: 1}], Bug::ScanArgs.lead_opt_trail_hash("a", "b", c: 1))
assert_equal([3, "a", "b", "c", nil], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c"))
assert_equal([3, "a", "b", "c", {c: 1}], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c", c: 1))
assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c", "d")}
- assert_equal([3, "a", "b", {"c"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c"=>0, c: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([3, "a", "b", {"c"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c"=>0, c: 1))
+ end
end
def test_var_trail_hash
@@ -187,45 +221,104 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, ["a"], "b", nil], Bug::ScanArgs.var_trail_hash("a", "b"))
assert_equal([1, [], "a", {c: 1}], Bug::ScanArgs.var_trail_hash("a", c: 1))
assert_equal([2, ["a"], "b", {c: 1}], Bug::ScanArgs.var_trail_hash("a", "b", c: 1))
- assert_equal([1, [], {c: 1}, nil], Bug::ScanArgs.var_trail_hash(c: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, [], {c: 1}, nil], Bug::ScanArgs.var_trail_hash(c: 1))
+ end
assert_equal([3, ["a", "b"], "c", nil], Bug::ScanArgs.var_trail_hash("a", "b", "c"))
assert_equal([3, ["a", "b"], "c", {c: 1}], Bug::ScanArgs.var_trail_hash("a", "b", "c", c: 1))
- assert_equal([3, ["a", "b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.var_trail_hash("a", "b", "c"=>0, c: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([3, ["a", "b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.var_trail_hash("a", "b", "c"=>0, c: 1))
+ end
end
def test_lead_var_trail_hash
assert_raise(ArgumentError) {Bug::ScanArgs.lead_var_trail_hash()}
assert_raise(ArgumentError) {Bug::ScanArgs.lead_var_trail_hash("a")}
assert_raise(ArgumentError) {Bug::ScanArgs.lead_var_trail_hash(c: 1)}
- assert_equal([2, "a", [], {c: 1}, nil], Bug::ScanArgs.lead_var_trail_hash("a", c: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([2, "a", [], {c: 1}, nil], Bug::ScanArgs.lead_var_trail_hash("a", c: 1))
+ end
assert_equal([2, "a", [], "b", nil], Bug::ScanArgs.lead_var_trail_hash("a", "b"))
assert_equal([2, "a", [], "b", {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", c: 1))
assert_equal([3, "a", ["b"], "c", nil], Bug::ScanArgs.lead_var_trail_hash("a", "b", "c"))
assert_equal([3, "a", ["b"], "c", {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", "c", c: 1))
- assert_equal([3, "a", ["b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", c: 1, "c"=>0))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([3, "a", ["b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", c: 1, "c"=>0))
+ end
end
def test_opt_var_trail_hash
assert_raise(ArgumentError) {Bug::ScanArgs.opt_var_trail_hash()}
assert_equal([1, nil, [], "a", nil], Bug::ScanArgs.opt_var_trail_hash("a"))
- assert_equal([1, nil, [], {c: 1}, nil], Bug::ScanArgs.opt_var_trail_hash(c: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, nil, [], {c: 1}, nil], Bug::ScanArgs.opt_var_trail_hash(c: 1))
+ end
assert_equal([1, nil, [], "a", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", c: 1))
assert_equal([2, "a", [], "b", nil], Bug::ScanArgs.opt_var_trail_hash("a", "b"))
assert_equal([2, "a", [], "b", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", c: 1))
assert_equal([3, "a", ["b"], "c", nil], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c"))
assert_equal([3, "a", ["b"], "c", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c", c: 1))
- assert_equal([3, "a", ["b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c"=>0, c: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([3, "a", ["b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c"=>0, c: 1))
+ end
end
def test_lead_opt_var_trail_hash
assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_var_trail_hash()}
assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_var_trail_hash("a")}
- assert_equal([2, "a", nil, [], {b: 1}, nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", b: 1))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([2, "a", nil, [], {b: 1}, nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", b: 1))
+ end
assert_equal([2, "a", nil, [], "b", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b"))
assert_equal([2, "a", nil, [], "b", {c: 1}], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", c: 1))
assert_equal([3, "a", "b", [], "c", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c"))
assert_equal([3, "a", "b", [], "c", {c: 1}], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", c: 1))
assert_equal([4, "a", "b", ["c"], "d", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", "d"))
- assert_equal([4, "a", "b", ["c"], {"d"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", "d"=>0, c: 1))
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([4, "a", "b", ["c"], {"d"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", "d"=>0, c: 1))
+ end
+ end
+
+ def test_k_lead_opt_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.k_lead_opt_hash}
+ assert_equal([1, "a", nil, {c: 1}], Bug::ScanArgs.k_lead_opt_hash("a", c: 1))
+ assert_equal([1, "a", nil, {c: 1}], Bug::ScanArgs.k_lead_opt_hash("a", {c: 1}))
+ assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.k_lead_opt_hash("a", "b", c: 1))
+ assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.k_lead_opt_hash("a", "b", {c: 1}))
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, {c: 1}, nil, nil], Bug::ScanArgs.k_lead_opt_hash(c: 1))
+ end
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.k_lead_opt_hash("a", "b"=>0, c: 1))
+ end
+ end
+
+ def test_e_lead_opt_hash
+ assert_warn(/The keyword argument is passed as the last hash parameter/) do
+ assert_equal([1, {}, nil, nil], Bug::ScanArgs.e_lead_opt_hash)
+ end
+ assert_equal([1, "a", nil, nil], Bug::ScanArgs.e_lead_opt_hash("a"))
+ assert_equal([2, "a", "b", nil], Bug::ScanArgs.e_lead_opt_hash("a", "b"))
+ assert_equal([2, "a", {c: 1}, nil], Bug::ScanArgs.e_lead_opt_hash("a", c: 1))
+ assert_raise(ArgumentError) {Bug::ScanArgs.e_lead_opt_hash("a", "b", c: 1)}
+ assert_equal([1, {c: 1}, nil, nil], Bug::ScanArgs.e_lead_opt_hash(c: 1))
+ assert_raise(ArgumentError) {Bug::ScanArgs.e_lead_opt_hash("a", "b", "c")}
+ assert_equal([2, "a", {"b"=>0, c: 1}, nil], Bug::ScanArgs.e_lead_opt_hash("a", "b"=>0, c: 1))
+ end
+
+ def test_n_lead_opt_hash
+ assert_raise(ArgumentError) {Bug::ScanArgs.n_lead_opt_hash}
+ assert_equal([1, "a", nil, nil], Bug::ScanArgs.n_lead_opt_hash("a"))
+ assert_equal([2, "a", "b", nil], Bug::ScanArgs.n_lead_opt_hash("a", "b"))
+ assert_equal([1, "a", nil, {c: 1}], Bug::ScanArgs.n_lead_opt_hash("a", c: 1))
+ assert_equal([1, "a", nil, {c: 1}], Bug::ScanArgs.n_lead_opt_hash("a", {c: 1}))
+ assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.n_lead_opt_hash("a", "b", c: 1))
+ assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.n_lead_opt_hash("a", "b", {c: 1}))
+ assert_equal([1, {c: 1}, nil, nil], Bug::ScanArgs.n_lead_opt_hash(c: 1))
+ assert_equal([1, {c: 1}, nil, nil], Bug::ScanArgs.n_lead_opt_hash({c: 1}))
+ assert_raise(ArgumentError) {Bug::ScanArgs.n_lead_opt_hash("a", "b", "c")}
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.n_lead_opt_hash("a", "b"=>0, c: 1))
+ end
end
end
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index af523f9f98..053c728247 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -933,7 +933,7 @@ class TestPathname < Test::Unit::TestCase
assert_equal(0444 & ~File.umask, File.stat("b").mode & 0777)
assert_equal("def", File.read("b"))
- Pathname("c").open("w", 0444, {}) {|f| f.write "ghi" }
+ Pathname("c").open("w", 0444, **{}) {|f| f.write "ghi" }
assert_equal(0444 & ~File.umask, File.stat("c").mode & 0777)
assert_equal("ghi", File.read("c"))
diff --git a/test/reline/helper.rb b/test/reline/helper.rb
index ca0001258d..b1759f1d80 100644
--- a/test/reline/helper.rb
+++ b/test/reline/helper.rb
@@ -31,7 +31,7 @@ class Reline::TestCase < Test::Unit::TestCase
if Reline::Unicode::EscapedChars.include?(c.ord)
c
else
- c.encode(@line_editor.instance_variable_get(:@encoding), Encoding::UTF_8, options)
+ c.encode(@line_editor.instance_variable_get(:@encoding), Encoding::UTF_8, **options)
end
}.join
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
diff --git a/test/ruby/test_dir_m17n.rb b/test/ruby/test_dir_m17n.rb
index 7584074c7e..c2c0c4999e 100644
--- a/test/ruby/test_dir_m17n.rb
+++ b/test/ruby/test_dir_m17n.rb
@@ -18,7 +18,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename = #{code}.chr('UTF-8').force_encoding("#{encoding}")
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
assert_include(ents, filename)
EOS
@@ -26,7 +26,7 @@ class TestDir_M17N < Test::Unit::TestCase
assert_separately(%w[-EASCII-8BIT], <<-EOS, :chdir=>dir)
filename = #{code}.chr('UTF-8').force_encoding("ASCII-8BIT")
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
expected_filename = #{code}.chr('UTF-8').encode(Encoding.find("filesystem")) rescue expected_filename = "?"
expected_filename = expected_filename.force_encoding("ASCII-8BIT")
if /mswin|mingw/ =~ RUBY_PLATFORM
@@ -35,7 +35,7 @@ class TestDir_M17N < Test::Unit::TestCase
when ents.include?(expected_filename)
filename = expected_filename
else
- ents = Dir.entries(".", {:encoding => Encoding.find("filesystem")})
+ ents = Dir.entries(".", :encoding => Encoding.find("filesystem"))
filename = expected_filename
end
end
@@ -52,7 +52,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\u3042"
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
assert_include(ents, filename)
EOS
}
@@ -67,7 +67,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\xff".force_encoding("ASCII-8BIT") # invalid byte sequence as UTF-8
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
filename = "%FF" if /darwin/ =~ RUBY_PLATFORM && ents.include?("%FF")
assert_include(ents, filename)
EOS
@@ -75,7 +75,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\xff".force_encoding("UTF-8") # invalid byte sequence as UTF-8
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
filename = "%FF" if /darwin/ =~ RUBY_PLATFORM && ents.include?("%FF")
assert_include(ents, filename)
EOS
@@ -88,7 +88,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\xc2\xa1".force_encoding("utf-8")
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
assert_include(ents, filename)
EOS
assert_separately(%w[-EUTF-8], <<-'EOS', :chdir=>d)
@@ -125,13 +125,13 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\u3042"
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
assert_include(ents, filename)
EOS
assert_separately(%w[-EUTF-8:EUC-JP], <<-'EOS', :chdir=>d)
filename = "\xA4\xA2".force_encoding("euc-jp")
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
assert_include(ents, filename)
EOS
assert_separately(%w[-EUTF-8:EUC-JP], <<-'EOS', :chdir=>d)
@@ -151,7 +151,7 @@ class TestDir_M17N < Test::Unit::TestCase
File.open(filename1, "w") {}
File.open(filename2, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
assert_include(ents, filename1)
assert_include(ents, filename2)
EOS
@@ -159,7 +159,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename1 = "\u2661" # WHITE HEART SUIT which is not representable in EUC-JP
filename2 = "\xA4\xA2".force_encoding("euc-jp") # HIRAGANA LETTER A in EUC-JP
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
assert_include(ents, filename1)
assert_include(ents, filename2)
EOS
@@ -183,7 +183,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\xA4\xA2".force_encoding("euc-jp")
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
ents.each {|e| e.force_encoding("ASCII-8BIT") }
if /darwin/ =~ RUBY_PLATFORM
filename = filename.encode("utf-8")
@@ -200,7 +200,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\xA4\xA2".force_encoding("euc-jp")
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
if /darwin/ =~ RUBY_PLATFORM
filename = filename.encode("utf-8").force_encoding("euc-jp")
end
@@ -210,14 +210,14 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\xA4\xA2".force_encoding('ASCII-8BIT')
win_expected_filename = filename.encode(Encoding.find("filesystem"), "euc-jp") rescue "?"
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
unless ents.include?(filename)
case RUBY_PLATFORM
when /darwin/
filename = filename.encode("utf-8", "euc-jp").b
when /mswin|mingw/
if ents.include?(win_expected_filename.b)
- ents = Dir.entries(".", {:encoding => Encoding.find("filesystem")})
+ ents = Dir.entries(".", :encoding => Encoding.find("filesystem"))
filename = win_expected_filename
end
end
@@ -246,7 +246,7 @@ class TestDir_M17N < Test::Unit::TestCase
filename = "\xA4\xA2".force_encoding("euc-jp")
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
if /darwin/ =~ RUBY_PLATFORM
filename = filename.encode("utf-8", "euc-jp").force_encoding("euc-jp")
end
@@ -255,7 +255,7 @@ class TestDir_M17N < Test::Unit::TestCase
assert_separately(%w[-EEUC-JP:UTF-8], <<-'EOS', :chdir=>d)
filename = "\u3042"
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
- ents = Dir.entries(".", opts)
+ ents = Dir.entries(".", **(opts||{}))
if /darwin/ =~ RUBY_PLATFORM
filename = filename.force_encoding("euc-jp")
end
@@ -420,7 +420,7 @@ class TestDir_M17N < Test::Unit::TestCase
else
orig.each {|o| o.force_encoding(enc) }
end
- ents = Dir.entries(".", opts).reject {|n| /\A\./ =~ n}
+ ents = Dir.entries(".", **(opts||{})).reject {|n| /\A\./ =~ n}
ents.sort!
PP.assert_equal(orig, ents, bug7267)
}
diff --git a/test/ruby/test_econv.rb b/test/ruby/test_econv.rb
index 6f098db454..115ff73ea8 100644
--- a/test/ruby/test_econv.rb
+++ b/test/ruby/test_econv.rb
@@ -3,7 +3,12 @@ require 'test/unit'
class TestEncodingConverter < Test::Unit::TestCase
def check_ec(edst, esrc, eres, dst, src, ec, off, len, opts=nil)
- res = ec.primitive_convert(src, dst, off, len, opts)
+ case opts
+ when Hash
+ res = ec.primitive_convert(src, dst, off, len, **opts)
+ else
+ res = ec.primitive_convert(src, dst, off, len, opts)
+ end
assert_equal([edst.b, esrc.b, eres],
[dst.b, src.b, res])
end
diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb
index 9de1b2d82c..e5c38091ac 100644
--- a/test/ruby/test_exception.rb
+++ b/test/ruby/test_exception.rb
@@ -1223,6 +1223,14 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status|
assert_equal("#{__FILE__}:#{__LINE__-1}: warning: test warning\n", warning[0])
assert_raise(ArgumentError) {warn("test warning", uplevel: -1)}
assert_in_out_err(["-e", "warn 'ok', uplevel: 1"], '', [], /warning:/)
+ warning = capture_warning_warn {warn("test warning", {uplevel: 0})}
+ assert_equal("#{__FILE__}:#{__LINE__-1}: warning: The last argument is used as the keyword parameter\n", warning[0])
+ assert_match(/warning: for method defined here|warning: test warning/, warning[1])
+ warning = capture_warning_warn {warn("test warning", **{uplevel: 0})}
+ assert_equal("#{__FILE__}:#{__LINE__-1}: warning: test warning\n", warning[0])
+ warning = capture_warning_warn {warn("test warning", {uplevel: 0}, **{})}
+ assert_equal("test warning\n{:uplevel=>0}\n", warning[0])
+ assert_raise(ArgumentError) {warn("test warning", foo: 1)}
end
def test_warning_warn_invalid_argument
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index ed60452b36..a610b608aa 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -423,7 +423,7 @@ class TestIO < Test::Unit::TestCase
path = t.path
t.close!
assert_raise(Errno::ENOENT, "[ruby-dev:33072]") do
- File.read(path, nil, nil, {})
+ File.read(path, nil, nil, **{})
end
end
@@ -2489,15 +2489,15 @@ class TestIO < Test::Unit::TestCase
assert_equal(["foo\n", "bar\n", "baz\n"], a)
a = []
- IO.foreach(t.path, {:mode => "r" }) {|x| a << x }
+ IO.foreach(t.path, :mode => "r") {|x| a << x }
assert_equal(["foo\n", "bar\n", "baz\n"], a)
a = []
- IO.foreach(t.path, {:open_args => [] }) {|x| a << x }
+ IO.foreach(t.path, :open_args => []) {|x| a << x }
assert_equal(["foo\n", "bar\n", "baz\n"], a)
a = []
- IO.foreach(t.path, {:open_args => ["r"] }) {|x| a << x }
+ IO.foreach(t.path, :open_args => ["r"]) {|x| a << x }
assert_equal(["foo\n", "bar\n", "baz\n"], a)
a = []
@@ -3119,7 +3119,9 @@ __END__
assert_equal(1, File.write(path, "f", 0, encoding: "UTF-8"))
assert_equal("ff", File.read(path))
assert_raise(TypeError) {
- File.write(path, "foo", Object.new => Object.new)
+ assert_warn(/The last argument is split into positional and keyword parameters/) do
+ File.write(path, "foo", Object.new => Object.new)
+ end
}
end
end
diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb
index 8101bfb62f..022ff330b5 100644
--- a/test/ruby/test_io_m17n.rb
+++ b/test/ruby/test_io_m17n.rb
@@ -23,7 +23,8 @@ class TestIO_M17N < Test::Unit::TestCase
def pipe(*args, wp, rp)
re, we = nil, nil
- r, w = IO.pipe(*args)
+ kw = args.last.is_a?(Hash) ? args.pop : {}
+ r, w = IO.pipe(*args, **kw)
rt = Thread.new do
begin
rp.call(r)
diff --git a/test/ruby/test_literal.rb b/test/ruby/test_literal.rb
index 3f87f860b1..7f4a329c4a 100644
--- a/test/ruby/test_literal.rb
+++ b/test/ruby/test_literal.rb
@@ -189,7 +189,7 @@ class TestRubyLiteral < Test::Unit::TestCase
def test_debug_frozen_string
src = 'n = 1; _="foo#{n ? "-#{n}" : ""}"'; f = "test.rb"; n = 1
opt = {frozen_string_literal: true, debug_frozen_string_literal: true}
- str = RubyVM::InstructionSequence.compile(src, f, f, n, opt).eval
+ str = RubyVM::InstructionSequence.compile(src, f, f, n, **opt).eval
assert_equal("foo-1", str)
assert_predicate(str, :frozen?)
assert_raise_with_message(FrozenError, /created at #{Regexp.quote(f)}:#{n}/) {
@@ -200,7 +200,7 @@ class TestRubyLiteral < Test::Unit::TestCase
def test_debug_frozen_string_in_array_literal
src = '["foo"]'; f = "test.rb"; n = 1
opt = {frozen_string_literal: true, debug_frozen_string_literal: true}
- ary = RubyVM::InstructionSequence.compile(src, f, f, n, opt).eval
+ ary = RubyVM::InstructionSequence.compile(src, f, f, n, **opt).eval
assert_equal("foo", ary.first)
assert_predicate(ary.first, :frozen?)
assert_raise_with_message(FrozenError, /created at #{Regexp.quote(f)}:#{n}/) {
diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb
index 6073ec1ee5..f87abbb4a8 100644
--- a/test/ruby/test_numeric.rb
+++ b/test/ruby/test_numeric.rb
@@ -230,7 +230,8 @@ class TestNumeric < Test::Unit::TestCase
end
def assert_step(expected, (from, *args), inf: false)
- enum = from.step(*args)
+ kw = args.last.is_a?(Hash) ? args.pop : {}
+ enum = from.step(*args, **kw)
size = enum.size
xsize = expected.size
@@ -239,7 +240,7 @@ class TestNumeric < Test::Unit::TestCase
assert_send [size, :>, 0], "step size: +infinity"
a = []
- from.step(*args) { |x| a << x; break if a.size == xsize }
+ from.step(*args, **kw) { |x| a << x; break if a.size == xsize }
assert_equal expected, a, "step"
a = []
@@ -249,7 +250,7 @@ class TestNumeric < Test::Unit::TestCase
assert_equal expected.size, size, "step size"
a = []
- from.step(*args) { |x| a << x }
+ from.step(*args, **kw) { |x| a << x }
assert_equal expected, a, "step"
a = []
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 36d246f9e0..dd403bf9d9 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -17,8 +17,8 @@ class TestString < Test::Unit::TestCase
super
end
- def S(*args)
- @cls.new(*args)
+ def S(*args, **kw)
+ @cls.new(*args, **kw)
end
def test_s_new