From a810f6cbefb5726c626b0c490522865588f4480e Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Sat, 17 Aug 2019 22:49:04 -0700 Subject: Update specs to handle non-Symbols for keyword splats in 2.7 Also handle some warnings for behavior that will change in 3.0. --- spec/ruby/language/block_spec.rb | 88 ++++++--- spec/ruby/language/hash_spec.rb | 15 +- spec/ruby/language/lambda_spec.rb | 32 +++- spec/ruby/language/method_spec.rb | 389 +++++++++++++++++++++++++++----------- 4 files changed, 376 insertions(+), 148 deletions(-) (limited to 'spec/ruby/language') diff --git a/spec/ruby/language/block_spec.rb b/spec/ruby/language/block_spec.rb index 9230740d07..f8bfc8d9ed 100644 --- a/spec/ruby/language/block_spec.rb +++ b/spec/ruby/language/block_spec.rb @@ -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] + end + end - result = m([obj]) { |a=nil, **b| [a, b] } - result.should == [{"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}, {}] + 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 "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 == [{"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 diff --git a/spec/ruby/language/hash_spec.rb b/spec/ruby/language/hash_spec.rb index 4bce241e76..79131e9da6 100644 --- a/spec/ruby/language/hash_spec.rb +++ b/spec/ruby/language/hash_spec.rb @@ -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 diff --git a/spec/ruby/language/lambda_spec.rb b/spec/ruby/language/lambda_spec.rb index a4c265d80c..3b06a0d595 100644 --- a/spec/ruby/language/lambda_spec.rb +++ b/spec/ruby/language/lambda_spec.rb @@ -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 diff --git a/spec/ruby/language/method_spec.rb b/spec/ruby/language/method_spec.rb index 3e5ff3fbb5..41c8268f83 100644 --- a/spec/ruby/language/method_spec.rb +++ b/spec/ruby/language/method_spec.rb @@ -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 - - 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) - 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}], {}] + 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] + + 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 + + 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 == [1, 2, 3] + m("a" => 1, a: 1).should == [] + m(1, **{a: 2}).should == [1] + + 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, a: 1} + + h = mock("keyword splat") + h.should_receive(:to_hash).and_return({a: 1}) + suppress_warning do + m(h).should == {a: 1} + end + end + + evaluate <<-ruby do + def m(a = nil, **k) [a, k] end + ruby + + 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}, {}] + + 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 + 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}] + 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 + 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}] + 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).should == 1 + m("a" => 1, a: 1, b: 2).should == 1 + end - 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) + 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 -- cgit v1.2.3