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

Update specs for keyword argument separation

This commit is contained in:
Jeremy Evans 2019-10-06 09:26:58 -07:00
parent ff96565686
commit e014e6bf66
Notes: git 2020-01-03 11:41:09 +09:00
5 changed files with 657 additions and 237 deletions

View file

@ -44,27 +44,51 @@ describe "A block yielded a single" do
m([1, 2]) { |a, **k| [a, k] }.should == [1, {}]
end
it "assigns elements to mixed argument types" do
suppress_keyword_warning do
ruby_version_is ''..."2.8" do
it "assigns elements to mixed argument types" do
suppress_keyword_warning do
result = m([1, 2, 3, {x: 9}]) { |a, b=5, *c, d, e: 2, **k| [a, b, c, d, e, k] }
result.should == [1, 2, [], 3, 2, {x: 9}]
end
end
it "assigns symbol keys from a Hash to keyword arguments" do
suppress_keyword_warning do
result = m(["a" => 1, a: 10]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 1}, a: 10]
end
end
it "assigns symbol keys from a Hash returned by #to_hash to keyword arguments" do
suppress_keyword_warning do
obj = mock("coerce block keyword arguments")
obj.should_receive(:to_hash).and_return({"a" => 1, b: 2})
result = m([obj]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 1}, b: 2]
end
end
end
ruby_version_is "2.8" do
it "assigns elements to mixed argument types" do
result = m([1, 2, 3, {x: 9}]) { |a, b=5, *c, d, e: 2, **k| [a, b, c, d, e, k] }
result.should == [1, 2, [], 3, 2, {x: 9}]
result.should == [1, 2, [3], {x: 9}, 2, {}]
end
end
it "assigns symbol keys from a Hash to keyword arguments" do
suppress_keyword_warning do
it "does not treat final Hash as keyword arguments" do
result = m(["a" => 1, a: 10]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 1}, a: 10]
result.should == [{"a" => 1, a: 10}, {}]
end
end
it "assigns symbol keys from a Hash returned by #to_hash to keyword arguments" do
suppress_keyword_warning do
obj = mock("coerce block keyword arguments")
obj.should_receive(:to_hash).and_return({"a" => 1, b: 2})
it "does not call #to_hash on final argument to get keyword arguments" do
suppress_keyword_warning do
obj = mock("coerce block keyword arguments")
obj.should_not_receive(:to_hash)
result = m([obj]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 1}, b: 2]
result = m([obj]) { |a=nil, **b| [a, b] }
result.should == [obj, {}]
end
end
end
@ -78,7 +102,7 @@ describe "A block yielded a single" do
end
end
ruby_version_is "2.7" do
ruby_version_is "2.7"...'2.8' do
it "calls #to_hash on the argument but ignores result when optional argument and keyword argument accepted" do
obj = mock("coerce block keyword arguments")
obj.should_receive(:to_hash).and_return({"a" => 1, "b" => 2})
@ -88,11 +112,31 @@ describe "A block yielded a single" do
end
end
ruby_version_is "2.8" do
it "does not call #to_hash on the argument when optional argument and keyword argument accepted" do
obj = mock("coerce block keyword arguments")
obj.should_not_receive(:to_hash)
result = m([obj]) { |a=nil, **b| [a, b] }
result.should == [obj, {}]
end
end
describe "when non-symbol keys are in a keyword arguments Hash" do
it "separates non-symbol keys and symbol keys" do
suppress_keyword_warning do
result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 10}, {b: 2}]
ruby_version_is ""..."2.8" do
it "separates non-symbol keys and symbol keys" do
suppress_keyword_warning do
result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 10}, {b: 2}]
end
end
end
ruby_version_is "2.8" do
it "does not separates non-symbol keys and symbol keys" do
suppress_keyword_warning do
result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 10, b: 2}, {}]
end
end
end
end
@ -102,51 +146,71 @@ describe "A block yielded a single" do
result.should == [{"a" => 10}, {}]
end
it "calls #to_hash on the last element if keyword arguments are present" do
suppress_keyword_warning do
obj = mock("destructure block keyword arguments")
obj.should_receive(:to_hash).and_return({x: 9})
ruby_version_is ''...'2.8' do
it "calls #to_hash on the last element if keyword arguments are present" do
suppress_keyword_warning do
obj = mock("destructure block keyword arguments")
obj.should_receive(:to_hash).and_return({x: 9})
result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
result.should == [1, [2], 3, {x: 9}]
result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
result.should == [1, [2], 3, {x: 9}]
end
end
it "assigns the last element to a non-keyword argument if #to_hash returns nil" do
suppress_keyword_warning do
obj = mock("destructure block keyword arguments")
obj.should_receive(:to_hash).and_return(nil)
result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
result.should == [1, [2, 3], obj, {}]
end
end
it "calls #to_hash on the last element when there are more arguments than parameters" do
suppress_keyword_warning do
x = mock("destructure matching block keyword argument")
x.should_receive(:to_hash).and_return({x: 9})
result = m([1, 2, 3, {y: 9}, 4, 5, x]) { |a, b=5, c, **k| [a, b, c, k] }
result.should == [1, 2, 3, {x: 9}]
end
end
it "raises a TypeError if #to_hash does not return a Hash" do
obj = mock("destructure block keyword arguments")
obj.should_receive(:to_hash).and_return(1)
-> { m([1, 2, 3, obj]) { |a, *b, c, **k| } }.should raise_error(TypeError)
end
it "raises the error raised inside #to_hash" do
obj = mock("destructure block keyword arguments")
error = RuntimeError.new("error while converting to a hash")
obj.should_receive(:to_hash).and_raise(error)
-> { m([1, 2, 3, obj]) { |a, *b, c, **k| } }.should raise_error(error)
end
end
it "assigns the last element to a non-keyword argument if #to_hash returns nil" do
suppress_keyword_warning do
ruby_version_is '2.8' do
it "does not call #to_hash on the last element if keyword arguments are present" do
obj = mock("destructure block keyword arguments")
obj.should_receive(:to_hash).and_return(nil)
obj.should_not_receive(:to_hash)
result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
result.should == [1, [2, 3], obj, {}]
end
end
it "calls #to_hash on the last element when there are more arguments than parameters" do
suppress_keyword_warning do
it "does not call #to_hash on the last element when there are more arguments than parameters" do
x = mock("destructure matching block keyword argument")
x.should_receive(:to_hash).and_return({x: 9})
x.should_not_receive(:to_hash)
result = m([1, 2, 3, {y: 9}, 4, 5, x]) { |a, b=5, c, **k| [a, b, c, k] }
result.should == [1, 2, 3, {x: 9}]
result.should == [1, 2, 3, {}]
end
end
it "raises a TypeError if #to_hash does not return a Hash" do
obj = mock("destructure block keyword arguments")
obj.should_receive(:to_hash).and_return(1)
-> { m([1, 2, 3, obj]) { |a, *b, c, **k| } }.should raise_error(TypeError)
end
it "raises the error raised inside #to_hash" do
obj = mock("destructure block keyword arguments")
error = RuntimeError.new("error while converting to a hash")
obj.should_receive(:to_hash).and_raise(error)
-> { m([1, 2, 3, obj]) { |a, *b, c, **k| } }.should raise_error(error)
end
it "does not call #to_ary on the Array" do
ary = [1, 2]
ary.should_not_receive(:to_ary)