From 24f595c73a358a46a54ae5f475589062f4c41637 Mon Sep 17 00:00:00 2001 From: Sergio Hilgert Date: Fri, 15 Oct 2021 11:10:06 -0300 Subject: [PATCH 1/3] Add specs to feature 14183 --- language/method_spec.rb | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/language/method_spec.rb b/language/method_spec.rb index 21227e914e..852fcfe833 100644 --- a/language/method_spec.rb +++ b/language/method_spec.rb @@ -1908,6 +1908,58 @@ def m(...) = mm(...) + mm(...) end end end + + describe "Keyword arguments are now separated from positional arguments" do + context "with a keyword argument defined in args" do + context "when it's not used explicity" do + evaluate <<-ruby do + def foo(a, b, c, hsh) + hsh[:key] + end + ruby + + foo(1, 2, 3, { key: 42 }).should raise_error(ArgumentError) + end + end + + context "when it's used explicity" do + evaluate <<-ruby do + def foo(a, b, c, **hsh) + hsh[:key] + end + ruby + + foo(1, 2, 3, { key: 42 }).should == 42 + end + end + end + + context "with a keyword argument in method call" do + context "when it's not used explicity" do + evaluate <<-ruby do + def foo(a, b, c, key: 1) + hsh[:key] + end + ruby + + h = { key: 42 } + foo(1, 2, 3, h).should raise_error(ArgumentError) + end + end + + context "when it's used explicity" do + evaluate <<-ruby do + def foo(a, b, c, key: 1) + hsh[:key] + end + ruby + + h = { key: 42 } + foo(1, 2, 3, **h).should == 42 + end + end + end + end end ruby_version_is "3.1" do From 284b8103c6bec04ba50356654d294cb0790bbd84 Mon Sep 17 00:00:00 2001 From: Sergio Hilgert Date: Fri, 15 Oct 2021 12:04:49 -0300 Subject: [PATCH 2/3] Improve specs grammar and fix tests --- language/method_spec.rb | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/language/method_spec.rb b/language/method_spec.rb index 852fcfe833..8dee67ab47 100644 --- a/language/method_spec.rb +++ b/language/method_spec.rb @@ -1910,32 +1910,30 @@ def m(...) = mm(...) + mm(...) end describe "Keyword arguments are now separated from positional arguments" do - context "with a keyword argument defined in args" do - context "when it's not used explicity" do - evaluate <<-ruby do - def foo(a, b, c, hsh) - hsh[:key] - end - ruby + context "when the method has only positional parameters" do + evaluate <<-ruby do + def foo(a, b, c, hsh) + hsh[:key] + end + ruby - foo(1, 2, 3, { key: 42 }).should raise_error(ArgumentError) - end + foo(1, 2, 3, key: 42).should raise_error(ArgumentError) end + end - context "when it's used explicity" do - evaluate <<-ruby do - def foo(a, b, c, **hsh) - hsh[:key] - end - ruby + context "when the method takes a ** paramater" do + evaluate <<-ruby do + def foo(a, b, c, **hsh) + hsh[:key] + end + ruby - foo(1, 2, 3, { key: 42 }).should == 42 - end + foo(1, 2, 3, key: 42).should == 42 end end - context "with a keyword argument in method call" do - context "when it's not used explicity" do + context "when the method takes a key: paramater" do + context "when it's called with no **" do evaluate <<-ruby do def foo(a, b, c, key: 1) hsh[:key] @@ -1947,7 +1945,7 @@ def foo(a, b, c, key: 1) end end - context "when it's used explicity" do + context "when it's called with **" do evaluate <<-ruby do def foo(a, b, c, key: 1) hsh[:key] From ef11a4c6ded95f09ace0925ef8cfa98ee7d7d342 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Fri, 15 Oct 2021 17:48:32 +0200 Subject: [PATCH 3/3] Improve kwargs specs for methods --- language/method_spec.rb | 68 +++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/language/method_spec.rb b/language/method_spec.rb index 8dee67ab47..0a5bb99d0b 100644 --- a/language/method_spec.rb +++ b/language/method_spec.rb @@ -1911,46 +1911,62 @@ def m(...) = mm(...) + mm(...) describe "Keyword arguments are now separated from positional arguments" do context "when the method has only positional parameters" do - evaluate <<-ruby do - def foo(a, b, c, hsh) - hsh[:key] - end - ruby + it "treats incoming keyword arguments as positional for compatibility" do + def foo(a, b, c, hsh) + hsh[:key] + end - foo(1, 2, 3, key: 42).should raise_error(ArgumentError) + foo(1, 2, 3, key: 42).should == 42 end end - context "when the method takes a ** paramater" do - evaluate <<-ruby do - def foo(a, b, c, **hsh) - hsh[:key] - end - ruby + context "when the method takes a ** parameter" do + it "captures the passed literal keyword arguments" do + def foo(a, b, c, **hsh) + hsh[:key] + end foo(1, 2, 3, key: 42).should == 42 end + + it "captures the passed ** keyword arguments" do + def foo(a, b, c, **hsh) + hsh[:key] + end + + h = { key: 42 } + foo(1, 2, 3, **h).should == 42 + end + + it "does not convert a positional Hash to keyword arguments" do + def foo(a, b, c, **hsh) + hsh[:key] + end + + -> { + foo(1, 2, 3, { key: 42 }) + }.should raise_error(ArgumentError, 'wrong number of arguments (given 4, expected 3)') + end end - context "when the method takes a key: paramater" do - context "when it's called with no **" do - evaluate <<-ruby do - def foo(a, b, c, key: 1) - hsh[:key] - end - ruby + context "when the method takes a key: parameter" do + context "when it's called with a positional Hash and no **" do + it "raises ArgumentError" do + def foo(a, b, c, key: 1) + key + end - h = { key: 42 } - foo(1, 2, 3, h).should raise_error(ArgumentError) + -> { + foo(1, 2, 3, { key: 42 }) + }.should raise_error(ArgumentError, 'wrong number of arguments (given 4, expected 3)') end end context "when it's called with **" do - evaluate <<-ruby do - def foo(a, b, c, key: 1) - hsh[:key] - end - ruby + it "captures the passed keyword arguments" do + def foo(a, b, c, key: 1) + key + end h = { key: 42 } foo(1, 2, 3, **h).should == 42