summaryrefslogtreecommitdiff
path: root/spec/ruby/language/block_spec.rb
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-08-17 22:49:04 -0700
committerJeremy Evans <code@jeremyevans.net>2019-08-30 12:39:31 -0700
commita810f6cbefb5726c626b0c490522865588f4480e (patch)
tree83bc883b724e75976a155578c667276d10220450 /spec/ruby/language/block_spec.rb
parent16cd0de6ec8464129041c52174f5608e32512ca1 (diff)
Update specs to handle non-Symbols for keyword splats in 2.7
Also handle some warnings for behavior that will change in 3.0.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2395
Diffstat (limited to 'spec/ruby/language/block_spec.rb')
-rw-r--r--spec/ruby/language/block_spec.rb88
1 files changed, 61 insertions, 27 deletions
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