summaryrefslogtreecommitdiff
path: root/spec/ruby/language/lambda_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/language/lambda_spec.rb')
-rw-r--r--spec/ruby/language/lambda_spec.rb67
1 files changed, 28 insertions, 39 deletions
diff --git a/spec/ruby/language/lambda_spec.rb b/spec/ruby/language/lambda_spec.rb
index 3ab3569ebe..43e2d60ae3 100644
--- a/spec/ruby/language/lambda_spec.rb
+++ b/spec/ruby/language/lambda_spec.rb
@@ -1,5 +1,5 @@
-require_relative '../spec_helper'
-require_relative 'fixtures/classes'
+require File.expand_path('../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/classes', __FILE__)
describe "A lambda literal -> () { }" do
SpecEvaluate.desc = "for definition"
@@ -7,7 +7,7 @@ describe "A lambda literal -> () { }" do
it "returns a Proc object when used in a BasicObject method" do
klass = Class.new(BasicObject) do
def create_lambda
- -> { }
+ -> () { }
end
end
@@ -15,19 +15,11 @@ describe "A lambda literal -> () { }" do
end
it "does not execute the block" do
- -> { fail }.should be_an_instance_of(Proc)
+ ->() { fail }.should be_an_instance_of(Proc)
end
it "returns a lambda" do
- -> { }.lambda?.should be_true
- end
-
- it "may include a rescue clause" do
- eval('-> do raise ArgumentError; rescue ArgumentError; 7; end').should be_an_instance_of(Proc)
- end
-
- it "may include a ensure clause" do
- eval('-> do 1; ensure; 2; end').should be_an_instance_of(Proc)
+ -> () { }.lambda?.should be_true
end
it "has its own scope for local variables" do
@@ -109,7 +101,7 @@ describe "A lambda literal -> () { }" do
@a = -> (a:) { a }
ruby
- -> { @a.() }.should raise_error(ArgumentError)
+ lambda { @a.() }.should raise_error(ArgumentError)
@a.(a: 1).should == 1
end
@@ -127,7 +119,7 @@ describe "A lambda literal -> () { }" do
@a.().should be_nil
@a.(a: 1, b: 2).should be_nil
- -> { @a.(1) }.should raise_error(ArgumentError)
+ lambda { @a.(1) }.should raise_error(ArgumentError)
end
evaluate <<-ruby do
@@ -151,8 +143,8 @@ describe "A lambda literal -> () { }" do
ruby
@a.(1, 2).should == [1, 2]
- -> { @a.() }.should raise_error(ArgumentError)
- -> { @a.(1) }.should raise_error(ArgumentError)
+ lambda { @a.() }.should raise_error(ArgumentError)
+ lambda { @a.(1) }.should raise_error(ArgumentError)
end
evaluate <<-ruby do
@@ -185,8 +177,8 @@ describe "A lambda literal -> () { }" do
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
h = mock("keyword splat")
- h.should_not_receive(:to_hash)
- @a.(h).should == {}
+ h.should_receive(:to_hash).and_return({a: 1})
+ @a.(h).should == {a: 1}
end
evaluate <<-ruby do
@@ -263,23 +255,25 @@ describe "A lambda literal -> () { }" do
end
describe "with circular optional argument reference" do
- it "raises a SyntaxError if using an existing local with the same name as the argument" do
+ it "shadows an existing local with the same name as the argument" do
a = 1
-> {
@proc = eval "-> (a=a) { a }"
- }.should raise_error(SyntaxError)
+ }.should complain(/circular argument reference/)
+ @proc.call.should == nil
end
- it "raises a SyntaxError if there is an existing method with the same name as the argument" do
+ it "shadows an existing method with the same name as the argument" do
def a; 1; end
-> {
@proc = eval "-> (a=a) { a }"
- }.should raise_error(SyntaxError)
+ }.should complain(/circular argument reference/)
+ @proc.call.should == nil
end
it "calls an existing method with the same name as the argument if explicitly using ()" do
def a; 1; end
- -> a=a() { a }.call.should == 1
+ -> (a=a()) { a }.call.should == 1
end
end
end
@@ -308,13 +302,7 @@ describe "A lambda expression 'lambda { ... }'" do
end
it "requires a block" do
- suppress_warning do
- lambda { lambda }.should raise_error(ArgumentError)
- end
- end
-
- it "may include a rescue clause" do
- eval('lambda do raise ArgumentError; rescue ArgumentError; 7; end').should be_an_instance_of(Proc)
+ lambda { lambda }.should raise_error(ArgumentError)
end
context "with an implicit block" do
@@ -322,13 +310,14 @@ describe "A lambda expression 'lambda { ... }'" do
def meth; lambda; end
end
- it "raises ArgumentError" do
+ it "can be created" do
implicit_lambda = nil
- suppress_warning do
- -> {
- meth { 1 }
- }.should raise_error(ArgumentError, /tried to create Proc object without a block/)
- end
+ -> {
+ implicit_lambda = meth { 1 }
+ }.should complain(/tried to create Proc object without a block/)
+
+ implicit_lambda.lambda?.should be_true
+ implicit_lambda.call.should == 1
end
end
@@ -504,8 +493,8 @@ describe "A lambda expression 'lambda { ... }'" do
@a.(1, 2, 3, a: 4, b: 5).should == {a: 4, b: 5}
h = mock("keyword splat")
- h.should_not_receive(:to_hash)
- @a.(h).should == {}
+ h.should_receive(:to_hash).and_return({a: 1})
+ @a.(h).should == {a: 1}
end
evaluate <<-ruby do