summaryrefslogtreecommitdiff
path: root/spec/ruby/core/proc/shared
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/proc/shared')
-rw-r--r--spec/ruby/core/proc/shared/call.rb31
-rw-r--r--spec/ruby/core/proc/shared/call_arguments.rb2
-rw-r--r--spec/ruby/core/proc/shared/compose.rb51
-rw-r--r--spec/ruby/core/proc/shared/dup.rb31
-rw-r--r--spec/ruby/core/proc/shared/equal.rb49
-rw-r--r--spec/ruby/core/proc/shared/to_s.rb23
6 files changed, 91 insertions, 96 deletions
diff --git a/spec/ruby/core/proc/shared/call.rb b/spec/ruby/core/proc/shared/call.rb
index 996d0e055d..dbec34df4b 100644
--- a/spec/ruby/core/proc/shared/call.rb
+++ b/spec/ruby/core/proc/shared/call.rb
@@ -3,7 +3,7 @@ require_relative '../fixtures/common'
describe :proc_call, shared: true do
it "invokes self" do
Proc.new { "test!" }.send(@method).should == "test!"
- lambda { "test!" }.send(@method).should == "test!"
+ -> { "test!" }.send(@method).should == "test!"
proc { "test!" }.send(@method).should == "test!"
end
@@ -12,9 +12,9 @@ describe :proc_call, shared: true do
Proc.new { |*args| args }.send(@method, 1, 2, 3, 4).should == [1, 2, 3, 4]
Proc.new { |_, *args| args }.send(@method, 1, 2, 3).should == [2, 3]
- lambda { |a, b| a + b }.send(@method, 1, 2).should == 3
- lambda { |*args| args }.send(@method, 1, 2, 3, 4).should == [1, 2, 3, 4]
- lambda { |_, *args| args }.send(@method, 1, 2, 3).should == [2, 3]
+ -> a, b { a + b }.send(@method, 1, 2).should == 3
+ -> *args { args }.send(@method, 1, 2, 3, 4).should == [1, 2, 3, 4]
+ -> _, *args { args }.send(@method, 1, 2, 3).should == [2, 3]
proc { |a, b| a + b }.send(@method, 1, 2).should == 3
proc { |*args| args }.send(@method, 1, 2, 3, 4).should == [1, 2, 3, 4]
@@ -49,6 +49,9 @@ describe :proc_call_on_proc_or_lambda, shared: true do
a = proc {|x| x}.send(@method, 1, 2, 3)
a.should == 1
+
+ a = proc {|x:| x}.send(@method, 2, x: 1)
+ a.should == 1
end
it "will call #to_ary on argument and return self if return is nil" do
@@ -65,28 +68,28 @@ describe :proc_call_on_proc_or_lambda, shared: true do
end
it "raises an ArgumentError on excess arguments when self is a lambda" do
- lambda {
- lambda {|x| x}.send(@method, 1, 2)
+ -> {
+ -> x { x }.send(@method, 1, 2)
}.should raise_error(ArgumentError)
- lambda {
- lambda {|x| x}.send(@method, 1, 2, 3)
+ -> {
+ -> x { x }.send(@method, 1, 2, 3)
}.should raise_error(ArgumentError)
end
it "raises an ArgumentError on missing arguments when self is a lambda" do
- lambda {
- lambda {|x| x}.send(@method)
+ -> {
+ -> x { x }.send(@method)
}.should raise_error(ArgumentError)
- lambda {
- lambda {|x,y| [x,y]}.send(@method, 1)
+ -> {
+ -> x, y { [x,y] }.send(@method, 1)
}.should raise_error(ArgumentError)
end
it "treats a single Array argument as a single argument when self is a lambda" do
- lambda { |a| a }.send(@method, [1, 2]).should == [1, 2]
- lambda { |a, b| [a, b] }.send(@method, [1, 2], 3).should == [[1,2], 3]
+ -> a { a }.send(@method, [1, 2]).should == [1, 2]
+ -> a, b { [a, b] }.send(@method, [1, 2], 3).should == [[1,2], 3]
end
it "treats a single Array argument as a single argument when self is a proc" do
diff --git a/spec/ruby/core/proc/shared/call_arguments.rb b/spec/ruby/core/proc/shared/call_arguments.rb
index a937bd99d0..91ada3439e 100644
--- a/spec/ruby/core/proc/shared/call_arguments.rb
+++ b/spec/ruby/core/proc/shared/call_arguments.rb
@@ -1,7 +1,7 @@
describe :proc_call_block_args, shared: true do
it "can receive block arguments" do
Proc.new {|&b| b.send(@method)}.send(@method) {1 + 1}.should == 2
- lambda {|&b| b.send(@method)}.send(@method) {1 + 1}.should == 2
+ -> &b { b.send(@method)}.send(@method) {1 + 1}.should == 2
proc {|&b| b.send(@method)}.send(@method) {1 + 1}.should == 2
end
diff --git a/spec/ruby/core/proc/shared/compose.rb b/spec/ruby/core/proc/shared/compose.rb
index 64338cada8..3d3f3b310d 100644
--- a/spec/ruby/core/proc/shared/compose.rb
+++ b/spec/ruby/core/proc/shared/compose.rb
@@ -1,47 +1,22 @@
describe :proc_compose, shared: true do
- ruby_version_is "2.6"..."2.7" do
- it "raises NoMethodError when called if passed not callable object" do
- not_callable = Object.new
- composed = @object.call.send(@method, not_callable)
+ it "raises TypeError if passed not callable object" do
+ lhs = @object.call
+ not_callable = Object.new
- -> {
- composed.call('a')
- }.should raise_error(NoMethodError, /undefined method `call' for/)
+ -> {
+ lhs.send(@method, not_callable)
+ }.should raise_error(TypeError, "callable object is expected")
- end
-
- it "when called does not try to coerce argument with #to_proc" do
- succ = Object.new
- def succ.to_proc(s); s.succ; end
-
- composed = @object.call.send(@method, succ)
-
- -> {
- composed.call('a')
- }.should raise_error(NoMethodError, /undefined method `call' for/)
- end
end
- ruby_version_is "2.7" do # https://bugs.ruby-lang.org/issues/15428
- it "raises TypeError if passed not callable object" do
- lhs = @object.call
- not_callable = Object.new
-
- -> {
- lhs.send(@method, not_callable)
- }.should raise_error(TypeError, "callable object is expected")
-
- end
-
- it "does not try to coerce argument with #to_proc" do
- lhs = @object.call
+ it "does not try to coerce argument with #to_proc" do
+ lhs = @object.call
- succ = Object.new
- def succ.to_proc(s); s.succ; end
+ succ = Object.new
+ def succ.to_proc(s); s.succ; end
- -> {
- lhs.send(@method, succ)
- }.should raise_error(TypeError, "callable object is expected")
- end
+ -> {
+ lhs.send(@method, succ)
+ }.should raise_error(TypeError, "callable object is expected")
end
end
diff --git a/spec/ruby/core/proc/shared/dup.rb b/spec/ruby/core/proc/shared/dup.rb
index fb6fff299d..1266337f94 100644
--- a/spec/ruby/core/proc/shared/dup.rb
+++ b/spec/ruby/core/proc/shared/dup.rb
@@ -1,10 +1,39 @@
describe :proc_dup, shared: true do
it "returns a copy of self" do
- a = lambda { "hello" }
+ a = -> { "hello" }
b = a.send(@method)
a.should_not equal(b)
a.call.should == b.call
end
+
+ it "returns an instance of subclass" do
+ cl = Class.new(Proc)
+
+ cl.new{}.send(@method).class.should == cl
+ end
+
+ ruby_version_is "3.4" do
+ it "copies instance variables" do
+ proc = -> { "hello" }
+ proc.instance_variable_set(:@ivar, 1)
+ cl = proc.send(@method)
+ cl.instance_variables.should == [:@ivar]
+ end
+
+ it "copies the finalizer" do
+ code = <<-'RUBY'
+ obj = Proc.new { }
+
+ ObjectSpace.define_finalizer(obj, Proc.new { STDOUT.write "finalized\n" })
+
+ obj.clone
+
+ exit 0
+ RUBY
+
+ ruby_exe(code).lines.sort.should == ["finalized\n", "finalized\n"]
+ end
+ end
end
diff --git a/spec/ruby/core/proc/shared/equal.rb b/spec/ruby/core/proc/shared/equal.rb
index a5d067cea3..d0503fb064 100644
--- a/spec/ruby/core/proc/shared/equal.rb
+++ b/spec/ruby/core/proc/shared/equal.rb
@@ -13,7 +13,7 @@ describe :proc_equal, shared: true do
p = Proc.new { :foo }
p.send(@method, p).should be_true
- p = lambda { :foo }
+ p = -> { :foo }
p.send(@method, p).should be_true
end
@@ -24,7 +24,7 @@ describe :proc_equal, shared: true do
p = Proc.new { :foo }
p.send(@method, p.dup).should be_true
- p = lambda { :foo }
+ p = -> { :foo }
p.send(@method, p.dup).should be_true
end
@@ -36,26 +36,26 @@ describe :proc_equal, shared: true do
a.send(@method, b).should be_false
end
- it "returns true if both procs have the same body and environment" do
+ it "returns false if procs are distinct but have the same body and environment" do
p = proc { :foo }
p2 = proc { :foo }
- p.send(@method, p2).should be_true
+ p.send(@method, p2).should be_false
end
- it "returns true if both lambdas with the same body and environment" do
- x = lambda { :foo }
- x2 = lambda { :foo }
- x.send(@method, x2).should be_true
+ it "returns false if lambdas are distinct but have same body and environment" do
+ x = -> { :foo }
+ x2 = -> { :foo }
+ x.send(@method, x2).should be_false
end
- it "returns true if both different kinds of procs with the same body and env" do
- p = lambda { :foo }
+ it "returns false if using comparing lambda to proc, even with the same body and env" do
+ p = -> { :foo }
p2 = proc { :foo }
- p.send(@method, p2).should be_true
+ p.send(@method, p2).should be_false
x = proc { :bar }
- x2 = lambda { :bar }
- x.send(@method, x2).should be_true
+ x2 = -> { :bar }
+ x.send(@method, x2).should be_false
end
it "returns false if other is not a Proc" do
@@ -65,7 +65,7 @@ describe :proc_equal, shared: true do
p = Proc.new { :foo }
p.send(@method, Object.new).should be_false
- p = lambda { :foo }
+ p = -> { :foo }
p.send(@method, :foo).should be_false
end
@@ -76,25 +76,8 @@ describe :proc_equal, shared: true do
end
it "returns false if self and other are both lambdas but have different bodies" do
- p = lambda { :foo }
- p2 = lambda { :bar }
+ p = -> { :foo }
+ p2 = -> { :bar }
p.send(@method, p2).should be_false
end
end
-
-describe :proc_equal_undefined, shared: true do
- it "is not defined" do
- Proc.should_not have_instance_method(@method, false)
- end
-
- it "returns false if other is a dup of the original" do
- p = proc { :foo }
- p.send(@method, p.dup).should be_false
-
- p = Proc.new { :foo }
- p.send(@method, p.dup).should be_false
-
- p = lambda { :foo }
- p.send(@method, p.dup).should be_false
- end
-end
diff --git a/spec/ruby/core/proc/shared/to_s.rb b/spec/ruby/core/proc/shared/to_s.rb
index 59fcf5bef0..a52688a89f 100644
--- a/spec/ruby/core/proc/shared/to_s.rb
+++ b/spec/ruby/core/proc/shared/to_s.rb
@@ -1,7 +1,7 @@
describe :proc_to_s, shared: true do
describe "for a proc created with Proc.new" do
- it "returns a description optionally including file and line number" do
- Proc.new { "hello" }.send(@method).should =~ /^#<Proc:([^ ]*?)(@([^ ]*)\/to_s\.rb:4)?>$/
+ it "returns a description including file and line number" do
+ Proc.new { "hello" }.send(@method).should =~ /^#<Proc:([^ ]*?) #{Regexp.escape __FILE__}:#{__LINE__ }>$/
end
it "has a binary encoding" do
@@ -10,18 +10,18 @@ describe :proc_to_s, shared: true do
end
describe "for a proc created with lambda" do
- it "returns a description including '(lambda)' and optionally including file and line number" do
- lambda { "hello" }.send(@method).should =~ /^#<Proc:([^ ]*?)(@([^ ]*)\/to_s\.rb:10)? \(lambda\)>$/
+ it "returns a description including '(lambda)' and including file and line number" do
+ -> { "hello" }.send(@method).should =~ /^#<Proc:([^ ]*?) #{Regexp.escape __FILE__}:#{__LINE__ } \(lambda\)>$/
end
it "has a binary encoding" do
- lambda { "hello" }.send(@method).encoding.should == Encoding::BINARY
+ -> { "hello" }.send(@method).encoding.should == Encoding::BINARY
end
end
describe "for a proc created with proc" do
- it "returns a description optionally including file and line number" do
- proc { "hello" }.send(@method).should =~ /^#<Proc:([^ ]*?)(@([^ ]*)\/to_s\.rb:16)?>$/
+ it "returns a description including file and line number" do
+ proc { "hello" }.send(@method).should =~ /^#<Proc:([^ ]*?) #{Regexp.escape __FILE__}:#{__LINE__ }>$/
end
it "has a binary encoding" do
@@ -32,7 +32,12 @@ describe :proc_to_s, shared: true do
describe "for a proc created with UnboundMethod#to_proc" do
it "returns a description including '(lambda)' and optionally including file and line number" do
def hello; end
- method("hello").to_proc.send(@method).should =~ /^#<Proc:([^ ]*?)(@([^ ]*)\/to_s\.rb:22)? \(lambda\)>$/
+ s = method("hello").to_proc.send(@method)
+ if s.include? __FILE__
+ s.should =~ /^#<Proc:([^ ]*?) #{Regexp.escape __FILE__}:#{__LINE__ - 3} \(lambda\)>$/
+ else
+ s.should =~ /^#<Proc:([^ ]*?) \(lambda\)>$/
+ end
end
it "has a binary encoding" do
@@ -44,7 +49,7 @@ describe :proc_to_s, shared: true do
describe "for a proc created with Symbol#to_proc" do
it "returns a description including '(&:symbol)'" do
proc = :foobar.to_proc
- proc.send(@method).should =~ /^#<Proc:0x\h+\(&:foobar\)>$/
+ proc.send(@method).should.include?('(&:foobar)')
end
it "has a binary encoding" do