Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ruby 2.7] Specs for #ruby2_keywords #821

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions core/module/ruby2_keywords_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

ruby_version_is "2.7" do
describe "Module#ruby2_keywords" do
it "marks the final hash argument as keyword hash" do
obj = Object.new

obj.singleton_class.class_exec do
def foo(*a) a.last end
ruby2_keywords :foo
end

last = obj.foo(1, 2, a: "a")
Hash.ruby2_keywords_hash?(last).should == true
end

ruby_version_is "2.7" ... "3.0" do
it "fixes delegation warnings when calling a method accepting keywords" do
obj = Object.new

obj.singleton_class.class_exec do
def foo(*a) bar(*a) end
def bar(*a, **b) end
end

-> { obj.foo(1, 2, {a: "a"}) }.should complain(/Using the last argument as keyword parameters is deprecated/)

obj.singleton_class.class_exec do
ruby2_keywords :foo
end

-> { obj.foo(1, 2, {a: "a"}) }.should_not complain
end
end

it "returns nil" do
obj = Object.new

obj.singleton_class.class_exec do
def foo(*a) end

ruby2_keywords(:foo).should == nil
end
end

it "raises NameError when passed not existing method name" do
obj = Object.new

-> {
obj.singleton_class.class_exec do
ruby2_keywords :not_existing
end
}.should raise_error(NameError, /undefined method `not_existing'/)
end

it "acceps String as well" do
obj = Object.new

obj.singleton_class.class_exec do
def foo(*a) a.last end
ruby2_keywords "foo"
end

last = obj.foo(1, 2, a: "a")
Hash.ruby2_keywords_hash?(last).should == true
end

it "raises TypeError when passed not Symbol or String" do
obj = Object.new

-> {
obj.singleton_class.class_exec do
ruby2_keywords Object.new
end
}.should raise_error(TypeError, /is not a symbol nor a string/)
end

it "prints warning when a method does not accept argument splat" do
obj = Object.new
def obj.foo(a, b, c) end

-> {
obj.singleton_class.class_exec do
ruby2_keywords :foo
end
}.should complain(/Skipping set of ruby2_keywords flag for/)
end

it "prints warning when a method accepts keywords" do
obj = Object.new
def obj.foo(a:, b:) end

-> {
obj.singleton_class.class_exec do
ruby2_keywords :foo
end
}.should complain(/Skipping set of ruby2_keywords flag for/)
end

it "prints warning when a method accepts keyword splat" do
obj = Object.new
def obj.foo(**a) end

-> {
obj.singleton_class.class_exec do
ruby2_keywords :foo
end
}.should complain(/Skipping set of ruby2_keywords flag for/)
end
end
end
64 changes: 64 additions & 0 deletions core/proc/ruby2_keywords_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require_relative '../../spec_helper'

ruby_version_is "2.7" do
describe "Proc#ruby2_keywords" do
it "marks the final hash argument as keyword hash" do
f = -> *a { a.last }
f.ruby2_keywords

last = f.call(1, 2, a: "a")
Hash.ruby2_keywords_hash?(last).should == true
end

ruby_version_is "2.7" ... "3.0" do
it "fixes delegation warnings when calling a method accepting keywords" do
obj = Object.new
def obj.foo(*a, **b) end

f = -> *a { obj.foo(*a) }

-> { f.call(1, 2, {a: "a"}) }.should complain(/Using the last argument as keyword parameters is deprecated/)
f.ruby2_keywords
-> { f.call(1, 2, {a: "a"}) }.should_not complain
end

it "fixes delegation warnings when calling a proc accepting keywords" do
g = -> *a, **b { }
f = -> *a { g.call(*a) }

-> { f.call(1, 2, {a: "a"}) }.should complain(/Using the last argument as keyword parameters is deprecated/)
f.ruby2_keywords
-> { f.call(1, 2, {a: "a"}) }.should_not complain
end
end

it "returns self" do
f = -> *a { }
f.ruby2_keywords.should equal f
end

it "prints warning when a proc does not accept argument splat" do
f = -> a, b, c { }

-> {
f.ruby2_keywords
}.should complain(/Skipping set of ruby2_keywords flag for/)
end

it "prints warning when a proc accepts keywords" do
f = -> a:, b: { }

-> {
f.ruby2_keywords
}.should complain(/Skipping set of ruby2_keywords flag for/)
end

it "prints warning when a proc accepts keyword splat" do
f = -> **a { }

-> {
f.ruby2_keywords
}.should complain(/Skipping set of ruby2_keywords flag for/)
end
end
end