diff options
Diffstat (limited to 'spec/ruby/language/yield_spec.rb')
| -rw-r--r-- | spec/ruby/language/yield_spec.rb | 69 |
1 files changed, 35 insertions, 34 deletions
diff --git a/spec/ruby/language/yield_spec.rb b/spec/ruby/language/yield_spec.rb index 05d713af70..3173f41b0c 100644 --- a/spec/ruby/language/yield_spec.rb +++ b/spec/ruby/language/yield_spec.rb @@ -13,7 +13,7 @@ describe "The yield call" do describe "taking no arguments" do it "raises a LocalJumpError when the method is not passed a block" do - -> { @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 @@ -28,7 +28,7 @@ describe "The yield call" do describe "taking a single argument" do describe "when no block is given" do it "raises a LocalJumpError" do - -> { @y.s(1) }.should raise_error(LocalJumpError) + -> { @y.s(1) }.should.raise(LocalJumpError) end end @@ -48,6 +48,12 @@ 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 @@ -70,20 +76,20 @@ describe "The yield call" do it "raises an ArgumentError if too few arguments are passed" do -> { @y.s(1, &-> a, b { [a,b] }) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "should not destructure an Array into multiple arguments" do -> { @y.s([1, 2], &-> a, b { [a,b] }) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end end describe "taking multiple arguments" do it "raises a LocalJumpError when the method is not passed a block" do - -> { @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 @@ -97,19 +103,19 @@ describe "The yield call" do it "raises an ArgumentError if too many arguments are passed to a lambda" do -> { @y.m(1, 2, 3, &-> a { }) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an ArgumentError if too few arguments are passed to a lambda" do -> { @y.m(1, 2, 3, &-> a, b, c, d { }) - }.should raise_error(ArgumentError) + }.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 - -> { @y.r(0) }.should raise_error(LocalJumpError) + -> { @y.r(0) }.should.raise(LocalJumpError) end it "passes a single value" do @@ -141,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 - -> { @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 @@ -166,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 - -> { @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 @@ -186,40 +192,35 @@ describe "The yield call" do end describe "Using yield in a singleton class literal" do - ruby_version_is ""..."3.0" do - it 'emits a deprecation warning' do - code = <<~RUBY - def m - class << Object.new - yield - end - end - m { :ok } - RUBY + it 'raises a SyntaxError' do + code = <<~RUBY + class << Object.new + yield + end + RUBY - -> { eval(code) }.should complain(/warning: `yield' in class syntax will not be supported from Ruby 3.0/) - end + -> { eval(code) }.should.raise(SyntaxError, /Invalid yield/) end +end - ruby_version_is "3.0" do - it 'raises a SyntaxError' do - code = <<~RUBY - class << Object.new - yield - end +describe "Using yield in non-lambda block" do + it 'raises a SyntaxError' do + code = <<~RUBY + 1.times { yield } RUBY - -> { eval(code) }.should raise_error(SyntaxError, /Invalid yield/) - end + -> { eval(code) }.should.raise(SyntaxError, /Invalid yield/) end end -describe "Using yield in non-lambda block" do +describe "Using yield in a module literal" do it 'raises a SyntaxError' do code = <<~RUBY - 1.times { yield } - RUBY + module YieldSpecs::ModuleWithYield + yield + end + RUBY - -> { eval(code) }.should raise_error(SyntaxError, /Invalid yield/) + -> { eval(code) }.should.raise(SyntaxError, /Invalid yield/) end end |
