Sha256: 345798da4bae08240c28d5391c5e112e96e0fc3a5e1614a8436057840b3d7331

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

class RescueReturningSpec
  def single
    begin
      raise "ERROR"
    rescue
      :foo
    end
  end

  def multiple
    begin
      raise "ERROR"
    rescue
      to_s
      :bar
    end
  end

  def empty_rescue
    begin
      raise "ERROR"
    rescue
    end
  end
end

describe "The rescue keyword" do
  it "returns last value of expression evaluated" do
    RescueReturningSpec.new.single.should == :foo
    RescueReturningSpec.new.multiple.should == :bar
  end

  it "returns nil if no expr given in rescue body" do
    RescueReturningSpec.new.empty_rescue.should be_nil
  end

  it "by default, catch StandardError, not all Exception" do
    lambda { begin;raise Exception.new;rescue;end }.should raise_error
    lambda { begin;raise "err";rescue;end }.should_not raise_error

    # one line rescue
    lambda { raise Exception rescue nil }.should raise_error(Exception)
    lambda { raise "err" rescue nil }.should_not raise_error
    (raise "err" rescue "foo").should == "foo"
    ("err" rescue "foo").should == "err"
  end

  it 'Fix using more than two "rescue" in sequence #1269' do
    $ScratchPad = []
    # As a statement
    begin
      raise IOError, 'foo'
    rescue RangeError              # this one is correct
    rescue TypeError               # miss a return
    rescue IOError                 # following two lines disappear in js
      $ScratchPad << "I got #{$!.message}"
    end
    $ScratchPad.last.should == "I got foo"

    # As an expression
    a = begin
      raise IOError, 'foo'
    rescue RangeError              # this one is correct
    rescue TypeError               # miss a return
    rescue IOError                 # following two lines disappear in js
      "I got #{$!.message}"
    end
    a.should == "I got foo"
  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
opal-0.10.6 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.6.beta spec/opal/core/runtime/rescue_spec.rb
opal-0.10.5 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.4 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.3 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.2 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.1 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.0 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.0.rc2 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.0.rc1 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.0.beta5 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.0.beta4 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.0.beta3 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.0.beta2 spec/opal/core/runtime/rescue_spec.rb
opal-0.10.0.beta1 spec/opal/core/runtime/rescue_spec.rb