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

Update specs to handle non-Symbols for keyword splats in 2.7

Also handle some warnings for behavior that will change in 3.0.
This commit is contained in:
Jeremy Evans 2019-08-17 22:49:04 -07:00
parent 16cd0de6ec
commit a810f6cbef
Notes: git 2019-08-31 04:40:14 +09:00
5 changed files with 362 additions and 134 deletions

View file

@ -480,6 +480,15 @@ describe "A method" do
end
context "assigns local variables from method parameters" do
suppress_keyword_warning = ->(&block) do
if RUBY_VERSION >= '2.7'
suppress_warning(&block)
else
block.call
end
end
evaluate <<-ruby do
def m(a) a end
ruby
@ -546,7 +555,9 @@ describe "A method" do
-> { m() }.should raise_error(ArgumentError)
m(a: 1).should == 1
-> { m("a" => 1, a: 1) }.should raise_error(ArgumentError)
suppress_keyword_warning.call do
-> { m("a" => 1, a: 1) }.should raise_error(ArgumentError)
end
end
evaluate <<-ruby do
@ -850,7 +861,9 @@ describe "A method" do
m(b: 2).should == [1, 2]
m(2, b: 1).should == [2, 1]
m("a" => 1, b: 2).should == [{"a" => 1}, 2]
suppress_keyword_warning.call do
m("a" => 1, b: 2).should == [{"a" => 1}, 2]
end
end
evaluate <<-ruby do
@ -860,16 +873,31 @@ describe "A method" do
m().should == [1, 2]
m(2).should == [2, 2]
m(b: 3).should == [1, 3]
m("a" => 1, b: 2).should == [{"a" => 1}, 2]
suppress_keyword_warning.call do
m("a" => 1, b: 2).should == [{"a" => 1}, 2]
end
end
evaluate <<-ruby do
def m(a=1, **) a end
ruby
ruby_version_is "0"..."2.7" do
evaluate <<-ruby do
def m(a=1, **) a end
ruby
m().should == 1
m(2, a: 1, b: 0).should == 2
m("a" => 1, a: 2).should == {"a" => 1}
m().should == 1
m(2, a: 1, b: 0).should == 2
m("a" => 1, a: 2).should == {"a" => 1}
end
end
ruby_version_is "2.7" do
evaluate <<-ruby do
def m(a=1, **) a end
ruby
m().should == 1
m(2, a: 1, b: 0).should == 2
m("a" => 1, a: 2).should == 1
end
end
evaluate <<-ruby do
@ -915,7 +943,9 @@ describe "A method" do
m(a: 1).should == 1
m(1, 2, a: 3).should == 3
m("a" => 1, a: 2).should == 2
suppress_keyword_warning.call do
m("a" => 1, a: 2).should == 2
end
end
evaluate <<-ruby do
@ -924,7 +954,9 @@ describe "A method" do
m(b: 1).should == [[], 1]
m(1, 2, b: 3).should == [[1, 2], 3]
m("a" => 1, b: 2).should == [[{"a" => 1}], 2]
suppress_keyword_warning.call do
m("a" => 1, b: 2).should == [[{"a" => 1}], 2]
end
end
evaluate <<-ruby do
@ -935,7 +967,9 @@ describe "A method" do
m(1, 2).should == 1
m(a: 2).should == 2
m(1, a: 2).should == 2
m("a" => 1, a: 2).should == 2
suppress_keyword_warning.call do
m("a" => 1, a: 2).should == 2
end
end
evaluate <<-ruby do
@ -944,7 +978,9 @@ describe "A method" do
m().should == [[], 1]
m(1, 2, 3, b: 4).should == [[1, 2, 3], 4]
m("a" => 1, b: 2).should == [[{"a" => 1}], 2]
suppress_keyword_warning.call do
m("a" => 1, b: 2).should == [[{"a" => 1}], 2]
end
a = mock("splat")
a.should_not_receive(:to_ary)
@ -961,7 +997,9 @@ describe "A method" do
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
m(h).should be_nil
suppress_keyword_warning.call do
m(h).should be_nil
end
h = mock("keyword splat")
error = RuntimeError.new("error while converting to a hash")
@ -969,89 +1007,184 @@ describe "A method" do
-> { m(h) }.should raise_error(error)
end
evaluate <<-ruby do
def m(*a, **) a end
ruby
ruby_version_is "0"..."2.7" do
evaluate <<-ruby do
def m(*a, **) a end
ruby
m().should == []
m(1, 2, 3, a: 4, b: 5).should == [1, 2, 3]
m("a" => 1, a: 1).should == [{"a" => 1}]
m(1, **{a: 2}).should == [1]
m().should == []
m(1, 2, 3, a: 4, b: 5).should == [1, 2, 3]
m("a" => 1, a: 1).should == [{"a" => 1}]
m(1, **{a: 2}).should == [1]
h = mock("keyword splat")
h.should_receive(:to_hash)
-> { m(**h) }.should raise_error(TypeError)
h = mock("keyword splat")
h.should_receive(:to_hash)
-> { m(**h) }.should raise_error(TypeError)
end
evaluate <<-ruby do
def m(*, **k) k end
ruby
m().should == {}
m(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
m("a" => 1, a: 1).should == {a: 1}
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
m(h).should == {a: 1}
end
evaluate <<-ruby do
def m(a = nil, **k) [a, k] end
ruby
m().should == [nil, {}]
m("a" => 1).should == [{"a" => 1}, {}]
m(a: 1).should == [nil, {a: 1}]
m("a" => 1, a: 1).should == [{"a" => 1}, {a: 1}]
m({ "a" => 1 }, a: 1).should == [{"a" => 1}, {a: 1}]
m({a: 1}, {}).should == [{a: 1}, {}]
h = {"a" => 1, b: 2}
m(h).should == [{"a" => 1}, {b: 2}]
h.should == {"a" => 1, b: 2}
h = {"a" => 1}
m(h).first.should == h
h = {}
r = m(h)
r.first.should be_nil
r.last.should == {}
hh = {}
h = mock("keyword splat empty hash")
h.should_receive(:to_hash).and_return(hh)
r = m(h)
r.first.should be_nil
r.last.should == {}
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({"a" => 1, a: 2})
m(h).should == [{"a" => 1}, {a: 2}]
end
evaluate <<-ruby do
def m(*a, **k) [a, k] end
ruby
m().should == [[], {}]
m(1).should == [[1], {}]
m(a: 1, b: 2).should == [[], {a: 1, b: 2}]
m(1, 2, 3, a: 2).should == [[1, 2, 3], {a: 2}]
m("a" => 1).should == [[{"a" => 1}], {}]
m(a: 1).should == [[], {a: 1}]
m("a" => 1, a: 1).should == [[{"a" => 1}], {a: 1}]
m({ "a" => 1 }, a: 1).should == [[{"a" => 1}], {a: 1}]
m({a: 1}, {}).should == [[{a: 1}], {}]
m({a: 1}, {"a" => 1}).should == [[{a: 1}, {"a" => 1}], {}]
bo = BasicObject.new
def bo.to_a; [1, 2, 3]; end
def bo.to_hash; {:b => 2, :c => 3}; end
m(*bo, **bo).should == [[1, 2, 3], {:b => 2, :c => 3}]
end
end
evaluate <<-ruby do
def m(*, **k) k end
ruby
ruby_version_is "2.7" do
evaluate <<-ruby do
def m(*a, **) a end
ruby
m().should == {}
m(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
m("a" => 1, a: 1).should == {a: 1}
m().should == []
m(1, 2, 3, a: 4, b: 5).should == [1, 2, 3]
m("a" => 1, a: 1).should == []
m(1, **{a: 2}).should == [1]
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
m(h).should == {a: 1}
end
h = mock("keyword splat")
h.should_receive(:to_hash)
-> { m(**h) }.should raise_error(TypeError)
end
evaluate <<-ruby do
def m(a = nil, **k) [a, k] end
ruby
evaluate <<-ruby do
def m(*, **k) k end
ruby
m().should == [nil, {}]
m("a" => 1).should == [{"a" => 1}, {}]
m(a: 1).should == [nil, {a: 1}]
m("a" => 1, a: 1).should == [{"a" => 1}, {a: 1}]
m({ "a" => 1 }, a: 1).should == [{"a" => 1}, {a: 1}]
m({a: 1}, {}).should == [{a: 1}, {}]
m().should == {}
m(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
m("a" => 1, a: 1).should == {"a" => 1, a: 1}
h = {"a" => 1, b: 2}
m(h).should == [{"a" => 1}, {b: 2}]
h.should == {"a" => 1, b: 2}
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
suppress_warning do
m(h).should == {a: 1}
end
end
h = {"a" => 1}
m(h).first.should == h
evaluate <<-ruby do
def m(a = nil, **k) [a, k] end
ruby
h = {}
r = m(h)
r.first.should be_nil
r.last.should == {}
m().should == [nil, {}]
m("a" => 1).should == [nil, {"a" => 1}]
m(a: 1).should == [nil, {a: 1}]
m("a" => 1, a: 1).should == [nil, {"a" => 1, a: 1}]
m({ "a" => 1 }, a: 1).should == [{"a" => 1}, {a: 1}]
suppress_warning do
m({a: 1}, {}).should == [{a: 1}, {}]
hh = {}
h = mock("keyword splat empty hash")
h.should_receive(:to_hash).and_return(hh)
r = m(h)
r.first.should be_nil
r.last.should == {}
h = {"a" => 1, b: 2}
m(h).should == [{"a" => 1}, {b: 2}]
h.should == {"a" => 1, b: 2}
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({"a" => 1, a: 2})
m(h).should == [{"a" => 1}, {a: 2}]
end
h = {"a" => 1}
m(h).first.should == h
evaluate <<-ruby do
def m(*a, **k) [a, k] end
ruby
h = {}
r = m(h)
r.first.should be_nil
r.last.should == {}
m().should == [[], {}]
m(1).should == [[1], {}]
m(a: 1, b: 2).should == [[], {a: 1, b: 2}]
m(1, 2, 3, a: 2).should == [[1, 2, 3], {a: 2}]
hh = {}
h = mock("keyword splat empty hash")
h.should_receive(:to_hash).and_return(hh)
r = m(h)
r.first.should be_nil
r.last.should == {}
m("a" => 1).should == [[{"a" => 1}], {}]
m(a: 1).should == [[], {a: 1}]
m("a" => 1, a: 1).should == [[{"a" => 1}], {a: 1}]
m({ "a" => 1 }, a: 1).should == [[{"a" => 1}], {a: 1}]
m({a: 1}, {}).should == [[{a: 1}], {}]
m({a: 1}, {"a" => 1}).should == [[{a: 1}, {"a" => 1}], {}]
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({"a" => 1, a: 2})
m(h).should == [{"a" => 1}, {a: 2}]
end
end
bo = BasicObject.new
def bo.to_a; [1, 2, 3]; end
def bo.to_hash; {:b => 2, :c => 3}; end
evaluate <<-ruby do
def m(*a, **k) [a, k] end
ruby
m(*bo, **bo).should == [[1, 2, 3], {:b => 2, :c => 3}]
m().should == [[], {}]
m(1).should == [[1], {}]
m(a: 1, b: 2).should == [[], {a: 1, b: 2}]
m(1, 2, 3, a: 2).should == [[1, 2, 3], {a: 2}]
m("a" => 1).should == [[], {"a" => 1}]
m(a: 1).should == [[], {a: 1}]
m("a" => 1, a: 1).should == [[], {"a" => 1, a: 1}]
m({ "a" => 1 }, a: 1).should == [[{"a" => 1}], {a: 1}]
suppress_warning do
m({a: 1}, {}).should == [[{a: 1}], {}]
end
m({a: 1}, {"a" => 1}).should == [[{a: 1}, {"a" => 1}], {}]
bo = BasicObject.new
def bo.to_a; [1, 2, 3]; end
def bo.to_hash; {:b => 2, :c => 3}; end
m(*bo, **bo).should == [[1, 2, 3], {:b => 2, :c => 3}]
end
end
evaluate <<-ruby do
@ -1077,7 +1210,9 @@ describe "A method" do
ruby
m(a: 1, b: 2).should == [1, 2]
-> { m("a" => 1, a: 1, b: 2) }.should raise_error(ArgumentError)
suppress_keyword_warning.call do
-> { m("a" => 1, a: 1, b: 2) }.should raise_error(ArgumentError)
end
end
evaluate <<-ruby do
@ -1086,25 +1221,51 @@ describe "A method" do
m(a: 1).should == [1, 1]
m(a: 1, b: 2).should == [1, 2]
-> { m("a" => 1, a: 1, b: 2) }.should raise_error(ArgumentError)
suppress_keyword_warning.call do
-> { m("a" => 1, a: 1, b: 2) }.should raise_error(ArgumentError)
end
end
evaluate <<-ruby do
def m(a:, **) a end
ruby
ruby_version_is '0'...'2.7' do
evaluate <<-ruby do
def m(a:, **) a end
ruby
m(a: 1).should == 1
m(a: 1, b: 2).should == 1
-> { m("a" => 1, a: 1, b: 2) }.should raise_error(ArgumentError)
m(a: 1).should == 1
m(a: 1, b: 2).should == 1
-> { m("a" => 1, a: 1, b: 2) }.should raise_error(ArgumentError)
end
evaluate <<-ruby do
def m(a:, **k) [a, k] end
ruby
m(a: 1).should == [1, {}]
m(a: 1, b: 2, c: 3).should == [1, {b: 2, c: 3}]
-> { m("a" => 1, a: 1, b: 2) }.should raise_error(ArgumentError)
end
end
evaluate <<-ruby do
def m(a:, **k) [a, k] end
ruby
ruby_version_is '2.7' do
evaluate <<-ruby do
def m(a:, **) a end
ruby
m(a: 1).should == [1, {}]
m(a: 1, b: 2, c: 3).should == [1, {b: 2, c: 3}]
-> { m("a" => 1, a: 1, b: 2) }.should raise_error(ArgumentError)
m(a: 1).should == 1
m(a: 1, b: 2).should == 1
m("a" => 1, a: 1, b: 2).should == 1
end
evaluate <<-ruby do
def m(a:, **k) [a, k] end
ruby
m(a: 1).should == [1, {}]
m(a: 1, b: 2, c: 3).should == [1, {b: 2, c: 3}]
suppress_warning do
m("a" => 1, a: 1, b: 2).should == [1, {"a" => 1, b: 2}]
end
end
end
evaluate <<-ruby do
@ -1210,8 +1371,10 @@ describe "A method" do
result = m(1, 2)
result.should == [1, nil, nil, 2, nil, {}]
result = m(1, 2, {foo: :bar})
result.should == [1, nil, nil, 2, nil, {foo: :bar}]
suppress_warning do
result = m(1, 2, {foo: :bar})
result.should == [1, nil, nil, 2, nil, {foo: :bar}]
end
result = m(1, {foo: :bar})
result.should == [1, nil, nil, {foo: :bar}, nil, {}]
@ -1225,7 +1388,9 @@ describe "A method" do
options = {a: 1}.freeze
-> do
m(options).should == 1
suppress_warning do
m(options).should == 1
end
end.should_not raise_error
options.should == {a: 1}
end