Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specs to feature 14183 #878

Merged
merged 3 commits into from
Oct 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions language/method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,72 @@ def m(...) = mm(...) + mm(...)
end
end
end

describe "Keyword arguments are now separated from positional arguments" do
context "when the method has only positional parameters" do
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 == 42
end
end

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

-> {
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
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
end
end
end
end
end

ruby_version_is "3.1" do
Expand Down