diff options
Diffstat (limited to 'spec/ruby/language/yield_spec.rb')
| -rw-r--r-- | spec/ruby/language/yield_spec.rb | 101 |
1 files changed, 74 insertions, 27 deletions
diff --git a/spec/ruby/language/yield_spec.rb b/spec/ruby/language/yield_spec.rb index 663110cbe6..3173f41b0c 100644 --- a/spec/ruby/language/yield_spec.rb +++ b/spec/ruby/language/yield_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../spec_helper', __FILE__) -require File.expand_path('../fixtures/yield', __FILE__) +require_relative '../spec_helper' +require_relative 'fixtures/yield' # Note that these specs use blocks defined as { |*a| ... } to capture the # arguments with which the block is invoked. This is slightly confusing @@ -13,18 +13,22 @@ describe "The yield call" do describe "taking no arguments" do it "raises a LocalJumpError when the method is not passed a block" do - lambda { @y.z }.should raise_error(LocalJumpError) + -> { @y.z }.should.raise(LocalJumpError) end it "ignores assignment to the explicit block argument and calls the passed block" do @y.ze { 42 }.should == 42 end + + it "does not pass a named block to the block being yielded to" do + @y.z() { |&block| block == nil }.should == true + end end describe "taking a single argument" do describe "when no block is given" do it "raises a LocalJumpError" do - lambda { @y.s(1) }.should raise_error(LocalJumpError) + -> { @y.s(1) }.should.raise(LocalJumpError) end end @@ -44,44 +48,48 @@ describe "The yield call" do it "passes a single, multi-value Array" do @y.s([1, 2, 3]) { |*a| a }.should == [[1, 2, 3]] end + + describe "with optional argument" do + it "does not destructure a single array argument" do + @y.s([1, 2, 3]) { |a = 99| a }.should == [1, 2, 3] + end + end end describe "yielding to a lambda" do it "passes an empty Array when the argument is an empty Array" do - @y.s([], &lambda { |*a| a }).should == [[]] + @y.s([], &-> *a { a }).should == [[]] end it "passes nil as a value" do - @y.s(nil, &lambda { |*a| a }).should == [nil] + @y.s(nil, &-> *a { a }).should == [nil] end it "passes a single value" do - @y.s(1, &lambda { |*a| a }).should == [1] + @y.s(1, &-> *a { a }).should == [1] end it "passes a single, multi-value Array" do - @y.s([1, 2, 3], &lambda { |*a| a }).should == [[1, 2, 3]] + @y.s([1, 2, 3], &-> *a { a }).should == [[1, 2, 3]] end it "raises an ArgumentError if too few arguments are passed" do - lambda { - @y.s(1, &lambda { |a,b| [a,b] }) - }.should raise_error(ArgumentError) + -> { + @y.s(1, &-> a, b { [a,b] }) + }.should.raise(ArgumentError) end - ruby_bug "#12705", "2.2"..."2.5" do - it "should not destructure an Array into multiple arguments" do - lambda { - @y.s([1, 2], &lambda { |a,b| [a,b] }) - }.should raise_error(ArgumentError) - end + it "should not destructure an Array into multiple arguments" do + -> { + @y.s([1, 2], &-> a, b { [a,b] }) + }.should.raise(ArgumentError) end end end describe "taking multiple arguments" do it "raises a LocalJumpError when the method is not passed a block" do - lambda { @y.m(1, 2, 3) }.should raise_error(LocalJumpError) + -> { @y.m(1, 2, 3) }.should.raise(LocalJumpError) end it "passes the arguments to the block" do @@ -93,21 +101,21 @@ describe "The yield call" do end it "raises an ArgumentError if too many arguments are passed to a lambda" do - lambda { - @y.m(1, 2, 3, &lambda { |a| }) - }.should raise_error(ArgumentError) + -> { + @y.m(1, 2, 3, &-> a { }) + }.should.raise(ArgumentError) end it "raises an ArgumentError if too few arguments are passed to a lambda" do - lambda { - @y.m(1, 2, 3, &lambda { |a,b,c,d| }) - }.should raise_error(ArgumentError) + -> { + @y.m(1, 2, 3, &-> a, b, c, d { }) + }.should.raise(ArgumentError) end end describe "taking a single splatted argument" do it "raises a LocalJumpError when the method is not passed a block" do - lambda { @y.r(0) }.should raise_error(LocalJumpError) + -> { @y.r(0) }.should.raise(LocalJumpError) end it "passes a single value" do @@ -139,7 +147,7 @@ describe "The yield call" do describe "taking multiple arguments with a splat" do it "raises a LocalJumpError when the method is not passed a block" do - lambda { @y.rs(1, 2, [3, 4]) }.should raise_error(LocalJumpError) + -> { @y.rs(1, 2, [3, 4]) }.should.raise(LocalJumpError) end it "passes the arguments to the block" do @@ -164,7 +172,7 @@ describe "The yield call" do describe "taking matching arguments with splats and post args" do it "raises a LocalJumpError when the method is not passed a block" do - lambda { @y.rs(1, 2, [3, 4]) }.should raise_error(LocalJumpError) + -> { @y.rs(1, 2, [3, 4]) }.should.raise(LocalJumpError) end it "passes the arguments to the block" do @@ -172,8 +180,47 @@ describe "The yield call" do end end + describe "taking a splat and a keyword argument" do + it "passes it as an array of the values and a hash" do + @y.k([1, 2]) { |*a| a }.should == [1, 2, {:b=>true}] + end + end + it "uses captured block of a block used in define_method" do @y.deep(2).should == 4 end +end + +describe "Using yield in a singleton class literal" do + it 'raises a SyntaxError' do + code = <<~RUBY + class << Object.new + yield + end + RUBY + + -> { eval(code) }.should.raise(SyntaxError, /Invalid yield/) + end +end + +describe "Using yield in non-lambda block" do + it 'raises a SyntaxError' do + code = <<~RUBY + 1.times { yield } + RUBY + -> { eval(code) }.should.raise(SyntaxError, /Invalid yield/) + end +end + +describe "Using yield in a module literal" do + it 'raises a SyntaxError' do + code = <<~RUBY + module YieldSpecs::ModuleWithYield + yield + end + RUBY + + -> { eval(code) }.should.raise(SyntaxError, /Invalid yield/) + end end |
