Skip to content

Commit

Permalink
Update specs for keyword argument separation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Jan 2, 2020
1 parent e4069f4 commit 55631ad
Show file tree
Hide file tree
Showing 5 changed files with 633 additions and 213 deletions.
38 changes: 29 additions & 9 deletions spec/ruby/core/io/shared/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,21 @@
@io.internal_encoding.to_s.should == 'IBM866'
end

it "accepts nil options" do
@io = suppress_keyword_warning do
IO.send(@method, @fd, 'w', nil)
ruby_version_is ''...'2.8' do
it "accepts nil options" do
@io = suppress_keyword_warning do
IO.send(@method, @fd, 'w', nil)
end
@io.write("foo").should == 3
end
end

ruby_version_is '2.8' do
it "raises ArgumentError for nil options" do
-> {
IO.send(@method, @fd, 'w', nil)
}.should raise_error(ArgumentError)
end
@io.write("foo").should == 3
end

it "coerces mode with #to_str" do
Expand Down Expand Up @@ -372,11 +382,21 @@
}.should raise_error(ArgumentError)
end

it "raises TypeError if passed a hash for mode and nil for options" do
-> {
suppress_keyword_warning do
ruby_version_is ''...'2.8' do
it "raises TypeError if passed a hash for mode and nil for options" do
-> {
suppress_keyword_warning do
@io = IO.send(@method, @fd, {mode: 'w'}, nil)
end
}.should raise_error(TypeError)
end
end

ruby_version_is '2.8' do
it "raises ArgumentError if passed a hash for mode and nil for options" do
-> {
@io = IO.send(@method, @fd, {mode: 'w'}, nil)
end
}.should raise_error(TypeError)
}.should raise_error(ArgumentError)
end
end
end
158 changes: 111 additions & 47 deletions spec/ruby/language/block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,51 @@ def m(a) yield a end
m([1, 2]) { |a, **k| [a, k] }.should == [1, {}]
end

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}]
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

it "assigns symbol keys from a Hash to keyword arguments" do
suppress_keyword_warning do
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], {x: 9}, 2, {}]
end

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

Expand All @@ -78,7 +102,7 @@ def m(a) yield a end
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})
Expand All @@ -88,11 +112,31 @@ def m(a) yield a end
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
Expand All @@ -102,51 +146,71 @@ def m(a) yield a end
result.should == [{"a" => 10}, {}]
end

it "calls #to_hash on the last element if keyword arguments are present" do
suppress_keyword_warning do
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}]
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({x: 9})
obj.should_receive(:to_hash).and_return(1)

result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
result.should == [1, [2], 3, {x: 9}]
-> { 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)
Expand Down
64 changes: 48 additions & 16 deletions spec/ruby/language/lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,33 @@ def create_lambda
result.should == [1, 2, 3, [4, 5], 6, [7, 8], 9, 10, 11, 12]
end

evaluate <<-ruby do
@a = -> (*, **k) { k }
ruby
ruby_version_is ''...'2.8' do
evaluate <<-ruby do
@a = -> (*, **k) { k }
ruby

@a.().should == {}
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}

suppress_keyword_warning do
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
@a.(h).should == {a: 1}
end
end
end

@a.().should == {}
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
ruby_version_is '2.8' do
evaluate <<-ruby do
@a = -> (*, **k) { k }
ruby

@a.().should == {}
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}

suppress_keyword_warning do
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
@a.(h).should == {a: 1}
h.should_not_receive(:to_hash)
@a.(h).should == {}
end
end

Expand Down Expand Up @@ -533,17 +549,33 @@ def m2() yield end
result.should == [1, 2, 3, [4, 5], 6, [7, 8], 9, 10, 11, 12]
end

evaluate <<-ruby do
@a = lambda { |*, **k| k }
ruby
ruby_version_is ''...'2.8' do
evaluate <<-ruby do
@a = lambda { |*, **k| k }
ruby

@a.().should == {}
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
@a.().should == {}
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}

suppress_keyword_warning do
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
@a.(h).should == {a: 1}
end
end
end

ruby_version_is '2.8' do
evaluate <<-ruby do
@a = lambda { |*, **k| k }
ruby

@a.().should == {}
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}

suppress_keyword_warning do
h = mock("keyword splat")
h.should_receive(:to_hash).and_return({a: 1})
@a.(h).should == {a: 1}
h.should_not_receive(:to_hash)
@a.(h).should == {}
end
end

Expand Down
Loading

0 comments on commit 55631ad

Please sign in to comment.