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
Родитель 16cd0de6ec
Коммит a810f6cbef
5 изменённых файлов: 362 добавлений и 134 удалений

Просмотреть файл

@ -6,6 +6,14 @@ describe "A block yielded a single" do
def m(a) yield a end
end
def supress_keyword_warning(&block)
if RUBY_VERSION > '2.7'
suppress_warning(&block)
else
yield
end
end
context "Array" do
it "assigns the Array to a single argument" do
m([1, 2]) { |a| a }.should == [1, 2]
@ -45,35 +53,55 @@ describe "A block yielded a single" do
end
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}]
supress_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
result = m(["a" => 1, a: 10]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 1}, a: 10]
supress_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
obj = mock("coerce block keyword arguments")
obj.should_receive(:to_hash).and_return({"a" => 1, b: 2})
supress_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]
result = m([obj]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 1}, b: 2]
end
end
it "calls #to_hash on the argument but does not use the result when no keywords are present" do
obj = mock("coerce block keyword arguments")
obj.should_receive(:to_hash).and_return({"a" => 1, "b" => 2})
ruby_version_is "0"..."2.7" do
it "calls #to_hash on the argument and uses resulting hash as first argument 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})
result = m([obj]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 1, "b" => 2}, {}]
result = m([obj]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 1, "b" => 2}, {}]
end
end
ruby_version_is "2.7" 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})
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
result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 10}, {b: 2}]
supress_keyword_warning do
result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
result.should == [{"a" => 10}, {b: 2}]
end
end
end
@ -83,27 +111,33 @@ describe "A block yielded a single" do
end
it "calls #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({x: 9})
supress_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
obj = mock("destructure block keyword arguments")
obj.should_receive(:to_hash).and_return(nil)
supress_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, {}]
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
x = mock("destructure matching block keyword argument")
x.should_receive(:to_hash).and_return({x: 9})
supress_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}]
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

Просмотреть файл

@ -128,9 +128,18 @@ describe "Hash literal" do
{a: 1, **obj, c: 3}.should == {a:1, b: 2, c: 3, d: 4}
end
it "raises a TypeError if any splatted elements keys are not symbols" do
h = {1 => 2, b: 3}
-> { {a: 1, **h} }.should raise_error(TypeError)
ruby_version_is "0"..."2.7" do
it "raises a TypeError if any splatted elements keys are not symbols" do
h = {1 => 2, b: 3}
-> { {a: 1, **h} }.should raise_error(TypeError)
end
end
ruby_version_is "2.7" do
it "allows splatted elements keys that are not symbols" do
h = {1 => 2, b: 3}
{a: 1, **h}.should == {a: 1, 1 => 2, b: 3}
end
end
it "raises a TypeError if #to_hash does not return a Hash" do

Просмотреть файл

@ -186,9 +186,19 @@ describe "A lambda literal -> () { }" do
@a.().should == {}
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
@a.(h).should == {a: 1}
def self.suppress_keyword_warning(&block)
if RUBY_VERSION > '2.7'
suppress_warning(&block)
else
yield
end
end
suppress_keyword_warning do
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
@a.(h).should == {a: 1}
end
end
evaluate <<-ruby do
@ -520,9 +530,19 @@ describe "A lambda expression 'lambda { ... }'" do
@a.().should == {}
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
@a.(h).should == {a: 1}
def self.suppress_keyword_warning(&block)
if RUBY_VERSION > '2.7'
suppress_warning(&block)
else
yield
end
end
suppress_keyword_warning do
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
@a.(h).should == {a: 1}
end
end
evaluate <<-ruby do

Просмотреть файл

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

Просмотреть файл

@ -175,7 +175,7 @@ describe "C-API Util function" do
it "raises an error if a required argument is not in the hash" do
h = { :a => 7, :c => 12, :b => 5 }
-> { @o.rb_get_kwargs(h, [:b, :d], 2, 0) }.should raise_error(ArgumentError, /missing keyword: d/)
-> { @o.rb_get_kwargs(h, [:b, :d], 2, 0) }.should raise_error(ArgumentError, /missing keyword: :?d/)
h.should == {:a => 7, :c => 12}
end
@ -187,7 +187,7 @@ describe "C-API Util function" do
it "raises an error if there are additional arguments and optional is positive" do
h = { :a => 7, :c => 12, :b => 5 }
-> { @o.rb_get_kwargs(h, [:b, :a], 2, 0) }.should raise_error(ArgumentError, /unknown keyword: c/)
-> { @o.rb_get_kwargs(h, [:b, :a], 2, 0) }.should raise_error(ArgumentError, /unknown keyword: :?c/)
h.should == {:c => 12}
end