summaryrefslogtreecommitdiff
path: root/spec/ruby/core/proc
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/proc')
-rw-r--r--spec/ruby/core/proc/allocate_spec.rb9
-rw-r--r--spec/ruby/core/proc/arity_spec.rb640
-rw-r--r--spec/ruby/core/proc/binding_spec.rb21
-rw-r--r--spec/ruby/core/proc/block_pass_spec.rb41
-rw-r--r--spec/ruby/core/proc/call_spec.rb16
-rw-r--r--spec/ruby/core/proc/case_compare_spec.rb16
-rw-r--r--spec/ruby/core/proc/clone_spec.rb6
-rw-r--r--spec/ruby/core/proc/curry_spec.rb180
-rw-r--r--spec/ruby/core/proc/dup_spec.rb6
-rw-r--r--spec/ruby/core/proc/element_reference_spec.rb16
-rw-r--r--spec/ruby/core/proc/eql_spec.rb6
-rw-r--r--spec/ruby/core/proc/equal_value_spec.rb6
-rw-r--r--spec/ruby/core/proc/fixtures/common.rb51
-rw-r--r--spec/ruby/core/proc/fixtures/source_location.rb55
-rw-r--r--spec/ruby/core/proc/hash_spec.rb17
-rw-r--r--spec/ruby/core/proc/inspect_spec.rb6
-rw-r--r--spec/ruby/core/proc/lambda_spec.rb60
-rw-r--r--spec/ruby/core/proc/new_spec.rb190
-rw-r--r--spec/ruby/core/proc/parameters_spec.rb95
-rw-r--r--spec/ruby/core/proc/shared/call.rb96
-rw-r--r--spec/ruby/core/proc/shared/call_arguments.rb7
-rw-r--r--spec/ruby/core/proc/shared/dup.rb10
-rw-r--r--spec/ruby/core/proc/shared/equal.rb100
-rw-r--r--spec/ruby/core/proc/shared/to_s.rb27
-rw-r--r--spec/ruby/core/proc/source_location_spec.rb72
-rw-r--r--spec/ruby/core/proc/to_proc_spec.rb9
-rw-r--r--spec/ruby/core/proc/to_s_spec.rb6
-rw-r--r--spec/ruby/core/proc/yield_spec.rb16
28 files changed, 1780 insertions, 0 deletions
diff --git a/spec/ruby/core/proc/allocate_spec.rb b/spec/ruby/core/proc/allocate_spec.rb
new file mode 100644
index 0000000000..6bfb94dbc2
--- /dev/null
+++ b/spec/ruby/core/proc/allocate_spec.rb
@@ -0,0 +1,9 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Proc.allocate" do
+ it "raises a TypeError" do
+ lambda {
+ Proc.allocate
+ }.should raise_error(TypeError)
+ end
+end
diff --git a/spec/ruby/core/proc/arity_spec.rb b/spec/ruby/core/proc/arity_spec.rb
new file mode 100644
index 0000000000..251710a663
--- /dev/null
+++ b/spec/ruby/core/proc/arity_spec.rb
@@ -0,0 +1,640 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Proc#arity" do
+ SpecEvaluate.desc = "for definition"
+
+ context "for instances created with -> () { }" do
+ context "returns zero" do
+ evaluate <<-ruby do
+ @a = -> () {}
+ ruby
+
+ @a.arity.should == 0
+ end
+
+ evaluate <<-ruby do
+ @a = -> (&b) {}
+ ruby
+
+ @a.arity.should == 0
+ end
+ end
+
+ context "returns positive values" do
+ evaluate <<-ruby do
+ @a = -> (a) { }
+ @b = -> (a, b) { }
+ @c = -> (a, b, c) { }
+ @d = -> (a, b, c, d) { }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 2
+ @c.arity.should == 3
+ @d.arity.should == 4
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a:) { }
+ @b = -> (a:, b:) { }
+ @c = -> (a: 1, b:, c:, d: 2) { }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 1
+ @c.arity.should == 1
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a, b:) { }
+ @b = -> (a, b:, &l) { }
+ ruby
+
+ @a.arity.should == 2
+ @b.arity.should == 2
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a, b, c:, d: 1) { }
+ @b = -> (a, b, c:, d: 1, **k, &l) { }
+ ruby
+
+ @a.arity.should == 3
+ @b.arity.should == 3
+ end
+
+ evaluate <<-ruby do
+ @a = -> ((a, (*b, c))) { }
+ @b = -> (a, (*b, c), d, (*e), (*)) { }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 5
+ end
+ end
+
+ context "returns negative values" do
+ evaluate <<-ruby do
+ @a = -> (a=1) { }
+ @b = -> (a=1, b=2) { }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a, b=1) { }
+ @b = -> (a, b, c=1, d=2) { }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -3
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a=1, *b) { }
+ @b = -> (a=1, b=2, *c) { }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = -> (*) { }
+ @b = -> (*a) { }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a, *) { }
+ @b = -> (a, *b) { }
+ @c = -> (a, b, *c) { }
+ @d = -> (a, b, c, *d) { }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -2
+ @c.arity.should == -3
+ @d.arity.should == -4
+ end
+
+ evaluate <<-ruby do
+ @a = -> (*a, b) { }
+ @b = -> (*a, b, c) { }
+ @c = -> (*a, b, c, d) { }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -3
+ @c.arity.should == -4
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a, *b, c) { }
+ @b = -> (a, b, *c, d, e) { }
+ ruby
+
+ @a.arity.should == -3
+ @b.arity.should == -5
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a, b=1, c=2, *d, e, f) { }
+ @b = -> (a, b, c=1, *d, e, f, g) { }
+ ruby
+
+ @a.arity.should == -4
+ @b.arity.should == -6
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a: 1) { }
+ @b = -> (a: 1, b: 2) { }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a=1, b: 2) { }
+ @b = -> (*a, b: 1) { }
+ @c = -> (a=1, b: 2) { }
+ @d = -> (a=1, *b, c: 2, &l) { }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ @c.arity.should == -1
+ @d.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = -> (**k, &l) { }
+ @b= -> (*a, **k) { }
+ @c = ->(a: 1, b: 2, **k) { }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ @c.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a=1, *b, c:, d: 2, **k, &l) { }
+ ruby
+
+ @a.arity.should == -2
+ end
+
+ evaluate <<-ruby do
+ @a = -> (a, b=1, *c, d, e:, f: 2, **k, &l) { }
+ @b = -> (a, b=1, *c, d:, e:, f: 2, **k, &l) { }
+ @c = -> (a=0, b=1, *c, d, e:, f: 2, **k, &l) { }
+ @d = -> (a=0, b=1, *c, d:, e:, f: 2, **k, &l) { }
+ ruby
+
+ @a.arity.should == -4
+ @b.arity.should == -3
+ @c.arity.should == -3
+ @d.arity.should == -2
+ end
+ end
+ end
+
+ context "for instances created with lambda { || }" do
+ context "returns zero" do
+ evaluate <<-ruby do
+ @a = lambda { }
+ @b = lambda { || }
+ ruby
+
+ @a.arity.should == 0
+ @b.arity.should == 0
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |&b| }
+ ruby
+
+ @a.arity.should == 0
+ end
+ end
+
+ context "returns positive values" do
+ evaluate <<-ruby do
+ @a = lambda { |a| }
+ @b = lambda { |a, b| }
+ @c = lambda { |a, b, c| }
+ @d = lambda { |a, b, c, d| }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 2
+ @c.arity.should == 3
+ @d.arity.should == 4
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a:| }
+ @b = lambda { |a:, b:| }
+ @c = lambda { |a: 1, b:, c:, d: 2| }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 1
+ @c.arity.should == 1
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a, b:| }
+ @b = lambda { |a, b:, &l| }
+ ruby
+
+ @a.arity.should == 2
+ @b.arity.should == 2
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a, b, c:, d: 1| }
+ @b = lambda { |a, b, c:, d: 1, **k, &l| }
+ ruby
+
+ @a.arity.should == 3
+ @b.arity.should == 3
+ end
+ end
+
+ context "returns negative values" do
+ evaluate <<-ruby do
+ @a = lambda { |a=1| }
+ @b = lambda { |a=1, b=2| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a, b=1| }
+ @b = lambda { |a, b, c=1, d=2| }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -3
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a=1, *b| }
+ @b = lambda { |a=1, b=2, *c| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |*| }
+ @b = lambda { |*a| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a, *| }
+ @b = lambda { |a, *b| }
+ @c = lambda { |a, b, *c| }
+ @d = lambda { |a, b, c, *d| }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -2
+ @c.arity.should == -3
+ @d.arity.should == -4
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |*a, b| }
+ @b = lambda { |*a, b, c| }
+ @c = lambda { |*a, b, c, d| }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -3
+ @c.arity.should == -4
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a, *b, c| }
+ @b = lambda { |a, b, *c, d, e| }
+ ruby
+
+ @a.arity.should == -3
+ @b.arity.should == -5
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a, b=1, c=2, *d, e, f| }
+ @b = lambda { |a, b, c=1, *d, e, f, g| }
+ ruby
+
+ @a.arity.should == -4
+ @b.arity.should == -6
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a: 1| }
+ @b = lambda { |a: 1, b: 2| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a=1, b: 2| }
+ @b = lambda { |*a, b: 1| }
+ @c = lambda { |a=1, b: 2| }
+ @d = lambda { |a=1, *b, c: 2, &l| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ @c.arity.should == -1
+ @d.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |**k, &l| }
+ @b = lambda { |*a, **k| }
+ @c = lambda { |a: 1, b: 2, **k| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ @c.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a=1, *b, c:, d: 2, **k, &l| }
+ ruby
+
+ @a.arity.should == -2
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |(a, (*b, c)), d=1| }
+ @b = lambda { |a, (*b, c), d, (*e), (*), **k| }
+ @c = lambda { |a, (b, c), *, d:, e: 2, **| }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -6
+ @c.arity.should == -4
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a, b=1, *c, d, e:, f: 2, **k, &l| }
+ @b = lambda { |a, b=1, *c, d:, e:, f: 2, **k, &l| }
+ @c = lambda { |a=0, b=1, *c, d, e:, f: 2, **k, &l| }
+ @d = lambda { |a=0, b=1, *c, d:, e:, f: 2, **k, &l| }
+ ruby
+
+ @a.arity.should == -4
+ @b.arity.should == -3
+ @c.arity.should == -3
+ @d.arity.should == -2
+ end
+ end
+ end
+
+ context "for instances created with proc { || }" do
+ context "returns zero" do
+ evaluate <<-ruby do
+ @a = proc { }
+ @b = proc { || }
+ ruby
+
+ @a.arity.should == 0
+ @b.arity.should == 0
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |&b| }
+ ruby
+
+ @a.arity.should == 0
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a=1| }
+ @b = proc { |a=1, b=2| }
+ ruby
+
+ @a.arity.should == 0
+ @b.arity.should == 0
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a: 1| }
+ @b = proc { |a: 1, b: 2| }
+ ruby
+
+ @a.arity.should == 0
+ @b.arity.should == 0
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |**k, &l| }
+ @b = proc { |a: 1, b: 2, **k| }
+ ruby
+
+ @a.arity.should == 0
+ @b.arity.should == 0
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a=1, b: 2| }
+ @b = proc { |a=1, b: 2| }
+ ruby
+
+ @a.arity.should == 0
+ @b.arity.should == 0
+ end
+ end
+
+ context "returns positive values" do
+ evaluate <<-ruby do
+ @a = proc { |a| }
+ @b = proc { |a, b| }
+ @c = proc { |a, b, c| }
+ @d = proc { |a, b, c, d| }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 2
+ @c.arity.should == 3
+ @d.arity.should == 4
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a, b=1| }
+ @b = proc { |a, b, c=1, d=2| }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 2
+ end
+
+ evaluate <<-ruby do
+ @a = lambda { |a:| }
+ @b = lambda { |a:, b:| }
+ @c = lambda { |a: 1, b:, c:, d: 2| }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 1
+ @c.arity.should == 1
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a, b:| }
+ @b = proc { |a, b:, &l| }
+ ruby
+
+ @a.arity.should == 2
+ @b.arity.should == 2
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a, b, c:, d: 1| }
+ @b = proc { |a, b, c:, d: 1, **k, &l| }
+ ruby
+
+ @a.arity.should == 3
+ @b.arity.should == 3
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |(a, (*b, c)), d=1| }
+ @b = proc { |a, (*b, c), d, (*e), (*), **k| }
+ ruby
+
+ @a.arity.should == 1
+ @b.arity.should == 5
+ end
+ end
+
+ context "returns negative values" do
+ evaluate <<-ruby do
+ @a = proc { |a=1, *b| }
+ @b = proc { |a=1, b=2, *c| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |*| }
+ @b = proc { |*a| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a, *| }
+ @b = proc { |a, *b| }
+ @c = proc { |a, b, *c| }
+ @d = proc { |a, b, c, *d| }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -2
+ @c.arity.should == -3
+ @d.arity.should == -4
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |*a, b| }
+ @b = proc { |*a, b, c| }
+ @c = proc { |*a, b, c, d| }
+ ruby
+
+ @a.arity.should == -2
+ @b.arity.should == -3
+ @c.arity.should == -4
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a, *b, c| }
+ @b = proc { |a, b, *c, d, e| }
+ ruby
+
+ @a.arity.should == -3
+ @b.arity.should == -5
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a, b=1, c=2, *d, e, f| }
+ @b = proc { |a, b, c=1, *d, e, f, g| }
+ ruby
+
+ @a.arity.should == -4
+ @b.arity.should == -6
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |*a, b: 1| }
+ @b = proc { |a=1, *b, c: 2, &l| }
+ ruby
+
+ @a.arity.should == -1
+ @b.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |*a, **k| }
+ ruby
+
+ @a.arity.should == -1
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a=1, *b, c:, d: 2, **k, &l| }
+ ruby
+
+ @a.arity.should == -2
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a, (b, c), *, d:, e: 2, **| }
+ ruby
+
+ @a.arity.should == -4
+ end
+
+ evaluate <<-ruby do
+ @a = proc { |a, b=1, *c, d, e:, f: 2, **k, &l| }
+ @b = proc { |a, b=1, *c, d:, e:, f: 2, **k, &l| }
+ @c = proc { |a=0, b=1, *c, d, e:, f: 2, **k, &l| }
+ @d = proc { |a=0, b=1, *c, d:, e:, f: 2, **k, &l| }
+ ruby
+
+ @a.arity.should == -4
+ @b.arity.should == -3
+ @c.arity.should == -3
+ @d.arity.should == -2
+ end
+ end
+ end
+end
diff --git a/spec/ruby/core/proc/binding_spec.rb b/spec/ruby/core/proc/binding_spec.rb
new file mode 100644
index 0000000000..05cc68217e
--- /dev/null
+++ b/spec/ruby/core/proc/binding_spec.rb
@@ -0,0 +1,21 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Proc#binding" do
+ it "returns a Binding instance" do
+ [Proc.new{}, lambda {}, proc {}].each { |p|
+ p.binding.should be_kind_of(Binding)
+ }
+ end
+
+ it "returns the binding associated with self" do
+ obj = mock('binding')
+ def obj.test_binding(some, params)
+ lambda {}
+ end
+
+ lambdas_binding = obj.test_binding(1, 2).binding
+
+ eval("some", lambdas_binding).should == 1
+ eval("params", lambdas_binding).should == 2
+ end
+end
diff --git a/spec/ruby/core/proc/block_pass_spec.rb b/spec/ruby/core/proc/block_pass_spec.rb
new file mode 100644
index 0000000000..e956885654
--- /dev/null
+++ b/spec/ruby/core/proc/block_pass_spec.rb
@@ -0,0 +1,41 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Proc as a block pass argument" do
+ def revivify(&b)
+ b
+ end
+
+ it "remains the same object if re-vivified by the target method" do
+ p = Proc.new {}
+ p2 = revivify(&p)
+ p.object_id.should == p2.object_id
+ p.should == p2
+ end
+
+ it "remains the same object if reconstructed with Proc.new" do
+ p = Proc.new {}
+ p2 = Proc.new(&p)
+ p.object_id.should == p2.object_id
+ p.should == p2
+ end
+end
+
+describe "Proc as an implicit block pass argument" do
+ def revivify
+ Proc.new
+ end
+
+ it "remains the same object if re-vivified by the target method" do
+ p = Proc.new {}
+ p2 = revivify(&p)
+ p.object_id.should == p2.object_id
+ p.should == p2
+ end
+
+ it "remains the same object if reconstructed with Proc.new" do
+ p = Proc.new {}
+ p2 = Proc.new(&p)
+ p.object_id.should == p2.object_id
+ p.should == p2
+ end
+end
diff --git a/spec/ruby/core/proc/call_spec.rb b/spec/ruby/core/proc/call_spec.rb
new file mode 100644
index 0000000000..1c28eae9b0
--- /dev/null
+++ b/spec/ruby/core/proc/call_spec.rb
@@ -0,0 +1,16 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/call', __FILE__)
+require File.expand_path('../shared/call_arguments', __FILE__)
+
+describe "Proc#call" do
+ it_behaves_like :proc_call, :call
+ it_behaves_like :proc_call_block_args, :call
+end
+
+describe "Proc#call on a Proc created with Proc.new" do
+ it_behaves_like :proc_call_on_proc_new, :call
+end
+
+describe "Proc#call on a Proc created with Kernel#lambda or Kernel#proc" do
+ it_behaves_like :proc_call_on_proc_or_lambda, :call
+end
diff --git a/spec/ruby/core/proc/case_compare_spec.rb b/spec/ruby/core/proc/case_compare_spec.rb
new file mode 100644
index 0000000000..55760c4ff3
--- /dev/null
+++ b/spec/ruby/core/proc/case_compare_spec.rb
@@ -0,0 +1,16 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/call', __FILE__)
+require File.expand_path('../shared/call_arguments', __FILE__)
+
+describe "Proc#===" do
+ it_behaves_like :proc_call, :===
+ it_behaves_like :proc_call_block_args, :===
+end
+
+describe "Proc#=== on a Proc created with Proc.new" do
+ it_behaves_like :proc_call_on_proc_new, :===
+end
+
+describe "Proc#=== on a Proc created with Kernel#lambda or Kernel#proc" do
+ it_behaves_like :proc_call_on_proc_or_lambda, :===
+end
diff --git a/spec/ruby/core/proc/clone_spec.rb b/spec/ruby/core/proc/clone_spec.rb
new file mode 100644
index 0000000000..0f0806645b
--- /dev/null
+++ b/spec/ruby/core/proc/clone_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/dup', __FILE__)
+
+describe "Proc#clone" do
+ it_behaves_like(:proc_dup, :clone)
+end
diff --git a/spec/ruby/core/proc/curry_spec.rb b/spec/ruby/core/proc/curry_spec.rb
new file mode 100644
index 0000000000..a294606957
--- /dev/null
+++ b/spec/ruby/core/proc/curry_spec.rb
@@ -0,0 +1,180 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Proc#curry" do
+ before :each do
+ @proc_add = Proc.new {|x,y,z| (x||0) + (y||0) + (z||0) }
+ @lambda_add = lambda {|x,y,z| (x||0) + (y||0) + (z||0) }
+ end
+
+ it "returns a Proc when called on a proc" do
+ p = proc { true }
+ p.curry.should be_an_instance_of(Proc)
+ end
+
+ it "returns a Proc when called on a lambda" do
+ p = lambda { true }
+ p.curry.should be_an_instance_of(Proc)
+ end
+
+ it "calls the curried proc with the arguments if sufficient arguments have been given" do
+ @proc_add.curry[1][2][3].should == 6
+ @lambda_add.curry[1][2][3].should == 6
+ end
+
+ it "returns a Proc that consumes the remainder of the arguments unless sufficient arguments have been given" do
+ proc2 = @proc_add.curry[1][2]
+ proc2.should be_an_instance_of(Proc)
+ proc2.call(3).should == 6
+
+ lambda2 = @lambda_add.curry[1][2]
+ lambda2.should be_an_instance_of(Proc)
+ lambda2.call(3).should == 6
+
+ @proc_add.curry.call(1,2,3).should == 6
+ @lambda_add.curry.call(1,2,3).should == 6
+ end
+
+ it "can be called multiple times on the same Proc" do
+ @proc_add.curry
+ lambda { @proc_add.curry }.should_not raise_error
+
+ @lambda_add.curry
+ lambda { @lambda_add.curry }.should_not raise_error
+ end
+
+ it "can be passed superfluous arguments if created from a proc" do
+ @proc_add.curry[1,2,3,4].should == 6
+
+ @proc_add.curry[1,2].curry[3,4,5,6].should == 6
+ end
+
+ it "raises an ArgumentError if passed superfluous arguments when created from a lambda" do
+ lambda { @lambda_add.curry[1,2,3,4] }.should raise_error(ArgumentError)
+ lambda { @lambda_add.curry[1,2].curry[3,4,5,6] }.should raise_error(ArgumentError)
+ end
+
+ it "returns Procs with arities of -1" do
+ @proc_add.curry.arity.should == -1
+ @lambda_add.curry.arity.should == -1
+ l = lambda { |*a| }
+ l.curry.arity.should == -1
+ end
+
+ it "produces Procs that raise ArgumentError for #binding" do
+ lambda do
+ @proc_add.curry.binding
+ end.should raise_error(ArgumentError)
+ end
+
+ it "produces Procs that return [[:rest]] for #parameters" do
+ @proc_add.curry.parameters.should == [[:rest]]
+ end
+
+ it "produces Procs that return nil for #source_location" do
+ @proc_add.curry.source_location.should == nil
+ end
+
+ it "produces Procs that can be passed as the block for instance_exec" do
+ curried = @proc_add.curry.call(1, 2)
+
+ instance_exec(3, &curried).should == 6
+ end
+
+ it "combines arguments and calculates incoming arity accurately for successively currying" do
+ l = lambda{|a,b,c| a+b+c }
+ l1 = l.curry.call(1)
+ # the l1 currying seems unnecessary, but it triggered the original issue
+ l2 = l1.curry.call(2)
+
+ l2.curry.call(3).should == 6
+ l1.curry.call(2,3).should == 6
+ end
+end
+
+describe "Proc#curry with arity argument" do
+ before :each do
+ @proc_add = proc {|x,y,z| (x||0) + (y||0) + (z||0) }
+ @lambda_add = lambda {|x,y,z| (x||0) + (y||0) + (z||0) }
+ end
+
+ it "accepts an optional Integer argument for the arity" do
+ lambda { @proc_add.curry(3) }.should_not raise_error
+ lambda { @lambda_add.curry(3) }.should_not raise_error
+ end
+
+ it "returns a Proc when called on a proc" do
+ @proc_add.curry(3).should be_an_instance_of(Proc)
+ end
+
+ it "returns a Proc when called on a lambda" do
+ @lambda_add.curry(3).should be_an_instance_of(Proc)
+ end
+
+ # [ruby-core:24127]
+ it "retains the lambda-ness of the Proc on which its called" do
+ @lambda_add.curry(3).lambda?.should be_true
+ @proc_add.curry(3).lambda?.should be_false
+ end
+
+ it "raises an ArgumentError if called on a lambda that requires more than _arity_ arguments" do
+ lambda { @lambda_add.curry(2) }.should raise_error(ArgumentError)
+ lambda { lambda{|x, y, z, *more|}.curry(2) }.should raise_error(ArgumentError)
+ end
+
+ it 'returns a Proc if called on a lambda that requires fewer than _arity_ arguments but may take more' do
+ lambda{|a, b, c, d=nil, e=nil|}.curry(4).should be_an_instance_of(Proc)
+ lambda{|a, b, c, d=nil, *e|}.curry(4).should be_an_instance_of(Proc)
+ lambda{|a, b, c, *d|}.curry(4).should be_an_instance_of(Proc)
+ end
+
+ it "raises an ArgumentError if called on a lambda that requires fewer than _arity_ arguments" do
+ lambda { @lambda_add.curry(4) }.should raise_error(ArgumentError)
+ lambda { lambda { true }.curry(1) }.should raise_error(ArgumentError)
+ lambda { lambda {|a, b=nil|}.curry(5) }.should raise_error(ArgumentError)
+ lambda { lambda {|a, &b|}.curry(2) }.should raise_error(ArgumentError)
+ lambda { lambda {|a, b=nil, &c|}.curry(3) }.should raise_error(ArgumentError)
+ end
+
+ it "calls the curried proc with the arguments if _arity_ arguments have been given" do
+ @proc_add.curry(3)[1][2][3].should == 6
+ @lambda_add.curry(3)[1][2][3].should == 6
+ end
+
+ it "returns a Proc that consumes the remainder of the arguments when fewer than _arity_ arguments are given" do
+ proc2 = @proc_add.curry(3)[1][2]
+ proc2.should be_an_instance_of(Proc)
+ proc2.call(3).should == 6
+
+ lambda2 = @lambda_add.curry(3)[1][2]
+ lambda2.should be_an_instance_of(Proc)
+ lambda2.call(3).should == 6
+ end
+
+ it "can be specified multiple times on the same Proc" do
+ @proc_add.curry(2)
+ lambda { @proc_add.curry(1) }.should_not raise_error
+
+ @lambda_add.curry(3)
+ lambda { @lambda_add.curry(3) }.should_not raise_error
+ end
+
+ it "can be passed more than _arity_ arguments if created from a proc" do
+ lambda { @proc_add.curry(3)[1,2,3,4].should == 6 }.should_not
+ raise_error(ArgumentError)
+ lambda { @proc_add.curry(1)[1,2].curry(3)[3,4,5,6].should == 6 }.should_not
+ raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError if passed more than _arity_ arguments when created from a lambda" do
+ lambda { @lambda_add.curry(3)[1,2,3,4] }.should raise_error(ArgumentError)
+ lambda { @lambda_add.curry(1)[1,2].curry(3)[3,4,5,6] }.should raise_error(ArgumentError)
+ end
+
+ it "returns Procs with arities of -1 regardless of the value of _arity_" do
+ @proc_add.curry(1).arity.should == -1
+ @proc_add.curry(2).arity.should == -1
+ @lambda_add.curry(3).arity.should == -1
+ l = lambda { |*a| }
+ l.curry(3).arity.should == -1
+ end
+end
diff --git a/spec/ruby/core/proc/dup_spec.rb b/spec/ruby/core/proc/dup_spec.rb
new file mode 100644
index 0000000000..ea3fe8aac9
--- /dev/null
+++ b/spec/ruby/core/proc/dup_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/dup', __FILE__)
+
+describe "Proc#dup" do
+ it_behaves_like(:proc_dup, :dup)
+end
diff --git a/spec/ruby/core/proc/element_reference_spec.rb b/spec/ruby/core/proc/element_reference_spec.rb
new file mode 100644
index 0000000000..f3dec21253
--- /dev/null
+++ b/spec/ruby/core/proc/element_reference_spec.rb
@@ -0,0 +1,16 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/call', __FILE__)
+require File.expand_path('../shared/call_arguments', __FILE__)
+
+describe "Proc#[]" do
+ it_behaves_like :proc_call, :[]
+ it_behaves_like :proc_call_block_args, :[]
+end
+
+describe "Proc#call on a Proc created with Proc.new" do
+ it_behaves_like :proc_call_on_proc_new, :call
+end
+
+describe "Proc#call on a Proc created with Kernel#lambda or Kernel#proc" do
+ it_behaves_like :proc_call_on_proc_or_lambda, :call
+end
diff --git a/spec/ruby/core/proc/eql_spec.rb b/spec/ruby/core/proc/eql_spec.rb
new file mode 100644
index 0000000000..e0cc80bb48
--- /dev/null
+++ b/spec/ruby/core/proc/eql_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/equal', __FILE__)
+
+describe "Proc#eql?" do
+ it_behaves_like(:proc_equal_undefined, :eql?)
+end
diff --git a/spec/ruby/core/proc/equal_value_spec.rb b/spec/ruby/core/proc/equal_value_spec.rb
new file mode 100644
index 0000000000..e50ea1a53b
--- /dev/null
+++ b/spec/ruby/core/proc/equal_value_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/equal', __FILE__)
+
+describe "Proc#==" do
+ it_behaves_like(:proc_equal_undefined, :==)
+end
diff --git a/spec/ruby/core/proc/fixtures/common.rb b/spec/ruby/core/proc/fixtures/common.rb
new file mode 100644
index 0000000000..6e27a2dee7
--- /dev/null
+++ b/spec/ruby/core/proc/fixtures/common.rb
@@ -0,0 +1,51 @@
+module ProcSpecs
+ class ToAryAsNil
+ def to_ary
+ nil
+ end
+ end
+ def self.new_proc_in_method
+ Proc.new
+ end
+
+ def self.new_proc_from_amp(&block)
+ block
+ end
+
+ def self.proc_for_1
+ proc { 1 }
+ end
+
+ class ProcSubclass < Proc
+ end
+
+ def self.new_proc_subclass_in_method
+ ProcSubclass.new
+ end
+
+ class MyProc < Proc
+ end
+
+ class MyProc2 < Proc
+ def initialize(a, b)
+ @first = a
+ @second = b
+ end
+
+ attr_reader :first, :second
+ end
+
+ class Arity
+ def arity_check(&block)
+ pn = Proc.new(&block).arity
+ pr = proc(&block).arity
+ lm = lambda(&block).arity
+
+ if pn == pr and pr == lm
+ return pn
+ else
+ return :arity_check_failed
+ end
+ end
+ end
+end
diff --git a/spec/ruby/core/proc/fixtures/source_location.rb b/spec/ruby/core/proc/fixtures/source_location.rb
new file mode 100644
index 0000000000..468262e02a
--- /dev/null
+++ b/spec/ruby/core/proc/fixtures/source_location.rb
@@ -0,0 +1,55 @@
+module ProcSpecs
+ class SourceLocation
+ def self.my_proc
+ proc { true }
+ end
+
+ def self.my_lambda
+ lambda { true }
+ end
+
+ def self.my_proc_new
+ Proc.new { true }
+ end
+
+ def self.my_method
+ method(__method__).to_proc
+ end
+
+ def self.my_multiline_proc
+ proc do
+ 'a'.upcase
+ 1 + 22
+ end
+ end
+
+ def self.my_multiline_lambda
+ lambda do
+ 'a'.upcase
+ 1 + 22
+ end
+ end
+
+ def self.my_multiline_proc_new
+ Proc.new do
+ 'a'.upcase
+ 1 + 22
+ end
+ end
+
+ def self.my_detached_proc
+ body = proc { true }
+ proc(&body)
+ end
+
+ def self.my_detached_lambda
+ body = lambda { true }
+ lambda(&body)
+ end
+
+ def self.my_detached_proc_new
+ body = Proc.new { true }
+ Proc.new(&body)
+ end
+ end
+end
diff --git a/spec/ruby/core/proc/hash_spec.rb b/spec/ruby/core/proc/hash_spec.rb
new file mode 100644
index 0000000000..1f5b6d5aa1
--- /dev/null
+++ b/spec/ruby/core/proc/hash_spec.rb
@@ -0,0 +1,17 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Proc#hash" do
+ it "is provided" do
+ proc {}.respond_to?(:hash).should be_true
+ lambda {}.respond_to?(:hash).should be_true
+ end
+
+ it "returns an Integer" do
+ proc { 1 + 489 }.hash.should be_kind_of(Fixnum)
+ end
+
+ it "is stable" do
+ body = proc { :foo }
+ proc(&body).hash.should == proc(&body).hash
+ end
+end
diff --git a/spec/ruby/core/proc/inspect_spec.rb b/spec/ruby/core/proc/inspect_spec.rb
new file mode 100644
index 0000000000..860647baa7
--- /dev/null
+++ b/spec/ruby/core/proc/inspect_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/to_s', __FILE__)
+
+describe "Proc#inspect" do
+ it_behaves_like :proc_to_s, :inspect
+end
diff --git a/spec/ruby/core/proc/lambda_spec.rb b/spec/ruby/core/proc/lambda_spec.rb
new file mode 100644
index 0000000000..33c9ebdd55
--- /dev/null
+++ b/spec/ruby/core/proc/lambda_spec.rb
@@ -0,0 +1,60 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/common', __FILE__)
+
+describe "Proc#lambda?" do
+ it "returns true if the Proc was created from a block with the lambda keyword" do
+ lambda {}.lambda?.should be_true
+ end
+
+ it "returns false if the Proc was created from a block with the proc keyword" do
+ proc {}.lambda?.should be_false
+ end
+
+ it "returns false if the Proc was created from a block with Proc.new" do
+ Proc.new {}.lambda?.should be_false
+ end
+
+ it "is preserved when passing a Proc with & to the lambda keyword" do
+ lambda(&lambda{}).lambda?.should be_true
+ lambda(&proc{}).lambda?.should be_false
+ end
+
+ it "is preserved when passing a Proc with & to the proc keyword" do
+ proc(&lambda{}).lambda?.should be_true
+ proc(&proc{}).lambda?.should be_false
+ end
+
+ it "is preserved when passing a Proc with & to Proc.new" do
+ Proc.new(&lambda{}).lambda?.should be_true
+ Proc.new(&proc{}).lambda?.should be_false
+ end
+
+ it "returns false if the Proc was created from a block with &" do
+ ProcSpecs.new_proc_from_amp{}.lambda?.should be_false
+ end
+
+ it "is preserved when the Proc was passed using &" do
+ ProcSpecs.new_proc_from_amp(&lambda{}).lambda?.should be_true
+ ProcSpecs.new_proc_from_amp(&proc{}).lambda?.should be_false
+ ProcSpecs.new_proc_from_amp(&Proc.new{}).lambda?.should be_false
+ end
+
+ it "returns true for a Method converted to a Proc" do
+ m = :foo.method(:to_s)
+ m.to_proc.lambda?.should be_true
+ ProcSpecs.new_proc_from_amp(&m).lambda?.should be_true
+ end
+
+ # [ruby-core:24127]
+ it "is preserved when a Proc is curried" do
+ lambda{}.curry.lambda?.should be_true
+ proc{}.curry.lambda?.should be_false
+ Proc.new{}.curry.lambda?.should be_false
+ end
+
+ it "is preserved when a curried Proc is called without enough arguments" do
+ lambda{|x,y|}.curry.call(42).lambda?.should be_true
+ proc{|x,y|}.curry.call(42).lambda?.should be_false
+ Proc.new{|x,y|}.curry.call(42).lambda?.should be_false
+ end
+end
diff --git a/spec/ruby/core/proc/new_spec.rb b/spec/ruby/core/proc/new_spec.rb
new file mode 100644
index 0000000000..5dc0061a47
--- /dev/null
+++ b/spec/ruby/core/proc/new_spec.rb
@@ -0,0 +1,190 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/common', __FILE__)
+
+describe "Proc.new with an associated block" do
+ it "returns a proc that represents the block" do
+ Proc.new { }.call.should == nil
+ Proc.new { "hello" }.call.should == "hello"
+ end
+
+ describe "called on a subclass of Proc" do
+ before :each do
+ @subclass = Class.new(Proc) do
+ attr_reader :ok
+ def initialize
+ @ok = true
+ super
+ end
+ end
+ end
+
+ it "returns an instance of the subclass" do
+ proc = @subclass.new {"hello"}
+
+ proc.class.should == @subclass
+ proc.call.should == "hello"
+ proc.ok.should == true
+ end
+
+ # JRUBY-5026
+ describe "using a reified block parameter" do
+ it "returns an instance of the subclass" do
+ cls = Class.new do
+ def self.subclass=(subclass)
+ @subclass = subclass
+ end
+ def self.foo(&block)
+ @subclass.new(&block)
+ end
+ end
+ cls.subclass = @subclass
+ proc = cls.foo {"hello"}
+
+ proc.class.should == @subclass
+ proc.call.should == "hello"
+ proc.ok.should == true
+ end
+ end
+ end
+
+ # JRUBY-5261; Proc sets up the block during .new, not in #initialize
+ describe "called on a subclass of Proc that does not 'super' in 'initialize'" do
+ before :each do
+ @subclass = Class.new(Proc) do
+ attr_reader :ok
+ def initialize
+ @ok = true
+ end
+ end
+ end
+
+ it "still constructs a functional proc" do
+ proc = @subclass.new {'ok'}
+ proc.call.should == 'ok'
+ proc.ok.should == true
+ end
+ end
+
+ it "raises a LocalJumpError when context of the block no longer exists" do
+ def some_method
+ Proc.new { return }
+ end
+ res = some_method()
+
+ lambda { res.call }.should raise_error(LocalJumpError)
+ end
+
+ it "returns from within enclosing method when 'return' is used in the block" do
+ # we essentially verify that the created instance behaves like proc,
+ # not like lambda.
+ def some_method
+ Proc.new { return :proc_return_value }.call
+ :method_return_value
+ end
+ some_method.should == :proc_return_value
+ end
+
+ it "returns a subclass of Proc" do
+ obj = ProcSpecs::MyProc.new { }
+ obj.should be_kind_of(ProcSpecs::MyProc)
+ end
+
+ it "calls initialize on the Proc object" do
+ obj = ProcSpecs::MyProc2.new(:a, 2) { }
+ obj.first.should == :a
+ obj.second.should == 2
+ end
+
+ it "returns a new Proc instance from the block passed to the containing method" do
+ prc = ProcSpecs.new_proc_in_method { "hello" }
+ prc.should be_an_instance_of(Proc)
+ prc.call.should == "hello"
+ end
+
+ it "returns a new Proc instance from the block passed to the containing method" do
+ prc = ProcSpecs.new_proc_subclass_in_method { "hello" }
+ prc.should be_an_instance_of(ProcSpecs::ProcSubclass)
+ prc.call.should == "hello"
+ end
+end
+
+describe "Proc.new with a block argument" do
+ it "returns the passed proc created from a block" do
+ passed_prc = Proc.new { "hello".size }
+ prc = Proc.new(&passed_prc)
+
+ prc.should equal(passed_prc)
+ prc.call.should == 5
+ end
+
+ it "returns the passed proc created from a method" do
+ method = "hello".method(:size)
+ passed_prc = Proc.new(&method)
+ prc = Proc.new(&passed_prc)
+
+ prc.should equal(passed_prc)
+ prc.call.should == 5
+ end
+
+ it "returns the passed proc created from a symbol" do
+ passed_prc = Proc.new(&:size)
+ prc = Proc.new(&passed_prc)
+
+ prc.should equal(passed_prc)
+ prc.call("hello").should == 5
+ end
+end
+
+describe "Proc.new with a block argument called indirectly from a subclass" do
+ it "returns the passed proc created from a block" do
+ passed_prc = ProcSpecs::MyProc.new { "hello".size }
+ passed_prc.class.should == ProcSpecs::MyProc
+ prc = ProcSpecs::MyProc.new(&passed_prc)
+
+ prc.should equal(passed_prc)
+ prc.call.should == 5
+ end
+
+ it "returns the passed proc created from a method" do
+ method = "hello".method(:size)
+ passed_prc = ProcSpecs::MyProc.new(&method)
+ passed_prc.class.should == ProcSpecs::MyProc
+ prc = ProcSpecs::MyProc.new(&passed_prc)
+
+ prc.should equal(passed_prc)
+ prc.call.should == 5
+ end
+
+ it "returns the passed proc created from a symbol" do
+ passed_prc = ProcSpecs::MyProc.new(&:size)
+ passed_prc.class.should == ProcSpecs::MyProc
+ prc = ProcSpecs::MyProc.new(&passed_prc)
+
+ prc.should equal(passed_prc)
+ prc.call("hello").should == 5
+ end
+end
+
+describe "Proc.new without a block" do
+ it "raises an ArgumentError" do
+ lambda { Proc.new }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError if invoked from within a method with no block" do
+ lambda { ProcSpecs.new_proc_in_method }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError if invoked on a subclass from within a method with no block" do
+ lambda { ProcSpecs.new_proc_subclass_in_method }.should raise_error(ArgumentError)
+ end
+
+ it "uses the implicit block from an enclosing method" do
+ def some_method
+ Proc.new
+ end
+
+ prc = some_method { "hello" }
+
+ prc.call.should == "hello"
+ end
+end
diff --git a/spec/ruby/core/proc/parameters_spec.rb b/spec/ruby/core/proc/parameters_spec.rb
new file mode 100644
index 0000000000..3ba8a0cc32
--- /dev/null
+++ b/spec/ruby/core/proc/parameters_spec.rb
@@ -0,0 +1,95 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Proc#parameters" do
+ it "returns an empty Array for a proc expecting no parameters" do
+ proc {}.parameters.should == []
+ end
+
+ it "returns an Array of Arrays for a proc expecting parameters" do
+ p = proc {|x| }
+ p.parameters.should be_an_instance_of(Array)
+ p.parameters.first.should be_an_instance_of(Array)
+ end
+
+ it "sets the first element of each sub-Array to :opt for optional arguments" do
+ proc {|x| }.parameters.first.first.should == :opt
+ proc {|y,*x| }.parameters.first.first.should == :opt
+ end
+
+ it "regards named parameters in procs as optional" do
+ proc {|x| }.parameters.first.first.should == :opt
+ end
+
+ it "regards optional keyword parameters in procs as optional" do
+ proc {|x: :y| }.parameters.first.first.should == :key
+ end
+
+ it "regards parameters with default values as optional" do
+ lambda {|x=1| }.parameters.first.first.should == :opt
+ proc {|x=1| }.parameters.first.first.should == :opt
+ end
+
+ it "sets the first element of each sub-Array to :req for required arguments" do
+ lambda {|x,y=[]| }.parameters.first.first.should == :req
+ lambda {|y,*x| }.parameters.first.first.should == :req
+ end
+
+ it "regards named parameters in lambdas as required" do
+ lambda {|x| }.parameters.first.first.should == :req
+ end
+
+ it "regards keyword parameters in lambdas as required" do
+ eval("lambda {|x:| }").parameters.first.first.should == :keyreq
+ end
+
+ it "sets the first element of each sub-Array to :rest for parameters prefixed with asterisks" do
+ lambda {|*x| }.parameters.first.first.should == :rest
+ lambda {|x,*y| }.parameters.last.first.should == :rest
+ proc {|*x| }.parameters.first.first.should == :rest
+ proc {|x,*y| }.parameters.last.first.should == :rest
+ end
+
+ it "sets the first element of each sub-Array to :keyrest for parameters prefixed with double asterisks" do
+ lambda {|**x| }.parameters.first.first.should == :keyrest
+ lambda {|x,**y| }.parameters.last.first.should == :keyrest
+ proc {|**x| }.parameters.first.first.should == :keyrest
+ proc {|x,**y| }.parameters.last.first.should == :keyrest
+ end
+
+ it "sets the first element of each sub-Array to :block for parameters prefixed with ampersands" do
+ lambda {|&x| }.parameters.first.first.should == :block
+ lambda {|x,&y| }.parameters.last.first.should == :block
+ proc {|&x| }.parameters.first.first.should == :block
+ proc {|x,&y| }.parameters.last.first.should == :block
+ end
+
+ it "sets the second element of each sub-Array to the name of the argument" do
+ lambda {|x| }.parameters.first.last.should == :x
+ lambda {|x=Math::PI| }.parameters.first.last.should == :x
+ lambda {|an_argument, glark, &foo| }.parameters[1].last.should == :glark
+ lambda {|*rest| }.parameters.first.last.should == :rest
+ lambda {|&block| }.parameters.first.last.should == :block
+ proc {|x| }.parameters.first.last.should == :x
+ proc {|x=Math::PI| }.parameters.first.last.should == :x
+ proc {|an_argument, glark, &foo| }.parameters[1].last.should == :glark
+ proc {|*rest| }.parameters.first.last.should == :rest
+ proc {|&block| }.parameters.first.last.should == :block
+ end
+
+ it "ignores unnamed rest args" do
+ lambda {|x,|}.parameters.should == [[:req, :x]]
+ end
+
+ it "adds nameless rest arg for \"star\" argument" do
+ lambda {|x,*|}.parameters.should == [[:req, :x], [:rest]]
+ end
+
+ it "does not add locals as block options with a block and splat" do
+ lambda do |*args, &blk|
+ local_is_not_parameter = {}
+ end.parameters.should == [[:rest, :args], [:block, :blk]]
+ proc do |*args, &blk|
+ local_is_not_parameter = {}
+ end.parameters.should == [[:rest, :args], [:block, :blk]]
+ end
+end
diff --git a/spec/ruby/core/proc/shared/call.rb b/spec/ruby/core/proc/shared/call.rb
new file mode 100644
index 0000000000..11355537b0
--- /dev/null
+++ b/spec/ruby/core/proc/shared/call.rb
@@ -0,0 +1,96 @@
+require File.expand_path('../../fixtures/common', __FILE__)
+
+describe :proc_call, shared: true do
+ it "invokes self" do
+ Proc.new { "test!" }.send(@method).should == "test!"
+ lambda { "test!" }.send(@method).should == "test!"
+ proc { "test!" }.send(@method).should == "test!"
+ end
+
+ it "sets self's parameters to the given values" do
+ Proc.new { |a, b| a + b }.send(@method, 1, 2).should == 3
+ 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]
+
+ 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]
+ proc { |_, *args| args }.send(@method, 1, 2, 3).should == [2, 3]
+ end
+end
+
+
+describe :proc_call_on_proc_new, shared: true do
+ it "replaces missing arguments with nil" do
+ Proc.new { |a, b| [a, b] }.send(@method).should == [nil, nil]
+ Proc.new { |a, b| [a, b] }.send(@method, 1).should == [1, nil]
+ end
+
+ it "silently ignores extra arguments" do
+ Proc.new { |a, b| a + b }.send(@method, 1, 2, 5).should == 3
+ end
+
+ it "auto-explodes a single Array argument" do
+ p = Proc.new { |a, b| [a, b] }
+ p.send(@method, 1, 2).should == [1, 2]
+ p.send(@method, [1, 2]).should == [1, 2]
+ p.send(@method, [1, 2, 3]).should == [1, 2]
+ p.send(@method, [1, 2, 3], 4).should == [[1, 2, 3], 4]
+ end
+end
+
+describe :proc_call_on_proc_or_lambda, shared: true do
+ it "ignores excess arguments when self is a proc" do
+ a = proc {|x| x}.send(@method, 1, 2)
+ a.should == 1
+
+ a = proc {|x| x}.send(@method, 1, 2, 3)
+ a.should == 1
+ end
+
+ it "will call #to_ary on argument and return self if return is nil" do
+ argument = ProcSpecs::ToAryAsNil.new
+ result = proc { |x, _| x }.send(@method, argument)
+ result.should == argument
+ end
+
+ it "substitutes nil for missing arguments when self is a proc" do
+ proc {|x,y| [x,y]}.send(@method).should == [nil,nil]
+
+ a = proc {|x,y| [x, y]}.send(@method, 1)
+ a.should == [1,nil]
+ end
+
+ it "raises an ArgumentError on excess arguments when self is a lambda" do
+ lambda {
+ lambda {|x| x}.send(@method, 1, 2)
+ }.should raise_error(ArgumentError)
+
+ lambda {
+ lambda {|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)
+ }.should raise_error(ArgumentError)
+
+ lambda {
+ lambda {|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]
+ end
+
+ it "treats a single Array argument as a single argument when self is a proc" do
+ proc { |a| a }.send(@method, [1, 2]).should == [1, 2]
+ proc { |a, b| [a, b] }.send(@method, [1, 2], 3).should == [[1,2], 3]
+ end
+end
diff --git a/spec/ruby/core/proc/shared/call_arguments.rb b/spec/ruby/core/proc/shared/call_arguments.rb
new file mode 100644
index 0000000000..2e510b194e
--- /dev/null
+++ b/spec/ruby/core/proc/shared/call_arguments.rb
@@ -0,0 +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
+ proc {|&b| b.send(@method)}.send(@method) {1 + 1}.should == 2
+ end
+end
diff --git a/spec/ruby/core/proc/shared/dup.rb b/spec/ruby/core/proc/shared/dup.rb
new file mode 100644
index 0000000000..fb6fff299d
--- /dev/null
+++ b/spec/ruby/core/proc/shared/dup.rb
@@ -0,0 +1,10 @@
+describe :proc_dup, shared: true do
+ it "returns a copy of self" do
+ a = lambda { "hello" }
+ b = a.send(@method)
+
+ a.should_not equal(b)
+
+ a.call.should == b.call
+ end
+end
diff --git a/spec/ruby/core/proc/shared/equal.rb b/spec/ruby/core/proc/shared/equal.rb
new file mode 100644
index 0000000000..c2735ffb2f
--- /dev/null
+++ b/spec/ruby/core/proc/shared/equal.rb
@@ -0,0 +1,100 @@
+require File.expand_path('../../../../spec_helper', __FILE__)
+require File.expand_path('../../fixtures/common', __FILE__)
+
+describe :proc_equal, shared: true do
+ it "is a public method" do
+ Proc.should have_public_instance_method(@method, false)
+ end
+
+ it "returns true if self and other are the same object" do
+ p = proc { :foo }
+ p.send(@method, p).should be_true
+
+ p = Proc.new { :foo }
+ p.send(@method, p).should be_true
+
+ p = lambda { :foo }
+ p.send(@method, p).should be_true
+ end
+
+ it "returns true if other is a dup of the original" do
+ p = proc { :foo }
+ p.send(@method, p.dup).should be_true
+
+ p = Proc.new { :foo }
+ p.send(@method, p.dup).should be_true
+
+ p = lambda { :foo }
+ p.send(@method, p.dup).should be_true
+ end
+
+ # identical here means the same method invocation.
+ it "returns false when bodies are the same but capture env is not identical" do
+ a = ProcSpecs.proc_for_1
+ b = ProcSpecs.proc_for_1
+
+ a.send(@method, b).should be_false
+ end
+
+ it "returns true if both procs have the same body and environment" do
+ p = proc { :foo }
+ p2 = proc { :foo }
+ p.send(@method, p2).should be_true
+ 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
+ end
+
+ it "returns true if both different kinds of procs with the same body and env" do
+ p = lambda { :foo }
+ p2 = proc { :foo }
+ p.send(@method, p2).should be_true
+
+ x = proc { :bar }
+ x2 = lambda { :bar }
+ x.send(@method, x2).should be_true
+ end
+
+ it "returns false if other is not a Proc" do
+ p = proc { :foo }
+ p.send(@method, []).should be_false
+
+ p = Proc.new { :foo }
+ p.send(@method, Object.new).should be_false
+
+ p = lambda { :foo }
+ p.send(@method, :foo).should be_false
+ end
+
+ it "returns false if self and other are both procs but have different bodies" do
+ p = proc { :bar }
+ p2 = proc { :foo }
+ p.send(@method, p2).should be_false
+ end
+
+ it "returns false if self and other are both lambdas but have different bodies" do
+ p = lambda { :foo }
+ p2 = lambda { :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
new file mode 100644
index 0000000000..c3f82a73f3
--- /dev/null
+++ b/spec/ruby/core/proc/shared/to_s.rb
@@ -0,0 +1,27 @@
+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)?>$/
+ end
+ 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\)>$/
+ 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)?>$/
+ end
+ end
+
+ 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\)>$/
+ end
+ end
+end
diff --git a/spec/ruby/core/proc/source_location_spec.rb b/spec/ruby/core/proc/source_location_spec.rb
new file mode 100644
index 0000000000..311d13c050
--- /dev/null
+++ b/spec/ruby/core/proc/source_location_spec.rb
@@ -0,0 +1,72 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/source_location', __FILE__)
+
+describe "Proc#source_location" do
+ before :each do
+ @proc = ProcSpecs::SourceLocation.my_proc
+ @lambda = ProcSpecs::SourceLocation.my_lambda
+ @proc_new = ProcSpecs::SourceLocation.my_proc_new
+ @method = ProcSpecs::SourceLocation.my_method
+ end
+
+ it "returns an Array" do
+ @proc.source_location.should be_an_instance_of(Array)
+ @proc_new.source_location.should be_an_instance_of(Array)
+ @lambda.source_location.should be_an_instance_of(Array)
+ @method.source_location.should be_an_instance_of(Array)
+ end
+
+ it "sets the first value to the path of the file in which the proc was defined" do
+ file = @proc.source_location.first
+ file.should be_an_instance_of(String)
+ file.should == File.dirname(__FILE__) + '/fixtures/source_location.rb'
+
+ file = @proc_new.source_location.first
+ file.should be_an_instance_of(String)
+ file.should == File.dirname(__FILE__) + '/fixtures/source_location.rb'
+
+ file = @lambda.source_location.first
+ file.should be_an_instance_of(String)
+ file.should == File.dirname(__FILE__) + '/fixtures/source_location.rb'
+
+ file = @method.source_location.first
+ file.should be_an_instance_of(String)
+ file.should == File.dirname(__FILE__) + '/fixtures/source_location.rb'
+ end
+
+ it "sets the last value to a Fixnum representing the line on which the proc was defined" do
+ line = @proc.source_location.last
+ line.should be_an_instance_of(Fixnum)
+ line.should == 4
+
+ line = @proc_new.source_location.last
+ line.should be_an_instance_of(Fixnum)
+ line.should == 12
+
+ line = @lambda.source_location.last
+ line.should be_an_instance_of(Fixnum)
+ line.should == 8
+
+ line = @method.source_location.last
+ line.should be_an_instance_of(Fixnum)
+ line.should == 15
+ end
+
+ it "works even if the proc was created on the same line" do
+ proc { true }.source_location.should == [__FILE__, __LINE__]
+ Proc.new { true }.source_location.should == [__FILE__, __LINE__]
+ lambda { true }.source_location.should == [__FILE__, __LINE__]
+ end
+
+ it "returns the first line of a multi-line proc (i.e. the line containing 'proc do')" do
+ ProcSpecs::SourceLocation.my_multiline_proc.source_location.last.should == 20
+ ProcSpecs::SourceLocation.my_multiline_proc_new.source_location.last.should == 34
+ ProcSpecs::SourceLocation.my_multiline_lambda.source_location.last.should == 27
+ end
+
+ it "returns the location of the proc's body; not necessarily the proc itself" do
+ ProcSpecs::SourceLocation.my_detached_proc.source_location.last.should == 41
+ ProcSpecs::SourceLocation.my_detached_proc_new.source_location.last.should == 51
+ ProcSpecs::SourceLocation.my_detached_lambda.source_location.last.should == 46
+ end
+end
diff --git a/spec/ruby/core/proc/to_proc_spec.rb b/spec/ruby/core/proc/to_proc_spec.rb
new file mode 100644
index 0000000000..e5637c9f41
--- /dev/null
+++ b/spec/ruby/core/proc/to_proc_spec.rb
@@ -0,0 +1,9 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Proc#to_proc" do
+ it "returns self" do
+ [Proc.new {}, lambda {}, proc {}].each { |p|
+ p.to_proc.should equal(p)
+ }
+ end
+end
diff --git a/spec/ruby/core/proc/to_s_spec.rb b/spec/ruby/core/proc/to_s_spec.rb
new file mode 100644
index 0000000000..e0b248c369
--- /dev/null
+++ b/spec/ruby/core/proc/to_s_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/to_s', __FILE__)
+
+describe "Proc#to_s" do
+ it_behaves_like :proc_to_s, :to_s
+end
diff --git a/spec/ruby/core/proc/yield_spec.rb b/spec/ruby/core/proc/yield_spec.rb
new file mode 100644
index 0000000000..930e44ea78
--- /dev/null
+++ b/spec/ruby/core/proc/yield_spec.rb
@@ -0,0 +1,16 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/call', __FILE__)
+require File.expand_path('../shared/call_arguments', __FILE__)
+
+describe "Proc#yield" do
+ it_behaves_like :proc_call, :yield
+ it_behaves_like :proc_call_block_args, :yield
+end
+
+describe "Proc#yield on a Proc created with Proc.new" do
+ it_behaves_like :proc_call_on_proc_new, :yield
+end
+
+describe "Proc#yield on a Proc created with Kernel#lambda or Kernel#proc" do
+ it_behaves_like :proc_call_on_proc_or_lambda, :yield
+end